Skip to content

Commit

Permalink
Fix helper dialog close and add failsafe for similar cases (#23468)
Browse files Browse the repository at this point in the history
  • Loading branch information
MindFreeze authored Dec 30, 2024
1 parent d032791 commit e1a6f6f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/common/navigate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +33,7 @@ export const navigate = async (path: string, options?: NavigateOptions) => {
return new Promise<boolean>((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);
});
});
}
Expand Down
1 change: 1 addition & 0 deletions src/dialogs/make-dialog-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ export const makeDialogManager = (
};

const _handleClosedFocus = async (ev: HASSDomEvent<DialogClosedParams>) => {
if (!LOADED[ev.detail.dialog]) return;
const closedFocusTargets = LOADED[ev.detail.dialog].closedFocusTargets;
delete LOADED[ev.detail.dialog].closedFocusTargets;
if (!closedFocusTargets) return;
Expand Down
2 changes: 2 additions & 0 deletions src/panels/config/helpers/dialog-helper-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit e1a6f6f

Please sign in to comment.