import {ref, type Ref, type UnwrapNestedRefs} from 'vue'; import {defineStore} from 'pinia'; import {type AdminModalContext} from '../types'; import {type AdminModalKey, NotificationCategory} from '../data'; import {useLoading} from '../composables'; import {useNotificationStore} from './useNotificationStore'; type ModalOpenFn = (modal: K, context?: AdminModalContext) => void; type ModalCloseFn = (modal: K) => void; type ModalStore = { loading: Ref; opened: Ref; context: Ref>; onOpen(callback: ModalOpenFn): void; onClose(callback: ModalCloseFn): void; open(modal: K, newContext?: AdminModalContext): void; close(): void; }; export const useModalStore = defineStore('modal', (): ModalStore => { const {loading} = useLoading(); const opened = ref(null); const context = ref(null); const openHooks = ref([]); const closeHooks = ref([]); return { loading, opened, context, onOpen(callback) { openHooks.value.push(callback); }, onClose(callback) { closeHooks.value.push(callback); }, open(modal, newContext) { // TODO: fix excessive depth error // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore context.value = newContext; opened.value = modal; openHooks.value.forEach((hook) => hook(modal, newContext)); }, close() { useNotificationStore().remove(NotificationCategory.Modal); const modal = opened.value as AdminModalKey; opened.value = null; context.value = null; closeHooks.value.forEach((hook) => hook(modal)); }, }; });