26 lines
821 B
TypeScript
26 lines
821 B
TypeScript
|
|
export type ToastKind = 'info' | 'error' | 'success';
|
||
|
|
export type Toast = { id: number; kind: ToastKind; message: string };
|
||
|
|
|
||
|
|
class ToastStore {
|
||
|
|
toasts = $state<Toast[]>([]);
|
||
|
|
private nextId = 1;
|
||
|
|
private readonly dismissMs = 3000;
|
||
|
|
|
||
|
|
private push(kind: ToastKind, message: string): number {
|
||
|
|
const id = this.nextId++;
|
||
|
|
this.toasts = [...this.toasts, { id, kind, message }];
|
||
|
|
setTimeout(() => this.dismiss(id), this.dismissMs);
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
|
||
|
|
info(message: string): number { return this.push('info', message); }
|
||
|
|
error(message: string): number { return this.push('error', message); }
|
||
|
|
success(message: string): number { return this.push('success', message); }
|
||
|
|
|
||
|
|
dismiss(id: number): void {
|
||
|
|
this.toasts = this.toasts.filter((t) => t.id !== id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export const toastStore = new ToastStore();
|