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
This commit is contained in:
kay delaney 2024-07-30 10:34:30 +01:00 committed by GitHub
parent 35315f43fe
commit b80e16075f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 3 deletions

View File

@ -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"],

View File

@ -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<Panel>) {

View File

@ -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');