import { useState, useEffect, useCallback } from 'react'; interface ToastMessage { id: number; text: string; type: 'success' | 'info' | 'error'; } let addToastFn: ((text: string, type?: 'success' | 'info' | 'error') => void) | null = null; /** Show a toast from anywhere: import { showToast } from './Toast'; showToast('Done!'); */ export function showToast(text: string, type: 'success' | 'info' | 'error' = 'success') { if (addToastFn) addToastFn(text, type); } // Also expose globally for non-React (Astro inline scripts) if (typeof window !== 'undefined') { (window as any).__showToast = showToast; } let nextId = 0; export default function ToastContainer() { const [toasts, setToasts] = useState([]); const addToast = useCallback((text: string, type: 'success' | 'info' | 'error' = 'success') => { const id = nextId++; setToasts(prev => [...prev, { id, text, type }]); setTimeout(() => setToasts(prev => prev.filter(t => t.id !== id)), 3500); }, []); useEffect(() => { addToastFn = addToast; (window as any).__showToast = addToast; return () => { addToastFn = null; }; }, [addToast]); if (toasts.length === 0) return null; return (
{toasts.map(t => (
{t.text}
))}
); }