Skip to content

Commit 55bf180

Browse files
committed
feat(web): open threads side by side with drag-and-drop split panes
Users who drive several agents at once had to flip between threads one at a time. This adds a split layout for the chat area: drag a sidebar thread onto the current chat and a highlighted half shows where it will land (left, right, top, or bottom); drop to open it beside the thread you are reading. Pane headers drag to rearrange, dividers resize, and the layout persists per window. Panes nest up to a 2x2 grid. The route stays the source of truth for the focused thread: clicking into a pane navigates to its thread, and a navigation to a thread outside the layout takes over the focused pane instead of tearing the layout down. Background panes render and stream but never own window-level input (shortcuts, autofocus, paste), so nothing fires twice. Entry points: sidebar drag (reuses the existing dnd-kit gesture, published to a small drag store the pane overlays read), the thread context menu (Open in split view), and pane.close for closing the focused pane. Written by Claude Fable 5.1 in Claude Code.
1 parent 02a38d5 commit 55bf180

16 files changed

Lines changed: 1311 additions & 31 deletions

‎apps/web/src/chatPaneDragStore.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
import { create } from "zustand";
3+
4+
import type { ChatPaneId, DropZone } from "./chatPanes.logic";
5+
6+
export interface ChatPaneDropTarget {
7+
readonly paneId: ChatPaneId;
8+
readonly zone: DropZone;
9+
}
10+
11+
/**
12+
* A thread being carried over the chat area. The sidebar's dnd-kit gesture
13+
* and a pane header's pointer drag both publish here, so the pane drop
14+
* overlays have one source to read and never care where the drag began. The
15+
* gesture owner reads `target` on release and applies the drop itself.
16+
*/
17+
interface ChatPaneDragState {
18+
threadRef: ScopedThreadRef | null;
19+
title: string;
20+
/** Pane the thread came from, when rearranging an open pane. */
21+
sourcePaneId: ChatPaneId | null;
22+
target: ChatPaneDropTarget | null;
23+
start: (input: { threadRef: ScopedThreadRef; title: string; sourcePaneId?: ChatPaneId }) => void;
24+
setTarget: (paneId: ChatPaneId, zone: DropZone | null) => void;
25+
end: () => void;
26+
}
27+
28+
export const useChatPaneDragStore = create<ChatPaneDragState>((set, get) => ({
29+
threadRef: null,
30+
title: "",
31+
sourcePaneId: null,
32+
target: null,
33+
start: ({ threadRef, title, sourcePaneId }) =>
34+
set({ threadRef, title, sourcePaneId: sourcePaneId ?? null, target: null }),
35+
setTarget: (paneId, zone) =>
36+
set((state) => {
37+
if (state.threadRef === null) return state;
38+
if (zone === null) {
39+
return state.target?.paneId === paneId ? { target: null } : state;
40+
}
41+
return state.target?.paneId === paneId && state.target.zone === zone
42+
? state
43+
: { target: { paneId, zone } };
44+
}),
45+
end: () => {
46+
if (get().threadRef !== null) {
47+
set({ threadRef: null, title: "", sourcePaneId: null, target: null });
48+
}
49+
},
50+
}));
51+
52+
export function isChatPaneDragActive(): boolean {
53+
return useChatPaneDragStore.getState().threadRef !== null;
54+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2+
import { type EnvironmentId, ThreadId } from "@t3tools/contracts";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import {
6+
canSplitPane,
7+
collectLeaves,
8+
removePane,
9+
resolveDropZone,
10+
setPaneRatio,
11+
splitPane,
12+
type ChatPaneLeaf,
13+
type ChatPaneNode,
14+
} from "./chatPanes.logic";
15+
16+
const leaf = (id: string): ChatPaneLeaf => ({
17+
kind: "leaf",
18+
id,
19+
threadRef: scopeThreadRef("env-1" as EnvironmentId, ThreadId.make(`thread-${id}`)),
20+
});
21+
const allowAll = () => true;
22+
const rect = { left: 0, top: 0, width: 300, height: 100 };
23+
24+
describe("splitPane", () => {
25+
it("places the new leaf on the dropped side", () => {
26+
const root = splitPane(leaf("a"), "a", "left", leaf("b"), "s1");
27+
expect(root).toMatchObject({
28+
kind: "split",
29+
direction: "horizontal",
30+
first: { id: "b" },
31+
second: { id: "a" },
32+
});
33+
const bottom = splitPane(leaf("a"), "a", "bottom", leaf("b"), "s1");
34+
expect(bottom).toMatchObject({
35+
direction: "vertical",
36+
first: { id: "a" },
37+
second: { id: "b" },
38+
});
39+
});
40+
41+
it("caps depth at a 2x2 grid and refuses same-axis nesting", () => {
42+
let root: ChatPaneNode = splitPane(leaf("a"), "a", "right", leaf("b"), "s1");
43+
expect(canSplitPane(root, "a", "horizontal")).toBe(false);
44+
expect(canSplitPane(root, "a", "vertical")).toBe(true);
45+
root = splitPane(root, "a", "bottom", leaf("c"), "s2");
46+
expect(canSplitPane(root, "c", "horizontal")).toBe(false);
47+
expect(canSplitPane(root, "c", "vertical")).toBe(false);
48+
expect(splitPane(root, "c", "right", leaf("d"), "s3")).toBe(root);
49+
expect(collectLeaves(root).map((item) => item.id)).toEqual(["a", "c", "b"]);
50+
});
51+
});
52+
53+
describe("removePane", () => {
54+
it("collapses the sibling into the parent slot and empties the last leaf", () => {
55+
const root = splitPane(leaf("a"), "a", "right", leaf("b"), "s1");
56+
expect(removePane(root, "a")).toEqual(leaf("b"));
57+
expect(removePane(leaf("a"), "a")).toBeNull();
58+
expect(removePane(root, "missing")).toBe(root);
59+
});
60+
});
61+
62+
describe("setPaneRatio", () => {
63+
it("clamps into the allowed range", () => {
64+
const root = splitPane(leaf("a"), "a", "right", leaf("b"), "s1");
65+
expect(setPaneRatio(root, "s1", 0.05)).toMatchObject({ ratio: 0.2 });
66+
expect(setPaneRatio(root, "s1", 2)).toMatchObject({ ratio: 0.8 });
67+
expect(setPaneRatio(root, "nope", 0.3)).toBe(root);
68+
});
69+
});
70+
71+
describe("resolveDropZone", () => {
72+
it("prefers the long axis edges and falls back to the short axis in the middle", () => {
73+
expect(resolveDropZone(rect, 10, 50, allowAll)).toBe("left");
74+
expect(resolveDropZone(rect, 290, 50, allowAll)).toBe("right");
75+
expect(resolveDropZone(rect, 150, 10, allowAll)).toBe("top");
76+
expect(resolveDropZone(rect, 150, 90, allowAll)).toBe("bottom");
77+
const tall = { left: 0, top: 0, width: 100, height: 300 };
78+
expect(resolveDropZone(tall, 50, 10, allowAll)).toBe("top");
79+
expect(resolveDropZone(tall, 10, 150, allowAll)).toBe("left");
80+
});
81+
82+
it("skips zones the tree refuses and ignores pointers outside the rect", () => {
83+
const onlyVertical = (zone: string) => zone === "top" || zone === "bottom";
84+
expect(resolveDropZone(rect, 10, 60, onlyVertical)).toBe("bottom");
85+
expect(resolveDropZone(rect, 150, 50, () => false)).toBeNull();
86+
expect(resolveDropZone(rect, -1, 50, allowAll)).toBeNull();
87+
});
88+
});

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

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
3+
/**
4+
* Pure helpers for the chat pane layout: a binary tree of splits whose leaves
5+
* each show one server thread. The tree stays shallow on purpose (a leaf can
6+
* split at most twice, and only across its parent's axis) so the result is
7+
* never deeper than a 2x2 grid. Anything beyond that stops being readable.
8+
*/
9+
10+
export type ChatPaneId = string;
11+
export type SplitDirection = "horizontal" | "vertical";
12+
export type DropZone = "top" | "bottom" | "left" | "right";
13+
14+
export interface ChatPaneLeaf {
15+
readonly kind: "leaf";
16+
readonly id: ChatPaneId;
17+
readonly threadRef: ScopedThreadRef;
18+
}
19+
20+
export interface ChatPaneSplit {
21+
readonly kind: "split";
22+
readonly id: ChatPaneId;
23+
readonly direction: SplitDirection;
24+
readonly first: ChatPaneNode;
25+
readonly second: ChatPaneNode;
26+
/** Share of the axis given to `first`, clamped to [MIN_RATIO, MAX_RATIO]. */
27+
readonly ratio: number;
28+
}
29+
30+
export type ChatPaneNode = ChatPaneLeaf | ChatPaneSplit;
31+
32+
const MAX_PANE_DEPTH = 2;
33+
const MIN_PANE_RATIO = 0.2;
34+
const MAX_PANE_RATIO = 0.8;
35+
const EDGE_REGION_FRACTION = 1 / 3;
36+
37+
export function clampPaneRatio(ratio: number): number {
38+
if (!Number.isFinite(ratio)) return 0.5;
39+
return Math.min(MAX_PANE_RATIO, Math.max(MIN_PANE_RATIO, ratio));
40+
}
41+
42+
export function collectLeaves(node: ChatPaneNode): ChatPaneLeaf[] {
43+
return node.kind === "leaf"
44+
? [node]
45+
: [...collectLeaves(node.first), ...collectLeaves(node.second)];
46+
}
47+
48+
export function findLeafByThread(
49+
node: ChatPaneNode,
50+
threadRef: ScopedThreadRef,
51+
): ChatPaneLeaf | null {
52+
return (
53+
collectLeaves(node).find(
54+
(leaf) =>
55+
leaf.threadRef.environmentId === threadRef.environmentId &&
56+
leaf.threadRef.threadId === threadRef.threadId,
57+
) ?? null
58+
);
59+
}
60+
61+
function findPaneDepth(node: ChatPaneNode, paneId: ChatPaneId): number | null {
62+
if (node.id === paneId) return 0;
63+
if (node.kind === "leaf") return null;
64+
const first = findPaneDepth(node.first, paneId);
65+
if (first !== null) return first + 1;
66+
const second = findPaneDepth(node.second, paneId);
67+
return second === null ? null : second + 1;
68+
}
69+
70+
function findParent(node: ChatPaneNode, paneId: ChatPaneId): ChatPaneSplit | null {
71+
if (node.kind === "leaf") return null;
72+
if (node.first.id === paneId || node.second.id === paneId) return node;
73+
return findParent(node.first, paneId) ?? findParent(node.second, paneId);
74+
}
75+
76+
/** A leaf can split while it is above the depth cap and never along the same axis as its parent. */
77+
export function canSplitPane(
78+
root: ChatPaneNode,
79+
paneId: ChatPaneId,
80+
direction: SplitDirection,
81+
): boolean {
82+
const depth = findPaneDepth(root, paneId);
83+
if (depth === null || depth >= MAX_PANE_DEPTH) return false;
84+
const parent = findParent(root, paneId);
85+
return parent === null || parent.direction !== direction;
86+
}
87+
88+
export function zoneToSplit(zone: DropZone): {
89+
direction: SplitDirection;
90+
side: "first" | "second";
91+
} {
92+
switch (zone) {
93+
case "left":
94+
return { direction: "horizontal", side: "first" };
95+
case "right":
96+
return { direction: "horizontal", side: "second" };
97+
case "top":
98+
return { direction: "vertical", side: "first" };
99+
case "bottom":
100+
return { direction: "vertical", side: "second" };
101+
}
102+
}
103+
104+
export function splitPane(
105+
root: ChatPaneNode,
106+
paneId: ChatPaneId,
107+
zone: DropZone,
108+
newLeaf: ChatPaneLeaf,
109+
splitId: ChatPaneId,
110+
): ChatPaneNode {
111+
const { direction, side } = zoneToSplit(zone);
112+
if (!canSplitPane(root, paneId, direction)) return root;
113+
const replace = (node: ChatPaneNode): ChatPaneNode => {
114+
if (node.id === paneId) {
115+
return {
116+
kind: "split",
117+
id: splitId,
118+
direction,
119+
ratio: 0.5,
120+
first: side === "first" ? newLeaf : node,
121+
second: side === "first" ? node : newLeaf,
122+
};
123+
}
124+
if (node.kind === "leaf") return node;
125+
const first = replace(node.first);
126+
const second = replace(node.second);
127+
return first === node.first && second === node.second ? node : { ...node, first, second };
128+
};
129+
return replace(root);
130+
}
131+
132+
/** Removes a leaf. Its sibling takes the parent's slot; the last leaf leaves an empty layout. */
133+
export function removePane(root: ChatPaneNode, paneId: ChatPaneId): ChatPaneNode | null {
134+
if (root.id === paneId) return null;
135+
if (root.kind === "leaf") return root;
136+
const first = removePane(root.first, paneId);
137+
const second = removePane(root.second, paneId);
138+
if (first === null) return second;
139+
if (second === null) return first;
140+
return first === root.first && second === root.second ? root : { ...root, first, second };
141+
}
142+
143+
export function setPaneRatio(root: ChatPaneNode, splitId: ChatPaneId, ratio: number): ChatPaneNode {
144+
if (root.kind === "leaf") return root;
145+
if (root.id === splitId) return { ...root, ratio: clampPaneRatio(ratio) };
146+
const first = setPaneRatio(root.first, splitId, ratio);
147+
const second = setPaneRatio(root.second, splitId, ratio);
148+
return first === root.first && second === root.second ? root : { ...root, first, second };
149+
}
150+
151+
/**
152+
* Which edge the pointer is closest to, VS Code style: the outer third of the
153+
* long axis wins outright, the middle falls back to the short axis. Zones the
154+
* tree cannot honor are skipped so the preview never promises a split that
155+
* the drop would refuse.
156+
*/
157+
export function resolveDropZone(
158+
rect: { left: number; top: number; width: number; height: number },
159+
clientX: number,
160+
clientY: number,
161+
isZoneAllowed: (zone: DropZone) => boolean,
162+
): DropZone | null {
163+
if (rect.width <= 0 || rect.height <= 0) return null;
164+
const relX = (clientX - rect.left) / rect.width;
165+
const relY = (clientY - rect.top) / rect.height;
166+
if (relX < 0 || relX > 1 || relY < 0 || relY > 1) return null;
167+
const horizontal = relX < 0.5 ? (["left", "right"] as const) : (["right", "left"] as const);
168+
const vertical = relY < 0.5 ? (["top", "bottom"] as const) : (["bottom", "top"] as const);
169+
const pick = (candidates: readonly DropZone[]) => candidates.find(isZoneAllowed) ?? null;
170+
const wide = rect.width >= rect.height;
171+
const [longAxis, longRel, shortAxis] = wide
172+
? [horizontal, relX, vertical]
173+
: [vertical, relY, horizontal];
174+
if (longRel < EDGE_REGION_FRACTION || longRel > 1 - EDGE_REGION_FRACTION) {
175+
const edge = pick([longAxis[0]]);
176+
if (edge) return edge;
177+
}
178+
return pick(shortAxis) ?? pick(longAxis);
179+
}

0 commit comments

Comments
 (0)