Skip to content

Commit f52ba0c

Browse files
authored
fix: Track active panel focus and open files/changesets in that panel (#491)
...
1 parent d33e8a8 commit f52ba0c

2 files changed

Lines changed: 53 additions & 11 deletions

File tree

apps/array/src/renderer/features/panels/store/panelLayoutStore.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe("panelLayoutStore", () => {
6161
usePanelLayoutStore.getState().initializeTask("task-1");
6262
});
6363

64-
it("adds file tab to main panel", () => {
64+
it("adds file tab to main panel by default", () => {
6565
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
6666

6767
assertTabCount(getPanelTree("task-1"), "main-panel", 2);
@@ -73,6 +73,43 @@ describe("panelLayoutStore", () => {
7373
]);
7474
});
7575

76+
it("opens file in the focused panel", () => {
77+
// Focus the terminal panel
78+
usePanelLayoutStore
79+
.getState()
80+
.setFocusedPanel("task-1", "terminal-panel");
81+
82+
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
83+
84+
// File should open in terminal-panel (the focused panel), not main-panel
85+
assertPanelLayout(getPanelTree("task-1"), [
86+
{
87+
panelId: "terminal-panel",
88+
expectedTabs: ["shell", "file-src/App.tsx"],
89+
activeTab: "file-src/App.tsx",
90+
},
91+
]);
92+
assertTabCount(getPanelTree("task-1"), "main-panel", 1); // Only logs
93+
});
94+
95+
it("falls back to main panel if focused panel does not exist", () => {
96+
// Set focus to a non-existent panel
97+
usePanelLayoutStore
98+
.getState()
99+
.setFocusedPanel("task-1", "non-existent-panel");
100+
101+
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
102+
103+
// File should fall back to main-panel
104+
assertTabCount(getPanelTree("task-1"), "main-panel", 2);
105+
assertPanelLayout(getPanelTree("task-1"), [
106+
{
107+
panelId: "main-panel",
108+
expectedTabs: ["logs", "file-src/App.tsx"],
109+
},
110+
]);
111+
});
112+
76113
it("sets newly opened file as active", () => {
77114
usePanelLayoutStore.getState().openFile("task-1", "src/App.tsx");
78115

apps/array/src/renderer/features/panels/store/panelLayoutStore.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,22 @@ function openTab(
239239
return { panelTree: updatedTree };
240240
}
241241

242-
// Tab doesn't exist, add it to main panel
243-
const mainPanel = getLeafPanel(
244-
layout.panelTree,
245-
DEFAULT_PANEL_IDS.MAIN_PANEL,
246-
);
247-
if (!mainPanel) return {};
242+
// Tab doesn't exist, add it to the focused panel (or main panel as fallback)
243+
const targetPanelId = layout.focusedPanelId ?? DEFAULT_PANEL_IDS.MAIN_PANEL;
244+
let targetPanel = getLeafPanel(layout.panelTree, targetPanelId);
245+
246+
// Fall back to main panel if the focused panel doesn't exist or isn't a leaf
247+
if (!targetPanel) {
248+
targetPanel = getLeafPanel(
249+
layout.panelTree,
250+
DEFAULT_PANEL_IDS.MAIN_PANEL,
251+
);
252+
}
253+
if (!targetPanel) return {};
248254

249-
const updatedTree = updateTreeNode(
250-
layout.panelTree,
251-
DEFAULT_PANEL_IDS.MAIN_PANEL,
252-
(panel) => addNewTabToPanel(panel, tabId, true, asPreview),
255+
const panelId = targetPanel.id;
256+
const updatedTree = updateTreeNode(layout.panelTree, panelId, (panel) =>
257+
addNewTabToPanel(panel, tabId, true, asPreview),
253258
);
254259

255260
const metadata = updateMetadataForTab(layout, tabId, "add");

0 commit comments

Comments
 (0)