From 51c858f32e5e8308b4d2af8abf79d24bbb8f11c2 Mon Sep 17 00:00:00 2001 From: Victor Marin <36818606+mdvictor@users.noreply.github.com> Date: Wed, 19 Jun 2024 09:31:01 +0300 Subject: [PATCH] Fix dashboard crash on dashboard leave in panel edit (#89353) * Fix dashboard crashing when leaving dashboard in panel edit * add test * test --- .../dashboard-scene/scene/DashboardScene.test.tsx | 14 ++++++++++++++ .../dashboard-scene/scene/DashboardScene.tsx | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx index bb4488f705f..91e50fba9e1 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.test.tsx @@ -200,6 +200,20 @@ describe('DashboardScene', () => { expect(resoredLayout.state.children.map((c) => c.state.key)).toEqual(originalPanelOrder); }); + it('Should exit edit mode and discard panel changes if leaving the dashboard while in panel edit', () => { + const panel = findVizPanelByKey(scene, 'panel-1'); + const editPanel = buildPanelEditScene(panel!); + scene.setState({ + editPanel, + }); + + expect(scene.state.editPanel!['_discardChanges']).toBe(false); + + scene.exitEditMode({ skipConfirm: true }); + + expect(scene.state.editPanel!['_discardChanges']).toBe(true); + }); + it.each` prop | value ${'title'} | ${'new title'} diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.tsx index 83b0edbc9c3..e2bc1828f39 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.tsx @@ -317,6 +317,13 @@ export class DashboardScene extends SceneObjectBase { this.setState({ isEditing: false }); } + // if we are in edit panel, we need to onDiscard() + // so the useEffect cleanup comes later and + // doesn't try to commit the changes + if (this.state.editPanel) { + this.state.editPanel.onDiscard(); + } + // Disable grid dragging this.propagateEditModeChange(); }