'use client'; import * as React from 'react'; import { X, CheckCircle2, AlertCircle, Info } from 'lucide-react'; import { cn } from '@/lib/utils/cn'; export type ToastType = 'success' | 'error' | 'info'; export interface Toast { id: string; message: string; type: ToastType; } interface ToastContextType { toasts: Toast[]; addToast: (message: string, type?: ToastType) => void; removeToast: (id: string) => void; } const ToastContext = React.createContext(undefined); export function ToastProvider({ children }: { children: React.ReactNode }) { const [toasts, setToasts] = React.useState([]); const addToast = React.useCallback((message: string, type: ToastType = 'success') => { const id = Math.random().toString(36).substring(7); setToasts((prev) => [...prev, { id, message, type }]); // Auto remove after 3 seconds setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 3000); }, []); const removeToast = React.useCallback((id: string) => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, []); return ( {children}
{toasts.map((toast) => ( removeToast(toast.id)} /> ))}
); } function ToastItem({ toast, onClose }: { toast: Toast; onClose: () => void }) { const Icon = toast.type === 'success' ? CheckCircle2 : toast.type === 'error' ? AlertCircle : Info; return (

{toast.message}

); } export function useToast() { const context = React.useContext(ToastContext); if (!context) { throw new Error('useToast must be used within ToastProvider'); } return context; }