trail save fix
This commit is contained in:
31
web/src/lib/stores/toast_store.svelte.ts
Normal file
31
web/src/lib/stores/toast_store.svelte.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import cryptoRandomString from "crypto-random-string";
|
||||
|
||||
export type Toast = {
|
||||
id?: string;
|
||||
type: "info" | "success" | "warning" | "error"
|
||||
icon: string;
|
||||
text: string;
|
||||
}
|
||||
|
||||
class ToastStore {
|
||||
toasts: Toast[] = $state([]);
|
||||
}
|
||||
|
||||
export const toastStore = new ToastStore()
|
||||
|
||||
|
||||
export function show_toast(newToast: Toast, duration: number = 3000) {
|
||||
newToast.id = cryptoRandomString({ length: 15 })
|
||||
toastStore.toasts.push(newToast);
|
||||
|
||||
setTimeout(() => {
|
||||
closeToast(newToast)
|
||||
}, duration);
|
||||
}
|
||||
|
||||
export function closeToast(toast: Toast) {
|
||||
const toastIndex = toastStore.toasts.findIndex(t => t.id == toast.id)
|
||||
console.log(toastIndex);
|
||||
|
||||
toastStore.toasts.splice(toastIndex, 1)
|
||||
}
|
||||
Reference in New Issue
Block a user