Skip to content

Commit 6742822

Browse files
committed
OU-1264: improve external panel additional logic
Better distinction between of the queued state and confirming the addition a bit later in the process.
1 parent 92a3a27 commit 6742822

1 file changed

Lines changed: 25 additions & 18 deletions

File tree

web/src/components/dashboards/perses/ExternalPanelAddition.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ export function ExternalPanelAddition({
2121
);
2222
const { openAddPanel } = useDashboardActions();
2323
const dashboardStore = useDashboardStore();
24-
const [externallyAddedPanel, setExternallyAddedPanel] = useState(null);
24+
const [queuedPanel, setQueuedPanel] = useState(null);
2525

26-
const addPanelExternally = useCallback(
26+
// Turn the dashboard into editable mode and added it to `queuedPanel`.
27+
// The `queuedPanel` will be added in the next refresh cycle.
28+
const queuePanelAddition = useCallback(
2729
(panelDefinition: any): void => {
2830
// Simulate opening a panel to add the pane so that we can use it to programatically
2931
// add a panel to the dashboard from an external source (AI assistant).
@@ -36,37 +38,42 @@ export function ExternalPanelAddition({
3638
groupId: 0,
3739
panelDefinition,
3840
};
39-
setExternallyAddedPanel(change);
41+
setQueuedPanel(change);
4042
},
4143
[isEditMode, onEditButtonClick, openAddPanel],
4244
);
4345

4446
useEffect(() => {
4547
// Listen for external panel addition requests
46-
if (addPersesPanelExternally) {
47-
addPanelExternally(addPersesPanelExternally);
48-
dispatch(dashboardsPersesPanelExternallyAdded());
48+
if (addPersesPanelExternally && !queuedPanel) {
49+
queuePanelAddition(addPersesPanelExternally);
4950
}
5051

5152
// Apply externally added panel
52-
if (externallyAddedPanel) {
53-
const groupId = dashboardStore.panelGroupOrder[0];
54-
externallyAddedPanel.groupId = groupId;
53+
if (queuedPanel) {
54+
try {
55+
const groupId = dashboardStore.panelGroupOrder[0];
56+
queuedPanel.groupId = groupId;
5557

56-
// Use the temporary panelEditor to add changes to the dashboard.
57-
const panelEditor = dashboardStore.panelEditor;
58-
panelEditor.applyChanges(externallyAddedPanel);
59-
panelEditor.close();
60-
61-
// Clear the externally added panel after applying changes
62-
setExternallyAddedPanel(null);
58+
// Use the temporary panelEditor to add changes to the dashboard.
59+
const panelEditor = dashboardStore.panelEditor;
60+
panelEditor.applyChanges(queuedPanel);
61+
panelEditor.close();
62+
} finally {
63+
// Clear the externally added panel after applying changes.
64+
// We assume only one panel is being added in one cycle: no need to complicate
65+
// with multiple panels in the queue.
66+
// We make sure we clean up even on error to avoid endless loops.
67+
setQueuedPanel(null);
68+
dispatch(dashboardsPersesPanelExternallyAdded());
69+
}
6370
}
6471
}, [
6572
dispatch,
6673
dashboardStore.panelGroupOrder,
6774
dashboardStore.panelEditor,
68-
externallyAddedPanel,
69-
addPanelExternally,
75+
queuePanelAddition,
76+
queuedPanel,
7077
addPersesPanelExternally,
7178
]);
7279

0 commit comments

Comments
 (0)