"use client"; import { createContext, useCallback, useContext, useRef, useState, type ReactNode, } from "react"; import Icon from "@/components/Icon"; import { cn } from "@/lib/utils"; type ToastType = "success" | "error" | "info"; interface Toast { id: number; message: string; type: ToastType; } const ToastContext = createContext<{ toast: (message: string, type?: ToastType) => void }>({ toast: () => undefined, }); export function useToast() { return useContext(ToastContext); } export function ToastProvider({ children }: { children: ReactNode }) { const [toasts, setToasts] = useState([]); const nextId = useRef(1); const toast = useCallback((message: string, type: ToastType = "success") => { const id = nextId.current++; setToasts((prev) => [...prev.slice(-3), { id, message, type }]); setTimeout(() => { setToasts((prev) => prev.filter((t) => t.id !== id)); }, 3400); }, []); return ( {children}
{toasts.map((t) => (
{t.message}
))}
); }