diff --git a/src/common/navigate.ts b/src/common/navigate.ts index 34c1663269..a68fd8cb58 100644 --- a/src/common/navigate.ts +++ b/src/common/navigate.ts @@ -14,23 +14,20 @@ export interface NavigateOptions { data?: any; } -export const navigate = async (path: string, options?: NavigateOptions) => - new Promise((resolve) => { - // need to wait for history state to be updated in case a dialog was closed before calling navigate +export const navigate = async (path: string, options?: NavigateOptions) => { + const { history } = mainWindow; + if (history.state?.dialog) { + const closed = await closeAllDialogs(); + if (!closed) { + // eslint-disable-next-line no-console + console.warn("Navigation blocked, because dialog refused to close"); + return false; + } + } + return new Promise((resolve) => { + // need to wait for history state to be updated in case a dialog was closed setTimeout(async () => { - const { history } = mainWindow; - let replace = options?.replace || false; - if (history.state?.dialog) { - const closed = await closeAllDialogs(); - if (!closed) { - // eslint-disable-next-line no-console - console.warn("Navigation blocked, because dialog refused to close"); - resolve(false); - return; - } - // if there were open dialogs, we discard the current state - replace = true; - } + const replace = options?.replace || false; if (__DEMO__) { if (replace) { @@ -57,3 +54,4 @@ export const navigate = async (path: string, options?: NavigateOptions) => resolve(true); }); }); +}; diff --git a/src/dialogs/make-dialog-manager.ts b/src/dialogs/make-dialog-manager.ts index 1dbc51be58..942b7d751e 100644 --- a/src/dialogs/make-dialog-manager.ts +++ b/src/dialogs/make-dialog-manager.ts @@ -177,9 +177,9 @@ export const closeLastDialog = async () => { }; export const closeAllDialogs = async () => { - while (OPEN_DIALOG_STACK.length) { + for (let i = OPEN_DIALOG_STACK.length - 1; i >= 0; i--) { // eslint-disable-next-line no-await-in-loop - const closed = await closeLastDialog(); + const closed = await closeDialog(OPEN_DIALOG_STACK[i].dialogTag); if (!closed) { return false; } @@ -187,7 +187,7 @@ export const closeAllDialogs = async () => { return true; }; -const _handleClosed = async (ev: HASSDomEvent) => { +const _handleClosed = (ev: HASSDomEvent) => { // If not closed by navigating back, remove the open state from history const dialogIndex = OPEN_DIALOG_STACK.findIndex( (state) => state.dialogTag === ev.detail.dialog