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
Restore lost useMemo calls in ChatView and fix threadActivities referential stability
Revert bare function calls / IIFEs back to useMemo for: modelOptions, workLogEntries, latestTurnHasToolActivity, pendingApprovals, timelineEntries, revertTurnCountByUserMessageId, completionSummary, and completionDividerBeforeEntryId. Add EMPTY_ACTIVITIES module-level constant so the ?? [] fallback doesn't create a new array reference every render, which was defeating memoization. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Julius Marminge committed
Mar 2, 2026 at 13:03 UTC
fd4ff8ca4648e11d945ccf2a8dea65f2037dcddc
1 file changed
+38
-16
apps/web/src/components/ChatView.tsx
+38
-16
@@ -19,6 +19,7 @@ import {
19
type TurnId,
20
normalizeModelSlug,
21
resolveModelSlug,
22
+ OrchestrationThreadActivity,
23
} from "@t3tools/contracts";
24
import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
25
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
@@ -140,6 +141,7 @@ const ATTACHMENT_PREVIEW_HANDOFF_TTL_MS = 5000;
141
const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`;
142
const IMAGE_ONLY_BOOTSTRAP_PROMPT =
143
"[User attached one or more images without additional text. Respond using the conversation context and the attached image(s).]";
144
+const EMPTY_ACTIVITIES: OrchestrationThreadActivity[] = [];
145
const EMPTY_KEYBINDINGS: ResolvedKeybindingsConfig = [];
146
const EMPTY_PROJECT_ENTRIES: ProjectEntry[] = [];
147
const COMPOSER_PATH_QUERY_DEBOUNCE_MS = 120;
@@ -611,22 +613,28 @@ export default function ChatView({ threadId }: ChatViewProps) {
613
);
614
const selectedModel = resolveModelSlug(composerDraft.model ?? baseThreadModel);
615
const selectedEffort = composerDraft.effort ?? DEFAULT_REASONING;
614
- const modelOptions = getAppModelOptions(settings.customCodexModels, selectedModel);
616
+ const modelOptions = useMemo(
617
+ () => getAppModelOptions(settings.customCodexModels, selectedModel),
618
+ [selectedModel, settings.customCodexModels],
619
+ );
620
const phase = derivePhase(activeThread?.session ?? null);
621
const isSendBusy = sendPhase !== "idle";
622
const isPreparingWorktree = sendPhase === "preparing-worktree";
623
const isWorking = phase === "running" || isSendBusy || isConnecting || isRevertingCheckpoint;
624
const nowIso = new Date(nowTick).toISOString();
620
- const threadActivities = activeThread?.activities ?? [];
621
- const workLogEntries = deriveWorkLogEntries(
622
- threadActivities,
623
- activeLatestTurn?.turnId ?? undefined,
625
+ const threadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES;
626
+ const workLogEntries = useMemo(
627
+ () => deriveWorkLogEntries(threadActivities, activeLatestTurn?.turnId ?? undefined),
628
+ [activeLatestTurn?.turnId, threadActivities],
629
+ );
630
+ const latestTurnHasToolActivity = useMemo(
631
+ () => hasToolActivityForTurn(threadActivities, activeLatestTurn?.turnId),
632
+ [activeLatestTurn?.turnId, threadActivities],
633
);
625
- const latestTurnHasToolActivity = hasToolActivityForTurn(
626
- threadActivities,
627
- activeLatestTurn?.turnId,
634
+ const pendingApprovals = useMemo(
635
+ () => derivePendingApprovals(threadActivities),
636
+ [threadActivities],
637
);
629
- const pendingApprovals = derivePendingApprovals(threadActivities);
638
useEffect(() => {
639
attachmentPreviewHandoffByMessageIdRef.current = attachmentPreviewHandoffByMessageId;
640
}, [attachmentPreviewHandoffByMessageId]);
@@ -744,7 +752,10 @@ export default function ChatView({ threadId }: ChatViewProps) {
752
}
753
return [...serverMessagesWithPreviewHandoff, ...pendingMessages];
754
}, [serverMessages, attachmentPreviewHandoffByMessageId, optimisticUserMessages]);
747
- const timelineEntries = deriveTimelineEntries(timelineMessages, workLogEntries);
755
+ const timelineEntries = useMemo(
756
+ () => deriveTimelineEntries(timelineMessages, workLogEntries),
757
+ [timelineMessages, workLogEntries],
758
+ );
759
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
760
useTurnDiffSummaries(activeThread);
761
const turnDiffSummaryByAssistantMessageId = useMemo(() => {
@@ -755,7 +766,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
766
}
767
return byMessageId;
768
}, [turnDiffSummaries]);
758
- const revertTurnCountByUserMessageId = (() => {
769
+ const revertTurnCountByUserMessageId = useMemo(() => {
770
const byUserMessageId = new Map<MessageId, number>();
771
for (let index = 0; index < timelineEntries.length; index += 1) {
772
const entry = timelineEntries[index];
@@ -786,9 +797,9 @@ export default function ChatView({ threadId }: ChatViewProps) {
797
}
798
799
return byUserMessageId;
789
- })();
800
+ }, [inferredCheckpointTurnCountByTurnId, timelineEntries, turnDiffSummaryByAssistantMessageId]);
801
791
- const completionSummary = (() => {
802
+ const completionSummary = useMemo(() => {
803
if (!latestTurnSettled) return null;
804
if (!activeLatestTurn?.startedAt) return null;
805
if (!activeLatestTurn.completedAt) return null;
@@ -796,8 +807,13 @@ export default function ChatView({ threadId }: ChatViewProps) {
807
808
const elapsed = formatElapsed(activeLatestTurn.startedAt, activeLatestTurn.completedAt);
809
return elapsed ? `Worked for ${elapsed}` : null;
799
- })();
800
- const completionDividerBeforeEntryId = (() => {
810
+ }, [
811
+ activeLatestTurn?.completedAt,
812
+ activeLatestTurn?.startedAt,
813
+ latestTurnHasToolActivity,
814
+ latestTurnSettled,
815
+ ]);
816
+ const completionDividerBeforeEntryId = useMemo(() => {
817
if (!latestTurnSettled) return null;
818
if (!activeLatestTurn?.startedAt) return null;
819
if (!activeLatestTurn.completedAt) return null;
@@ -821,7 +837,13 @@ export default function ChatView({ threadId }: ChatViewProps) {
837
}
838
}
839
return inRangeMatch ?? fallbackMatch;
824
- })();
840
+ }, [
841
+ activeLatestTurn?.completedAt,
842
+ activeLatestTurn?.startedAt,
843
+ completionSummary,
844
+ latestTurnSettled,
845
+ timelineEntries,
846
+ ]);
847
const gitCwd = activeThread?.worktreePath ?? activeProject?.cwd ?? null;
848
const composerTrigger = useMemo(
849
() => detectComposerTrigger(prompt, composerCursor),