"use client"; import { createContext, useContext, useState, useCallback, ReactNode } from "react"; import { X } from "lucide-react"; import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert"; interface Toast { id: string; type: "success" | "error" | "info"; title: string; description?: string; } interface ToastContextType { toasts: Toast[]; addToast: (toast: Omit) => void; removeToast: (id: string) => void; } const ToastContext = createContext(null); export function ToastProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]); const addToast = useCallback((toast: Omit) => { const id = Math.random().toString(36).substring(2, 9); setToasts((prev) => [...prev, { ...toast, id }]); // Auto remove after 4 seconds setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 4000); }, []); const removeToast = useCallback((id: string) => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, []); return ( {children} {/* Toast Container */}
{toasts.map((toast) => ( {toast.title} {toast.description && ( {toast.description} )}
))}
); } export function useToast() { const context = useContext(ToastContext); if (!context) { throw new Error("useToast must be used within a ToastProvider"); } return context; }