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, 05:30 UTC
refactor(web): the guide excerpt keeps its caching without "use memo" (#20798)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 17, 2026 at 00:41 UTC
efdaa4b3536aab52efe6dbb4a2a1f53628b46400
1 file changed
+148
-109
apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx
+148
-109
@@ -75,8 +75,9 @@ interface ExcerptDraft {
75
readonly position: PullRequestReviewPosition;
76
}
77
78
-function useExcerptSlice(file: FileDiffMetadata | null, excerpt: PullRequestReviewGuideExcerpt) {
79
- "use memo";
78
+type ExcerptSlice = NonNullable<ReturnType<typeof excerptSlice>>;
79
+
80
+function excerptSlice(file: FileDiffMetadata | null, excerpt: PullRequestReviewGuideExcerpt) {
81
if (file === null) return null;
82
const rows = fileDiffRows(file);
83
const shown = excerptRows(rows, excerpt);
@@ -97,13 +98,12 @@ function useExcerptSlice(file: FileDiffMetadata | null, excerpt: PullRequestRevi
98
// The excerpt expands against an old side of its own: the file's real old side differs from
99
// the new one wherever another hunk changed it, and the renderer needs the two to agree around
100
// the lines it shows. A slice across hunks has nothing consistent to expand into.
100
-function useExcerptLoader(
101
+function excerptLoader(
102
excerpt: PullRequestReviewGuideExcerpt,
102
- sliceRows: ReturnType<typeof excerptRows> | null,
103
+ sliceRows: ExcerptSlice["rows"],
104
loadDiffFiles: FileDiffContentsLoader,
105
): FileDiffContentsLoader | null {
105
- "use memo";
106
- if (sliceRows === null || sliceRows.some((row) => row.hunk !== sliceRows[0]?.hunk)) {
106
+ if (sliceRows.some((row) => row.hunk !== sliceRows[0]?.hunk)) {
107
return null;
108
}
109
return async (fileDiff) => {
@@ -138,10 +138,72 @@ export function GuideExcerpt({
138
loadDiffFiles: FileDiffContentsLoader;
139
onOpenFile: (path: string) => void;
140
}) {
141
+ // Kept here, not in ExcerptDiff, so a selection survives the diff reloading for a new head.
142
const [draft, setDraft] = useState<ExcerptDraft | null>(null);
143
+ const slice = excerptSlice(file, excerpt);
144
+
145
+ return (
146
+ <figure className="flex min-w-0 flex-col gap-1.5">
147
+ {excerpt.caption.trim() !== "" ? (
148
+ <figcaption className="text-xs text-muted-foreground">
149
+ <GuideProse text={excerpt.caption} plain />
150
+ </figcaption>
151
+ ) : null}
152
+ {slice !== null && file !== null ? (
153
+ <ExcerptDiff
154
+ excerpt={excerpt}
155
+ file={file}
156
+ slice={slice}
157
+ draft={draft}
158
+ setDraft={setDraft}
159
+ loadDiffFiles={loadDiffFiles}
160
+ notes={notes}
161
+ commenting={commenting}
162
+ onOpenFile={onOpenFile}
163
+ />
164
+ ) : loading ? (
165
+ <p className="rounded-lg border border-border/60 px-3 py-2 text-xs text-muted-foreground">
166
+ Loading {excerpt.path}...
167
+ </p>
168
+ ) : (
169
+ <div className="flex items-center justify-between gap-3 rounded-lg border border-border/60 px-3 py-2 text-xs text-muted-foreground">
170
+ <span className="min-w-0 truncate">
171
+ <span className="font-mono text-foreground">{excerpt.path}</span> lines {excerpt.start}-
172
+ {excerpt.end} are not in the loaded diff.
173
+ </span>
174
+ <Button size="xs" variant="outline" onClick={() => onOpenFile(excerpt.path)}>
175
+ Open in Code
176
+ </Button>
177
+ </div>
178
+ )}
179
+ </figure>
180
+ );
181
+}
182
+
183
+// Split out so draft changes re-render the diff without re-slicing the patch.
184
+function ExcerptDiff({
185
+ excerpt,
186
+ file,
187
+ slice,
188
+ draft,
189
+ setDraft,
190
+ loadDiffFiles,
191
+ notes,
192
+ commenting,
193
+ onOpenFile,
194
+}: {
195
+ excerpt: PullRequestReviewGuideExcerpt;
196
+ file: FileDiffMetadata;
197
+ slice: ExcerptSlice;
198
+ draft: ExcerptDraft | null;
199
+ setDraft: (draft: ExcerptDraft | null) => void;
200
+ loadDiffFiles: FileDiffContentsLoader;
201
+ notes: ReadonlyArray<ExcerptNoteMarker>;
202
+ commenting: ExcerptCommenting | null;
203
+ onOpenFile: (path: string) => void;
204
+}) {
205
const { resolvedTheme } = useTheme();
143
- const slice = useExcerptSlice(file, excerpt);
144
- const loadExcerptFiles = useExcerptLoader(excerpt, slice?.rows ?? null, loadDiffFiles);
206
+ const loadExcerptFiles = excerptLoader(excerpt, slice.rows, loadDiffFiles);
207
208
const annotations = (() => {
209
const groups = new Map<string, DiffLineAnnotation<ExcerptAnnotation>>();
@@ -176,7 +238,7 @@ export function GuideExcerpt({
238
if (comment.path !== excerpt.path) continue;
239
const anchor = getReviewPositionAnchor(comment.position);
240
const side = toViewerSide(anchor.side);
179
- if (!slice?.drawn[side].has(anchor.line)) continue;
241
+ if (!slice.drawn[side].has(anchor.line)) continue;
242
update(side, anchor.line, (metadata) => ({
243
...metadata,
244
pending: [...metadata.pending, comment],
@@ -190,7 +252,7 @@ export function GuideExcerpt({
252
})();
253
254
const beginComment = (range: SelectedLineRange | null) => {
193
- if (range === null || file === null) return;
255
+ if (range === null) return;
256
const position = resolveDiffReviewPosition(file, range.end, range.endSide ?? range.side);
257
if (position === null) return;
258
setDraft({ range, position });
@@ -198,7 +260,7 @@ export function GuideExcerpt({
260
const interactive = commenting !== null && draft === null;
261
const onAskAgent = commenting?.onAskAgent ?? null;
262
const askAgent = (request: string) => {
201
- if (draft === null || file === null || onAskAgent === null) return;
263
+ if (draft === null || onAskAgent === null) return;
264
const comment = buildDiffReviewComment({
265
id: `pull-request-guide:${excerpt.path}:${draft.range.start}:${draft.range.end}`,
266
sectionId: `pull-request-guide:${excerpt.path}`,
@@ -230,107 +292,84 @@ export function GuideExcerpt({
292
};
293
294
return (
233
- <figure className="flex min-w-0 flex-col gap-1.5">
234
- {excerpt.caption.trim() !== "" ? (
235
- <figcaption className="text-xs text-muted-foreground">
236
- <GuideProse text={excerpt.caption} plain />
237
- </figcaption>
238
- ) : null}
239
- {slice !== null ? (
240
- <div className="min-w-0 overflow-hidden rounded-lg border border-border/60">
241
- <FileDiff<ExcerptAnnotation>
242
- fileDiff={slice.fileDiff}
243
- options={options}
244
- lineAnnotations={annotations}
245
- selectedLines={draft?.range ?? null}
246
- renderAnnotation={(annotation) => (
247
- <div className="font-sans">
248
- {annotation.metadata.markers.length > 0 ? (
249
- <div className="flex flex-wrap items-center gap-x-3 gap-y-1 px-3 py-1 text-xs">
250
- {annotation.metadata.markers.map((entry) => (
251
- <span key={entry.marker} className="inline-flex items-center gap-1.5">
252
- <GuideNoteMarker marker={entry.marker} />
253
- <span className="text-foreground">{entry.title}</span>
254
- </span>
255
- ))}
256
- </div>
257
- ) : null}
258
- {annotation.metadata.pending.map((comment) => (
259
- <PendingReviewCommentCard
260
- key={comment.id}
261
- comment={comment}
262
- onRemove={() => commenting?.onRemove(comment.id)}
263
- />
295
+ <div className="min-w-0 overflow-hidden rounded-lg border border-border/60">
296
+ <FileDiff<ExcerptAnnotation>
297
+ fileDiff={slice.fileDiff}
298
+ options={options}
299
+ lineAnnotations={annotations}
300
+ selectedLines={draft?.range ?? null}
301
+ renderAnnotation={(annotation) => (
302
+ <div className="font-sans">
303
+ {annotation.metadata.markers.length > 0 ? (
304
+ <div className="flex flex-wrap items-center gap-x-3 gap-y-1 px-3 py-1 text-xs">
305
+ {annotation.metadata.markers.map((entry) => (
306
+ <span key={entry.marker} className="inline-flex items-center gap-1.5">
307
+ <GuideNoteMarker marker={entry.marker} />
308
+ <span className="text-foreground">{entry.title}</span>
309
+ </span>
310
))}
265
- {annotation.metadata.draft && draft !== null && commenting !== null ? (
266
- <DiffCommentAnnotation
267
- kind="draft"
268
- rangeLabel={`${excerpt.path}:${getReviewPositionAnchor(draft.position).line}`}
269
- text=""
270
- placeholder={
271
- commenting.canComment ? "Add a comment…" : "Ask the agent about these lines…"
272
- }
273
- submitLabel={commenting.canComment ? "Add to review" : "Ask agent"}
274
- {...(commenting.canComment && onAskAgent !== null
275
- ? {
276
- secondaryAction: {
277
- label: "Ask agent",
278
- onAction: (text: string) => askAgent(text),
279
- },
280
- }
281
- : {})}
282
- onCancel={() => setDraft(null)}
283
- onComment={(body) => {
284
- if (!commenting.canComment) {
285
- askAgent(body);
286
- return;
287
- }
288
- commenting.onComment({
289
- path: excerpt.path,
290
- oldPath: file === null ? null : previousPathOf(file, excerpt.path),
291
- position: draft.position,
292
- body,
293
- });
294
- setDraft(null);
295
- }}
296
- />
297
- ) : null}
311
</div>
299
- )}
300
- renderHeaderMetadata={() => (
301
- <span className="flex items-center gap-2 text-xs text-muted-foreground">
302
- <span className="tabular-nums">
303
- {excerpt.side === "old" ? "removed code, " : ""}showing {slice.shown} of{" "}
304
- {slice.total} diff lines
305
- </span>
306
- <Button
307
- size="icon-xs"
308
- variant="ghost-muted"
309
- aria-label={`Open ${excerpt.path} in Code`}
310
- onClick={() => onOpenFile(excerpt.path)}
311
- >
312
- <ArrowUpRightIcon aria-hidden />
313
- </Button>
314
- </span>
315
- )}
316
- />
317
- </div>
318
- ) : loading ? (
319
- <p className="rounded-lg border border-border/60 px-3 py-2 text-xs text-muted-foreground">
320
- Loading {excerpt.path}...
321
- </p>
322
- ) : (
323
- <div className="flex items-center justify-between gap-3 rounded-lg border border-border/60 px-3 py-2 text-xs text-muted-foreground">
324
- <span className="min-w-0 truncate">
325
- <span className="font-mono text-foreground">{excerpt.path}</span> lines {excerpt.start}-
326
- {excerpt.end} are not in the loaded diff.
312
+ ) : null}
313
+ {annotation.metadata.pending.map((comment) => (
314
+ <PendingReviewCommentCard
315
+ key={comment.id}
316
+ comment={comment}
317
+ onRemove={() => commenting?.onRemove(comment.id)}
318
+ />
319
+ ))}
320
+ {annotation.metadata.draft && draft !== null && commenting !== null ? (
321
+ <DiffCommentAnnotation
322
+ kind="draft"
323
+ rangeLabel={`${excerpt.path}:${getReviewPositionAnchor(draft.position).line}`}
324
+ text=""
325
+ placeholder={
326
+ commenting.canComment ? "Add a comment…" : "Ask the agent about these lines…"
327
+ }
328
+ submitLabel={commenting.canComment ? "Add to review" : "Ask agent"}
329
+ {...(commenting.canComment && onAskAgent !== null
330
+ ? {
331
+ secondaryAction: {
332
+ label: "Ask agent",
333
+ onAction: (text: string) => askAgent(text),
334
+ },
335
+ }
336
+ : {})}
337
+ onCancel={() => setDraft(null)}
338
+ onComment={(body) => {
339
+ if (!commenting.canComment) {
340
+ askAgent(body);
341
+ return;
342
+ }
343
+ commenting.onComment({
344
+ path: excerpt.path,
345
+ oldPath: previousPathOf(file, excerpt.path),
346
+ position: draft.position,
347
+ body,
348
+ });
349
+ setDraft(null);
350
+ }}
351
+ />
352
+ ) : null}
353
+ </div>
354
+ )}
355
+ renderHeaderMetadata={() => (
356
+ <span className="flex items-center gap-2 text-xs text-muted-foreground">
357
+ <span className="tabular-nums">
358
+ {excerpt.side === "old" ? "removed code, " : ""}showing {slice.shown} of {slice.total}{" "}
359
+ diff lines
360
+ </span>
361
+ <Button
362
+ size="icon-xs"
363
+ variant="ghost-muted"
364
+ aria-label={`Open ${excerpt.path} in Code`}
365
+ onClick={() => onOpenFile(excerpt.path)}
366
+ >
367
+ <ArrowUpRightIcon aria-hidden />
368
+ </Button>
369
</span>
328
- <Button size="xs" variant="outline" onClick={() => onOpenFile(excerpt.path)}>
329
- Open in Code
330
- </Button>
331
- </div>
332
- )}
333
- </figure>
370
+ )}
371
+ />
372
+ </div>
373
);
374
}
375