import { useEffect, useState } from 'react' import { CheckCircle, XCircle, Info, AlertCircle, X } from 'lucide-react' import clsx from 'clsx' export interface Toast { id: string type: 'success' | 'error' | 'info' | 'warning' title: string message?: string duration?: number } // Global toast state (in production, use Zustand or Context) let toastListeners: ((toasts: Toast[]) => void)[] = [] let toastList: Toast[] = [] export const toast = { success: (title: string, message?: string) => addToast('success', title, message), error: (title: string, message?: string) => addToast('error', title, message), info: (title: string, message?: string) => addToast('info', title, message), warning: (title: string, message?: string) => addToast('warning', title, message), } function addToast(type: Toast['type'], title: string, message?: string, duration = 5000) { const id = Date.now().toString() const newToast: Toast = { id, type, title, message, duration } toastList = [...toastList, newToast] toastListeners.forEach(listener => listener(toastList)) // Auto-remove after duration setTimeout(() => { removeToast(id) }, duration) } function removeToast(id: string) { toastList = toastList.filter(t => t.id !== id) toastListeners.forEach(listener => listener(toastList)) } export function ToastContainer() { const [toasts, setToasts] = useState([]) useEffect(() => { const listener = (newToasts: Toast[]) => setToasts(newToasts) toastListeners.push(listener) return () => { toastListeners = toastListeners.filter(l => l !== listener) } }, []) const icons = { success: , error: , info: , warning: , } const colors = { success: 'border-pick bg-pick/10', error: 'border-reject bg-reject/10', info: 'border-primary bg-primary/10', warning: 'border-star bg-star/10', } return (
{toasts.map((toast) => (
{icons[toast.type]}
{toast.title}
{toast.message && (
{toast.message}
)}
))}
) }