import { useEffect, useState, useCallback } from 'react' import { X, CheckCircle, AlertTriangle, Info } from 'lucide-react' import { addToastListener, type ToastMessage } from '../hooks/useSSE' const ICONS = { info: Info, success: CheckCircle, warning: AlertTriangle, } const STYLES = { info: 'border-blue-600 bg-blue-900/40 text-blue-100', success: 'border-emerald-600 bg-emerald-900/40 text-emerald-100', warning: 'border-amber-600 bg-amber-900/40 text-amber-100', } export default function ToastContainer() { const [toasts, setToasts] = useState([]) const remove = useCallback((id: string) => { setToasts(prev => prev.filter(t => t.id !== id)) }, []) useEffect(() => { return addToastListener((toast) => { setToasts(prev => { // Prevent duplicate identical toasts within 3 seconds const now = Date.now() const recent = prev.filter( t => t.title === toast.title && now - parseInt(t.id.split('-').pop() || '0', 10) < 3000 ) if (recent.length > 0) return prev return [...prev.slice(-4), toast] }) // Auto-dismiss after 5 seconds setTimeout(() => remove(toast.id), 5000) }) }, [remove]) return (
{toasts.map((toast) => { const Icon = ICONS[toast.type] return (

{toast.title}

{toast.body && (

{toast.body}

)}
) })}
) }