import React, { createContext, useContext, useState, useCallback } from 'react'; import { X, Check, AlertCircle, Info } from 'lucide-react'; type ToastType = 'success' | 'error' | 'info' | 'warning'; interface Toast { id: string; message: string; type: ToastType; } interface ToastContextType { showToast: (message: string, type?: ToastType) => void; } const ToastContext = createContext(undefined); export const useToast = () => { const context = useContext(ToastContext); if (!context) { throw new Error('useToast must be used within a ToastProvider'); } return context; }; export const ToastProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const [toasts, setToasts] = useState([]); const showToast = useCallback((message: string, type: ToastType = 'info') => { const id = Math.random().toString(36).substring(2, 9); setToasts((prev) => [...prev, { id, message, type }]); // Auto remove after 3 seconds setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 3000); }, []); const removeToast = (id: string) => { setToasts((prev) => prev.filter((t) => t.id !== id)); }; return ( {children}
{toasts.map((toast) => (
{toast.type === 'success' && } {toast.type === 'error' && } {toast.type === 'warning' && } {toast.type === 'info' && }
{toast.message}
))}
); };