101
threadId: ThreadId,
102
updater: (t: Thread) => Thread,
103
): Thread[] {
104
- return threads.map((t) => (t.id === threadId ? updater(t) : t));
104
+ let changed = false;
105
+ const next = threads.map((t) => {
106
+ if (t.id !== threadId) return t;
107
+ const updated = updater(t);
108
+ if (updated !== t) changed = true;
109
+ return updated;
110
+ });
111
+ return changed ? next : threads;
112
}
113
114
function mapProjectsFromReadModel(
284
): AppState {
285
const at = visitedAt ?? new Date().toISOString();
286
const visitedAtMs = Date.parse(at);
280
- return {
281
- ...state,
282
- threads: updateThread(state.threads, threadId, (thread) => {
283
- const previousVisitedAtMs = thread.lastVisitedAt ? Date.parse(thread.lastVisitedAt) : NaN;
284
- if (
285
- Number.isFinite(previousVisitedAtMs) &&
286
- Number.isFinite(visitedAtMs) &&
287
- previousVisitedAtMs >= visitedAtMs
288
- ) {
289
- return thread;
290
- }
291
- return { ...thread, lastVisitedAt: at };
292
- }),
293
- };
287
+ const threads = updateThread(state.threads, threadId, (thread) => {
288
+ const previousVisitedAtMs = thread.lastVisitedAt ? Date.parse(thread.lastVisitedAt) : NaN;
289
+ if (
290
+ Number.isFinite(previousVisitedAtMs) &&
291
+ Number.isFinite(visitedAtMs) &&
292
+ previousVisitedAtMs >= visitedAtMs
293
+ ) {
294
+ return thread;
295
+ }
296
+ return { ...thread, lastVisitedAt: at };
297
+ });
298
+ return threads === state.threads ? state : { ...state, threads };
299
}
300
301
export function markThreadUnread(state: AppState, threadId: ThreadId): AppState {
297
- return {
298
- ...state,
299
- threads: updateThread(state.threads, threadId, (thread) => {
300
- if (!thread.latestTurn?.completedAt) return thread;
301
- const latestTurnCompletedAtMs = Date.parse(thread.latestTurn.completedAt);
302
- if (Number.isNaN(latestTurnCompletedAtMs)) return thread;
303
- const unreadVisitedAt = new Date(latestTurnCompletedAtMs - 1).toISOString();
304
- if (thread.lastVisitedAt === unreadVisitedAt) return thread;
305
- return { ...thread, lastVisitedAt: unreadVisitedAt };
306
- }),
307
- };
302
+ const threads = updateThread(state.threads, threadId, (thread) => {
303
+ if (!thread.latestTurn?.completedAt) return thread;
304
+ const latestTurnCompletedAtMs = Date.parse(thread.latestTurn.completedAt);
305
+ if (Number.isNaN(latestTurnCompletedAtMs)) return thread;
306
+ const unreadVisitedAt = new Date(latestTurnCompletedAtMs - 1).toISOString();
307
+ if (thread.lastVisitedAt === unreadVisitedAt) return thread;
308
+ return { ...thread, lastVisitedAt: unreadVisitedAt };
309
+ });
310
+ return threads === state.threads ? state : { ...state, threads };
311
}
312
313
export function toggleProject(state: AppState, projectId: Project["id"]): AppState {
324
projectId: Project["id"],
325
expanded: boolean,
326
): AppState {
324
- return {
325
- ...state,
326
- projects: state.projects.map((p) =>
327
- p.id === projectId ? { ...p, expanded } : p,
328
- ),
329
- };
327
+ let changed = false;
328
+ const projects = state.projects.map((p) => {
329
+ if (p.id !== projectId || p.expanded === expanded) return p;
330
+ changed = true;
331
+ return { ...p, expanded };
332
+ });
333
+ return changed ? { ...state, projects } : state;
334
}
335
336
export function setError(state: AppState, threadId: ThreadId, error: string | null): AppState {
333
- return {
334
- ...state,
335
- threads: updateThread(state.threads, threadId, (t) => ({ ...t, error })),
336
- };
337
+ const threads = updateThread(state.threads, threadId, (t) => {
338
+ if (t.error === error) return t;
339
+ return { ...t, error };
340
+ });
341
+ return threads === state.threads ? state : { ...state, threads };
342
}
343
344
export function setThreadBranch(
347
branch: string | null,
348
worktreePath: string | null,
349
): AppState {
345
- return {
346
- ...state,
347
- threads: updateThread(state.threads, threadId, (t) => {
348
- const cwdChanged = t.worktreePath !== worktreePath;
349
- return {
350
- ...t,
351
- branch,
352
- worktreePath,
353
- ...(cwdChanged ? { session: null } : {}),
354
- };
355
- }),
356
- };
350
+ const threads = updateThread(state.threads, threadId, (t) => {
351
+ if (t.branch === branch && t.worktreePath === worktreePath) return t;
352
+ const cwdChanged = t.worktreePath !== worktreePath;
353
+ return {
354
+ ...t,
355
+ branch,
356
+ worktreePath,
357
+ ...(cwdChanged ? { session: null } : {}),
358
+ };
359
+ });
360
+ return threads === state.threads ? state : { ...state, threads };
361
}
362
363
export function setRuntimeMode(state: AppState, mode: RuntimeMode): AppState {
364
+ if (state.runtimeMode === mode) return state;
365
return { ...state, runtimeMode: mode };
366
}
367
382
setRuntimeMode: (mode: RuntimeMode) => void;
383
}
384
380
-export const useStore = create<AppStore>((set, _get) => ({
385
+export const useStore = create<AppStore>((set) => ({
386
...readPersistedState(),
387
syncServerReadModel: (readModel) =>
388
set((state) => syncServerReadModel(state, readModel)),