import { useEffect, useState, type ReactNode } from "react"; import { CheckCircle2, XCircle } from "lucide-react"; type Toast = { id: number; kind: "ok" | "err"; text: ReactNode }; const items: Toast[] = []; const subs = new Set<(t: Toast[]) => void>(); let nextId = 1; export function toast(kind: Toast["kind"], text: ReactNode): void { const t: Toast = { id: nextId++, kind, text }; items.push(t); subs.forEach((f) => f([...items])); setTimeout(() => { const i = items.findIndex((x) => x.id === t.id); if (i >= 0) items.splice(i, 1); subs.forEach((f) => f([...items])); }, 4000); } export function Toaster() { const [list, setList] = useState([]); useEffect(() => { subs.add(setList); return () => { subs.delete(setList); }; }, []); return (
{list.map((t) => (
{t.kind === "ok" ? : } {t.text}
))}
); }