Fix store reactivity: stable dispatch ref, reactive terminal state, no-op guard

- useStore(): wrap dispatch actions in useMemo so the reference is stable
  across renders, preventing infinite effect loops in EventRouter
- Sidebar: subscribe to terminalStateByThreadId data instead of the
  getTerminalState function reference so terminal indicators re-render
- reduceThreadTerminalState: return original state (not normalized copy)
  for no-op actions so the reference equality guard in applyTerminalAction
  can short-circuit unnecessary store updates

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Cursor AgentandJulius Marminge committed 2026-03-02 18:40:49 +00:00
1 parent c283d2a85b
commit 004658797e
3 files changed
+23 -13

No files matched your search

+2 -2
View File
@@ -237,7 +237,7 @@ export default function Sidebar() {
(store) => store.getDraftThreadByProjectId,
);
const getDraftThread = useComposerDraftStore((store) => store.getDraftThread);
const getTerminalState = useTerminalStateStore((s) => s.getTerminalState);
const terminalStateByThreadId = useTerminalStateStore((s) => s.terminalStateByThreadId);
const setProjectDraftThreadId = useComposerDraftStore((store) => store.setProjectDraftThreadId);
const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
const clearProjectDraftThreadId = useComposerDraftStore(
@@ -816,7 +816,7 @@ export default function Sidebar() {
);
const prStatus = prStatusIndicator(prByThreadId.get(thread.id) ?? null);
const terminalStatus = terminalStatusFromRunningIds(
getTerminalState(thread.id).runningTerminalIds,
terminalStateByThreadId[thread.id]?.runningTerminalIds ?? [],
);
return (
+15 -5
View File
@@ -1,4 +1,4 @@
import { Fragment, type ReactNode, createElement, useEffect } from "react";
import { Fragment, type ReactNode, createElement, useEffect, useMemo } from "react";
import {
DEFAULT_MODEL,
ProviderSessionId,
@@ -416,8 +416,18 @@ export function useStore() {
threadsHydrated: s.threadsHydrated,
runtimeMode: s.runtimeMode,
}));
return {
state,
dispatch: useAppStore.getState(),
};
const dispatch = useMemo(() => {
const s = useAppStore.getState();
return {
syncServerReadModel: s.syncServerReadModel,
markThreadVisited: s.markThreadVisited,
markThreadUnread: s.markThreadUnread,
toggleProject: s.toggleProject,
setProjectExpanded: s.setProjectExpanded,
setError: s.setError,
setThreadBranch: s.setThreadBranch,
setRuntimeMode: s.setRuntimeMode,
};
}, []);
return { state, dispatch };
}
+6 -6
View File
@@ -251,7 +251,7 @@ export function reduceThreadTerminalState(
const normalized = normalizeThreadTerminalState(state);
if (action.type === "set-open") {
if (normalized.terminalOpen === action.open) return normalized;
if (normalized.terminalOpen === action.open) return state;
return { ...normalized, terminalOpen: action.open };
}
@@ -261,14 +261,14 @@ export function reduceThreadTerminalState(
action.height <= 0 ||
normalized.terminalHeight === action.height
) {
return normalized;
return state;
}
return { ...normalized, terminalHeight: action.height };
}
if (action.type === "set-active") {
if (!normalized.terminalIds.includes(action.terminalId)) {
return normalized;
return state;
}
const activeTerminalGroupId =
normalized.terminalGroups.find((group) => group.terminalIds.includes(action.terminalId))?.id ??
@@ -286,7 +286,7 @@ export function reduceThreadTerminalState(
if (action.type === "set-activity") {
if (!normalized.terminalIds.includes(action.terminalId)) {
return normalized;
return state;
}
const runningTerminalIds = new Set(normalized.runningTerminalIds);
if (action.hasRunningSubprocess) {
@@ -298,12 +298,12 @@ export function reduceThreadTerminalState(
}
if (!action.terminalId || action.terminalId.trim().length === 0) {
return normalized;
return state;
}
const isNewTerminal = !normalized.terminalIds.includes(action.terminalId);
if (isNewTerminal && normalized.terminalIds.length >= MAX_THREAD_TERMINAL_COUNT) {
return normalized;
return state;
}
const terminalIds = isNewTerminal