Dashboard: Fixes layout switching crash (#102619)

This commit is contained in:
Torkel Ödegaard
2025-03-21 15:45:40 +01:00
committed by GitHub
parent 1a00801e6a
commit 599906cbfd
@@ -1,4 +1,5 @@
import { vizPanelToSchemaV2 } from '../../serialization/transformSceneToSaveModelSchemaV2';
import { VizPanel } from '@grafana/scenes';
import { DashboardLayoutManager } from '../types/DashboardLayoutManager';
export class LayoutRestorer {
@@ -8,12 +9,10 @@ export class LayoutRestorer {
newLayout: DashboardLayoutManager,
currentLayout: DashboardLayoutManager
): DashboardLayoutManager | undefined {
// If we have an old version of this layout and panels are the same we can reuse it
//If we have an old version of this layout and panels are the same we can reuse it
const prevLayout = this.layoutMap[newLayout.descriptor.id];
if (prevLayout) {
const oldPanelSchema = prevLayout.getVizPanels().map(vizPanelToSchemaV2);
const newPanelSchema = newLayout.getVizPanels().map(vizPanelToSchemaV2);
if (JSON.stringify(oldPanelSchema) === JSON.stringify(newPanelSchema)) {
if (panelsAreUnchanged(prevLayout.getVizPanels(), newLayout.getVizPanels())) {
return prevLayout;
}
}
@@ -23,3 +22,28 @@ export class LayoutRestorer {
return newLayout;
}
}
/**
* Simple check that panels are in same order and same options but not a comprehensive check
* Ideally we should check if persisted state is the same but not possible anymore with current serialization code that requires all panels be connected to a DashboardScene
*/
function panelsAreUnchanged(a: VizPanel[], b: VizPanel[]) {
for (let i = 0; i < a.length; i++) {
const ap = a[i];
const bp = b[i];
if (ap.state.key !== bp.state.key) {
return false;
}
if (JSON.stringify(ap.state.options) !== JSON.stringify(bp.state.options)) {
return false;
}
if (JSON.stringify(ap.state.fieldConfig) !== JSON.stringify(bp.state.fieldConfig)) {
return false;
}
}
return true;
}