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; // TODO make `props` optional if component only has `onClose` // type OptionalIfEmpty = keyof T extends never ? undefined : T; class ModalManager { open>(Component: Component, props: Omit) { 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();