Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: add support for closing dialogs imperatively

Abdullah Attab717a64607f0
diff --git a/apps/web/src/common/dialog-manager.tsx b/apps/web/src/common/dialog-manager.tsx
index 9ce63e7a9..84ad329a3 100644
--- a/apps/web/src/common/dialog-manager.tsx
+++ b/apps/web/src/common/dialog-manager.tsx
@@ -25,7 +25,8 @@ export interface BaseDialogProps<T = unknown> {
}
class _DialogManager {
- private openedDialogs: Set<React.JSXElementConstructor<any>> = new Set();
+ private openedDialogs: Map<React.JSXElementConstructor<any>, () => void> =
+ new Map();
open<
Props extends BaseDialogProps<any>,
@@ -39,19 +40,21 @@ class _DialogManager {
const container = document.createElement("div");
const root = createRoot(container);
- const close = () => {
- this.openedDialogs.delete(component);
- root.unmount();
- container.remove();
- };
-
const Dialog = component as React.FunctionComponent<
Omit<Props, "onClose"> & {
onClose: (result: any) => void;
}
>;
- this.openedDialogs.add(component);
- return new Promise<Result>((resolve, reject) =>
+ return new Promise<Result>((resolve, reject) => {
+ const close = () => {
+ this.openedDialogs.delete(component);
+ root.unmount();
+ container.remove();
+ };
+ this.openedDialogs.set(component, () => {
+ close();
+ reject(new Error("Dialog force closed."));
+ });
root.render(
<Dialog
{...props}
@@ -64,8 +67,8 @@ class _DialogManager {
}
}}
/>
- )
- );
+ );
+ });
}
closeAll() {
@@ -80,7 +83,12 @@ class _DialogManager {
component: React.ComponentType<Props>
) {
return {
- show: (props: Omit<Props, "onClose">) => this.open(component, props)
+ show: (props: Omit<Props, "onClose">) => this.open(component, props),
+ close: () => {
+ const dialog = this.openedDialogs.get(component);
+ if (!dialog) return;
+ dialog();
+ }
};
}
}