import { createContext, useContext, useState, useCallback, ReactNode } from 'react'; interface Toast { id: number; message: string; type: 'success' | 'error' | 'info'; } interface ToastContextType { showToast: (message: string, type?: 'success' | 'error' | 'info') => void; } const ToastContext = createContext(null); export function useToast() { const context = useContext(ToastContext); if (!context) { throw new Error('useToast must be used within a ToastProvider'); } return context; } interface ToastProviderProps { children: ReactNode; } export function ToastProvider({ children }: ToastProviderProps) { const [toasts, setToasts] = useState([]); const showToast = useCallback((message: string, type: 'success' | 'error' | 'info' = 'success') => { const id = Date.now(); setToasts((prev) => [...prev, { id, message, type }]); // Auto-dismiss after 3 seconds setTimeout(() => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }, 3000); }, []); const removeToast = useCallback((id: number) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }, []); return ( {children} ); } interface ToastContainerProps { toasts: Toast[]; onRemove: (id: number) => void; } function ToastContainer({ toasts, onRemove }: ToastContainerProps) { if (toasts.length === 0) return null; return ( <>
{toasts.map((toast) => (
{toast.type === 'success' && '✓'} {toast.type === 'error' && '✕'} {toast.type === 'info' && 'ℹ'} {toast.message}
))}
); }