"use client" import * as React from "react" import Link from "next/link" import { useRouter } from "next/navigation" import { IconBuilding, IconCheck, IconSelector, IconUser, } from "@tabler/icons-react" import { getUserOrganizations, switchOrganization, } from "@/app/actions/organizations" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" import { SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar, } from "@/components/ui/sidebar" import { cn } from "@/lib/utils" type OrgInfo = { readonly id: string readonly name: string readonly slug: string readonly type: string readonly role: string } export function OrgSwitcher({ activeOrgId, activeOrgName, }: { readonly activeOrgId: string | null readonly activeOrgName: string | null }): React.ReactElement { const router = useRouter() const { isMobile } = useSidebar() const [orgs, setOrgs] = React.useState([]) const [isLoading, setIsLoading] = React.useState(false) React.useEffect(() => { async function loadOrgs(): Promise { const result = await getUserOrganizations() setOrgs(result) } void loadOrgs() }, []) async function handleOrgSwitch(orgId: string): Promise { if (orgId === activeOrgId) return setIsLoading(true) const result = await switchOrganization(orgId) if (result.success) { router.refresh() } else { console.error("Failed to switch organization:", result.error) setIsLoading(false) } } const displayName = activeOrgName ?? "Compass" const hasOrgs = orgs.length > 1 return ( {orgs.map((org, i) => { const isActive = org.id === activeOrgId const OrgIcon = org.type === "personal" ? IconUser : IconBuilding return ( {i > 0 && } void handleOrgSwitch(org.id)} disabled={isLoading} className="gap-2 px-2 py-1.5" > {org.name} {isActive && ( )} ) })} ) }