import { useEffect } from 'react';
import { ToastEntry } from './types';
interface Props {
toasts: ToastEntry[];
onDismiss: (id: number) => void;
}
export function ToastStack({ toasts, onDismiss }: Props) {
return (
{toasts.map((t) => (
onDismiss(t.id)} />
))}
);
}
function Toast({ entry, onDismiss }: { entry: ToastEntry; onDismiss: () => void }) {
useEffect(() => {
if (entry.kind === 'error') return;
const timer = setTimeout(onDismiss, 4500);
return () => clearTimeout(timer);
}, [entry.kind, onDismiss]);
return (
{entry.text}
{entry.details && entry.details.length > 0 && (
{entry.details.slice(0, 8).map((d, i) => - {d}
)}
{entry.details.length > 8 && - … {entry.details.length - 8} more
}
)}
);
}
let nextId = 1;
export function makeToast(kind: ToastEntry['kind'], text: string, details?: string[]): ToastEntry {
return { id: nextId++, kind, text, details };
}