Simplify dialog history logic (#23271)

This commit is contained in:
Petar Petrov 2024-12-12 13:41:57 +02:00 committed by GitHub
parent 0a413cba03
commit df4d5a4567
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 26 deletions

View File

@ -125,25 +125,6 @@ export const showDialog = async (
return true;
};
export const showDialogFromHistory = async (dialogTag: string) => {
const dialogState = OPEN_DIALOG_STACK.find(
(state) => state.dialogTag === dialogTag
);
if (dialogState) {
showDialog(
dialogState.element,
dialogState.root,
dialogTag,
dialogState.dialogParams,
dialogState.dialogImport,
false
);
} else {
// remove the dialog from history if already closed
mainWindow.history.back();
}
};
export const closeDialog = async (dialogTag: string): Promise<boolean> => {
if (!(dialogTag in LOADED)) {
return true;

View File

@ -1,10 +1,7 @@
/* eslint-disable no-console */
import type { PropertyValueMap, ReactiveElement } from "lit";
import { mainWindow } from "../common/dom/get_main_window";
import {
closeLastDialog,
showDialogFromHistory,
} from "../dialogs/make-dialog-manager";
import { closeLastDialog } from "../dialogs/make-dialog-manager";
import type { ProvideHassElement } from "../mixins/provide-hass-lit-mixin";
import type { Constructor } from "../types";
@ -43,7 +40,9 @@ export const urlSyncMixin = <
): void {
super.firstUpdated(changedProperties);
if (mainWindow.history.state?.dialog) {
showDialogFromHistory(mainWindow.history.state.dialog);
// this is a page refresh with a dialog open
// the dialog stack must be empty in this case so this state should be cleaned up
mainWindow.history.back();
}
}
@ -59,8 +58,8 @@ export const urlSyncMixin = <
}
if ("dialog" in ev.state) {
// coming to a dialog
// in practice the dialog stack is empty when navigating forward, so this is a no-op
showDialogFromHistory(ev.state.dialog);
// the dialog stack must be empty in this case so this state should be cleaned up
mainWindow.history.back();
}
}
};