"use client"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@app/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@app/components/ui/popover"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@app/components/ui/tooltip"; import { Badge } from "@app/components/ui/badge"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { cn } from "@app/lib/cn"; import { ListUserOrgsResponse } from "@server/routers/org"; import { Check, ChevronsUpDown, Plus, Building2, Users } from "lucide-react"; import { Button } from "@app/components/ui/button"; import { usePathname, useRouter } from "next/navigation"; import { useMemo, useState } from "react"; import { useUserContext } from "@app/hooks/useUserContext"; import { useTranslations } from "next-intl"; interface OrgSelectorProps { orgId?: string; orgs?: ListUserOrgsResponse["orgs"]; isCollapsed?: boolean; } export function OrgSelector({ orgId, orgs, isCollapsed = false }: OrgSelectorProps) { const { user } = useUserContext(); const [open, setOpen] = useState(false); const router = useRouter(); const pathname = usePathname(); const { env } = useEnvContext(); const t = useTranslations(); const selectedOrg = orgs?.find((org) => org.orgId === orgId); const sortedOrgs = useMemo(() => { if (!orgs?.length) return orgs ?? []; return [...orgs].sort((a, b) => { const aPrimary = Boolean(a.isPrimaryOrg); const bPrimary = Boolean(b.isPrimaryOrg); if (aPrimary && !bPrimary) return -1; if (!aPrimary && bPrimary) return 1; return 0; }); }, [orgs]); const orgSelectorContent = (
{isCollapsed ? ( ) : (
{t("org")} {selectedOrg?.name || t("noneSelected")}
)}
{t("orgNotFound2")}
{sortedOrgs.map((org) => ( { setOpen(false); const newPath = pathname.includes( "/settings/" ) ? pathname.replace( /^\/[^/]+/, `/${org.orgId}` ) : `/${org.orgId}`; router.push(newPath); }} className="mx-1 rounded-md py-1.5 h-auto min-h-0" >
{org.name}
{org.orgId} {org.isPrimaryOrg && ( {t("primary")} )}
))}
{(!env.flags.disableUserCreateOrg || user.serverAdmin) && (
)}
); if (isCollapsed) { return ( {orgSelectorContent}

{selectedOrg?.name || t("noneSelected")}

{t("org")}

); } return orgSelectorContent; }