From b17e256a3c85667de9ba3a059fec9bc6f74f3501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 30 Sep 2024 12:19:47 +0200 Subject: [PATCH] DashboardScene: Fixes url issue with subpath when exiting edit mode (#93962) --- .../scene/DashboardScene.test.tsx | 14 +++++++++++++- .../dashboard-scene/scene/DashboardScene.tsx | 18 +++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx index 6dd0aedbfdf..741bc4bb5a7 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx @@ -1,4 +1,4 @@ -import { CoreApp, LoadingState, getDefaultTimeRange, store } from '@grafana/data'; +import { CoreApp, GrafanaConfig, LoadingState, getDefaultTimeRange, locationUtil, store } from '@grafana/data'; import { locationService } from '@grafana/runtime'; import { sceneGraph, @@ -76,6 +76,12 @@ jest.mock('app/features/manage-dashboards/state/actions', () => ({ deleteDashboard: jest.fn().mockResolvedValue({}), })); +locationUtil.initialize({ + config: { appSubUrl: '/subUrl' } as GrafanaConfig, + getVariablesUrlParams: jest.fn(), + getTimeRangeForUrl: jest.fn(), +}); + const worker = createWorker(); mockResultsOfDetectChangesWorker({ hasChanges: true, hasTimeChanges: false, hasVariableValueChanges: false }); @@ -134,6 +140,7 @@ describe('DashboardScene', () => { beforeEach(() => { scene = buildTestScene(); + locationService.push('/d/dash-1'); deactivateScene = scene.activate(); scene.onEnterEditMode(); jest.clearAllMocks(); @@ -143,6 +150,11 @@ describe('DashboardScene', () => { expect(scene.state.isEditing).toBe(true); }); + it('Can exit edit mode', () => { + scene.exitEditMode({ skipConfirm: true }); + expect(locationService.getLocation().pathname).toBe('/d/dash-1'); + }); + it('Exiting already saved dashboard should not restore initial state', () => { scene.setState({ title: 'Updated title' }); expect(scene.state.isDirty).toBe(true); diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.tsx index 72c7025a25f..da51aa2499a 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.tsx @@ -301,15 +301,15 @@ export class DashboardScene extends SceneObjectBase { // We are updating url and removing editview and editPanel. // The initial url may be including edit view, edit panel or inspect query params if the user pasted the url, // hence we need to cleanup those query params to get back to the dashboard view. Otherwise url sync can trigger overlays. - locationService.replace( - locationUtil.getUrlForPartial(this._initialUrlState!, { - editPanel: null, - editview: null, - inspect: null, - inspectTab: null, - shareView: null, - }) - ); + const url = locationUtil.getUrlForPartial(this._initialUrlState!, { + editPanel: null, + editview: null, + inspect: null, + inspectTab: null, + shareView: null, + }); + + locationService.replace(locationUtil.stripBaseFromUrl(url)); if (this._fromExplore) { this.cleanupStateFromExplore();