import React, { createContext, useContext, useState, useCallback, useEffect } from 'react' import { CheckCircle, XCircle, AlertCircle, Info, X } from 'lucide-react' const ToastContext = createContext(null) let toastIdCounter = 0 export function ToastContainer() { const [toasts, setToasts] = useState([]) const addToast = useCallback((message, type = 'info', duration = 3000) => { const id = ++toastIdCounter setToasts(prev => [...prev, { id, message, type, duration, removing: false }]) if (duration > 0) { setTimeout(() => { setToasts(prev => prev.map(t => t.id === id ? { ...t, removing: true } : t)) setTimeout(() => { setToasts(prev => prev.filter(t => t.id !== id)) }, 300) }, duration) } return id }, []) const removeToast = useCallback((id) => { setToasts(prev => prev.map(t => t.id === id ? { ...t, removing: true } : t)) setTimeout(() => { setToasts(prev => prev.filter(t => t.id !== id)) }, 300) }, []) // Make addToast globally accessible useEffect(() => { window.__addToast = addToast return () => { delete window.__addToast } }, [addToast]) const icons = { success: , error: , warning: , info: , } const bgColors = { success: 'bg-white border-l-4 border-emerald-500', error: 'bg-white border-l-4 border-red-500', warning: 'bg-white border-l-4 border-amber-500', info: 'bg-white border-l-4 border-blue-500', } return (
{toasts.map(toast => (
{icons[toast.type] || icons.info}

{toast.message}

))}
) } export const useToast = () => { const show = useCallback((message, type, duration) => { if (window.__addToast) { window.__addToast(message, type, duration) } }, []) return { success: (message, duration) => show(message, 'success', duration), error: (message, duration) => show(message, 'error', duration), warning: (message, duration) => show(message, 'warning', duration), info: (message, duration) => show(message, 'info', duration), } } export default useToast