"use client"; import { useState, useCallback, useRef } from "react"; import { AlertDialog, AlertDialogContent, AlertDialogHeader, AlertDialogTitle, AlertDialogDescription, AlertDialogFooter, AlertDialogCancel, AlertDialogAction, } from "@/components/ui/alert-dialog"; interface UseUnsavedChangesGuardOptions { hasChanges: () => boolean; onClose: () => void; title?: string; description?: string; } interface UnsavedChangesGuard { handleOpenChange: (open: boolean) => void; tryClose: () => void; doClose: () => void; showDialog: boolean; confirmClose: () => void; cancelClose: () => void; title: string; description: string; } export function useUnsavedChangesGuard({ hasChanges, onClose, title = "변경사항이 있습니다", description = "저장하지 않은 변경사항이 사라집니다. 정말 닫으시겠습니까?", }: UseUnsavedChangesGuardOptions): UnsavedChangesGuard { const [showDialog, setShowDialog] = useState(false); const hasChangesRef = useRef(hasChanges); hasChangesRef.current = hasChanges; const attemptClose = useCallback(() => { if (hasChangesRef.current()) { setShowDialog(true); } else { onClose(); } }, [onClose]); const handleOpenChange = useCallback( (open: boolean) => { if (!open) { attemptClose(); } }, [attemptClose], ); const confirmClose = useCallback(() => { setShowDialog(false); onClose(); }, [onClose]); const cancelClose = useCallback(() => { setShowDialog(false); }, []); const doClose = useCallback(() => { setShowDialog(false); onClose(); }, [onClose]); return { handleOpenChange, tryClose: attemptClose, doClose, showDialog, confirmClose, cancelClose, title, description, }; } interface UnsavedChangesDialogProps { guard: UnsavedChangesGuard; } export function UnsavedChangesDialog({ guard }: UnsavedChangesDialogProps) { return ( !open && guard.cancelClose()}> {guard.title} {guard.description} 취소 닫기 ); }