"use client"; import { Button } from "@app/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@app/components/ui/command"; import { Popover, PopoverContent, PopoverTrigger } from "@app/components/ui/popover"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { useUserContext } from "@app/hooks/useUserContext"; import { cn } from "@app/lib/cn"; import { build } from "@server/build"; import { ListUserOrgsResponse } from "@server/routers/org"; import { CheckIcon, Plus } from "lucide-react"; import { useTranslations } from "next-intl"; import { usePathname, useRouter } from "next/navigation"; import { useMemo, useState, type ReactNode } from "react"; export type OrgPickerProps = { orgId?: string; orgs?: ListUserOrgsResponse["orgs"]; contentClassName?: string; sideOffset?: number; children: ReactNode; }; export function OrgPicker({ orgId, orgs, contentClassName, sideOffset = 0, children }: OrgPickerProps) { const [open, setOpen] = useState(false); const router = useRouter(); const pathname = usePathname(); const t = useTranslations(); const { env } = useEnvContext(); const { user } = useUserContext(); let canCreateOrg = !env.flags.disableUserCreateOrg || user.serverAdmin; if (build === "saas" && user.type !== "internal") { canCreateOrg = false; } 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]); function selectOrg(nextOrgId: string) { setOpen(false); const newPath = pathname.includes("/settings/") ? pathname.replace(/^\/[^/]+/, `/${nextOrgId}`) : `/${nextOrgId}`; router.push(newPath); } return ( {children} {t("orgNotFound2")} {sortedOrgs.map((org) => ( selectOrg(org.orgId)} >
{org.name} {org.orgId} {org.isPrimaryOrg ? ` ยท ${t("primary")}` : ""}
))}
{canCreateOrg && (
)}
); }