mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 23:49:33 +01:00
web: add onBeforeOpen hook to dialog manager
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user