From 0325f4e519bc45eb043e9d2d1d499d82900ecb96 Mon Sep 17 00:00:00 2001 From: Maksym Tsymbarov Date: Tue, 20 Oct 2026 17:57:51 +0200 Subject: [PATCH] Fixed dashboard keeping a deleted or renamed state as current after editing states in "Manage dashboard states" (#15912) * Fixed stale dashboard state after state deletion/rename * Resolved PR comments * Moved removed-state handling from dashboard page to state controllers --- .../dashboard-page.component.ts | 8 ++-- .../default-state-controller.component.ts | 41 ++++++----------- .../entity-state-controller.component.ts | 44 +++++++------------ .../states/state-controller.component.ts | 14 +++++- 4 files changed, 46 insertions(+), 61 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts b/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts index fe1d2663c9..c0b63eb8d9 100644 --- a/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts +++ b/ui-ngx/src/app/modules/home/components/dashboard-page/dashboard-page.component.ts @@ -104,6 +104,7 @@ import { } from '@home/components/dashboard-page/states/manage-dashboard-states-dialog.component'; import { ImportExportService } from '@shared/import-export/import-export.service'; import { AuthState } from '@app/core/auth/auth.models'; +import { IStateControllerComponent } from '@home/components/dashboard-page/states/state-controller.models'; import { DashboardReportService } from '@core/http/dashboard-report.service'; import { EntityGroupInfo, resolveGroupParams } from '@shared/models/entity-group.models'; import { UserPermissionsService } from '@core/http/user-permissions.service'; @@ -1198,11 +1199,10 @@ export class DashboardPageComponent extends PageComponent implements IDashboardC private updateStates(states: {[id: string]: DashboardState }) { this.dashboard.configuration.states = states; this.dashboardUtils.removeUnusedWidgets(this.dashboard); - let targetState = this.dashboardCtx.state; - if (!this.dashboard.configuration.states[targetState]) { - targetState = this.dashboardUtils.getRootStateId(this.dashboardConfiguration.states); + if (states[this.dashboardCtx.state]) { + this.openDashboardState(this.dashboardCtx.state); } - this.openDashboardState(targetState); + (this.dashboardCtx.stateController as IStateControllerComponent).states = states; } public importWidget($event: Event) { diff --git a/ui-ngx/src/app/modules/home/components/dashboard-page/states/default-state-controller.component.ts b/ui-ngx/src/app/modules/home/components/dashboard-page/states/default-state-controller.component.ts index 10c2cc4522..a8e96124c3 100644 --- a/ui-ngx/src/app/modules/home/components/dashboard-page/states/default-state-controller.component.ts +++ b/ui-ngx/src/app/modules/home/components/dashboard-page/states/default-state-controller.component.ts @@ -36,9 +36,9 @@ export class DefaultStateControllerComponent extends StateControllerComponent im protected utils: UtilsService, private entityService: EntityService, private mobileService: MobileService, - private dashboardUtils: DashboardUtilsService, + dashboardUtils: DashboardUtilsService, private breadcrumbService: BreadcrumbService) { - super(router, route, utils, window, ngZone, statesControllerService); + super(router, route, utils, window, ngZone, statesControllerService, dashboardUtils); } ngOnInit(): void { @@ -51,7 +51,7 @@ export class DefaultStateControllerComponent extends StateControllerComponent im public init() { if (this.preservedState) { - this.stateObject = this.preservedState; + this.stateObject = this.normalizeStateObject(this.preservedState); setTimeout(() => { this.gotoState(this.stateObject[0].id, true); }, 1); @@ -71,6 +71,13 @@ export class DefaultStateControllerComponent extends StateControllerComponent im } protected onStatesChanged() { + const prevStateId = this.getStateId(); + this.stateObject = this.normalizeStateObject(this.stateObject); + const newStateId = this.getStateId(); + if (newStateId !== prevStateId) { + this.gotoState(newStateId, false); + this.updateStateParam(objToBase64(this.stateObject), true); + } } protected onStateChanged() { @@ -204,33 +211,13 @@ export class DefaultStateControllerComponent extends StateControllerComponent im try { result = base64toObj(stateBase64); } catch (e) { - result = [ { id: null, params: {} } ]; + result = []; } } - if (!result) { - result = []; - } - if (!result.length) { - result[0] = { id: null, params: {} }; - } else if (result.length > 1) { - const newResult = []; - newResult.push(result[result.length - 1]); - result = newResult; - } - const rootStateId = this.dashboardUtils.getRootStateId(this.states); - if (!result[0].id) { - result[0].id = rootStateId; - } - if (!this.states[result[0].id]) { - result[0].id = rootStateId; - } - let i = result.length; - while (i--) { - if (!result[i].id || !this.states[result[i].id]) { - result.splice(i, 1); - } + if (result && result.length > 1) { + result = [ result[result.length - 1] ]; } - return result; + return this.normalizeStateObject(result); } private gotoState(stateId: string, update: boolean, openRightLayout?: boolean) { diff --git a/ui-ngx/src/app/modules/home/components/dashboard-page/states/entity-state-controller.component.ts b/ui-ngx/src/app/modules/home/components/dashboard-page/states/entity-state-controller.component.ts index ed14502b39..03917752a3 100644 --- a/ui-ngx/src/app/modules/home/components/dashboard-page/states/entity-state-controller.component.ts +++ b/ui-ngx/src/app/modules/home/components/dashboard-page/states/entity-state-controller.component.ts @@ -41,9 +41,9 @@ export class EntityStateControllerComponent extends StateControllerComponent imp protected utils: UtilsService, private entityService: EntityService, private mobileService: MobileService, - private dashboardUtils: DashboardUtilsService, + dashboardUtils: DashboardUtilsService, private breadcrumbService: BreadcrumbService) { - super(router, route, utils, window, ngZone, statesControllerService); + super(router, route, utils, window, ngZone, statesControllerService, dashboardUtils); } ngOnInit(): void { @@ -56,7 +56,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp public init() { if (this.preservedState) { - this.stateObject = this.preservedState; + this.stateObject = this.normalizeStateObject(this.preservedState); this.selectedStateIndex = this.stateObject.length - 1; setTimeout(() => { this.gotoState(this.stateObject[this.stateObject.length - 1].id, true); @@ -78,6 +78,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp } protected onStatesChanged() { + const prevStateId = this.getStateId(); + this.stateObject = this.normalizeStateObject(this.stateObject); + this.selectedStateIndex = this.stateObject.length - 1; + const newStateId = this.getStateId(); + if (newStateId !== prevStateId) { + this.gotoState(newStateId, true, undefined, true); + } } protected onStateChanged() { @@ -248,32 +255,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp try { result = base64toObj(stateBase64); } catch (e) { - result = [ { id: null, params: {} } ]; - } - } - if (!result) { - result = []; - } - if (!result.length) { - result[0] = { id: null, params: {} }; - } - const rootStateId = this.dashboardUtils.getRootStateId(this.states); - if (!result[0].id) { - result[0].id = rootStateId; - } - if (!this.states[result[0].id]) { - result[0].id = rootStateId; - } - let i = result.length; - while (i--) { - if (!result[i].id || !this.states[result[i].id]) { - result.splice(i, 1); + result = []; } } - return result; + return this.normalizeStateObject(result); } - private gotoState(stateId: string, update: boolean, openRightLayout?: boolean) { + private gotoState(stateId: string, update: boolean, openRightLayout?: boolean, replaceCurrentHistoryUrl?: boolean) { const isStateIdChanged = this.dashboardCtrl.dashboardCtx.state !== stateId; this.dashboardCtrl.openDashboardState(stateId, openRightLayout); if (isStateIdChanged) { @@ -283,11 +271,11 @@ export class EntityStateControllerComponent extends StateControllerComponent imp this.mobileService.handleDashboardStateName(this.getStateName(this.stateObject.length - 1)); } if (update) { - this.updateLocation(isStateIdChanged); + this.updateLocation(replaceCurrentHistoryUrl ?? !isStateIdChanged); } } - private updateLocation(isStateIdChanged: boolean) { + private updateLocation(replaceCurrentHistoryUrl: boolean) { if (this.stateObject[this.stateObject.length - 1].id) { let newState; if (this.isDefaultState()) { @@ -295,7 +283,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp } else { newState = objToBase64(this.stateObject); } - this.updateStateParam(newState, !isStateIdChanged); + this.updateStateParam(newState, replaceCurrentHistoryUrl); } } diff --git a/ui-ngx/src/app/modules/home/components/dashboard-page/states/state-controller.component.ts b/ui-ngx/src/app/modules/home/components/dashboard-page/states/state-controller.component.ts index c4615bd0c2..8f33c61860 100644 --- a/ui-ngx/src/app/modules/home/components/dashboard-page/states/state-controller.component.ts +++ b/ui-ngx/src/app/modules/home/components/dashboard-page/states/state-controller.component.ts @@ -4,12 +4,13 @@ import { IStateControllerComponent, StateControllerState } from '@home/components/dashboard-page/states/state-controller.models'; import { IDashboardController } from '../dashboard-page.models'; import { DashboardState } from '@app/shared/models/dashboard.models'; -import { BehaviorSubject, Observable, Subject, Subscription } from 'rxjs'; +import { Observable, Subject, Subscription } from 'rxjs'; import { NgZone, OnDestroy, OnInit, Directive } from '@angular/core'; import { ActivatedRoute, Params, Router } from '@angular/router'; import { StatesControllerService } from '@home/components/dashboard-page/states/states-controller.service'; import { EntityId } from '@app/shared/models/id/entity-id'; import { StateObject, StateParams } from '@app/core/api/widget-api.models'; +import { DashboardUtilsService } from '@core/services/dashboard-utils.service'; import { WindowMessage } from '@shared/models/window-message.model'; import { UtilsService } from '@core/services/utils.service'; @@ -91,7 +92,8 @@ export abstract class StateControllerComponent implements IStateControllerCompon protected utils: UtilsService, protected window: Window, protected ngZone: NgZone, - protected statesControllerService: StatesControllerService) { + protected statesControllerService: StatesControllerService, + protected dashboardUtils: DashboardUtilsService) { } ngOnInit(): void { @@ -123,6 +125,14 @@ export abstract class StateControllerComponent implements IStateControllerCompon this.stateChangedSubject.complete(); } + protected normalizeStateObject(stateObject: StateControllerState): StateControllerState { + const result = (stateObject || []).filter((stateObj) => stateObj.id && this.states[stateObj.id]); + if (!result.length) { + result.push({ id: this.dashboardUtils.getRootStateId(this.states), params: {} }); + } + return result; + } + protected updateStateParam(newState: string, replaceCurrentHistoryUrl = false) { this.currentState = newState; if (this.syncStateWithQueryParam) {