Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

Fix terminal focus on toggle, clean up orphaned terminal state, and revoke optimistic message blob URLs on unmount

Cursor Agent32fe80d1647e
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 8d8719855..73178a2dc 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -479,6 +479,8 @@ export default function ChatView({ threadId }: ChatViewProps) {
const [isDragOverComposer, setIsDragOverComposer] = useState(false);
const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
const [optimisticUserMessages, setOptimisticUserMessages] = useState<ChatMessage[]>([]);
+ const optimisticUserMessagesRef = useRef(optimisticUserMessages);
+ optimisticUserMessagesRef.current = optimisticUserMessages;
const [localDraftErrorsByThreadId, setLocalDraftErrorsByThreadId] = useState<
Record<ThreadId, string | null>
>({});
@@ -634,6 +636,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
useEffect(() => {
return () => {
clearAttachmentPreviewHandoffs();
+ for (const message of optimisticUserMessagesRef.current) {
+ revokeUserMessagePreviewUrls(message);
+ }
};
}, [clearAttachmentPreviewHandoffs]);
const handoffAttachmentPreviews = useCallback((messageId: MessageId, previewUrls: string[]) => {
@@ -1542,6 +1547,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
if (!previous && current) {
terminalOpenByThreadRef.current[activeThreadId] = current;
+ setTerminalFocusRequestId((value) => value + 1);
return;
} else if (previous && !current) {
terminalOpenByThreadRef.current[activeThreadId] = current;
diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx
index d5921da8a..d32b604cf 100644
--- a/apps/web/src/components/Sidebar.tsx
+++ b/apps/web/src/components/Sidebar.tsx
@@ -241,6 +241,7 @@ export default function Sidebar() {
);
const getDraftThread = useComposerDraftStore((store) => store.getDraftThread);
const terminalStateByThreadId = useTerminalStateStore((state) => state.terminalStateByThreadId);
+ const clearTerminalState = useTerminalStateStore((state) => state.clearTerminalState);
const setProjectDraftThreadId = useComposerDraftStore((store) => store.setProjectDraftThreadId);
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const clearProjectDraftThreadId = useComposerDraftStore(
@@ -578,6 +579,7 @@ export default function Sidebar() {
});
clearComposerDraftForThread(threadId);
clearProjectDraftThreadById(thread.projectId, thread.id);
+ clearTerminalState(threadId);
if (shouldNavigateToFallback) {
if (fallbackThreadId) {
void navigate({
@@ -619,6 +621,7 @@ export default function Sidebar() {
appSettings.confirmThreadDelete,
clearComposerDraftForThread,
clearProjectDraftThreadById,
+ clearTerminalState,
markThreadUnread,
navigate,
projects,
diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx
index 219a22d84..36f8abac8 100644
--- a/apps/web/src/routes/__root.tsx
+++ b/apps/web/src/routes/__root.tsx
@@ -143,11 +143,20 @@ function EventRouter() {
let syncing = false;
let pending = false;
+ const removeOrphanedTerminalStates =
+ useTerminalStateStore.getState().removeOrphanedTerminalStates;
+
const flushSnapshotSync = async (): Promise<void> => {
const snapshot = await api.orchestration.getSnapshot();
if (disposed) return;
latestSequence = Math.max(latestSequence, snapshot.snapshotSequence);
syncServerReadModel(snapshot);
+ const activeThreadIds = new Set(
+ snapshot.threads
+ .filter((t) => t.deletedAt === null)
+ .map((t) => t.id),
+ );
+ removeOrphanedTerminalStates(activeThreadIds);
if (pending) {
pending = false;
await flushSnapshotSync();
diff --git a/apps/web/src/terminalStateStore.ts b/apps/web/src/terminalStateStore.ts
index 20f66910a..5f65dca3a 100644
--- a/apps/web/src/terminalStateStore.ts
+++ b/apps/web/src/terminalStateStore.ts
@@ -461,6 +461,7 @@ interface TerminalStateStoreState {
hasRunningSubprocess: boolean,
) => void;
clearTerminalState: (threadId: ThreadId) => void;
+ removeOrphanedTerminalStates: (activeThreadIds: Set<ThreadId>) => void;
}
export const useTerminalStateStore = create<TerminalStateStoreState>()(
@@ -505,6 +506,18 @@ export const useTerminalStateStore = create<TerminalStateStoreState>()(
),
clearTerminalState: (threadId) =>
updateTerminal(threadId, () => createDefaultThreadTerminalState()),
+ removeOrphanedTerminalStates: (activeThreadIds) =>
+ set((state) => {
+ const orphanedIds = Object.keys(state.terminalStateByThreadId).filter(
+ (id) => !activeThreadIds.has(id as ThreadId),
+ );
+ if (orphanedIds.length === 0) return state;
+ const next = { ...state.terminalStateByThreadId };
+ for (const id of orphanedIds) {
+ delete next[id as ThreadId];
+ }
+ return { terminalStateByThreadId: next };
+ }),
};
},
{