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 <[email protected]>
This commit is contained in:
1 parent
6e93d42023
commit
32fe80d164
4 files changed
+31
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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 };
|
||||
}),
|
||||
};
|
||||
},
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user