Fix navigate with replace when there are open dialogs (#23268)

This commit is contained in:
Petar Petrov 2024-12-12 10:25:33 +02:00 committed by GitHub
parent 18cce45b88
commit 93947d76a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 19 deletions

View File

@ -14,23 +14,20 @@ export interface NavigateOptions {
data?: any; data?: any;
} }
export const navigate = async (path: string, options?: NavigateOptions) => export const navigate = async (path: string, options?: NavigateOptions) => {
new Promise<boolean>((resolve) => {
// need to wait for history state to be updated in case a dialog was closed before calling navigate
setTimeout(async () => {
const { history } = mainWindow; const { history } = mainWindow;
let replace = options?.replace || false;
if (history.state?.dialog) { if (history.state?.dialog) {
const closed = await closeAllDialogs(); const closed = await closeAllDialogs();
if (!closed) { if (!closed) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.warn("Navigation blocked, because dialog refused to close"); console.warn("Navigation blocked, because dialog refused to close");
resolve(false); return false;
return;
} }
// if there were open dialogs, we discard the current state
replace = true;
} }
return new Promise<boolean>((resolve) => {
// need to wait for history state to be updated in case a dialog was closed
setTimeout(async () => {
const replace = options?.replace || false;
if (__DEMO__) { if (__DEMO__) {
if (replace) { if (replace) {
@ -57,3 +54,4 @@ export const navigate = async (path: string, options?: NavigateOptions) =>
resolve(true); resolve(true);
}); });
}); });
};

View File

@ -177,9 +177,9 @@ export const closeLastDialog = async () => {
}; };
export const closeAllDialogs = 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 // eslint-disable-next-line no-await-in-loop
const closed = await closeLastDialog(); const closed = await closeDialog(OPEN_DIALOG_STACK[i].dialogTag);
if (!closed) { if (!closed) {
return false; return false;
} }
@ -187,7 +187,7 @@ export const closeAllDialogs = async () => {
return true; return true;
}; };
const _handleClosed = async (ev: HASSDomEvent<DialogClosedParams>) => { const _handleClosed = (ev: HASSDomEvent<DialogClosedParams>) => {
// If not closed by navigating back, remove the open state from history // If not closed by navigating back, remove the open state from history
const dialogIndex = OPEN_DIALOG_STACK.findIndex( const dialogIndex = OPEN_DIALOG_STACK.findIndex(
(state) => state.dialogTag === ev.detail.dialog (state) => state.dialogTag === ev.detail.dialog