"use client"; import { createContext, useCallback, useContext, useMemo, useState, type ReactNode, } from "react"; import { CheckCircle2, Info, X, XCircle } from "lucide-react"; import { Button } from "@/components/ui/button"; type ToastType = "success" | "error" | "info"; type Toast = { id: number; type: ToastType; title: string; description?: string; }; type ToastInput = Omit; type ToastContextValue = { showToast: (toast: ToastInput) => void; }; const ToastContext = createContext(null); const iconByType = { success: CheckCircle2, error: XCircle, info: Info, }; const colorByType = { success: "border-emerald-200 bg-emerald-50 text-emerald-950", error: "border-red-200 bg-red-50 text-red-950", info: "border-slate-200 bg-white text-slate-950", }; export function ToastProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]); const dismissToast = useCallback((id: number) => { setToasts((current) => current.filter((toast) => toast.id !== id)); }, []); const showToast = useCallback((toast: ToastInput) => { const id = Date.now() + Math.floor(Math.random() * 1000); setToasts((current) => [...current, { ...toast, id }]); window.setTimeout(() => dismissToast(id), 5000); }, [dismissToast]); const value = useMemo(() => ({ showToast }), [showToast]); return ( {children}
{toasts.map((toast) => { const Icon = iconByType[toast.type]; return (

{toast.title}

{toast.description && (

{toast.description}

)}
); })}
); } export function useToast() { const context = useContext(ToastContext); if (!context) { throw new Error("useToast muss innerhalb des ToastProvider verwendet werden"); } return context; }