"use client"; import { useSheetOnMobile } from "@suphark/ui/components/shared/ui-defaults"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@suphark/ui/components/ui/alert-dialog"; import { Input } from "@suphark/ui/components/ui/input"; import { Label } from "@suphark/ui/components/ui/label"; 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 { cn } from "@suphark/ui/lib/utils"; import { Loader2 } from "lucide-react"; import { type ComponentProps, type ReactNode, useEffect, useId, useState } from "react"; import { Button } from "./button/button"; import { useUiText } from "./ui-text"; /** สเปกการยืนยัน — string = description อย่างเดียว (title/ปุ่มใช้ค่าเริ่มต้นจาก UiText) */ export type ConfirmSpec = | string | { title?: ReactNode; description: ReactNode; confirmLabel?: string; cancelLabel?: string; /** ปุ่มยืนยันสีแดง (RowActions ใช้ `destructive` ของรายการเป็นค่าเริ่มต้นอยู่แล้ว) */ destructive?: boolean; /** ต้องพิมพ์ข้อความนี้ให้ตรงก่อนกดยืนยันได้ (ลบของที่กู้คืนไม่ได้ — แบบ GitHub) */ requireText?: string; /** คำอธิบายเหนือช่องพิมพ์ (ค่าเริ่มต้น "พิมพ์ \"X\" เพื่อยืนยัน") */ requireTextLabel?: ReactNode; }; /** แปลง ConfirmSpec เป็น object เสมอ */ export function normalizeConfirm(spec: ConfirmSpec): Exclude { return typeof spec === "string" ? { description: spec } : spec; } /** * กล่องยืนยันมาตรฐานของแพ็กเกจ (AlertDialog) — ใช้แทน `window.confirm` ทุกที่ * (webview/automation บางตัวปิด window.confirm ทิ้ง กดแล้วไม่เกิดอะไร และไม่เข้า design system) * controlled: `open`/`onOpenChange`; `pending` = กำลังรัน action → ปุ่มยืนยันหมุน ปิดไม่ได้ชั่วคราว */ export function ConfirmDialog({ open, onOpenChange, title, description, confirmLabel, cancelLabel, destructive = false, pending: pendingProp, isLoading, requireText, requireTextLabel, onConfirm, sheetOnMobile: sheetOnMobileProp, finalFocus, }: { open: boolean; onOpenChange: (open: boolean) => void; title?: ReactNode; description: ReactNode; confirmLabel?: string; cancelLabel?: string; /** ปุ่มยืนยันสีแดง (ลบ/เพิกถอน) */ destructive?: boolean; pending?: boolean; /** ชื่อเดียวกับ `pending` (ให้ตรงกับ Button/ActionButton) */ isLoading?: boolean; /** ต้องพิมพ์ข้อความนี้ให้ตรงก่อนปุ่มยืนยันจะกดได้ (ชื่อแอป/โปรเจกต์ที่กำลังจะลบ) */ requireText?: string; requireTextLabel?: ReactNode; onConfirm: () => void; /** * จอ < 768px แสดงเป็นแผ่นล่าง (Drawer): ปุ่มยืนยันบน ปุ่มยกเลิกล่าง เต็มความกว้างสูง 48px, ช่องพิมพ์ยืนยัน 44px, ปัดลงเพื่อยกเลิก * (ปิดไม่ได้ระหว่าง `pending`) — จอกว้างยังเป็นกล่องกลางจอเหมือนเดิม · ค่าเริ่มต้น false */ sheetOnMobile?: boolean; /** * โฟกัสหลังปิด (Base UI `finalFocus`) — ค่าเริ่มต้นคืนไปที่ปุ่มที่เปิด แต่ถ้ายืนยันแล้วปุ่มนั้นหายไปด้วย * (ลบแถว/เลิกปักหมุด) โฟกัสหลุดไปที่ body → ส่ง ref หรือฟังก์ชันที่คืน element ข้างเคียง */ finalFocus?: ComponentProps["finalFocus"]; }) { const sheetOnMobile = useSheetOnMobile(sheetOnMobileProp); const pending = pendingProp ?? isLoading ?? false; const text = useUiText(); const isMobile = useIsMobile(); // Drawer โหลดเมื่อต้องใช้ (ระหว่างโหลดแสดงกล่องกลางจอ) const drawer = useLazyDrawer(sheetOnMobile && isMobile); // ตรึงแบบระหว่างเปิด — หมุนจอแล้วช่องพิมพ์ยืนยัน (requireText) ไม่ remount const sheet = useFrozenWhileOpen(sheetOnMobile && isMobile && drawer !== null, open); const inputId = useId(); const [typed, setTyped] = useState(""); // เปิดใหม่ทุกครั้ง = ล้างสิ่งที่พิมพ์ไว้ (ไม่ให้ยืนยันรายการถัดไปได้ทันที) useEffect(() => { if (!open) setTyped(""); }, [open]); const blocked = Boolean(requireText) && typed.trim() !== requireText; const requireField = requireText ? (
setTyped(e.target.value)} autoComplete="off" spellCheck={false} disabled={pending} className={cn(sheet && "h-11")} />
) : null; if (sheet && drawer) { const { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle } = drawer; return ( !pending && onOpenChange(o)} showSwipeHandle> {title ?? text.confirmTitle ?? "ยืนยันการทำรายการ"} {description} {requireField &&
{requireField}
}
); } return ( !pending && onOpenChange(o)}> {title ?? text.confirmTitle ?? "ยืนยันการทำรายการ"} {description} {requireField} {cancelLabel ?? text.cancel} {pending && ); }