"use client"; import { createContext, useCallback, useContext, useMemo, useState, type ReactNode } from "react"; type ToastItem = { id: string; text: string; tone?: "ok" | "warn" | "err" }; const ToastCtx = createContext<{ push: (text: string, tone?: ToastItem["tone"]) => void } | null>(null); export function ToastProvider({ children }: { children: ReactNode }) { const [items, setItems] = useState([]); const push = useCallback((text: string, tone: ToastItem["tone"] = "ok") => { const id = `${Date.now()}-${Math.random()}`; setItems((prev) => [...prev.slice(-4), { id, text, tone }]); window.setTimeout(() => { setItems((prev) => prev.filter((x) => x.id !== id)); }, 3200); }, []); const value = useMemo(() => ({ push }), [push]); return ( {children}
{items.map((t) => (
{t.text}
))}
); } export function useToast() { const ctx = useContext(ToastCtx); if (!ctx) return { push: (_t: string, _tone?: ToastItem["tone"]) => undefined }; return ctx; }