From b80e16075f3f2637c5bed83043eb07e88f3bb596 Mon Sep 17 00:00:00 2001 From: kay delaney <45561153+kaydelaney@users.noreply.github.com> Date: Tue, 30 Jul 2024 10:34:30 +0100 Subject: [PATCH] Scenes/Dashboards: Fix issue where changes in panel height weren't saved (#91125) * Scenes/Dashboards: Fix issue where changes in panel height weren't saved --- .betterer.results | 4 +- .../transformSceneToSaveModel.test.ts | 40 ++++++++++++++++++- .../transformSceneToSaveModel.ts | 2 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/.betterer.results b/.betterer.results index 47ed66a9318..634983e4e11 100644 --- a/.betterer.results +++ b/.betterer.results @@ -2906,7 +2906,9 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "9"], [0, 0, 0, "Unexpected any. Specify a different type.", "10"], [0, 0, 0, "Unexpected any. Specify a different type.", "11"], - [0, 0, 0, "Unexpected any. Specify a different type.", "12"] + [0, 0, 0, "Unexpected any. Specify a different type.", "12"], + [0, 0, 0, "Unexpected any. Specify a different type.", "13"], + [0, 0, 0, "Unexpected any. Specify a different type.", "14"] ], "public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts:5381": [ [0, 0, 0, "Do not use any type assertions.", "0"], diff --git a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.test.ts b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.test.ts index d20e8c05286..e31b146b0de 100644 --- a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.test.ts +++ b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.test.ts @@ -15,7 +15,14 @@ import { } from '@grafana/data'; import { getPanelPlugin } from '@grafana/data/test/__mocks__/pluginMocks'; import { getPluginLinkExtensions, setPluginImportUtils } from '@grafana/runtime'; -import { MultiValueVariable, SceneGridLayout, SceneGridRow, SceneTimeRange, VizPanel } from '@grafana/scenes'; +import { + MultiValueVariable, + sceneGraph, + SceneGridLayout, + SceneGridRow, + SceneTimeRange, + VizPanel, +} from '@grafana/scenes'; import { Dashboard, LoadingState, Panel, RowPanel, VariableRefresh } from '@grafana/schema'; import { PanelModel } from 'app/features/dashboard/state'; import { getTimeRange } from 'app/features/dashboard/utils/timeRange'; @@ -1094,6 +1101,37 @@ describe('transformSceneToSaveModel', () => { expect((saveModel.panels![1] as any).options.content).toBe('new content'); }); }); + + describe('Given a scene with repeated panels and non-repeated panels', () => { + it('should save repeated panels itemHeight as height', () => { + const scene = transformSaveModelToScene({ dashboard: repeatingRowsAndPanelsDashboardJson as any, meta: {} }); + const gridItem = sceneGraph.findByKey(scene, 'grid-item-2') as DashboardGridItem; + expect(gridItem).toBeInstanceOf(DashboardGridItem); + expect(gridItem.state.height).toBe(10); + expect(gridItem.state.itemHeight).toBe(10); + expect(gridItem.state.itemHeight).toBe(10); + expect(gridItem.state.variableName).toBe('pod'); + gridItem.setState({ itemHeight: 24 }); + const saveModel = transformSceneToSaveModel(scene); + expect(saveModel.panels?.[3].gridPos?.h).toBe(24); + }); + + it('should not save non-repeated panels itemHeight as height', () => { + const scene = transformSaveModelToScene({ dashboard: repeatingRowsAndPanelsDashboardJson as any, meta: {} }); + const gridItem = sceneGraph.findByKey(scene, 'grid-item-15') as DashboardGridItem; + expect(gridItem).toBeInstanceOf(DashboardGridItem); + expect(gridItem.state.height).toBe(2); + expect(gridItem.state.itemHeight).toBe(2); + expect(gridItem.state.variableName).toBeUndefined(); + gridItem.setState({ itemHeight: 24 }); + let saveModel = transformSceneToSaveModel(scene); + expect(saveModel.panels?.[1].gridPos?.h).toBe(2); + + gridItem.setState({ height: 34 }); + saveModel = transformSceneToSaveModel(scene); + expect(saveModel.panels?.[1].gridPos?.h).toBe(34); + }); + }); }); export function buildGridItemFromPanelSchema(panel: Partial) { diff --git a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts index bf3202c2abc..1077fcc0145 100644 --- a/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts +++ b/public/app/features/dashboard-scene/serialization/transformSceneToSaveModel.ts @@ -200,7 +200,7 @@ export function gridItemToPanel( x = gridItem_.state.x ?? 0; y = gridItem_.state.y ?? 0; w = gridItem_.state.width ?? 0; - h = gridItem_.state.itemHeight ?? gridItem_.state.height ?? 0; + h = (gridItem_.state.variableName ? gridItem_.state.itemHeight : gridItem_.state.height) ?? 0; if (!vizPanel) { throw new Error('Unsupported grid item type');