Skip to content

Commit 2414165

Browse files
committed
fix(web): keep pane ownership consistent across layout remounts
The route-change ownership reset used a component ref, so a thread picked while the chat layout was unmounted (from Settings) went unnoticed, and both remounting ChatViews raced their composer autofocus — whichever frame fired last stole ownership. The last seen route key now lives at module level and the mount autofocus only runs in the active pane. The palette's close-time focus also claims the active secondary pane when it shows a composer-less notice, the saved-prompt footer hint uses the same composer resolution as the insert, and a server thread's mention scope comes from the thread's own projectId instead of degrading to env-only while the project entity loads. Findings by gpt-5.6-sol; fixes by Claude Fable 5 in Claude Code.
1 parent 714b490 commit 2414165

4 files changed

Lines changed: 49 additions & 21 deletions

File tree

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4350,12 +4350,22 @@ function ChatViewContent(props: ChatViewProps) {
43504350
useEffect(() => {
43514351
if (!activeThread?.id || terminalUiState.terminalOpen) return;
43524352
const frame = window.requestAnimationFrame(() => {
4353+
// Split view: both panes mount fresh when the chat layout remounts
4354+
// (e.g. returning from Settings) — only the active pane's autofocus
4355+
// may run, or whichever pane's frame fires last steals ownership.
4356+
if (!isThreadPaneActive(threadPaneId)) return;
43534357
focusComposer();
43544358
});
43554359
return () => {
43564360
window.cancelAnimationFrame(frame);
43574361
};
4358-
}, [activeThread?.id, activeThreadKey, focusComposer, terminalUiState.terminalOpen]);
4362+
}, [
4363+
activeThread?.id,
4364+
activeThreadKey,
4365+
focusComposer,
4366+
terminalUiState.terminalOpen,
4367+
threadPaneId,
4368+
]);
43594369

43604370
useEffect(() => {
43614371
if (!activeThread?.id) return;
@@ -7472,9 +7482,15 @@ function ChatViewContent(props: ChatViewProps) {
74727482
composerDraftTarget={composerDraftTarget}
74737483
environmentId={environmentId}
74747484
mentionScope={
7475-
activeProject
7476-
? `${activeProject.environmentId}:${activeProject.id}`
7477-
: environmentId
7485+
// The thread itself carries the project id even
7486+
// while the project entity is still loading —
7487+
// degrading to env-only there would briefly
7488+
// accept cross-project drops.
7489+
activeThread
7490+
? `${activeThread.environmentId}:${activeThread.projectId}`
7491+
: activeProject
7492+
? `${activeProject.environmentId}:${activeProject.id}`
7493+
: environmentId
74787494
}
74797495
routeKind={routeKind}
74807496
routeThreadRef={routeThreadRef}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ import type { ChatComposerHandle } from "./chat/ChatComposer";
200200
import { buildOpenInSplitThreadItems } from "./thread-split/splitPaletteItems";
201201
import {
202202
activeThreadPaneComposerHandle,
203-
focusActiveThreadPaneComposer,
203+
focusActiveThreadPane,
204204
focusOtherThreadPane,
205205
THREAD_SPLIT_MEDIA_QUERY,
206206
useThreadSplitStore,
@@ -618,7 +618,7 @@ function CommandPaletteDialog(props: {
618618
// The app-root composer belongs to the primary pane; while the split
619619
// is open with the secondary pane active (including a pick that just
620620
// activated it), focus must return there instead.
621-
if (!focusActiveThreadPaneComposer()) {
621+
if (!focusActiveThreadPane()) {
622622
composerHandleRef?.current?.focusAtEnd();
623623
}
624624
return false;
@@ -2521,10 +2521,11 @@ function OpenCommandPaletteDialog(props: {
25212521
const isSavedPromptsView = currentView?.groups[0]?.value === SAVED_PROMPTS_GROUP_VALUE;
25222522
// Ref read at render: fine for a footer hint — the composer mounts long
25232523
// before the palette opens, and re-opening re-renders this component. The
2524-
// active split pane's composer takes precedence over the app-root one.
2524+
// active split pane's composer takes precedence over the app-root one —
2525+
// same resolution as the insert below, so the hint never advertises an
2526+
// insert the action would refuse (composer-less secondary notice state).
25252527
const canInsertSavedPrompt =
2526-
isSavedPromptsView &&
2527-
(activeThreadPaneComposerHandle()?.current ?? composerHandleRef?.current) != null;
2528+
isSavedPromptsView && (activeThreadPaneComposerHandle() ?? composerHandleRef)?.current != null;
25282529
const hasHighlightedBrowseItem = highlightedItemValue?.startsWith("browse:") ?? false;
25292530
const canSubmitBrowsePath =
25302531
isBrowsing &&

‎apps/web/src/components/thread-split/SplitThreadLayout.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ const SPLIT_GRID_TEMPLATE_COLUMNS =
2929
"minmax(min(20rem, 45%), var(--thread-split-a)) auto minmax(min(20rem, 45%), var(--thread-split-b))";
3030
const SINGLE_GRID_TEMPLATE_COLUMNS = "minmax(0, 1fr)";
3131

32+
// Module-level so route changes made while the chat layout is unmounted
33+
// (e.g. picking another thread from Settings) still register as a change on
34+
// remount — a component ref would re-initialize and miss them.
35+
const UNOBSERVED_ROUTE_THREAD_KEY = Symbol("unobserved");
36+
const lastSeenRouteThreadKey: { value: string | null | typeof UNOBSERVED_ROUTE_THREAD_KEY } = {
37+
value: UNOBSERVED_ROUTE_THREAD_KEY,
38+
};
39+
3240
/**
3341
* Wraps the chat routes' content. The tree shape is identical whether the
3442
* split is open or closed — only the separator and the secondary pane mount
@@ -72,10 +80,11 @@ export function SplitThreadLayout({ children }: { children: ReactNode }) {
7280
// mod+shift+E archive) must follow, or the secondary pane would keep
7381
// swallowing thread-targeted shortcuts after the user switched threads.
7482
const routeThreadKey = routeThreadRef === null ? null : scopedThreadKey(routeThreadRef);
75-
const previousRouteThreadKeyRef = useRef(routeThreadKey);
7683
useEffect(() => {
77-
if (previousRouteThreadKeyRef.current === routeThreadKey) return;
78-
previousRouteThreadKeyRef.current = routeThreadKey;
84+
if (lastSeenRouteThreadKey.value === routeThreadKey) return;
85+
const isFirstObservation = lastSeenRouteThreadKey.value === UNOBSERVED_ROUTE_THREAD_KEY;
86+
lastSeenRouteThreadKey.value = routeThreadKey;
87+
if (isFirstObservation) return;
7988
useThreadSplitStore.getState().setActivePane("primary");
8089
}, [routeThreadKey]);
8190

‎apps/web/src/components/thread-split/threadSplitStore.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,19 @@ export function activeThreadPaneComposerHandle(): ComposerHandleRef | null {
219219
}
220220

221221
/**
222-
* Overlay close hook (command palette): return focus to the active pane's
223-
* composer. Returns false when the primary/app-root composer is the right
224-
* target anyway, so callers can keep their default.
222+
* Overlay close hook (command palette): return focus to the active pane.
223+
* Falls through to the pane root when it shows a composer-less notice, so
224+
* dismissing an overlay never silently hands ownership to the other pane.
225+
* Returns false when the primary/app-root composer is the right target
226+
* anyway, so callers can keep their default.
225227
*/
226-
export function focusActiveThreadPaneComposer(): boolean {
227-
const composer = activeThreadPaneComposerHandle();
228-
if (composer?.current) {
229-
composer.current.focusAtEnd();
230-
return true;
228+
export function focusActiveThreadPane(): boolean {
229+
const state = useThreadSplitStore.getState();
230+
if (!state.splitMounted || state.activePaneId !== "secondary") {
231+
return false;
231232
}
232-
return false;
233+
focusThreadPane("secondary");
234+
return true;
233235
}
234236

235237
/**

0 commit comments

Comments
 (0)