"use client"; import { useSheetOnMobile } from "@suphark/ui/components/shared/ui-defaults"; import { type ReactNode, useState } from "react"; import { FormDialog } from "./form-dialog"; import type { FormState } from "./form-message"; import { type RowAction, RowActions } from "./row-actions"; export type RowMenuItem = RowAction | { label: string; dialog: string; disabled?: boolean }; export interface RowMenuDialog { title: ReactNode; description?: ReactNode; action: (prev: FormState, formData: FormData) => Promise; submitLabel?: string; children: ReactNode; } /** * เมนู "…" ที่บางรายการเปิด dialog ฟอร์ม — ใช้จาก server component ได้ * (รายการอ้าง dialog ด้วย key; children ของ dialog เป็น JSX ที่ render จาก server ได้) */ export function RowMenu({ items, dialogs = {}, label, sheetOnMobile: sheetOnMobileProp, sheetTitle, }: { items: RowMenuItem[]; dialogs?: Record; label?: string; /** จอ < 768px เมนูเป็นแผ่นคำสั่งล่าง และ dialog ฟอร์มเป็นแผ่นล่าง (ส่งต่อให้ `RowActions` + `FormDialog`) */ sheetOnMobile?: boolean; /** หัวของแผ่นคำสั่งบนมือถือ (ดู `RowActions sheetTitle`) */ sheetTitle?: string; }) { const sheetOnMobile = useSheetOnMobile(sheetOnMobileProp); const [open, setOpen] = useState(null); const actions: RowAction[] = items.map((it) => "dialog" in it ? { label: it.label, disabled: it.disabled, onSelect: () => setOpen(it.dialog) } : it, ); return ( <> {Object.entries(dialogs).map(([key, d]) => ( setOpen(o ? key : null)} title={d.title} description={d.description} action={d.action} submitLabel={d.submitLabel} sheetOnMobile={sheetOnMobile} > {d.children} ))} ); }