diff --git a/src/common/navigate.ts b/src/common/navigate.ts index a134cf57f13c..da636bf58b0a 100644 --- a/src/common/navigate.ts +++ b/src/common/navigate.ts @@ -14,9 +14,16 @@ export interface NavigateOptions { data?: any; } -export const navigate = async (path: string, options?: NavigateOptions) => { +// max time to wait for dialogs to close before navigating +const DIALOG_WAIT_TIMEOUT = 500; + +export const navigate = async ( + path: string, + options?: NavigateOptions, + timestamp = Date.now() +) => { const { history } = mainWindow; - if (history.state?.dialog) { + if (history.state?.dialog && Date.now() - timestamp < DIALOG_WAIT_TIMEOUT) { const closed = await closeAllDialogs(); if (!closed) { // eslint-disable-next-line no-console @@ -26,7 +33,7 @@ export const navigate = async (path: string, options?: NavigateOptions) => { return new Promise((resolve) => { // need to wait for history state to be updated in case a dialog was closed setTimeout(() => { - navigate(path, options).then(resolve); + navigate(path, options, timestamp).then(resolve); }); }); } diff --git a/src/dialogs/make-dialog-manager.ts b/src/dialogs/make-dialog-manager.ts index 5dd52af16645..6a780e0189dd 100644 --- a/src/dialogs/make-dialog-manager.ts +++ b/src/dialogs/make-dialog-manager.ts @@ -225,6 +225,7 @@ export const makeDialogManager = ( }; const _handleClosedFocus = async (ev: HASSDomEvent) => { + if (!LOADED[ev.detail.dialog]) return; const closedFocusTargets = LOADED[ev.detail.dialog].closedFocusTargets; delete LOADED[ev.detail.dialog].closedFocusTargets; if (!closedFocusTargets) return; diff --git a/src/panels/config/helpers/dialog-helper-detail.ts b/src/panels/config/helpers/dialog-helper-detail.ts index 7bce02c8a373..bcbcfd74a81e 100644 --- a/src/panels/config/helpers/dialog-helper-detail.ts +++ b/src/panels/config/helpers/dialog-helper-detail.ts @@ -32,6 +32,7 @@ import { brandsUrl } from "../../../util/brands-url"; import type { Helper, HelperDomain } from "./const"; import { isHelperDomain } from "./const"; import type { ShowDialogHelperDetailParams } from "./show-dialog-helper-detail"; +import { fireEvent } from "../../../common/dom/fire_event"; type HelperCreators = { [domain in HelperDomain]: { @@ -129,6 +130,7 @@ export class DialogHelperDetail extends LitElement { this._error = undefined; this._domain = undefined; this._params = undefined; + fireEvent(this, "dialog-closed", { dialog: this.localName }); } protected render() {