teoldalad/lib/stores/modal.ts
2024-03-13 00:30:45 +01:00

39 lines
1.2 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import { browser } from '$app/environment';
import Modal from '$lib/models/modal';
import { toast } from '@zerodevx/svelte-toast';
import { writable } from 'svelte/store';
interface ModalStore {
visible: boolean;
type?: Modal | null;
data?: any;
warn?: boolean | null;
showModal: (type: Modal, data?: any) => void;
hideModal: (noConfirm?: boolean | null) => void;
disableWarn: () => void;
}
export const modalStore = browser
? writable({
visible: false,
type: null,
data: null,
warn: null,
showModal: (type: Modal, data?: any) => {
modalStore?.update((store) => ({ ...store, type, data, visible: true, warn: true }));
},
hideModal: (noConfirm) => {
modalStore?.update((store) => {
if (!noConfirm && store.type === Modal.EditWidget && store.warn) {
toast.push('Click again to dismiss.');
return { ...store, warn: false };
}
return { ...store, type: null, data: null, visible: false, warn: false };
});
},
disableWarn: () => {
modalStore?.update((store) => ({ ...store, warn: false }));
},
} as ModalStore)
: null;