mirror of
https://github.com/immich-app/immich.git
synced 2025-12-18 01:11:07 +03:00
refactor: introduce modal manager (#18039)
This commit is contained in:
33
web/src/lib/managers/modal-manager.svelte.ts
Normal file
33
web/src/lib/managers/modal-manager.svelte.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user