From 970dceab8c4ce3df3b7f5bbd954a2d78d83ea58c Mon Sep 17 00:00:00 2001 From: Sergej-Vlasov <37613182+Sergej-Vlasov@users.noreply.github.com> Date: Mon, 26 May 2025 10:12:25 +0100 Subject: [PATCH] LayoutRestorer: Remove layout restorer usage (#105963) * remove layout restorer usage * remove layout restorer code --- .../dashboard-scene/scene/DashboardScene.tsx | 5 +- .../scene/layout-rows/RowItem.tsx | 4 +- .../scene/layout-tabs/TabItem.tsx | 4 +- .../scene/layouts-shared/LayoutRestorer.ts | 53 ------------------- 4 files changed, 3 insertions(+), 63 deletions(-) delete mode 100644 public/app/features/dashboard-scene/scene/layouts-shared/LayoutRestorer.ts diff --git a/public/app/features/dashboard-scene/scene/DashboardScene.tsx b/public/app/features/dashboard-scene/scene/DashboardScene.tsx index 718e9b87b3e..065d5148e6b 100644 --- a/public/app/features/dashboard-scene/scene/DashboardScene.tsx +++ b/public/app/features/dashboard-scene/scene/DashboardScene.tsx @@ -84,7 +84,6 @@ import { setupKeyboardShortcuts } from './keyboardShortcuts'; import { AutoGridItem } from './layout-auto-grid/AutoGridItem'; import { DashboardGridItem } from './layout-default/DashboardGridItem'; import { DefaultGridLayoutManager } from './layout-default/DefaultGridLayoutManager'; -import { LayoutRestorer } from './layouts-shared/LayoutRestorer'; import { addNewRowTo, addNewTabTo } from './layouts-shared/addNew'; import { clearClipboard } from './layouts-shared/paste'; import { DashboardLayoutManager } from './types/DashboardLayoutManager'; @@ -191,8 +190,6 @@ export class DashboardScene extends SceneObjectBase impleme DashboardJson | DashboardV2Spec >; - private _layoutRestorer = new LayoutRestorer(); - public constructor(state: Partial, serializerVersion: 'v1' | 'v2' = 'v1') { super({ title: 'Dashboard', @@ -661,7 +658,7 @@ export class DashboardScene extends SceneObjectBase impleme } public switchLayout(layout: DashboardLayoutManager) { - this.setState({ body: this._layoutRestorer.getLayout(layout, this.state.body) }); + this.setState({ body: layout }); this.state.body.activateRepeaters?.(); } diff --git a/public/app/features/dashboard-scene/scene/layout-rows/RowItem.tsx b/public/app/features/dashboard-scene/scene/layout-rows/RowItem.tsx index d79611ee459..645a8a7cede 100644 --- a/public/app/features/dashboard-scene/scene/layout-rows/RowItem.tsx +++ b/public/app/features/dashboard-scene/scene/layout-rows/RowItem.tsx @@ -22,7 +22,6 @@ import { serializeRow } from '../../serialization/layoutSerializers/RowsLayoutSe import { getElements } from '../../serialization/layoutSerializers/utils'; import { getDashboardSceneFor } from '../../utils/utils'; import { AutoGridLayoutManager } from '../layout-auto-grid/AutoGridLayoutManager'; -import { LayoutRestorer } from '../layouts-shared/LayoutRestorer'; import { clearClipboard } from '../layouts-shared/paste'; import { scrollCanvasElementIntoView } from '../layouts-shared/scrollCanvasElementIntoView'; import { BulkActionElement } from '../types/BulkActionElement'; @@ -59,7 +58,6 @@ export class RowItem public readonly isEditableDashboardElement = true; public readonly isDashboardDropTarget = true; - private _layoutRestorer = new LayoutRestorer(); public containerRef: React.MutableRefObject = React.createRef(); public constructor(state?: Partial) { @@ -101,7 +99,7 @@ export class RowItem } public switchLayout(layout: DashboardLayoutManager) { - this.setState({ layout: this._layoutRestorer.getLayout(layout, this.state.layout) }); + this.setState({ layout }); } public useEditPaneOptions(isNewElement: boolean): OptionsPaneCategoryDescriptor[] { diff --git a/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx b/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx index 2e2663c5bb0..ae0b494ce94 100644 --- a/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx +++ b/public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx @@ -22,7 +22,6 @@ import { serializeTab } from '../../serialization/layoutSerializers/TabsLayoutSe import { getElements } from '../../serialization/layoutSerializers/utils'; import { getDashboardSceneFor, getDefaultVizPanel } from '../../utils/utils'; import { AutoGridLayoutManager } from '../layout-auto-grid/AutoGridLayoutManager'; -import { LayoutRestorer } from '../layouts-shared/LayoutRestorer'; import { clearClipboard } from '../layouts-shared/paste'; import { scrollCanvasElementIntoView } from '../layouts-shared/scrollCanvasElementIntoView'; import { BulkActionElement } from '../types/BulkActionElement'; @@ -57,7 +56,6 @@ export class TabItem public readonly isEditableDashboardElement = true; public readonly isDashboardDropTarget = true; - private _layoutRestorer = new LayoutRestorer(); public containerRef = React.createRef(); constructor(state?: Partial) { @@ -99,7 +97,7 @@ export class TabItem } public switchLayout(layout: DashboardLayoutManager) { - this.setState({ layout: this._layoutRestorer.getLayout(layout, this.state.layout) }); + this.setState({ layout }); } public useEditPaneOptions(isNewElement: boolean): OptionsPaneCategoryDescriptor[] { diff --git a/public/app/features/dashboard-scene/scene/layouts-shared/LayoutRestorer.ts b/public/app/features/dashboard-scene/scene/layouts-shared/LayoutRestorer.ts deleted file mode 100644 index 302247a6c40..00000000000 --- a/public/app/features/dashboard-scene/scene/layouts-shared/LayoutRestorer.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { VizPanel } from '@grafana/scenes'; - -import { DashboardLayoutManager } from '../types/DashboardLayoutManager'; - -export class LayoutRestorer { - private layoutMap: Record = {}; - - public getLayout( - newLayout: DashboardLayoutManager, - currentLayout: DashboardLayoutManager - ): DashboardLayoutManager | undefined { - //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) { - if (panelsAreUnchanged(prevLayout.getVizPanels(), newLayout.getVizPanels())) { - return prevLayout; - } - } - - this.layoutMap[currentLayout.descriptor.id] = currentLayout; - - 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[]) { - if (a.length < b.length) { - return false; - } - - 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; -}