import { useState, useEffect } from "react" import { Building2, Plus } from "lucide-react" import { Select, SelectContent, SelectItem, SelectTrigger, SelectSeparator } from "@/components/ui/select" import { useAppTranslation } from "@src/i18n/TranslationContext" import { vscode } from "@src/utils/vscode" import { useExtensionState } from "@src/context/ExtensionStateContext" import { cn } from "@src/lib/utils" export const CloudAccountSwitcher = () => { const { t } = useAppTranslation() const { cloudUserInfo, cloudOrganizations = [], cloudApiUrl } = useExtensionState() const [selectedOrgId, setSelectedOrgId] = useState(cloudUserInfo?.organizationId || null) const [isLoading, setIsLoading] = useState(false) // Update selected org when userInfo changes useEffect(() => { setSelectedOrgId(cloudUserInfo?.organizationId || null) }, [cloudUserInfo?.organizationId]) // Show the switcher whenever user is authenticated if (!cloudUserInfo) { return null } const handleOrganizationChange = async (value: string) => { // Handle "Create Team Account" option if (value === "create-team") { if (cloudApiUrl) { const billingUrl = `${cloudApiUrl}/billing` vscode.postMessage({ type: "openExternal", url: billingUrl }) } return } const newOrgId = value === "personal" ? null : value // Don't do anything if selecting the same organization if (newOrgId === selectedOrgId) { return } setIsLoading(true) // Send message to switch organization vscode.postMessage({ type: "switchOrganization", organizationId: newOrgId, }) // Update local state optimistically setSelectedOrgId(newOrgId) // Reset loading state after a delay setTimeout(() => { setIsLoading(false) }, 1000) } const currentValue = selectedOrgId || "personal" const currentOrg = cloudOrganizations.find((org) => org.organization.id === selectedOrgId) // Render the account icon based on current context const renderAccountIcon = () => { if (selectedOrgId && currentOrg?.organization.image_url) { // Organization with logo return ( {currentOrg.organization.name} ) } else if (selectedOrgId) { // Organization without logo return } else if (cloudUserInfo.picture) { // Personal account with avatar return ( {cloudUserInfo.name ) } else { // Personal account without avatar - show initials const initial = cloudUserInfo.name?.charAt(0) || cloudUserInfo.email?.charAt(0) || "?" return (
{initial}
) } } return (
) }