import { useState, useEffect, createContext, useContext, useCallback, type ReactNode } from "react"; import { CheckCircle, AlertCircle, Info, X } from "lucide-react"; type ToastType = "success" | "error" | "info"; interface Toast { id: string; type: ToastType; message: string; } interface ToastContextValue { showToast: (type: ToastType, message: string) => void; } const ToastContext = createContext(undefined); export function useToast() { const context = useContext(ToastContext); if (!context) { throw new Error("useToast must be used within a ToastProvider"); } return context; } const icons = { success: CheckCircle, error: AlertCircle, info: Info, }; const colors = { success: "border-green-500/30 bg-green-900/30 text-green-400", error: "border-red-500/30 bg-red-900/30 text-red-400", info: "border-cyan-500/30 bg-cyan-900/30 text-cyan-400", }; export function ToastProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]); const showToast = useCallback((type: ToastType, message: string) => { const id = Math.random().toString(36).substr(2, 9); setToasts((prev) => [...prev, { id, type, message }]); }, []); const removeToast = useCallback((id: string) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }, []); return ( {children}
{toasts.map((toast) => ( removeToast(toast.id)} /> ))}
); } function ToastItem({ toast, onClose }: { toast: Toast; onClose: () => void }) { const Icon = icons[toast.type]; useEffect(() => { const timer = setTimeout(onClose, 5000); return () => clearTimeout(timer); }, [onClose]); return (

{toast.message}

); }