Skip to content

Commit 8fd7316

Browse files
committed
🐛 Revisit placeholder loading logic to handle different types of embeddables
1 parent 30e8c89 commit 8fd7316

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/plugins/dashboard/public/application/actions/clone_panel_action.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ test('Clone adds a new embeddable', async () => {
9898
);
9999
expect(newPanelId).toBeDefined();
100100
const newPanel = container.getInput().panels[newPanelId!];
101-
expect(newPanel.type).toEqual(embeddable.type);
101+
expect(newPanel.type).toEqual('placeholder');
102+
// let the placeholder load
103+
await dashboard.untilEmbeddableLoaded(newPanelId!);
104+
// now wait for the full embeddable to replace it
105+
const loadedPanel = await dashboard.untilEmbeddableLoaded(newPanelId!);
106+
expect(loadedPanel.type).toEqual(embeddable.type);
102107
});
103108

104109
test('Clones an embeddable without a saved object ID', async () => {

src/plugins/dashboard/public/application/embeddable/dashboard_container.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,21 @@ export class DashboardContainer extends Container<InheritedChildInput, Dashboard
154154
placementMethod,
155155
placementArgs
156156
);
157+
157158
this.updateInput({
158159
panels: {
159160
...this.input.panels,
160161
[placeholderPanelState.explicitInput.id]: placeholderPanelState,
161162
},
162163
});
163-
newStateComplete.then((newPanelState: Partial<PanelState>) =>
164-
this.replacePanel(placeholderPanelState, newPanelState)
165-
);
164+
165+
// wait until the placeholder is ready, then replace it with new panel
166+
// this is useful as sometimes panels can load faster than the placeholder one (i.e. by value embeddables)
167+
this.untilEmbeddableLoaded(originalPanelState.explicitInput.id)
168+
.then(() => newStateComplete)
169+
.then((newPanelState: Partial<PanelState>) =>
170+
this.replacePanel(placeholderPanelState, newPanelState)
171+
);
166172
}
167173

168174
public replacePanel(

src/plugins/embeddable/public/lib/containers/container.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ export abstract class Container<
335335
return embeddable;
336336
}
337337

338+
private panelHasChanged(currentPanel: PanelState, prevPanel: PanelState) {
339+
if (currentPanel.type !== prevPanel.type) {
340+
return true;
341+
}
342+
}
343+
338344
private maybeUpdateChildren(
339345
currentPanels: TContainerInput['panels'],
340346
prevPanels: TContainerInput['panels']
@@ -349,7 +355,7 @@ export abstract class Container<
349355
}
350356
// In case of type change, remove and add a panel with the same id
351357
if (currentPanels[id] && prevPanels[id]) {
352-
if (currentPanels[id].type !== prevPanels[id].type) {
358+
if (this.panelHasChanged(currentPanels[id], prevPanels[id])) {
353359
this.onPanelRemoved(id);
354360
this.onPanelAdded(currentPanels[id]);
355361
}

0 commit comments

Comments
 (0)