import React, { createContext, useContext, useState, useCallback } from 'react'; import { CheckCircle2, AlertCircle, Info, X } from 'lucide-react'; const ToastContext = createContext(null); export function ToastProvider({ children }) { const [toasts, setToasts] = useState([]); const addToast = useCallback((msg, type = 'info', duration = 3000) => { const id = Date.now() + Math.random().toString(36).substring(2, 6); setToasts((prev) => [...prev, { id, msg, type }]); if (duration > 0) { setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, duration); } }, []); const removeToast = useCallback((id) => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, []); return ( {children}
{toasts.map((t) => (
{t.type === 'success' && } {t.type === 'error' && } {t.type === 'info' && } {t.msg}
))}
); } export function useToast() { const ctx = useContext(ToastContext); if (!ctx) throw new Error('useToast must be used within ToastProvider'); return ctx; }