import React, { useEffect, useState } from "react"; export function toast({ title, message, kind = "info", ms = 4500 } = {}) { window.dispatchEvent(new CustomEvent("sw:toast", { detail: { title, message, kind, ms } })); } export default function ToastHost() { const [items, setItems] = useState([]); useEffect(() => { const onToast = (e) => { const id = Math.random().toString(16).slice(2); const it = { id, ...e.detail }; setItems((p) => [it, ...p].slice(0, 3)); setTimeout(() => setItems((p) => p.filter((x) => x.id !== id)), it.ms || 4500); }; window.addEventListener("sw:toast", onToast); return () => window.removeEventListener("sw:toast", onToast); }, []); if (!items.length) return null; return (