import React, { createContext, useContext, useState, ReactNode } from 'react'; import ConfirmDialog from '../components/ConfirmDialog'; interface ConfirmOptions { title?: string; message: string; confirmLabel?: string; cancelLabel?: string; } interface ConfirmContextType { confirm: (options: ConfirmOptions | string) => Promise; } const ConfirmContext = createContext(undefined); export const useConfirm = () => { const context = useContext(ConfirmContext); if (!context) { throw new Error('useConfirm must be used within a ConfirmProvider'); } return context; }; interface ConfirmProviderProps { children: ReactNode; } export const ConfirmProvider: React.FC = ({ children }) => { const [options, setOptions] = useState(null); const [resolveRef, setResolveRef] = useState<((value: boolean) => void) | null>(null); const confirm = (opts: ConfirmOptions | string): Promise => { return new Promise((resolve) => { if (typeof opts === 'string') { setOptions({ message: opts }); } else { setOptions(opts); } setResolveRef(() => resolve); }); }; const handleConfirm = () => { if (resolveRef) resolveRef(true); setOptions(null); setResolveRef(null); }; const handleCancel = () => { if (resolveRef) resolveRef(false); setOptions(null); setResolveRef(null); }; return ( {children} {options && ( )} ); };