"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useSheetOnMobile } from "@suphark/ui/components/shared/ui-defaults"; import { ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuGroup, ContextMenuItem, ContextMenuLabel, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuShortcut, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger, } from "@suphark/ui/components/ui/context-menu"; import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "@suphark/ui/components/ui/dropdown-menu"; import { useFrozenWhileOpen } from "@suphark/ui/hooks/use-frozen-while-open"; import { useLazyDrawer } from "@suphark/ui/hooks/use-lazy-drawer"; import { useIsMobile } from "@suphark/ui/hooks/use-mobile"; import { errorMessage, failedResult, isNavigationError } from "@suphark/ui/lib/action-result"; import { safeNavigableUrl } from "@suphark/ui/lib/safe-url"; import { cn } from "@suphark/ui/lib/utils"; import { type ComponentType, type ReactElement, type ReactNode, useState } from "react"; import { toast } from "sonner"; import { ActionSheet, ActionSheetLabel, ActionSheetSeparator, actionSheetRowClass, } from "./action-sheet"; import { ConfirmDialog, type ConfirmSpec, normalizeConfirm } from "./confirm-dialog"; import { ActionConfirm, ActionMore } from "./semantic-icons"; import { useUiText } from "./ui-text"; export interface ActionMenuCommand { type?: "item"; label: ReactNode; icon?: ReactNode; onClick?: () => unknown; /** ลิงก์ธรรมดา (``) — ลิงก์ภายในของ Next ที่ต้อง prefetch ใช้ onClick + router.push */ href?: string; /** เปิดแท็บใหม่ (ใช้กับ href) */ external?: boolean; shortcut?: string; destructive?: boolean; disabled?: boolean; /** ถามยืนยันก่อน onClick (string = คำถาม หรือ object กำหนดหัว/ปุ่มเอง) */ confirm?: ConfirmSpec; } /** เมนูย่อย — บนแผ่นล่างของมือถือแสดงเป็นหัวกลุ่ม + รายการต่อกัน */ export interface ActionMenuSubmenu { type: "sub"; label: ReactNode; icon?: ReactNode; disabled?: boolean; items: ReadonlyArray; } /** ตัวเลือกเดียวจากกลุ่ม (เช่น เรียงตาม / ภาษา) — เลือกแล้วเมนูยังเปิดอยู่ */ export interface ActionMenuRadioGroup { type: "radio"; /** หัวกลุ่ม (ไม่ส่ง = ไม่มีหัว) */ label?: ReactNode; value: string; onValueChange: (value: string) => void; options: ReadonlyArray<{ value: string; label: ReactNode; icon?: ReactNode; disabled?: boolean }>; } /** สวิตช์เปิด/ปิดในเมนู (เช่น แสดงงานที่เสร็จแล้ว) — กดแล้วเมนูยังเปิดอยู่ */ export interface ActionMenuCheckbox { type: "checkbox"; label: ReactNode; icon?: ReactNode; checked: boolean; onCheckedChange: (checked: boolean) => void; disabled?: boolean; } export type ActionMenuItem = | ActionMenuCommand | ActionMenuSubmenu | ActionMenuRadioGroup | ActionMenuCheckbox | { type: "separator" } | { type: "label"; label: ReactNode }; /** ชิ้นส่วนเมนูที่ต่างกันระหว่าง dropdown กับคลิกขวา (Base UI ใช้ Menu ตัวเดียวกันข้างใน) */ interface MenuParts { Group: ComponentType<{ children?: ReactNode }>; Item: typeof DropdownMenuItem; Label: typeof DropdownMenuLabel; Separator: typeof DropdownMenuSeparator; Shortcut: typeof DropdownMenuShortcut; Sub: typeof DropdownMenuSub; SubTrigger: typeof DropdownMenuSubTrigger; SubContent: ComponentType<{ children?: ReactNode; className?: string }>; RadioGroup: typeof DropdownMenuRadioGroup; RadioItem: typeof DropdownMenuRadioItem; CheckboxItem: typeof DropdownMenuCheckboxItem; } const DROPDOWN_PARTS: MenuParts = { Group: DropdownMenuGroup, Item: DropdownMenuItem, Label: DropdownMenuLabel, Separator: DropdownMenuSeparator, Shortcut: DropdownMenuShortcut, Sub: DropdownMenuSub, SubTrigger: DropdownMenuSubTrigger, SubContent: DropdownMenuSubContent, RadioGroup: DropdownMenuRadioGroup, RadioItem: DropdownMenuRadioItem, CheckboxItem: DropdownMenuCheckboxItem, }; const CONTEXT_PARTS: MenuParts = { Group: ContextMenuGroup, Item: ContextMenuItem as typeof DropdownMenuItem, Label: ContextMenuLabel as typeof DropdownMenuLabel, Separator: ContextMenuSeparator, Shortcut: ContextMenuShortcut, Sub: ContextMenuSub, SubTrigger: ContextMenuSubTrigger as typeof DropdownMenuSubTrigger, SubContent: ContextMenuSubContent, RadioGroup: ContextMenuRadioGroup, RadioItem: ContextMenuRadioItem as typeof DropdownMenuRadioItem, CheckboxItem: ContextMenuCheckboxItem as typeof DropdownMenuCheckboxItem, }; /** * รันคำสั่งของเมนู + ถามยืนยัน — ใช้ร่วมกันโดย `ActionMenu` และ `ContextActionMenu` * throw/reject หรือคืน `{ ok: false }` = toast แจ้ง (ไม่ปล่อยเป็น unhandled rejection เงียบ ๆ) */ function useCommandRunner(sheetOnMobile: boolean) { const text = useUiText(); const [confirming, setConfirming] = useState(null); const [pending, setPending] = useState(false); /** คืน true เมื่อสำเร็จ */ const run = async (fn?: () => unknown): Promise => { if (!fn) return true; setPending(true); try { // คืน { ok: false, error } (FormState) ก็ถือว่าล้มเหลวเหมือน throw const failed = failedResult(await fn(), text.actionFailed ?? "ดำเนินการไม่สำเร็จ"); if (failed) { toast.error(failed); return false; } return true; } catch (err) { // server action ที่ redirect() = สำเร็จ (Next นำทางให้แล้ว) — ไม่แสดง "NEXT_REDIRECT" if (isNavigationError(err)) return true; toast.error(errorMessage(err, text.actionFailed ?? "ดำเนินการไม่สำเร็จ")); return false; } finally { setPending(false); } }; /** * ลิงก์ที่มี confirm: ไปหลังยืนยันแล้วเท่านั้น — ไม่ได้ผ่าน `` (ที่ React กัน javascript: ให้) * จึงต้องรับเฉพาะ http(s) และ path ภายในเอง */ const follow = (item: ActionMenuCommand) => { const url = safeNavigableUrl(item.href); if (!url) return; if (item.external) window.open(url, "_blank", "noopener,noreferrer"); else window.location.assign(url); }; const command = (item: ActionMenuCommand) => { if (item.confirm) setConfirming(item); else if (!item.href) void run(item.onClick); }; const spec = confirming?.confirm ? normalizeConfirm(confirming.confirm) : null; const dialog = spec && confirming ? ( !o && setConfirming(null)} title={spec.title} description={spec.description} confirmLabel={spec.confirmLabel} cancelLabel={spec.cancelLabel} requireText={spec.requireText} requireTextLabel={spec.requireTextLabel} destructive={spec.destructive ?? confirming.destructive} pending={pending} sheetOnMobile={sheetOnMobile} onConfirm={async () => { // ลิงก์แท็บใหม่ที่ไม่มี onClick: เปิดทันทีใน event คลิก (หลัง await เบราว์เซอร์บล็อก popup) if (confirming.external && !confirming.onClick) { setConfirming(null); follow(confirming); return; } // ล้มเหลว = คง dialog ไว้ให้ลองใหม่ (toast แจ้งแล้วใน run) if (!(await run(confirming.onClick))) return; setConfirming(null); follow(confirming); }} /> ) : null; return { command, dialog }; } /** รายการในเมนู (dropdown หรือคลิกขวา) — เรียกตัวเองสำหรับเมนูย่อย */ function MenuItems({ items, parts: P, onCommand, }: { items: ReadonlyArray; parts: MenuParts; onCommand: (item: ActionMenuCommand) => void; }) { return items.map((item, i) => { // biome-ignore-start lint/suspicious/noArrayIndexKey: รายการไม่มี id (label เป็น ReactNode) — ลำดับคงที่ตาม items if (item.type === "separator") return ; if (item.type === "label") return {item.label}; if (item.type === "sub") { return ( {item.icon} {item.label} ); } if (item.type === "radio") { return ( {item.label !== undefined && {item.label}} item.onValueChange(String(value))} > {item.options.map((option) => ( {option.icon} {option.label} ))} ); } if (item.type === "checkbox") { return ( item.onCheckedChange(checked)} disabled={item.disabled} closeOnClick={false} > {item.icon} {item.label} ); } return ( ) : undefined } onClick={() => onCommand(item)} > {item.icon} {item.label} {item.shortcut && {item.shortcut}} ); // biome-ignore-end lint/suspicious/noArrayIndexKey: — }); } /** รายการบนแผ่นล่างของมือถือ: เมนูย่อยกลายเป็นหัวกลุ่ม, radio/checkbox มี ✓ ท้ายแถว */ function SheetItems({ items, onCommand, close, }: { items: ReadonlyArray; onCommand: (item: ActionMenuCommand) => void; close: () => void; }) { const check = {item.icon} {item.label} ); } return ( ); // biome-ignore-end lint/suspicious/noArrayIndexKey: — }); } /** * เมนูคำสั่งสำเร็จรูปจาก array (แทน DropdownMenu/Trigger/Content/Item/Separator/Label/Shortcut/Sub/Radio/Checkbox) * trigger ไม่ส่ง = ปุ่มไอคอน "…" (aria-label จาก `label` หรือ UiText.actions) * แถวในตาราง/รายการที่เรียก server action + refresh ใช้ `RowActions` (`@suphark/ui/next`) · เมนูคลิกขวา = `ContextActionMenu` * * , onClick: edit }, * { type: "sub", label: "ย้ายไป", items: projects.map((p) => ({ label: p.name, onClick: () => move(p.id) })) }, * { type: "radio", label: "เรียงตาม", value: sort, onValueChange: setSort, options: [{ value: "due", label: "วันครบกำหนด" }] }, * { type: "checkbox", label: "แสดงงานที่เสร็จแล้ว", checked: showDone, onCheckedChange: setShowDone }, * { type: "separator" }, * { label: "ลบ", icon: , destructive: true, confirm: "ลบรายการนี้?", onClick: remove }, * ]} /> */ export function ActionMenu({ items, trigger, label, align = "end", sheetOnMobile: sheetOnMobileProp, sheetTitle, }: { items: ReadonlyArray; trigger?: ReactElement; /** aria-label ของปุ่ม … (ค่าเริ่มต้น UiText.actions) */ label?: string; align?: "start" | "center" | "end"; /** * จอ < 768px แสดงเป็นแผ่นคำสั่งล่าง (Drawer): แถวสูง 48px ไอคอนชิดซ้าย ปุ่ม "ยกเลิก" ล่างสุด ปัดลงเพื่อปิด — * จอกว้างยังเป็น dropdown เดิม · ค่าเริ่มต้น false (ไม่เปลี่ยนแอปเดิม) */ sheetOnMobile?: boolean; /** หัวแผ่น (เช่นชื่อแถว) — ไม่ส่ง = ไม่แสดงหัว แต่โปรแกรมอ่านจอยังได้ชื่อจาก `label` */ sheetTitle?: string; }) { const sheetOnMobile = useSheetOnMobile(sheetOnMobileProp); const text = useUiText(); const isMobile = useIsMobile(); // Drawer โหลดเมื่อต้องใช้ — ระหว่างโหลดเป็น dropdown ไปก่อน const drawer = useLazyDrawer(sheetOnMobile && isMobile); const [menuOpen, setMenuOpen] = useState(false); // เปิด dropdown ไว้ก่อน Drawer โหลดเสร็จ = เป็น dropdown ต่อจนปิด (ไม่สลับรูปกลางทาง) const sheet = useFrozenWhileOpen(sheetOnMobile && isMobile && drawer !== null, menuOpen); const { command, dialog } = useCommandRunner(sheetOnMobile); const triggerElement = trigger ?? ( ); return ( <> {sheet && drawer ? ( setMenuOpen(false)} /> ) : ( )} {dialog} ); } /** * เมนูคลิกขวา (และกดค้างบนจอสัมผัส) จาก array เดียวกับ `ActionMenu` — ครอบพื้นที่ที่ต้องการด้วย children * ใช้คู่ปุ่ม … (`ActionMenu items={sameItems}`) เสมอ: คลิกขวาค้นพบยากและใช้คีย์บอร์ดไม่ได้ * * */ export function ContextActionMenu({ items, children, className, disabled = false, }: { items: ReadonlyArray; children: ReactNode; /** class ของพื้นที่ที่คลิกขวาได้ (ตัวครอบเป็น `div`) */ className?: string; /** ปิดเมนูคลิกขวา (เช่น ระหว่างลาก) — เมนูของเบราว์เซอร์กลับมาทำงาน */ disabled?: boolean; }) { const { command, dialog } = useCommandRunner(false); if (disabled) return
{children}
; return ( <> {children} {dialog} ); }