"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { SecretReveal } from "@suphark/ui/components/shared/secret-reveal"; import { useSheetOnMobile } from "@suphark/ui/components/shared/ui-defaults"; import { AlertDialog, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@suphark/ui/components/ui/alert-dialog"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@suphark/ui/components/ui/dialog"; import { useFrozenWhileOpen } from "@suphark/ui/hooks/use-frozen-while-open"; import { type DrawerParts, useLazyDrawer } from "@suphark/ui/hooks/use-lazy-drawer"; import { useIsMobile } from "@suphark/ui/hooks/use-mobile"; import { errorMessage, isNavigationError } from "@suphark/ui/lib/action-result"; import { cn } from "@suphark/ui/lib/utils"; import { XIcon } from "lucide-react"; import { useRouter } from "next/navigation"; import { type MutableRefObject, type ReactElement, type ReactNode, startTransition, useActionState, useEffect, useRef, useState, } from "react"; import { useFormInvalidNotice } from "./form/form-invalid-notice"; import { type FormAction, FormMessage, type FormRenderContext, type FormState, formRenderContext, } from "./form-message"; import { useUiText } from "./ui-text"; /** * ฟอร์ม "สร้าง/แก้ไข" ในกล่องโต้ตอบ — เปิดจากปุ่ม (trigger) ปิดเองเมื่อสำเร็จ * ใช้แทนฟอร์มที่เคยวางค้างไว้บนหน้า เพื่อให้หน้า list สะอาดและมีการกระทำหลักอันเดียว */ export function FormDialog({ trigger, title, description, action, submitLabel, children, open: controlledOpen, onOpenChange, keepOpen = false, renderSuccess, revealSuccess = false, successTitle, successDescription, contentClassName, sheetOnMobile: sheetOnMobileProp, confirmDiscard = false, }: { /** ปุ่มเปิด (ต้องเป็น ); } return (
{ e.preventDefault(); const fd = new FormData(e.currentTarget); setSubmitted((n) => n + 1); startTransition(() => formAction(fd)); }} className={cn("flex min-h-0 flex-1 flex-col", sheet ? "gap-3" : "gap-4")} > {sheet ? (
{title} {description && ( {description} )}
) : ( {title} {description && {description}} )}
{typeof children === "function" ? children(formRenderContext(state, pending)) : children}
{invalid.notice}
{confirmDiscard && ( // อยู่ในเนื้อกล่อง (ต้นไม้ React เดียวกัน) เพื่อไม่ให้คลิกในกล่องถามถูกนับเป็น "คลิกนอกกล่อง" ของฟอร์ม {text.formDialog?.discardTitle ?? "ทิ้งการแก้ไขที่ยังไม่บันทึก?"} {text.formDialog?.discardHint ?? "ข้อมูลที่กรอกไว้จะหายไป"} {text.formDialog?.keepEditing ?? "กลับไปแก้ต่อ"} )}
); } /** `data` ของ server action เป็น `{ values: Record }` ไหม (ใช้กับ `revealSuccess`) */ function revealableValues(data: unknown): Record | null { if (!data || typeof data !== "object" || !("values" in data)) return null; const { values } = data as { values: unknown }; if (!values || typeof values !== "object") return null; const entries = Object.entries(values as Record).filter( ([, v]) => typeof v === "string", ) as Array<[string, string]>; return entries.length > 0 ? Object.fromEntries(entries) : null; }