'use client'; import React, { createContext, useContext, useState, useCallback } from 'react'; import { AlertCircle, CheckCircle, Info, X, AlertTriangle } from 'lucide-react'; type ToastType = 'success' | 'error' | 'info' | 'warning'; interface Toast { id: string; message: string; type: ToastType; title?: string; } interface ToastContextType { toasts: Toast[]; addToast: (message: string, type: ToastType, title?: string) => void; removeToast: (id: string) => 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 addToast = useCallback((message: string, type: ToastType, title?: string) => { const id = Math.random().toString(36).substring(2, 9); setToasts((prev) => [...prev, { id, message, type, title }]); // Auto-remove toast after 5 seconds setTimeout(() => { removeToast(id); }, 5000); }, []); const removeToast = useCallback((id: string) => { setToasts((prev) => prev.filter((toast) => toast.id !== id)); }, []); return ( {children} ); }; const getToastIcon = (type: ToastType) => { switch (type) { case 'success': return ; case 'error': return ; case 'warning': return ; case 'info': return ; } }; const getToastColor = (type: ToastType) => { switch (type) { case 'success': return 'bg-success'; case 'error': return 'bg-error'; case 'warning': return 'bg-warning'; case 'info': return 'bg-info'; } }; const ToastContainer: React.FC<{ toasts: Toast[]; removeToast: (id: string) => void }> = ({ toasts, removeToast, }) => { if (toasts.length === 0) return null; return (
{toasts.map((toast) => (
{getToastIcon(toast.type)}
{toast.title &&

{toast.title}

}

{toast.message}

))}
); };