"use client"; import * as Toast from "@radix-ui/react-toast"; import { createContext, useContext, useState, useCallback } from "react"; import { X } from "lucide-react"; import { cn } from "@/lib/utils"; interface ToastMessage { id: string; title: string; description?: string; variant?: "default" | "destructive"; } interface ToastContextValue { toast: (message: Omit) => void; } const ToastContext = createContext({ toast: () => {} }); export function ToastProvider({ children }: { children: React.ReactNode }) { const [toasts, setToasts] = useState([]); const toast = useCallback((message: Omit) => { const id = Math.random().toString(36).slice(2); setToasts((prev) => [...prev, { ...message, id }]); setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 5000); }, []); return ( {children} {toasts.map((t) => (
{t.title} {t.description && ( {t.description} )}
))}
); } export const useToast = () => useContext(ToastContext);