Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix helper dialog close and add failsafe for similar cases #23468

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading