"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { Initials } from "@suphark/ui/components/shared/initials"; 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 { LogOut } from "lucide-react"; import { type ReactNode, useTransition } from "react"; import { toast } from "sonner"; export interface UserMenuUser { name: string; email?: string | null; /** URL รูป */ image?: string | null; /** บรรทัดรอง เช่น ตำแหน่ง · องค์กร */ description?: ReactNode; } export interface UserMenuProps { user: UserMenuUser; /** * รายการเมนู (DropdownMenuItem / DropdownMenuGroup / ThemeMenuItems …) — วางระหว่างหัวกับปุ่มออกจากระบบ * แอปเป็นคนใส่เอง เช่น บัญชีผู้ใช้, การแจ้งเตือน, ธีม, สลับภาษา */ children?: ReactNode; /** ไม่ส่ง = ไม่มีปุ่มออกจากระบบ */ onSignOut?: () => void | Promise; signOutLabel?: ReactNode; /** * `sidebar` = ปุ่มกว้างเต็ม footer ของ sidebar (Pi Auth) · `header` = ปุ่มกะทัดรัดใน header (supplychain) */ variant?: "sidebar" | "header"; align?: "start" | "end"; className?: string; /** aria-label ของปุ่มเปิดเมนู (ค่าเริ่มต้น `UiText.userMenu` "เมนูผู้ใช้") — ต่อท้ายชื่อผู้ใช้ให้ */ menuLabel?: string; } /** เมนูผู้ใช้ (ยกจาก supplychain NavUser + Pi Auth UserMenu) */ export function UserMenu({ user, children, onSignOut, signOutLabel, variant = "sidebar", align = variant === "header" ? "end" : "start", className, menuLabel, }: UserMenuProps) { const text = useUiText(); const [pending, start] = useTransition(); const description = user.description ?? user.email; return ( } > {user.name} {description && ( {description} )}
{user.name} {user.description && ( {user.description} )} {user.email && ( {user.email} )}
{children && ( <> {children} )} {onSignOut && ( <> start(async () => { try { await onSignOut(); } catch (err) { // ออกจากระบบไม่สำเร็จ = toast ไม่ใช่หน้า error ทั้งหน้า (redirect ปล่อยผ่าน) if (isNavigationError(err)) throw err; toast.error(errorMessage(err, text.actionFailed ?? "ดำเนินการไม่สำเร็จ")); } }) } > )}
); }