mirror of
https://github.com/immich-app/immich.git
synced 2025-11-02 18:47:07 -05:00
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
import ConfirmDialog from '$lib/components/shared-components/dialog/confirm-dialog.svelte';
|
|
import { mount, unmount, type Component, type ComponentProps } from 'svelte';
|
|
|
|
type OnCloseData<T> = T extends { onClose: (data: infer R) => void } ? R : never;
|
|
// TODO make `props` optional if component only has `onClose`
|
|
// type OptionalIfEmpty<T extends object> = keyof T extends never ? undefined : T;
|
|
|
|
class ModalManager {
|
|
open<T extends object, K = OnCloseData<T>>(Component: Component<T>, props: Omit<T, 'onClose'>) {
|
|
return new Promise<K>((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<ComponentProps<typeof ConfirmDialog>, 'onClose'>) {
|
|
return this.open(ConfirmDialog, options);
|
|
}
|
|
}
|
|
|
|
export const modalManager = new ModalManager();
|