Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scenes/Dashboard: Adjust duplication logic to more closely match old architecture #87635

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 25 additions & 43 deletions public/app/features/dashboard-scene/scene/DashboardScene.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add some test for this logic :)

Original file line number Diff line number Diff line change
Expand Up @@ -515,77 +515,59 @@ export class DashboardScene extends SceneObjectBase<DashboardSceneState> {
}

const libraryPanel = getLibraryVizPanelFromVizPanel(vizPanel);

const gridItem = libraryPanel ? libraryPanel.parent : vizPanel.parent;

if (!(gridItem instanceof DashboardGridItem)) {
console.error('Trying to duplicate a panel in a layout that is not DashboardGridItem');
return;
} else if (!(this.state.body instanceof SceneGridLayout)) {
console.error('Trying to duplicate a panel in a layout that is not SceneGridLayout ');
return;
}

let panelState;
let panelData;
let newGridItem;
const newPanelId = dashboardSceneGraph.getNextPanelId(this);

let body: ConstructorParameters<typeof DashboardGridItem>[0]['body'];
if (libraryPanel) {
const gridItemToDuplicateState = sceneUtils.cloneSceneObjectState(gridItem.state);

newGridItem = new DashboardGridItem({
x: gridItemToDuplicateState.x,
y: gridItemToDuplicateState.y,
width: gridItemToDuplicateState.width,
height: gridItemToDuplicateState.height,
body: new LibraryVizPanel({
title: libraryPanel.state.title,
uid: libraryPanel.state.uid,
name: libraryPanel.state.name,
panelKey: getVizPanelKeyForPanelId(newPanelId),
}),
body = new LibraryVizPanel({
title: libraryPanel.state.title,
uid: libraryPanel.state.uid,
name: libraryPanel.state.name,
panelKey: getVizPanelKeyForPanelId(newPanelId),
});
} else {
if (gridItem instanceof DashboardGridItem) {
panelState = sceneUtils.cloneSceneObjectState(gridItem.state.body.state);
panelData = sceneGraph.getData(gridItem.state.body).clone();
} else {
panelState = sceneUtils.cloneSceneObjectState(vizPanel.state);
panelData = sceneGraph.getData(vizPanel).clone();
}
const target = gridItem instanceof DashboardGridItem ? gridItem.state.body : vizPanel;
const panelState = sceneUtils.cloneSceneObjectState(target.state);
const panelData = sceneGraph.getData(target).clone();

// when we duplicate a panel we don't want to clone the alert state
delete panelData.state.data?.alertState;

newGridItem = new DashboardGridItem({
x: gridItem.state.x,
y: gridItem.state.y,
height: NEW_PANEL_HEIGHT,
width: NEW_PANEL_WIDTH,
body: new VizPanel({ ...panelState, $data: panelData, key: getVizPanelKeyForPanelId(newPanelId) }),
});
body = new VizPanel({ ...panelState, $data: panelData, key: getVizPanelKeyForPanelId(newPanelId) });
}

if (!(this.state.body instanceof SceneGridLayout)) {
console.error('Trying to duplicate a panel in a layout that is not SceneGridLayout ');
return;
}
const { x = 0, y = 0, width = 0, height } = gridItem.state;
const newGridItem = new DashboardGridItem({
x: x + width,
y: y - Number.MIN_VALUE,
width,
height,
body,
});

const sceneGridLayout = this.state.body;

if (gridItem.parent instanceof SceneGridRow) {
const row = gridItem.parent;

row.setState({
children: [...row.state.children, newGridItem],
});

sceneGridLayout.forceRender();

return;
} else {
sceneGridLayout.setState({
children: [...sceneGridLayout.state.children, newGridItem],
});
}

sceneGridLayout.setState({
children: [...sceneGridLayout.state.children, newGridItem],
});
}

public copyPanel(vizPanel: VizPanel) {
Expand Down