Skip to content

Commit 0c7bed7

Browse files
committed
fix(web): address split pane review findings
Open in split works again for the thread a closed split last showed. A hidden pane in a narrow window no longer counts as visible for toasts, notifications, the sidebar outline or device previews. Removing a thread replaces its history entry and no longer pulls the user out of Settings. A pane's right panel sheet stays within its pane and no longer locks the other pane. Single-pane routes and loading threads no longer show the unavailable pane, and the mobile sidebar closes after Open in split.
1 parent 9085f26 commit 0c7bed7

11 files changed

Lines changed: 107 additions & 24 deletions

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EnvironmentId, ThreadId } from "@t3tools/contracts";
33
import { describe, expect, it } from "vite-plus/test";
44

55
import {
6+
backgroundChatPaneThreadRefs,
67
chatPaneRestoreTarget,
78
closeChatPane,
89
isChatSplitOpen,
@@ -16,10 +17,8 @@ import {
1617
import { DraftId } from "./composerDraftStore";
1718
import type { ThreadRouteTarget } from "./threadRoutes";
1819

19-
const thread = (id: string): ThreadRouteTarget => ({
20-
kind: "server",
21-
threadRef: scopeThreadRef(EnvironmentId.make("env"), ThreadId.make(id)),
22-
});
20+
const threadRef = (id: string) => scopeThreadRef(EnvironmentId.make("env"), ThreadId.make(id));
21+
const thread = (id: string): ThreadRouteTarget => ({ kind: "server", threadRef: threadRef(id) });
2322
const draft = (id: string): ThreadRouteTarget => ({ kind: "draft", draftId: DraftId.make(id) });
2423

2524
function paneIds() {
@@ -72,6 +71,12 @@ describe("chat panes", () => {
7271
]);
7372
});
7473

74+
it("counts the unfocused pane's thread as visible only while the split fits", () => {
75+
const layout = split(thread("a"), thread("b"));
76+
expect(backgroundChatPaneThreadRefs(layout, thread("a"), true)).toEqual([threadRef("b")]);
77+
expect(backgroundChatPaneThreadRefs(layout, thread("a"), false)).toEqual([]);
78+
});
79+
7580
it("commits nothing when the route already matches the layout", () => {
7681
const layout = split(thread("a"), thread("b"));
7782
expect(commitChatPanes(layout, thread("a"))).toBe(layout);
@@ -134,6 +139,19 @@ describe("chat panes", () => {
134139
]);
135140
});
136141

142+
it("opens a split again for the thread the remaining pane showed before the route moved", () => {
143+
const closed = closeChatPane(split(thread("a"), thread("b")), "pane-1").layout;
144+
const reopened = openChatSplit(closed, {
145+
routeTarget: thread("c"),
146+
target: thread("a"),
147+
createPaneId: paneIds(),
148+
});
149+
expect(shown(reopened, thread("c"))).toEqual([
150+
{ id: "primary", target: "c", active: true },
151+
{ id: "pane-1", target: "a", active: false },
152+
]);
153+
});
154+
137155
it("closing the focused pane moves the route to the remaining pane", () => {
138156
const result = closeChatPane(split(thread("a"), thread("b")), "primary");
139157
expect(result.focusTarget).toEqual(thread("b"));

‎apps/web/src/chatPanesStore.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ export function resolveChatPanes(
8080
);
8181
}
8282

83-
/** Threads in unfocused panes for this route; a draft pane has no thread until it sends. */
83+
/**
84+
* Threads the unfocused pane shows for this route; a draft pane has no thread until it sends.
85+
* None while the window is too narrow for the split, since only the focused pane renders then.
86+
*/
8487
export function backgroundChatPaneThreadRefs(
8588
layout: ChatPanesLayout,
8689
routeTarget: ThreadRouteTarget | null,
90+
splitFits: boolean,
8791
): ReadonlyArray<ScopedThreadRef> {
88-
if (routeTarget === null) return [];
92+
if (routeTarget === null || !splitFits) return [];
8993
return resolveChatPanes(layout, routeTarget).flatMap((pane) =>
9094
!pane.active && pane.target.kind === "server" ? [pane.target.threadRef] : [],
9195
);
@@ -135,9 +139,15 @@ export function openChatSplit(
135139
): ChatPanesLayout {
136140
const targetKey = chatPaneTargetKey(input.target);
137141
const current = commitChatPanes(layout, input.routeTarget);
138-
if (current.panes.some((pane) => chatPaneTargetKey(pane.target) === targetKey)) return current;
139142
if (chatPaneTargetKey(input.routeTarget) === targetKey) return current;
140143
const direction = input.direction ?? current.direction;
144+
// A single pane's stored target can be stale; only a split shows its panes' targets.
145+
if (
146+
isChatSplitOpen(current) &&
147+
current.panes.some((pane) => chatPaneTargetKey(pane.target) === targetKey)
148+
) {
149+
return current;
150+
}
141151
if (!isChatSplitOpen(current)) {
142152
const activePaneId = current.panes[0]?.id ?? PRIMARY_CHAT_PANE_ID;
143153
return {
@@ -234,6 +244,9 @@ function readPersistedLayout(value: unknown): ChatPanesLayout {
234244

235245
interface ChatPanesStoreState {
236246
readonly layout: ChatPanesLayout;
247+
/** Whether the chat area is wide enough to render both panes of a split. */
248+
readonly splitFits: boolean;
249+
readonly setSplitFits: (splitFits: boolean) => void;
237250
readonly commit: (routeTarget: ThreadRouteTarget) => void;
238251
readonly openSplit: (input: {
239252
readonly routeTarget: ThreadRouteTarget;
@@ -251,6 +264,9 @@ export const useChatPanesStore = create<ChatPanesStoreState>()(
251264
persist(
252265
(set, get) => ({
253266
layout: SINGLE_CHAT_PANE_LAYOUT,
267+
splitFits: true,
268+
setSplitFits: (splitFits) =>
269+
set((state) => (state.splitFits === splitFits ? state : { splitFits })),
254270
commit: (routeTarget) =>
255271
set((state) => {
256272
const layout = commitChatPanes(state.layout, routeTarget);

‎apps/web/src/components/ChatPanes.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ export function ChatPanes({
7272
const vertical = layout.direction === "column";
7373
const minPaneSize = vertical ? CHAT_PANE_MIN_HEIGHT : CHAT_PANE_MIN_WIDTH;
7474
// Too small for two panes: only the focused one renders, and the split returns with room.
75-
const [fitsSplit, setFitsSplit] = useState(true);
75+
// The store holds it so notifications treat the hidden pane's thread as not visible.
76+
const fitsSplit = useChatPanesStore((state) => state.splitFits);
7677
const observeContainer = (container: HTMLDivElement) => {
7778
const measure = () => {
7879
const size = vertical ? container.clientHeight : container.clientWidth;
79-
setFitsSplit(size >= minPaneSize * 2 + 1);
80+
useChatPanesStore.getState().setSplitFits(size >= minPaneSize * 2 + 1);
8081
};
8182
measure();
8283
return observeResize(container, measure);
@@ -172,6 +173,7 @@ export function ChatPanes({
172173
onFocus={() => focusPane(pane.target)}
173174
focusIntentRef={focusIntentRef}
174175
direction={layout.direction}
176+
threadKey={paneThreadKeys[index] ?? null}
175177
hostsHiddenTerminals={alone || index === 0}
176178
otherPaneThreadKeys={
177179
alone
@@ -303,6 +305,7 @@ function ChatPane({
303305
edges,
304306
background,
305307
direction,
308+
threadKey,
306309
hostsHiddenTerminals,
307310
otherPaneThreadKeys,
308311
onClose,
@@ -320,6 +323,7 @@ function ChatPane({
320323
edges: WindowEdges;
321324
background: boolean;
322325
direction: ChatPaneDirection;
326+
threadKey: string | null;
323327
hostsHiddenTerminals: boolean;
324328
/** Newline-joined, so an unchanged set keeps the context value and spares ChatView a render. */
325329
otherPaneThreadKeys: string;
@@ -379,6 +383,7 @@ function ChatPane({
379383
data-chat-pane={pane.id}
380384
data-chat-pane-narrow={(!alone && narrow) || undefined}
381385
data-chat-pane-below-titlebar={!edges.top || undefined}
386+
data-chat-pane-thread={threadKey ?? undefined}
382387
data-background-thread-view={passive || undefined}
383388
onPointerDownCapture={passive ? onFocus : undefined}
384389
onFocusCapture={passive ? onFocusIn : undefined}
@@ -390,7 +395,7 @@ function ChatPane({
390395
target={pane.target}
391396
background={background}
392397
passive={passive}
393-
unavailable={<UnavailablePane onClose={close} />}
398+
unavailable={inSplit ? <UnavailablePane onClose={close} /> : null}
394399
onMissing={inSplit ? close : undefined}
395400
/>
396401
</ChatPaneContext>

‎apps/web/src/components/RightPanelSheet.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export function RightPanelSheet(props: {
1414
return (
1515
<Sheet
1616
open={props.open}
17+
// A modal sheet would also lock the other pane.
18+
modal={paneElement === undefined}
1719
onOpenChange={(open) => {
1820
if (!open) {
1921
props.onClose();
@@ -26,7 +28,7 @@ export function RightPanelSheet(props: {
2628
showCloseButton={false}
2729
keepMounted
2830
portalContainer={paneElement}
29-
className="w-[min(42vw,28rem)] min-w-80 max-w-[28rem] max-[760px]:w-[min(88vw,24rem)] max-[760px]:min-w-0 wco:mt-(--workspace-topbar-height) wco:h-[calc(100%-var(--workspace-topbar-height))] wco:max-h-[calc(100%-var(--workspace-topbar-height))]"
31+
className="w-[min(42vw,28rem)] min-w-80 max-w-[min(28rem,100%)] max-[760px]:w-[min(88vw,24rem)] max-[760px]:min-w-0 wco:mt-(--workspace-topbar-height) wco:h-[calc(100%-var(--workspace-topbar-height))] wco:max-h-[calc(100%-var(--workspace-topbar-height))]"
3032
>
3133
{props.children}
3234
</SheetPopup>

‎apps/web/src/components/Sidebar.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,9 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
10911091
const backgroundPaneThreadKeys = new Set(
10921092
useChatPanesStore(
10931093
useShallow((state) =>
1094-
backgroundChatPaneThreadRefs(state.layout, routeTarget).map(scopedThreadKey),
1094+
backgroundChatPaneThreadRefs(state.layout, routeTarget, state.splitFits).map(
1095+
scopedThreadKey,
1096+
),
10951097
),
10961098
),
10971099
);
@@ -2692,7 +2694,7 @@ export default function Sidebar({ routeReveal }: { routeReveal: SidebarRouteReve
26922694
}
26932695
switch (clicked.value) {
26942696
case "open-in-split":
2695-
openInSplit(threadRef);
2697+
if (openInSplit(threadRef) && isMobile) setOpenMobile(false);
26962698
return;
26972699
case "filter-by-project":
26982700
if (threadProjectGroup) {

‎apps/web/src/components/ThreadNotificationCoordinator.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export function startThreadNotifications(
155155
!backgroundChatPaneThreadRefs(
156156
useChatPanesStore.getState().layout,
157157
resolveThreadRouteTarget(active),
158+
useChatPanesStore.getState().splitFits,
158159
).some((ref) => ref.environmentId === environmentId && ref.threadId === thread.id)
159160
) {
160161
const toastKey = threadKey({ environmentId, threadId: thread.id });

‎apps/web/src/components/ThreadRouteView.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ export function ThreadRouteContent({
218218
);
219219
}
220220

221-
const showUnavailable = view === null && (target.kind === "draft" || bootstrapComplete);
221+
// A thread whose detail is still loading shows nothing yet, as an unsplit route does.
222+
const showUnavailable =
223+
view === null && (target.kind === "draft" || (bootstrapComplete && renderState !== "loading"));
222224
return (
223225
<>
224226
<BackgroundThreadContext value={!ownsRoute}>

‎apps/web/src/components/preview/agentOpenedPreviews.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ describe("applyDeviceSnapshot", () => {
127127
expect(miniPlayerSource()).toBeUndefined();
128128
});
129129

130+
it("leaves only the sessions of threads in a narrow split pane to the sheet", () => {
131+
setup();
132+
const otherRef = scopeThreadRef(ref.environmentId, ThreadId.make("thread-2"));
133+
const otherSession = { ...session, threadId: otherRef.threadId };
134+
const narrowPane = { ...floating, sheetLayoutThreadKeys: new Set([threadKey]) };
135+
applyDeviceSnapshot(ref.environmentId, deviceState({}), narrowPane);
136+
applyDeviceSnapshot(
137+
ref.environmentId,
138+
deviceState({ sessions: [session, otherSession] }),
139+
narrowPane,
140+
);
141+
expect(miniPlayerSource()).toBeUndefined();
142+
expect(
143+
usePreviewMiniPlayerStore.getState().byThreadKey[scopedThreadKey(otherRef)]?.source,
144+
).toMatchObject({ kind: "device", deviceId: "pixel" });
145+
});
146+
130147
it("closes a floating device when its session ends, even before a baseline", () => {
131148
setup();
132149
usePreviewMiniPlayerStore.getState().open(ref, {

‎apps/web/src/components/preview/agentOpenedPreviews.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ function hasLiveSession(
152152
export function applyDeviceSnapshot(
153153
environmentId: EnvironmentId,
154154
state: DeviceServiceState,
155-
options: { readonly sheetLayout: boolean; readonly autoShowFloatingPreview: boolean },
155+
options: {
156+
readonly sheetLayout: boolean;
157+
/** Threads whose own pane is too narrow for the inline panel, where sheetLayout applies. */
158+
readonly sheetLayoutThreadKeys?: ReadonlySet<string>;
159+
readonly autoShowFloatingPreview: boolean;
160+
},
156161
): void {
157162
const deviceFor = (session: DeviceServiceState["sessions"][number]) =>
158163
state.devices.find((entry) => entry.hostId === session.hostId && entry.id === session.deviceId);
@@ -176,6 +181,7 @@ export function applyDeviceSnapshot(
176181
const device = deviceFor(session);
177182
if (!device) continue;
178183
const threadRef = scopeThreadRef(environmentId, session.threadId);
184+
if (options.sheetLayoutThreadKeys?.has(scopedThreadKey(threadRef))) continue;
179185
const target = {
180186
hostId: session.hostId,
181187
deviceId: session.deviceId,
@@ -224,10 +230,16 @@ function watchEnvironmentDevices(environmentId: EnvironmentId): () => void {
224230
const state = AsyncResult.getOrElse(result, () => null);
225231
if (state === null) return;
226232
applyDeviceSnapshot(environmentId, state, {
233+
sheetLayout: window.matchMedia(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY).matches,
227234
// A split pane too narrow for the inline right panel opens it as a sheet too.
228-
sheetLayout:
229-
window.matchMedia(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY).matches ||
230-
document.querySelector("[data-chat-pane-narrow]") !== null,
235+
sheetLayoutThreadKeys: new Set(
236+
Array.from(
237+
document.querySelectorAll<HTMLElement>(
238+
"[data-chat-pane-narrow][data-chat-pane-thread]",
239+
),
240+
(pane) => pane.dataset.chatPaneThread ?? "",
241+
),
242+
),
231243
autoShowFloatingPreview: getClientSettings().browserAutoShowFloatingPreview,
232244
});
233245
},

‎apps/web/src/components/ui/toast.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ function useVisibleThreadRefsFromRoute(): ReadonlyArray<ScopedThreadRef> {
452452
routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
453453
);
454454
const backgroundRefs = useChatPanesStore(
455-
useShallow((state) => backgroundChatPaneThreadRefs(state.layout, routeTarget)),
455+
useShallow((state) => backgroundChatPaneThreadRefs(state.layout, routeTarget, state.splitFits)),
456456
);
457457

458458
if (routeTarget?.kind === "server") {

0 commit comments

Comments
 (0)