import { create } from 'zustand'; import { AlertDialog as AD } from '@base-ui/react/alert-dialog'; import { Button } from '@/components/ui/button'; // 전역 확인 다이얼로그. 네이티브 window.confirm 대신 shadcn 풍 AlertDialog(base-ui)로 띄운다. // showToast 처럼 명령형으로 어디서든(컴포넌트/훅) 호출하고 Promise 을 받는다. // if (await confirm({ title: '삭제하시겠습니까?', destructive: true })) { ... } export type ConfirmOptions = { title: string; description?: string; confirmText?: string; // 기본 '확인' cancelText?: string; // 기본 '취소' destructive?: boolean; // true 면 확인 버튼 빨강 }; type ConfirmStore = { open: boolean; options: ConfirmOptions | null; resolve: ((ok: boolean) => void) | null; request: (options: ConfirmOptions) => Promise; settle: (ok: boolean) => void; }; const useConfirmStore = create((set, get) => ({ open: false, options: null, resolve: null, request: (options) => new Promise((resolve) => set({ open: true, options, resolve })), settle: (ok) => { get().resolve?.(ok); set({ open: false, resolve: null }); }, })); export function confirm(options: ConfirmOptions): Promise { return useConfirmStore.getState().request(options); } // 앱 루트(provider)에 1회 마운트 — sonner 와 같은 역할. export function ConfirmHost() { const { open, options, settle } = useConfirmStore(); return ( { if (!next) settle(false); }}> {options?.title} {options?.description && ( {options.description} )}
); }