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 23, 08:00 UTC
perf(web): viewed-file tracking passes its flush target instead of reading refs (#20463)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 18:46 UTC
23290944bce8a2ff9f9438cb6d16f152559858d8
2 files changed
+38
-49
apps/web/reactCompiledFiles.ts
+1
@@ -38,4 +38,5 @@ export const REACT_COMPILED_FILES = [
38
"apps/web/src/hooks/useCopyToClipboard.ts",
39
"apps/web/src/components/auth/PairingRouteSurface.tsx",
40
"apps/web/src/components/Sidebar.logic.ts",
41
+ "apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts",
42
] as const;
apps/web/src/components/pullRequest/usePullRequestFilesViewed.ts
+37
-49
@@ -1,10 +1,11 @@
1
import { isAtomCommandInterrupted } from "@t3tools/client-runtime/state/runtime";
2
import type { EnvironmentId, PullRequestRef } from "@t3tools/contracts";
3
-import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+import { useEffect, useEffectEvent, useRef, useState } from "react";
4
5
import { pullRequestEnvironment } from "~/state/pullRequests";
6
import { useEnvironmentQuery } from "~/state/query";
7
import { useAtomCommand } from "~/state/use-atom-command";
8
+import { useStableCallback } from "~/hooks/useStableCallback";
9
10
import { toastManager } from "../ui/toast";
11
import {
@@ -45,7 +46,7 @@ export function usePullRequestFilesViewed(options: {
46
enabled ? pullRequestEnvironment.filesViewed({ environmentId, input: reference }) : null,
47
);
48
const refresh = query.refresh;
48
- const states = useMemo(() => toFileViewedStates(query.data), [query.data]);
49
+ const states = toFileViewedStates(query.data);
50
const truncated = query.data?.truncated === true;
51
const error = query.error;
52
const [overlay, setOverlay] = useState<FileViewedOverlay>(NO_OVERLAY);
@@ -68,10 +69,11 @@ export function usePullRequestFilesViewed(options: {
69
// answer that differs from it is the first read that could have seen the write, which is what
70
// retires the press rather than the host happening to agree with it.
71
const answeredFrom = useRef<Map<string, FileViewedStates | null>>(new Map());
71
- const statesRef = useRef(states);
72
- statesRef.current = states;
72
+ // The committed answer, read when a write completes rather than when it was sent.
73
+ const committedStates = useRef(states);
74
75
useEffect(() => {
76
+ committedStates.current = states;
77
const pending = new Set([...queued.current.keys(), ...sentBy.current.keys()]);
78
const answered = new Set<string>();
79
for (const [path, from] of answeredFrom.current) {
@@ -82,7 +84,11 @@ export function usePullRequestFilesViewed(options: {
84
setOverlay((current) => settleFileViewedOverlay(current, states, pending, answered));
85
}, [states]);
86
85
- const flush = useCallback(() => {
87
+ // Takes the target explicitly so a scope being left still sends its last presses to itself.
88
+ const flush = (target: {
89
+ readonly environmentId: EnvironmentId;
90
+ readonly reference: PullRequestRef;
91
+ }) => {
92
flushTimer.current = null;
93
const batch = toFileViewedBatch(queued.current);
94
if (batch.length === 0) return;
@@ -90,7 +96,10 @@ export function usePullRequestFilesViewed(options: {
96
const sentFrom = scope.current;
97
const request = ++requests.current;
98
for (const file of batch) sentBy.current.set(file.path, request);
93
- void setFilesViewed({ environmentId, input: { ...reference, files: batch } }).then((result) => {
99
+ void setFilesViewed({
100
+ environmentId: target.environmentId,
101
+ input: { ...target.reference, files: batch },
102
+ }).then((result) => {
103
const mine = batch
104
.map((file) => file.path)
105
.filter((path) => sentBy.current.get(path) === request);
@@ -106,26 +115,23 @@ export function usePullRequestFilesViewed(options: {
115
}
116
// Answered for from the next read on, whatever it says. A push landing between the write
117
// and that read comes back as `dismissed`, and the press must not stand over it.
109
- for (const path of mine) answeredFrom.current.set(path, statesRef.current);
118
+ for (const path of mine) answeredFrom.current.set(path, committedStates.current);
119
refresh();
120
});
112
- }, [environmentId, reference, refresh, setFilesViewed]);
121
+ };
122
114
- // Read through a ref rather than closed over: `setViewed` is handed to every file header the
115
- // viewer draws, and a new identity per render would rebuild all of them.
116
- const flushRef = useRef(flush);
117
- flushRef.current = flush;
123
+ const currentTarget = useEffectEvent(() => ({ environmentId, reference }));
124
+ const flushTarget = useEffectEvent(flush);
125
126
// Leaving a change request, the environment it lives on, or the page itself records what was
120
- // pressed and then drops the rest. The flush kept here is the one bound to the scope being
121
- // left, which is what sends those last presses where they were meant to go.
127
+ // pressed and then drops the rest, sending those last presses to the scope being left.
128
useEffect(() => {
123
- const flushScope = flushRef.current;
129
+ const leaving = currentTarget();
130
scope.current = scopeKey;
131
return () => {
132
if (flushTimer.current !== null) {
133
clearTimeout(flushTimer.current);
128
- flushScope();
134
+ flushTarget(leaving);
135
}
136
queued.current = new Map();
137
sentBy.current = new Map();
@@ -134,41 +140,23 @@ export function usePullRequestFilesViewed(options: {
140
};
141
}, [scopeKey]);
142
137
- const refreshRef = useRef(refresh);
138
- refreshRef.current = refresh;
139
- const refreshFromHost = useCallback(() => refreshRef.current(), []);
140
-
141
- const setViewed = useCallback((path: string, viewed: boolean) => {
143
+ // Handed to every file header the viewer draws; a new identity would rebuild all of them.
144
+ const setViewed = useStableCallback((path: string, viewed: boolean) => {
145
setOverlay((current) => new Map(current).set(path, viewed));
146
queued.current.set(path, viewed);
147
if (flushTimer.current !== null) clearTimeout(flushTimer.current);
145
- flushTimer.current = setTimeout(() => flushRef.current(), FLUSH_DELAY_MS);
146
- }, []);
147
-
148
- const isViewed = useCallback(
149
- (path: string) => isFileViewed(path, states, overlay),
150
- [overlay, states],
151
- );
152
- const isStale = useCallback(
153
- (path: string) => !overlay.has(path) && isStaleViewedState(states?.get(path)),
154
- [overlay, states],
155
- );
156
- const viewedCount = useMemo(
157
- () => countViewedFiles(paths, states, overlay),
158
- [overlay, paths, states],
159
- );
148
+ const target = { environmentId, reference };
149
+ flushTimer.current = setTimeout(() => flush(target), FLUSH_DELAY_MS);
150
+ });
151
161
- return useMemo(
162
- () => ({
163
- enabled,
164
- isViewed,
165
- isStale,
166
- setViewed,
167
- viewedCount,
168
- truncated,
169
- error,
170
- refresh: refreshFromHost,
171
- }),
172
- [enabled, error, isStale, isViewed, refreshFromHost, setViewed, truncated, viewedCount],
173
- );
152
+ return {
153
+ enabled,
154
+ isViewed: (path: string) => isFileViewed(path, states, overlay),
155
+ isStale: (path: string) => !overlay.has(path) && isStaleViewedState(states?.get(path)),
156
+ setViewed,
157
+ viewedCount: countViewedFiles(paths, states, overlay),
158
+ truncated,
159
+ error,
160
+ refresh,
161
+ };
162
}