@devries-cameron20 / t3code Mirror · read-only / commits / 7ea6db49e4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(web): a mounted chat view no longer keeps every thread copy it replaced (#36390)

Julius Marminge committed Nov 13, 2026 at 13:01 UTC 7ea6db49e425513065057b8931ac4275545d5560
3 files changed +81 -16
apps/web/src/components/ChatView.browser.test.tsx
+58 -2
@@ -1,10 +1,10 @@
1 import { createMemoryHistory } from "@tanstack/react-router";
2 import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
3 -import { ThreadId } from "@t3tools/contracts";
3 +import { RunId, ThreadId } from "@t3tools/contracts";
4 import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
5 import type * as Context from "effect/Context";
6 import { expect, it, vi } from "vite-plus/test";
7 -import { page, userEvent } from "vite-plus/test/browser";
7 +import { cdp, page, userEvent } from "vite-plus/test/browser";
8
9 import { AppRoot } from "~/AppRoot";
10 // The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines)
@@ -15,10 +15,14 @@ import { selectThreadDiffPanelSelection, useDiffPanelStore } from "~/diffPanelSt
15 import { __setClientSettingsForTests } from "~/hooks/useSettings";
16 import { useRightPanelStore } from "~/rightPanelStore";
17 import { getRouter } from "~/router";
18 +import { appAtomRegistry } from "~/rpc/atomRegistry";
19 +import { environmentThreadDetails } from "~/state/threads";
20 import {
21 ENVIRONMENT_ID,
22 PROJECT_ID,
23 + assistantItem,
24 dispatchedCommands,
25 + emitThreadEvent,
26 installFakeEnvironment,
27 rejectPendingCommand,
28 threadWithHistory,
@@ -303,3 +307,55 @@ it("opening a thread or returning to the window on a touch tablet does not focus
307 vi.advanceTimersToNextFrame();
308 expect(document.activeElement).not.toBe(composer.element());
309 });
310 +
311 +it("a mounted thread view lets go of the thread structures it has replaced", async () => {
312 + // Items are shared with the fixture, so a reopen's snapshot also carries the earlier updates.
313 + const history = threadWithHistory("updated", 3);
314 + const items = [...history.items];
315 + const updated = { ...history, items };
316 + const other = threadWithHistory("other", 3);
317 + using _environment = installFakeEnvironment([updated, other]);
318 + __setClientSettingsForTests({
319 + ...DEFAULT_CLIENT_SETTINGS,
320 + onboardingCompletedAt: "2026-01-01T00:00:00.000Z",
321 + });
322 + const router = getRouter(createMemoryHistory({ initialEntries: ["/"] }));
323 + await router.load();
324 + await using _app = await render(<AppRoot router={router} />, { router: false });
325 + const open = async (threadId: ThreadId, text: string) => {
326 + await act(() =>
327 + router.navigate({
328 + to: "/$environmentId/$threadId",
329 + params: { environmentId: ENVIRONMENT_ID, threadId },
330 + }),
331 + );
332 + await expect.element(page.getByText(text).first()).toBeVisible();
333 + };
334 + const structureAtom = environmentThreadDetails.quietThreadAtom(
335 + scopeThreadRef(ENVIRONMENT_ID, updated.id),
336 + );
337 +
338 + // Each round, a new answer arrives while the thread is shown, then the user switches away.
339 + const shown: Array<WeakRef<object>> = [];
340 + await open(other.id, "Answer 2 for other.");
341 + for (let round = 0; round < 6; round++) {
342 + await open(updated.id, round === 0 ? "Answer 2 for updated." : `Update ${round - 1}.`);
343 + const update = assistantItem(
344 + updated.id,
345 + RunId.make("updated-run-2"),
346 + `updated-update-${round}`,
347 + 6 + round,
348 + `Update ${round}.`,
349 + );
350 + items.push(update);
351 + await act(() => emitThreadEvent(updated.id, { type: "turn-item.updated", payload: update }));
352 + await expect.element(page.getByText(`Update ${round}.`)).toBeVisible();
353 + shown.push(new WeakRef(appAtomRegistry.get(structureAtom)!.projection));
354 + await open(other.id, "Answer 2 for other.");
355 + }
356 + await cdp().send("HeapProfiler.collectGarbage");
357 +
358 + // The thread keeps its latest structure, and the timeline may remember the one before it.
359 + const kept = shown.slice(0, -2).flatMap((structure, round) => (structure.deref() ? [round] : []));
360 + expect(kept).toEqual([]);
361 +});
apps/web/src/components/ChatView.tsx
+5 -14
@@ -19,6 +19,7 @@ import {
19 import { usePullRequestDraftThread } from "./chat/usePullRequestDraftThread";
20 import { usePullRequestLinking } from "../hooks/usePullRequestLinking";
21 import { useComposerSelection } from "./chat/useComposerSelection";
22 +import { useComposerFocus } from "./chat/useComposerFocus";
23 import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
24 import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
25 import {
@@ -195,7 +196,6 @@ import { isEditableFocused } from "../lib/editableFocus";
196 import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
197 import { shortcutLabelForCommand } from "../keybindings";
198 import { cn } from "~/lib/utils";
198 -import { isModelPickerOpen } from "../modelPickerVisibility";
199 import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
200 import { toastManager } from "./ui/toast";
201 import { projectScriptIdFromCommand } from "~/projectScripts";
@@ -1833,10 +1833,8 @@ export default function ChatView(props: ChatViewProps) {
1833 sendInFlightRef,
1834 });
1835
1836 - const focusComposer = () => {
1837 - composerRef.current?.focusAtEnd();
1838 - };
1839 - const focusComposerStable = useStableCallback(focusComposer);
1836 + const { focusComposer: focusComposerStable, scheduleComposerFocus: scheduleComposerFocusStable } =
1837 + useComposerFocus(composerRef);
1838 const canInterruptRunningThread =
1839 canOperateThread && deriveCanInterruptRunningThread(activeThread !== undefined, activeRuntime);
1840 const onInterrupt = async () => {
@@ -1863,13 +1861,6 @@ export default function ChatView(props: ChatViewProps) {
1861 () => (embedded ? undefined : subscribeSnapShotComposerFocus(focusComposerStable)),
1862 [embedded, focusComposerStable],
1863 );
1866 - const scheduleComposerFocus = () => {
1867 - window.requestAnimationFrame(() => {
1868 - // The model picker stays open through its own changes, so it keeps focus.
1869 - if (!isModelPickerOpen()) focusComposer();
1870 - });
1871 - };
1872 - const scheduleComposerFocusStable = useStableCallback(scheduleComposerFocus);
1864 const insertArtifactTemplate = (template: CodexArtifactTemplate) => {
1865 const composer = composerRef.current;
1866 if (!composer) return;
@@ -1884,7 +1875,7 @@ export default function ChatView(props: ChatViewProps) {
1875 });
1876 return;
1877 }
1887 - scheduleComposerFocus();
1878 + scheduleComposerFocusStable();
1879 };
1880 const insertArtifactTemplateStable = useStableCallback(insertArtifactTemplate);
1881 const sendAppMessage = async (text: string) => {
@@ -2890,7 +2881,7 @@ export default function ChatView(props: ChatViewProps) {
2881 canOverrideServerThreadEnvMode,
2882 setPendingServerThreadEnvModeEntry,
2883 setPendingServerThreadStartFromOriginByThreadId,
2893 - scheduleComposerFocus,
2884 + scheduleComposerFocus: scheduleComposerFocusStable,
2885 });
2886 const { onResume, isResuming } = useComposerResume({
2887 environmentId,
apps/web/src/components/chat/useComposerFocus.ts new
+18
@@ -0,0 +1,18 @@
1 +import type { ComposerHandleRef } from "../../composerHandleContext";
2 +import { useStableCallback } from "../../hooks/useStableCallback";
3 +import { isModelPickerOpen } from "../../modelPickerVisibility";
4 +
5 +// Kept out of ChatView: a compiler-memoized closure there pins its render's scope, which pins the
6 +// previous render's, so every thread copy ChatView showed stayed alive until it unmounted.
7 +export function useComposerFocus(composerRef: ComposerHandleRef) {
8 + const focusComposer = useStableCallback(() => {
9 + composerRef.current?.focusAtEnd();
10 + });
11 + const scheduleComposerFocus = useStableCallback(() => {
12 + window.requestAnimationFrame(() => {
13 + // The model picker stays open through its own changes, so it keeps focus.
14 + if (!isModelPickerOpen()) focusComposer();
15 + });
16 + });
17 + return { focusComposer, scheduleComposerFocus };
18 +}