diff options
Diffstat (limited to 'assets/src/state')
| -rw-r--r-- | assets/src/state/toast.ts | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/assets/src/state/toast.ts b/assets/src/state/toast.ts new file mode 100644 index 0000000..8eba306 --- /dev/null +++ b/assets/src/state/toast.ts @@ -0,0 +1,42 @@ +import { writable } from "svelte/store"; +import generateId from "../utils/generateId"; + +export enum ToastType { + INFO, + ERROR, +} + +export type ToastData = { + type: ToastType; + message: string; + title?: string; +}; + +export type Toast = { + id: string; +} & ToastData; + +export const toasts = writable<Toast[]>([]); + +export function toast(toast: ToastData): Toast { + const toastWithId: Toast = { + ...toast, + id: generateId(8), + }; + + toasts.update(toasts => [...toasts, toastWithId]); + + // Dissmiss toast after 5 seconds. + setTimeout(() => { + dismissToast(toastWithId); + }, 5000); + + return toastWithId; +} + +// @ts-ignore +window.toast = toast; + +export function dismissToast(toast: Toast) { + toasts.update(toasts => toasts.filter(t => t.id !== toast.id)); +} |
