Skip to content

Commit abe6231

Browse files
committed
Fix replacing existing preview panel
1 parent 6c3b509 commit abe6231

2 files changed

Lines changed: 38 additions & 39 deletions

File tree

packages/dashboard-core-plugins/src/ConsolePlugin.tsx

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -446,47 +446,24 @@ export function ConsolePlugin(
446446
const isFileOpen = fileIsOpen(fileMetadata);
447447
const isFileOpenAsPreview = fileIsOpenAsPreview(fileMetadata);
448448

449-
// If the file is already open, just show and focus it if necessary
450-
if (isFileOpen) {
451-
showFilePanel(fileMetadata);
452-
if (shouldFocus) {
453-
const panelId = getPanelIdForFileMetadata(fileMetadata);
454-
focusPanelById(panelId);
455-
}
456-
return;
449+
const [previewId] = [...previewFileMap.values()];
450+
const isPreview = !shouldFocus;
451+
const panelId =
452+
isPreview && previewId
453+
? previewId
454+
: getPanelIdForFileMetadata(fileMetadata);
455+
456+
if (!panelId) {
457+
throw new Error(
458+
'Unable to retrieve or create panelId for metadata',
459+
fileMetadata
460+
);
457461
}
458462

459-
// If the file is already open as a preview and we're not focusing it, just show it
460-
// If we're focusing it, that means we need to replace the panel anyway with a non-preview panel, so just fall into the logic below
461-
if (isFileOpenAsPreview && !shouldFocus) {
463+
// If the file is already open, show it
464+
if (isFileOpen || isFileOpenAsPreview) {
462465
showFilePanel(fileMetadata);
463-
return;
464-
}
465-
466-
// By this point, the user has double clicked the panel
467-
468-
const [previewTabName] = Array.from(previewFileMap.keys());
469-
const previewTabId = previewFileMap.get(previewTabName);
470-
let panelId: string | undefined;
471-
if (previewTabId != null) {
472-
panelId = previewTabId;
473-
474-
const stack = LayoutUtils.getStackForConfig(layout.root, {
475-
component: NotebookPanel.COMPONENT,
476-
id: panelId,
477-
});
478-
479-
const item = LayoutUtils.getContentItemInStack(stack, {
480-
component: NotebookPanel.COMPONENT,
481-
id: previewTabId,
482-
});
483-
if (item && isComponent(item)) {
484-
item.container.emit(NotebookEvent.PROMOTE_FROM_PREVIEW);
485-
deletePreviewFileMapEntry(previewTabName);
486-
addOpenFileMapEntry(previewTabName, previewTabId);
487-
}
488466
} else {
489-
panelId = getPanelIdForFileMetadata(fileMetadata);
490467
const stack = LayoutUtils.getStackForComponentTypes(layout.root, [
491468
NotebookPanel.COMPONENT,
492469
]);
@@ -497,14 +474,34 @@ export function ConsolePlugin(
497474
fileMetadata,
498475
session,
499476
sessionLanguage,
500-
isPreview: !shouldFocus,
477+
isPreview,
501478
});
502479

480+
// This will replace the existing preview by panelId if needed
503481
LayoutUtils.openComponentInStack(stack, config);
504482
}
505483

484+
// If the file is open as a preview and focused, promote to non-preview
485+
if (isFileOpenAsPreview && shouldFocus) {
486+
const fileId = fileMetadata.id;
487+
const stack = LayoutUtils.getStackForConfig(layout.root, {
488+
component: NotebookPanel.COMPONENT,
489+
id: panelId,
490+
});
491+
492+
const item = LayoutUtils.getContentItemInStack(stack, {
493+
component: NotebookPanel.COMPONENT,
494+
id: panelId,
495+
});
496+
if (item && isComponent(item)) {
497+
item.container.emit(NotebookEvent.PROMOTE_FROM_PREVIEW);
498+
deletePreviewFileMapEntry(fileId);
499+
addOpenFileMapEntry(fileId, panelId);
500+
}
501+
}
502+
506503
if (shouldFocus) {
507-
// Focus the tab we just opened if we're supposed to
504+
// Focus the tab if we're supposed to
508505
focusPanelById(panelId);
509506
}
510507
},

packages/dashboard/src/layout/LayoutUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,8 @@ class LayoutUtils {
559559
searchConfig
560560
);
561561

562+
console.log(oldContentItem, searchConfig);
563+
562564
if (replaceExisting && oldContentItem && stack) {
563565
const index = stack?.contentItems.indexOf(oldContentItem);
564566

0 commit comments

Comments
 (0)