about summary refs log tree commit diff
path: root/assets/src/state/toast.ts
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2022-02-21 01:56:43 +0100
committerMel <einebeere@gmail.com>2022-02-21 01:56:43 +0100
commitacd85f6373dbf0edbc2cce5c9256464e5986abf0 (patch)
treea2b96efa7e2bad416650a4017b2bb5ac913ca9f3 /assets/src/state/toast.ts
parent20cf91ce9f49c994fae9448c4ae6f93d4bc79323 (diff)
downloadrook-acd85f6373dbf0edbc2cce5c9256464e5986abf0.tar.zst
rook-acd85f6373dbf0edbc2cce5c9256464e5986abf0.zip
Add toasts
Diffstat (limited to 'assets/src/state/toast.ts')
-rw-r--r--assets/src/state/toast.ts42
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));
+}