LayoutRestorer: Remove layout restorer usage (#105963)

* remove layout restorer usage

* remove layout restorer code
pull/104680/head^2
Sergej-Vlasov 1 month ago committed by GitHub
parent 701297aa3f
commit 970dceab8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      public/app/features/dashboard-scene/scene/DashboardScene.tsx
  2. 4
      public/app/features/dashboard-scene/scene/layout-rows/RowItem.tsx
  3. 4
      public/app/features/dashboard-scene/scene/layout-tabs/TabItem.tsx
  4. 53
      public/app/features/dashboard-scene/scene/layouts-shared/LayoutRestorer.ts

@ -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<DashboardSceneState> impleme
DashboardJson | DashboardV2Spec
>;
private _layoutRestorer = new LayoutRestorer();
public constructor(state: Partial<DashboardSceneState>, serializerVersion: 'v1' | 'v2' = 'v1') {
super({
title: 'Dashboard',
@ -661,7 +658,7 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> impleme
}
public switchLayout(layout: DashboardLayoutManager) {
this.setState({ body: this._layoutRestorer.getLayout(layout, this.state.body) });
this.setState({ body: layout });
this.state.body.activateRepeaters?.();
}

@ -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<HTMLDivElement | null> = React.createRef<HTMLDivElement>();
public constructor(state?: Partial<RowItemState>) {
@ -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[] {

@ -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<HTMLDivElement>();
constructor(state?: Partial<TabItemState>) {
@ -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[] {

@ -1,53 +0,0 @@
import { VizPanel } from '@grafana/scenes';
import { DashboardLayoutManager } from '../types/DashboardLayoutManager';
export class LayoutRestorer {
private layoutMap: Record<string, DashboardLayoutManager> = {};
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;
}
Loading…
Cancel
Save