import ConfirmDialog from '$lib/components/shared-components/dialog/confirm-dialog.svelte'; import { mount, unmount, type Component, type ComponentProps } from 'svelte'; type OnCloseData = T extends { onClose: (data: infer R) => void } ? R : never; type OptionalIfEmpty = keyof T extends never ? undefined : T; class ModalManager { open>( Component: Component, props?: OptionalIfEmpty> | Record, ): Promise; open>(Component: Component, props: OptionalIfEmpty>) { return new Promise((resolve) => { let modal: object = {}; const onClose = async (data: K) => { await unmount(modal); resolve(data); }; modal = mount(Component, { target: document.body, props: { ...((props ?? {}) as T), onClose, }, }); }); } openDialog(options: Omit, 'onClose'>) { return this.open(ConfirmDialog, options); } } export const modalManager = new ModalManager();