aboutsummaryrefslogtreecommitdiff
path: root/web/ui/src/lib/stores/toasts.ts
blob: 27dd8c4231bc626e20ad24ff9012f903b336e9c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { writable } from 'svelte/store';

export type ToastKind = 'info' | 'success' | 'error';

export interface Toast {
  id: number;
  kind: ToastKind;
  message: string;
}

let nextId = 1;

export const toasts = writable<Toast[]>([]);

export function pushToast(message: string, kind: ToastKind = 'info'): void {
  const id = nextId++;
  toasts.update((list) => [...list, { id, kind, message }]);
  setTimeout(() => {
    toasts.update((list) => list.filter((t) => t.id !== id));
  }, 2500);
}