web: add onBeforeOpen hook to dialog manager

This commit is contained in:
Abdullah Atta
2025-08-04 11:54:38 +05:00
parent 8bb9308173
commit 31bb78bfca

View File

@@ -24,6 +24,17 @@ export interface BaseDialogProps<T = unknown> {
onClose: (result: T) => void;
}
type PropsWithoutOnClose<Props extends BaseDialogProps<any>> = Omit<
Props,
"onClose"
>;
type DialogOptions<Props extends BaseDialogProps<any>> = {
onBeforeOpen?: (
props: PropsWithoutOnClose<Props>
) => boolean | Promise<boolean>;
};
class _DialogManager {
private openedDialogs: Map<React.JSXElementConstructor<any>, () => void> =
new Map();
@@ -83,10 +94,15 @@ class _DialogManager {
}
register<Props extends BaseDialogProps<any>>(
component: React.ComponentType<Props>
component: React.ComponentType<Props>,
options?: DialogOptions<Props>
) {
return {
show: (props: Omit<Props, "onClose">) => this.open(component, props),
show: async (props: PropsWithoutOnClose<Props>) => {
if (options?.onBeforeOpen && !(await options?.onBeforeOpen?.(props)))
return false;
return await this.open(component, props);
},
close: () => {
const dialog = this.openedDialogs.get(component);
if (!dialog) return;