"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { SwitcherChevron } from "@suphark/ui/components/shared/switcher-chevron"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@suphark/ui/components/ui/dropdown-menu"; import { errorMessage, isNavigationError } from "@suphark/ui/lib/action-result"; import { cn } from "@suphark/ui/lib/utils"; import { Building2, Check } from "lucide-react"; import { useRouter } from "next/navigation"; import { type ReactNode, useTransition } from "react"; import { toast } from "sonner"; import { HeaderCrumb } from "./header-crumb"; export interface OrganizationOption { id: string; name: string; slug?: string; logoUrl?: string | null; } export interface OrganizationSwitcherProps { organizations: OrganizationOption[]; activeId: string | null; /** แอปเรียก server action สลับองค์กร — component ทำ useTransition + router.refresh() หลัง resolve */ onSwitch: (id: string) => Promise | void; /** refresh หน้าหลังสลับ (ค่าเริ่มต้น true) */ refresh?: boolean; /** ไม่มี active — ค่าเริ่มต้น `UiText.switcher.choose` */ fallbackLabel?: string; /** หัว dropdown — ค่าเริ่มต้น `UiText.switcher.organization` */ heading?: string; /** ข้อความจางท้ายชื่อ (ค่าเริ่มต้น `/slug` ถ้ามี) */ hint?: (org: OrganizationOption) => ReactNode; /** slot ท้ายเมนู เช่น "สร้างองค์กร…", "จัดการองค์กร" */ footer?: ReactNode; /** * ปุ่มท้ายแถวของแต่ละองค์กร เช่น ⚙ "ตั้งค่าองค์กร" (แบบ Vercel/Clerk) — คืน `null` เพื่อไม่แสดงในแถวนั้น * คลิกแล้ว **ไม่สลับองค์กร** (component หยุด event ให้); ส่ง element ที่กดได้เองและใส่ aria-label/title ด้วย */ itemAction?: (org: OrganizationOption) => ReactNode; /** * แถวที่ active กดได้ด้วย (เรียก `onSwitch` เหมือนแถวอื่น) — ค่าเริ่มต้น false = กดแล้วไม่ทำอะไร * ใช้เมื่อ "เลือก" = ไปหน้าของสิ่งนั้น ไม่ใช่แค่สลับ (เช่น crumb บัญชี: กดชื่อบัญชี = เข้าคอนโซลบัญชีนั้น) */ selectableActive?: boolean; /** `header` = HeaderCrumb ใน header (layout top); `sidebar` = ปุ่มเต็มกว้างใน `sidebarHeader` ของ AppShell */ placement?: "header" | "sidebar"; className?: string; } function OrgLogo({ org }: { org: OrganizationOption | undefined }) { if (org?.logoUrl) return ; return