@devries-cameron20 / t3code-1 / commits / a97f7f8e8d
Fork 0

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

refactor(web): ChatView's persistent terminal drawers live in their own component (#22680)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 21, 2026 at 01:36 UTC a97f7f8e8d872a4ed1c8ecdedbd7e75803f5d276
2 files changed +123 -68
apps/web/src/components/ChatView.tsx
+14 -68
@@ -255,7 +255,6 @@ import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
255 import { isEditableFocused } from "../lib/editableFocus";
256 import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
257 import { shortcutLabelForCommand } from "../keybindings";
258 -import { PersistentThreadTerminalDrawer } from "./PersistentThreadTerminals";
258 import {
259 AlarmClockIcon,
260 CheckCircle2Icon,
@@ -379,7 +378,6 @@ import {
378 useThreadHistory,
379 useQuietThreadShell,
380 useChildThreadInputs,
382 - useThreadRefs,
381 useThreadVisibleTurnItems,
382 useThreadCheckpointSummaries,
383 readThreadShell,
@@ -468,7 +466,6 @@ import {
466 } from "./chat/draftHeroTransition";
467 import type { ComposerDispatchMode } from "@t3tools/client-runtime/state/composer-dispatch";
468 import {
471 - MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
469 branchMismatchKey,
470 buildExpiredTerminalContextToastCopy,
471 buildLocalDraftThread,
@@ -490,7 +487,6 @@ import {
487 deriveLockedProvider,
488 readFileAsDataUrl,
489 resolveFileAttachmentUrl,
493 - reconcileMountedTerminalThreadIds,
490 resolveComposerInteractionMode,
491 resolveComposerProviderSelection,
492 getAntigravitySendBlockReason,
@@ -547,6 +543,7 @@ import {
543 } from "@t3tools/shared/composerContextReferences";
544 import { observeResize } from "~/lib/observeResize";
545 import { ThreadVisitWatermark } from "./chat/ThreadVisitWatermark";
546 +import { ChatTerminalDrawers } from "./chat/ChatTerminalDrawers";
547 import { useDraftHeroLayoutTransition } from "./chat/useDraftHeroLayoutTransition";
548 import { useLocalDispatchState } from "./chat/useLocalDispatchState";
549 import {
@@ -1033,31 +1030,12 @@ export default function ChatView(props: ChatViewProps) {
1030 const terminalUiState = useTerminalUiStateStore((state) =>
1031 selectThreadTerminalUiState(state.terminalUiStateByThreadKey, routeThreadRef),
1032 );
1036 - const openTerminalThreadKeys = useTerminalUiStateStore(
1037 - useShallow((state) =>
1038 - Object.entries(state.terminalUiStateByThreadKey).flatMap(
1039 - ([nextThreadKey, nextTerminalUiState]) =>
1040 - nextTerminalUiState.terminalOpen ? [nextThreadKey] : [],
1041 - ),
1042 - ),
1043 - );
1033 const storeSetTerminalOpen = useTerminalUiStateStore((s) => s.setTerminalOpen);
1034 const storeEnsureTerminal = useTerminalUiStateStore((state) => state.ensureTerminal);
1035 const storeSplitTerminal = useTerminalUiStateStore((s) => s.splitTerminal);
1036 const storeSplitTerminalVertical = useTerminalUiStateStore((s) => s.splitTerminalVertical);
1037 const storeNewTerminal = useTerminalUiStateStore((s) => s.newTerminal);
1038 const storeCloseTerminal = useTerminalUiStateStore((s) => s.closeTerminal);
1050 - const serverThreadRefs = useThreadRefs();
1051 - const serverThreadKeys = serverThreadRefs.map(scopedThreadKey);
1052 - const draftThreadsByThreadKey = useComposerDraftStore((store) => store.draftThreadsByThreadKey);
1053 - const draftThreadKeys = Object.values(draftThreadsByThreadKey).map((draftThread) =>
1054 - scopedThreadKey(scopeThreadRef(draftThread.environmentId, draftThread.threadId)),
1055 - );
1056 - const [mountedTerminalThreadKeys, setMountedTerminalThreadKeys] = useState<string[]>([]);
1057 - const mountedTerminalThreadRefs = mountedTerminalThreadKeys.flatMap((mountedThreadKey) => {
1058 - const mountedThreadRef = parseScopedThreadKey(mountedThreadKey);
1059 - return mountedThreadRef ? [{ key: mountedThreadKey, threadRef: mountedThreadRef }] : [];
1060 - });
1039
1040 const fallbackDraftProjectRef = draftThread
1041 ? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
@@ -1310,13 +1288,6 @@ export default function ChatView(props: ChatViewProps) {
1288 const rightPanelOpen = rightPanelState.isOpen;
1289 const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
1290 usePanelAnimationSettings();
1313 - const activeTerminalDrawerPresence = usePanelPresence(
1314 - Boolean(activeThreadKey && terminalUiState.terminalOpen),
1315 - true,
1316 - panelAnimationsActive,
1317 - activeThreadKey,
1318 - panelAnimationDurationMs,
1319 - );
1291 const rightPanelPresenceValue = {
1292 activeSurface: activeRightPanelSurface,
1293 surfaces: rightPanelState.surfaces,
@@ -1380,25 +1351,6 @@ export default function ChatView(props: ChatViewProps) {
1351 }
1352 }, [activePreviewMiniPlayer, activePreviewState.sessions, activeThreadRef, previewSessionsReady]);
1353
1383 - const existingOpenTerminalThreadKeys = (() => {
1384 - const existingThreadKeys = new Set<string>([...serverThreadKeys, ...draftThreadKeys]);
1385 - return openTerminalThreadKeys.filter((nextThreadKey) => existingThreadKeys.has(nextThreadKey));
1386 - })();
1387 - const reconciledMountedTerminalThreadKeys = reconcileMountedTerminalThreadIds({
1388 - currentThreadIds: mountedTerminalThreadKeys,
1389 - openThreadIds: existingOpenTerminalThreadKeys,
1390 - activeThreadId: activeThreadKey,
1391 - activeThreadTerminalOpen: activeTerminalDrawerPresence.present,
1392 - maxHiddenThreadCount: MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
1393 - });
1394 - if (
1395 - reconciledMountedTerminalThreadKeys.length !== mountedTerminalThreadKeys.length ||
1396 - reconciledMountedTerminalThreadKeys.some(
1397 - (threadKey, index) => threadKey !== mountedTerminalThreadKeys[index],
1398 - )
1399 - ) {
1400 - setMountedTerminalThreadKeys(reconciledMountedTerminalThreadKeys);
1401 - }
1354 const latestRunSettled = isLatestRunSettled(activeLatestRun, activeRuntime);
1355 const activePlan = deriveActivePlanState(serverProjection, activeActivityRun?.runId);
1356 const activeActivityRunSettled = isLatestRunSettled(activeActivityRun, activeRuntime);
@@ -8438,25 +8390,19 @@ export default function ChatView(props: ChatViewProps) {
8390 </ThreadFindCanvas>
8391 </div>
8392
8441 - {mountedTerminalThreadRefs.map(({ key: mountedThreadKey, threadRef: mountedThreadRef }) => (
8442 - <PersistentThreadTerminalDrawer
8443 - key={mountedThreadKey}
8444 - threadRef={mountedThreadRef}
8445 - threadId={mountedThreadRef.threadId}
8446 - active={mountedThreadKey === activeThreadKey}
8447 - composerHeight={mountedThreadKey === activeThreadKey ? composerReservedHeight : 0}
8448 - launchContext={
8449 - mountedThreadKey === activeThreadKey ? (activeTerminalLaunchContext ?? null) : null
8450 - }
8451 - focusRequestId={mountedThreadKey === activeThreadKey ? terminalFocusRequestId : 0}
8452 - splitShortcutLabel={splitTerminalShortcutLabel ?? undefined}
8453 - splitVerticalShortcutLabel={splitTerminalVerticalShortcutLabel ?? undefined}
8454 - newShortcutLabel={newTerminalShortcutLabel ?? undefined}
8455 - closeShortcutLabel={closeTerminalShortcutLabel ?? undefined}
8456 - keybindings={keybindings}
8457 - onAddTerminalContext={addTerminalContextToDraft}
8458 - />
8459 - ))}
8393 + <ChatTerminalDrawers
8394 + activeThreadKey={activeThreadKey}
8395 + activeTerminalOpen={Boolean(terminalUiState.terminalOpen)}
8396 + composerHeight={composerReservedHeight}
8397 + launchContext={activeTerminalLaunchContext ?? null}
8398 + focusRequestId={terminalFocusRequestId}
8399 + splitShortcutLabel={splitTerminalShortcutLabel ?? undefined}
8400 + splitVerticalShortcutLabel={splitTerminalVerticalShortcutLabel ?? undefined}
8401 + newShortcutLabel={newTerminalShortcutLabel ?? undefined}
8402 + closeShortcutLabel={closeTerminalShortcutLabel ?? undefined}
8403 + keybindings={keybindings}
8404 + onAddTerminalContext={addTerminalContextToDraftStable}
8405 + />
8406 </div>
8407
8408 {rightPanelPresent && !shouldUsePlanSidebarSheet && activeThreadRef ? (
apps/web/src/components/chat/ChatTerminalDrawers.tsx new
+109
@@ -0,0 +1,109 @@
1 +import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
2 +import {
3 + parseScopedThreadKey,
4 + scopedThreadKey,
5 + scopeThreadRef,
6 +} from "@t3tools/client-runtime/environment";
7 +import { useState } from "react";
8 +import { useShallow } from "zustand/react/shallow";
9 +import { useComposerDraftStore } from "../../composerDraftStore";
10 +import type { TerminalContextSelection } from "../../lib/terminalContext";
11 +import { usePanelAnimationSettings, usePanelPresence } from "../../panelAnimations";
12 +import { useThreadRefs } from "../../state/entities";
13 +import { useTerminalUiStateStore } from "../../terminalUiStateStore";
14 +import {
15 + MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
16 + reconcileMountedTerminalThreadIds,
17 +} from "../ChatView.logic";
18 +import {
19 + PersistentThreadTerminalDrawer,
20 + type PersistentTerminalLaunchContext,
21 +} from "../PersistentThreadTerminals";
22 +
23 +interface ChatTerminalDrawersProps {
24 + readonly activeThreadKey: string | null;
25 + readonly activeTerminalOpen: boolean;
26 + readonly composerHeight: number;
27 + readonly launchContext: PersistentTerminalLaunchContext | null;
28 + readonly focusRequestId: number;
29 + readonly splitShortcutLabel: string | undefined;
30 + readonly splitVerticalShortcutLabel: string | undefined;
31 + readonly newShortcutLabel: string | undefined;
32 + readonly closeShortcutLabel: string | undefined;
33 + readonly keybindings: ResolvedKeybindingsConfig;
34 + readonly onAddTerminalContext: (selection: TerminalContextSelection) => void;
35 +}
36 +
37 +/**
38 + * Keeps terminal drawers mounted for recently open threads, so switching back
39 + * to one finds its sessions as they were. Every thread's terminal and draft
40 + * state feeds the reconcile, so it subscribes here instead of in ChatView.
41 + */
42 +export function ChatTerminalDrawers(props: ChatTerminalDrawersProps) {
43 + const { activeThreadKey } = props;
44 + const openTerminalThreadKeys = useTerminalUiStateStore(
45 + useShallow((state) =>
46 + Object.entries(state.terminalUiStateByThreadKey).flatMap(
47 + ([nextThreadKey, nextTerminalUiState]) =>
48 + nextTerminalUiState.terminalOpen ? [nextThreadKey] : [],
49 + ),
50 + ),
51 + );
52 + const serverThreadRefs = useThreadRefs();
53 + const serverThreadKeys = serverThreadRefs.map(scopedThreadKey);
54 + const draftThreadsByThreadKey = useComposerDraftStore((store) => store.draftThreadsByThreadKey);
55 + const draftThreadKeys = Object.values(draftThreadsByThreadKey).map((draftThread) =>
56 + scopedThreadKey(scopeThreadRef(draftThread.environmentId, draftThread.threadId)),
57 + );
58 + const { active: panelAnimationsActive, durationMs: panelAnimationDurationMs } =
59 + usePanelAnimationSettings();
60 + const activeTerminalDrawerPresence = usePanelPresence(
61 + Boolean(activeThreadKey && props.activeTerminalOpen),
62 + true,
63 + panelAnimationsActive,
64 + activeThreadKey,
65 + panelAnimationDurationMs,
66 + );
67 + const [mountedTerminalThreadKeys, setMountedTerminalThreadKeys] = useState<string[]>([]);
68 + const existingOpenTerminalThreadKeys = (() => {
69 + const existingThreadKeys = new Set<string>([...serverThreadKeys, ...draftThreadKeys]);
70 + return openTerminalThreadKeys.filter((nextThreadKey) => existingThreadKeys.has(nextThreadKey));
71 + })();
72 + const reconciledMountedTerminalThreadKeys = reconcileMountedTerminalThreadIds({
73 + currentThreadIds: mountedTerminalThreadKeys,
74 + openThreadIds: existingOpenTerminalThreadKeys,
75 + activeThreadId: activeThreadKey,
76 + activeThreadTerminalOpen: activeTerminalDrawerPresence.present,
77 + maxHiddenThreadCount: MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
78 + });
79 + if (
80 + reconciledMountedTerminalThreadKeys.length !== mountedTerminalThreadKeys.length ||
81 + reconciledMountedTerminalThreadKeys.some(
82 + (threadKey, index) => threadKey !== mountedTerminalThreadKeys[index],
83 + )
84 + ) {
85 + setMountedTerminalThreadKeys(reconciledMountedTerminalThreadKeys);
86 + }
87 + const mountedTerminalThreadRefs = mountedTerminalThreadKeys.flatMap((mountedThreadKey) => {
88 + const mountedThreadRef = parseScopedThreadKey(mountedThreadKey);
89 + return mountedThreadRef ? [{ key: mountedThreadKey, threadRef: mountedThreadRef }] : [];
90 + });
91 +
92 + return mountedTerminalThreadRefs.map(({ key: mountedThreadKey, threadRef: mountedThreadRef }) => (
93 + <PersistentThreadTerminalDrawer
94 + key={mountedThreadKey}
95 + threadRef={mountedThreadRef}
96 + threadId={mountedThreadRef.threadId}
97 + active={mountedThreadKey === activeThreadKey}
98 + composerHeight={mountedThreadKey === activeThreadKey ? props.composerHeight : 0}
99 + launchContext={mountedThreadKey === activeThreadKey ? props.launchContext : null}
100 + focusRequestId={mountedThreadKey === activeThreadKey ? props.focusRequestId : 0}
101 + splitShortcutLabel={props.splitShortcutLabel}
102 + splitVerticalShortcutLabel={props.splitVerticalShortcutLabel}
103 + newShortcutLabel={props.newShortcutLabel}
104 + closeShortcutLabel={props.closeShortcutLabel}
105 + keybindings={props.keybindings}
106 + onAddTerminalContext={props.onAddTerminalContext}
107 + />
108 + ));
109 +}