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

{toast.message}

))}
); };