mirror of
https://github.com/immich-app/immich
synced 2025-11-07 17:27:20 +00:00
refactor: introduce modal manager (#18039)
This commit is contained in:
parent
15d431ba6a
commit
62fc5b3c7d
11 changed files with 265 additions and 236 deletions
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();
|
||||
Loading…
Add table
Add a link
Reference in a new issue