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 25, 11:00 UTC
Fix terminal focus on toggle, clean up orphaned terminal state, and revoke optimistic message blob URLs on unmount
- Add setTerminalFocusRequestId call when terminal opens via toggle so the terminal panel receives focus automatically - Call clearTerminalState when deleting threads in the Sidebar and add removeOrphanedTerminalStates to prune stale entries after read model sync - Track optimisticUserMessages via ref and revoke their blob URLs in the unmount cleanup effect to prevent memory leaks Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
Cursor Agent committed
Mar 2, 2026 at 20:22 UTC
32fe80d1647ea9fb466fe5fab67b74fea78f011b
4 files changed
+31
apps/web/src/components/ChatView.tsx
+6
@@ -479,6 +479,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
479
const [isDragOverComposer, setIsDragOverComposer] = useState(false);
480
const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
481
const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]);
482
+ const optimisticUserMessagesRef = useRef(optimisticUserMessages);
483
+ optimisticUserMessagesRef.current = optimisticUserMessages;
484
const [localDraftErrorsByThreadId, setLocalDraftErrorsByThreadId] = useState<
485
Record<ThreadId, string | null>
486
>({});
@@ -634,6 +636,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
636
useEffect(() => {
637
return () => {
638
clearAttachmentPreviewHandoffs();
639
+ for (const message of optimisticUserMessagesRef.current) {
640
+ revokeUserMessagePreviewUrls(message);
641
+ }
642
};
643
}, [clearAttachmentPreviewHandoffs]);
644
const handoffAttachmentPreviews = useCallback((messageId: MessageId, previewUrls: string[]) => {
@@ -1542,6 +1547,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
1547
1548
if (!previous && current) {
1549
terminalOpenByThreadRef.current[activeThreadId] = current;
1550
+ setTerminalFocusRequestId((value) => value + 1);
1551
return;
1552
} else if (previous && !current) {
1553
terminalOpenByThreadRef.current[activeThreadId] = current;
apps/web/src/components/Sidebar.tsx
+3
@@ -241,6 +241,7 @@ export default function Sidebar() {
241
);
242
const getDraftThread = useComposerDraftStore((store) => store.getDraftThread);
243
const terminalStateByThreadId = useTerminalStateStore((state) => state.terminalStateByThreadId);
244
+ const clearTerminalState = useTerminalStateStore((state) => state.clearTerminalState);
245
const setProjectDraftThreadId = useComposerDraftStore((store) => store.setProjectDraftThreadId);
246
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
247
const clearProjectDraftThreadId = useComposerDraftStore(
@@ -578,6 +579,7 @@ export default function Sidebar() {
579
});
580
clearComposerDraftForThread(threadId);
581
clearProjectDraftThreadById(thread.projectId, thread.id);
582
+ clearTerminalState(threadId);
583
if (shouldNavigateToFallback) {
584
if (fallbackThreadId) {
585
void navigate({
@@ -619,6 +621,7 @@ export default function Sidebar() {
621
appSettings.confirmThreadDelete,
622
clearComposerDraftForThread,
623
clearProjectDraftThreadById,
624
+ clearTerminalState,
625
markThreadUnread,
626
navigate,
627
projects,
apps/web/src/routes/__root.tsx
+9
@@ -143,11 +143,20 @@ function EventRouter() {
143
let syncing = false;
144
let pending = false;
145
146
+ const removeOrphanedTerminalStates =
147
+ useTerminalStateStore.getState().removeOrphanedTerminalStates;
148
+
149
const flushSnapshotSync = async (): Promise<void> => {
150
const snapshot = await api.orchestration.getSnapshot();
151
if (disposed) return;
152
latestSequence = Math.max(latestSequence, snapshot.snapshotSequence);
153
syncServerReadModel(snapshot);
154
+ const activeThreadIds = new Set(
155
+ snapshot.threads
156
+ .filter((t) => t.deletedAt === null)
157
+ .map((t) => t.id),
158
+ );
159
+ removeOrphanedTerminalStates(activeThreadIds);
160
if (pending) {
161
pending = false;
162
await flushSnapshotSync();
apps/web/src/terminalStateStore.ts
+13
@@ -461,6 +461,7 @@ interface TerminalStateStoreState {
461
hasRunningSubprocess: boolean,
462
) => void;
463
clearTerminalState: (threadId: ThreadId) => void;
464
+ removeOrphanedTerminalStates: (activeThreadIds: Set<ThreadId>) => void;
465
}
466
467
export const useTerminalStateStore = create<TerminalStateStoreState>()(
@@ -505,6 +506,18 @@ export const useTerminalStateStore = create<TerminalStateStoreState>()(
506
),
507
clearTerminalState: (threadId) =>
508
updateTerminal(threadId, () => createDefaultThreadTerminalState()),
509
+ removeOrphanedTerminalStates: (activeThreadIds) =>
510
+ set((state) => {
511
+ const orphanedIds = Object.keys(state.terminalStateByThreadId).filter(
512
+ (id) => !activeThreadIds.has(id as ThreadId),
513
+ );
514
+ if (orphanedIds.length === 0) return state;
515
+ const next = { ...state.terminalStateByThreadId };
516
+ for (const id of orphanedIds) {
517
+ delete next[id as ThreadId];
518
+ }
519
+ return { terminalStateByThreadId: next };
520
+ }),
521
};
522
},
523
{