import React from 'react'; type Toast = { id: number; text: string; type?: 'success'|'error' }; const Ctx = React.createContext<{ push: (t: Omit) => void } | null>(null); export function ToastProvider({ children }: { children: React.ReactNode }) { const [list, setList] = React.useState([]); const push = React.useCallback((t: Omit) => { const id = Date.now() + Math.random(); setList((arr) => [...arr, { id, ...t }]); setTimeout(() => setList((arr) => arr.filter((x) => x.id !== id)), 3000); }, []); React.useEffect(() => { const onEvt = (e: CustomEvent>) => push(e.detail); window.addEventListener('guest-toast', onEvt); return () => window.removeEventListener('guest-toast', onEvt); }, [push]); return ( {children}
{list.map((t) => (
{t.text}
))}
); } export function useToast() { const ctx = React.useContext(Ctx); if (!ctx) throw new Error('ToastProvider missing'); return ctx; }