import React, { useMemo } from 'react' import { CheckCircle, XCircle, AlertCircle, Info, X } from 'lucide-react' import { useToastQueue, type ToastType } from './useToastQueue' declare global { interface Window { __addToast?: (message: string, type?: ToastType, duration?: number) => number } } const ICON_COLORS: Record = { success: '#22c55e', error: '#ef4444', warning: '#f59e0b', info: '#6366f1', } export function ToastContainer() { const { toasts, dismissToast: removeToast } = useToastQueue({ exitMs: 400, restorePrevious: false }) const icons: Record = { success: , error: , warning: , info: , } return ( <>
{toasts.map(toast => (
{icons[toast.type] || icons.info} {toast.message}
))}
) } // One object for the life of the component: hooks list `toast` in their effect // dependencies, and a fresh object per render re-ran those effects on every keystroke. export const useToast = () => useMemo(() => { const show = (message: string, type: ToastType, duration?: number) => { if (window.__addToast) { window.__addToast(message, type, duration) } } return { success: (message: string, duration?: number) => show(message, 'success', duration), error: (message: string, duration?: number) => show(message, 'error', duration), warning: (message: string, duration?: number) => show(message, 'warning', duration), info: (message: string, duration?: number) => show(message, 'info', duration), } }, []) export default useToast