'use client'; import { createContext, useContext, useState, useCallback, useRef, useEffect } from 'react'; import { CheckCircle2, AlertTriangle, Info, X, AlertCircle } from 'lucide-react'; type ToastType = 'success' | 'error' | 'warning' | 'info'; interface Toast { id: string; message: string; type: ToastType; duration?: number; } interface ConfirmOptions { title: string; message: string; confirmLabel?: string; cancelLabel?: string; danger?: boolean; } interface ToastContextType { toast: (message: string, type?: ToastType, duration?: number) => void; confirm: (options: ConfirmOptions) => Promise; } const ToastContext = createContext(null); export function useToast() { const ctx = useContext(ToastContext); if (!ctx) throw new Error('useToast must be used within ToastProvider'); return ctx; } const icons: Record = { success: , error: , warning: , info: , }; const bgColors: Record = { success: 'bg-emerald-50 border-emerald-200', error: 'bg-red-50 border-red-200', warning: 'bg-amber-50 border-amber-200', info: 'bg-blue-50 border-blue-200', }; export function ToastProvider({ children }: { children: React.ReactNode }) { const [toasts, setToasts] = useState([]); const [confirmState, setConfirmState] = useState<{ options: ConfirmOptions; resolve: (value: boolean) => void; } | null>(null); const toast = useCallback((message: string, type: ToastType = 'info', duration = 4000) => { const id = Math.random().toString(36).slice(2) + Date.now().toString(36); setToasts(prev => [...prev, { id, message, type, duration }]); if (duration > 0) { setTimeout(() => { setToasts(prev => prev.filter(t => t.id !== id)); }, duration); } }, []); const confirm = useCallback((options: ConfirmOptions): Promise => { return new Promise((resolve) => { setConfirmState({ options, resolve }); }); }, []); const handleConfirm = (result: boolean) => { confirmState?.resolve(result); setConfirmState(null); }; const removeToast = (id: string) => { setToasts(prev => prev.filter(t => t.id !== id)); }; return ( {children} {/* Toast Container */}
{toasts.map((t) => (
{icons[t.type]}

{t.message}

))}
{/* Confirm Modal */} {confirmState && (
{confirmState.options.danger ? : }

{confirmState.options.title}

{confirmState.options.message}

)}
); }