Skip to content

Commit 00091f9

Browse files
committed
fix(web): keep surface panes in step with the panels they came from
Review findings on the pane surfaces: - A terminal pane kept rendering closed terminals and lost split ones, because its callbacks updated the right panel store the tab had already left. Terminal edits now target the pane's own surface, through helpers shared with the panel store. - Closing a pane from its header, the pane.close shortcut, or the panel toggle skipped the tab's cleanup, so terminal sessions kept running. The view now hands the header a close that goes through the same path as the tab, and the route follows the survivor. - Closing a device pane from inside the panel left the pane mounted. - A group left with only surface panes was unreachable and lingered. - The layout edge kept a stale priority target after the pointer left the band, so drops split the whole layout instead of the pane. - A surface pane opened from the route thread lost focus to its chat leaf. - The Agents surface was offered where it could not open, and dragging to select text in a device tab's rename field armed a pane drag. Made with Claude Fable 5.1 in Claude Code.
1 parent aca83d5 commit 00091f9

13 files changed

Lines changed: 306 additions & 138 deletions

‎apps/web/src/chatPanes.logic.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,20 @@ export function removePane(root: ChatPaneNode, paneId: ChatPaneId): ChatPaneNode
144144
return first === root.first && second === root.second ? root : { ...root, first, second };
145145
}
146146

147+
/** Replaces the surface `paneId` shows, leaving a pane without one alone. */
148+
export function setPaneSurface(
149+
root: ChatPaneNode,
150+
paneId: ChatPaneId,
151+
surface: RightPanelSurface,
152+
): ChatPaneNode {
153+
if (root.kind === "leaf") {
154+
return root.id === paneId && root.surface ? { ...root, surface } : root;
155+
}
156+
const first = setPaneSurface(root.first, paneId, surface);
157+
const second = setPaneSurface(root.second, paneId, surface);
158+
return first === root.first && second === root.second ? root : { ...root, first, second };
159+
}
160+
147161
export function setPaneRatio(root: ChatPaneNode, splitId: ChatPaneId, ratio: number): ChatPaneNode {
148162
if (root.kind === "leaf") return root;
149163
if (root.id === splitId) return { ...root, ratio: clampPaneRatio(ratio) };

‎apps/web/src/chatPanesStore.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { beforeEach, expect, it } from "vite-plus/test";
55
import { useChatPaneDragStore } from "./chatPaneDragStore";
66
import { collectLeaves } from "./chatPanes.logic";
77
import { commitChatPaneDrop, openInSplit, useChatPanesStore } from "./chatPanesStore";
8+
import type { RightPanelSurface } from "./rightPanelStore";
89

910
const thread = (id: string) => scopeThreadRef(EnvironmentId.make("env"), ThreadId.make(id));
1011
const threadIds = (index: number) =>
@@ -65,6 +66,32 @@ it("opens a panel pane beside the thread and closes it by surface", () => {
6566
expect(useChatPanesStore.getState().groups).toEqual([]);
6667
});
6768

69+
it("rewrites a panel pane's surface and follows the survivor when it closes", () => {
70+
const terminal: RightPanelSurface = {
71+
id: "terminal:t1",
72+
kind: "terminal",
73+
resourceId: "t1",
74+
terminalIds: ["t1"],
75+
activeTerminalId: "t1",
76+
};
77+
openInSplit(thread("a"), { threadRef: thread("a"), surface: terminal });
78+
const store = useChatPanesStore.getState();
79+
store.updateSurface(thread("a"), terminal.id, (surface) =>
80+
surface.kind === "terminal" ? { ...surface, terminalIds: ["t1", "t2"] } : surface,
81+
);
82+
const leaf = collectLeaves(useChatPanesStore.getState().groups[0]!)[1]!;
83+
expect(leaf.surface?.kind === "terminal" && leaf.surface.terminalIds).toEqual(["t1", "t2"]);
84+
expect(store.closeSurface(thread("a"), terminal.id)).toEqual(thread("a"));
85+
expect(useChatPanesStore.getState().groups).toEqual([]);
86+
});
87+
88+
it("drops a group left with only panel panes", () => {
89+
const diff = { id: "diff", kind: "diff" } as const;
90+
openInSplit(thread("a"), { threadRef: thread("a"), surface: diff });
91+
useChatPanesStore.getState().closeThread(thread("a"));
92+
expect(useChatPanesStore.getState().groups).toEqual([]);
93+
});
94+
6895
it.each([-1, 1.1, 0.5])("validates persisted pane ratio %s", async (ratio) => {
6996
openInSplit(thread("a"), { threadRef: thread("b") });
7097
const root = useChatPanesStore.getState().groups[0]!;

‎apps/web/src/chatPanesStore.ts‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
removePane,
1414
selectChatPaneRoot,
1515
setPaneRatio,
16+
setPaneSurface,
1617
splitPane,
1718
type ChatPaneId,
1819
type ChatPaneLeaf,
@@ -82,8 +83,18 @@ interface ChatPanesState {
8283
*/
8384
closePane: (paneId: ChatPaneId) => ScopedThreadRef | null;
8485
closeThread: (threadRef: ScopedThreadRef) => ScopedThreadRef | null;
85-
/** Closes the pane showing `surfaceId` for `threadRef`, if any. */
86-
closeSurface: (threadRef: ScopedThreadRef, surfaceId: string) => void;
86+
/** Closes the pane showing `surfaceId` for `threadRef`, if any; the survivor when it had focus. */
87+
closeSurface: (threadRef: ScopedThreadRef, surfaceId: string) => ScopedThreadRef | null;
88+
/**
89+
* Rewrites the panel state a pane owns. A surface shown as a pane is gone
90+
* from the right panel store, so its own store is the only thing that can
91+
* record what happens inside it, such as splitting a terminal group.
92+
*/
93+
updateSurface: (
94+
threadRef: ScopedThreadRef,
95+
surfaceId: string,
96+
update: (surface: RightPanelSurface) => RightPanelSurface,
97+
) => void;
8798
setRatio: (splitId: ChatPaneId, ratio: number) => void;
8899
}
89100

@@ -105,11 +116,14 @@ function newLeaf(content: ChatPaneContent): ChatPaneLeaf {
105116
}
106117

107118
/** Swaps one group for its replacement and drops every group down to one
108-
pane: `to` after a close, and the source movePane already shrank. */
119+
pane: `to` after a close, and the source movePane already shrank. A group
120+
of surfaces alone goes too: no route reaches it, so it would just linger. */
109121
function replaceGroup(groups: ReadonlyArray<ChatPaneNode>, from: ChatPaneNode, to: ChatPaneNode) {
110122
return groups
111123
.map((group) => (group === from ? to : group))
112-
.filter((group) => group.kind === "split");
124+
.filter(
125+
(group) => group.kind === "split" && collectLeaves(group).some((leaf) => !leaf.surface),
126+
);
113127
}
114128

115129
function closeLeaf(
@@ -185,8 +199,16 @@ export const useChatPanesStore = create<ChatPanesState>()(
185199
closeSurface: (threadRef, surfaceId) => {
186200
const state = get();
187201
const leaf = findContent(state.groups, threadRef, surfaceId);
188-
if (leaf) closeLeaf(set, state, leaf.id);
202+
return leaf ? closeLeaf(set, state, leaf.id) : null;
189203
},
204+
updateSurface: (threadRef, surfaceId, update) =>
205+
set((state) => {
206+
const leaf = findContent(state.groups, threadRef, surfaceId);
207+
if (!leaf?.surface) return state;
208+
const surface = update(leaf.surface);
209+
if (surface === leaf.surface) return state;
210+
return { groups: state.groups.map((group) => setPaneSurface(group, leaf.id, surface)) };
211+
}),
190212
setRatio: (splitId, ratio) =>
191213
set((state) => {
192214
const groups = state.groups.map((group) => setPaneRatio(group, splitId, ratio));
@@ -271,13 +293,20 @@ function placeCreatedSurface(
271293
create();
272294
const now = added();
273295
if (now) return place({ threadRef, surface: now });
274-
const stop = useRightPanelStore.subscribe(() => {
296+
const unsubscribe = useRightPanelStore.subscribe(() => {
275297
const surface = added();
276298
if (!surface) return;
277299
stop();
278-
clearTimeout(timeout);
279300
place({ threadRef, surface });
280301
});
302+
// The next press means the action opened a dialog the user dealt with, so
303+
// whatever surface they add afterwards is their own, not this one's result.
304+
const stop = () => {
305+
unsubscribe();
306+
clearTimeout(timeout);
307+
document.removeEventListener("pointerdown", stop, true);
308+
};
309+
document.addEventListener("pointerdown", stop, true);
281310
const timeout = setTimeout(stop, 15_000);
282311
}
283312

0 commit comments

Comments
 (0)