The best way to control Claude Code, Codex and more from any computer or phone
perf(web): the guide excerpt compiles without manual memoization (#20553)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 22:15 UTC
b82d2e3bd5ba4bbf5bfb009c1713855dbae82e4b
1 file changed
+73
-71
apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx
+73
-71
@@ -7,7 +7,7 @@ import type {
7
import { FileDiff } from "@pierre/diffs/react";
8
import type { PullRequestReviewGuideExcerpt, PullRequestReviewPosition } from "@t3tools/contracts";
9
import { ArrowUpRightIcon } from "lucide-react";
10
-import { useCallback, useMemo, useState } from "react";
10
+import { useState } from "react";
11
12
import { useTheme } from "~/hooks/useTheme";
13
import {
@@ -75,6 +75,52 @@ interface ExcerptDraft {
75
readonly position: PullRequestReviewPosition;
76
}
77
78
+function useExcerptSlice(file: FileDiffMetadata | null, excerpt: PullRequestReviewGuideExcerpt) {
79
+ "use memo";
80
+ if (file === null) return null;
81
+ const rows = fileDiffRows(file);
82
+ const shown = excerptRows(rows, excerpt);
83
+ if (shown.length === 0) return null;
84
+ const parsed = getRenderablePatch(
85
+ rowsToPatch(excerpt.path, resolveFileDiffPreviousPath(file), shown),
86
+ `pull-request-guide-excerpt:${excerpt.path}:${excerpt.side}:${excerpt.start}-${excerpt.end}`,
87
+ );
88
+ const fileDiff = parsed?.kind === "files" ? parsed.files[0] : undefined;
89
+ if (fileDiff === undefined) return null;
90
+ const drawn = {
91
+ additions: new Set(shown.flatMap((row) => (row.newLine === null ? [] : [row.newLine]))),
92
+ deletions: new Set(shown.flatMap((row) => (row.oldLine === null ? [] : [row.oldLine]))),
93
+ };
94
+ return { fileDiff, rows: shown, shown: shown.length, total: rows.length, drawn };
95
+}
96
+
97
+// The excerpt expands against an old side of its own: the file's real old side differs from
98
+// the new one wherever another hunk changed it, and the renderer needs the two to agree around
99
+// the lines it shows. A slice across hunks has nothing consistent to expand into.
100
+function useExcerptLoader(
101
+ excerpt: PullRequestReviewGuideExcerpt,
102
+ sliceRows: ReturnType<typeof excerptRows> | null,
103
+ loadDiffFiles: FileDiffContentsLoader,
104
+): FileDiffContentsLoader | null {
105
+ "use memo";
106
+ if (sliceRows === null || sliceRows.some((row) => row.hunk !== sliceRows[0]?.hunk)) {
107
+ return null;
108
+ }
109
+ return async (fileDiff) => {
110
+ const loaded = await loadDiffFiles(fileDiff);
111
+ const oldContents = excerptExpansionOldContents(loaded.newFile.contents, sliceRows);
112
+ if (oldContents === null) return loaded;
113
+ return {
114
+ newFile: loaded.newFile,
115
+ oldFile: {
116
+ name: loaded.oldFile?.name ?? loaded.newFile.name,
117
+ contents: oldContents,
118
+ cacheKey: `${loaded.newFile.cacheKey ?? loaded.newFile.name}:excerpt:${excerpt.side}:${excerpt.start}-${excerpt.end}`,
119
+ },
120
+ };
121
+ };
122
+}
123
+
124
export function GuideExcerpt({
125
excerpt,
126
file,
@@ -94,48 +140,10 @@ export function GuideExcerpt({
140
}) {
141
const [draft, setDraft] = useState<ExcerptDraft | null>(null);
142
const { resolvedTheme } = useTheme();
97
- const slice = useMemo(() => {
98
- if (file === null) return null;
99
- const rows = fileDiffRows(file);
100
- const shown = excerptRows(rows, excerpt);
101
- if (shown.length === 0) return null;
102
- const parsed = getRenderablePatch(
103
- rowsToPatch(excerpt.path, resolveFileDiffPreviousPath(file), shown),
104
- `pull-request-guide-excerpt:${excerpt.path}:${excerpt.side}:${excerpt.start}-${excerpt.end}`,
105
- );
106
- const fileDiff = parsed?.kind === "files" ? parsed.files[0] : undefined;
107
- if (fileDiff === undefined) return null;
108
- const drawn = {
109
- additions: new Set(shown.flatMap((row) => (row.newLine === null ? [] : [row.newLine]))),
110
- deletions: new Set(shown.flatMap((row) => (row.oldLine === null ? [] : [row.oldLine]))),
111
- };
112
- return { fileDiff, rows: shown, shown: shown.length, total: rows.length, drawn };
113
- }, [excerpt, file]);
143
+ const slice = useExcerptSlice(file, excerpt);
144
+ const loadExcerptFiles = useExcerptLoader(excerpt, slice?.rows ?? null, loadDiffFiles);
145
115
- // The excerpt expands against an old side of its own: the file's real old side differs from
116
- // the new one wherever another hunk changed it, and the renderer needs the two to agree around
117
- // the lines it shows. A slice across hunks has nothing consistent to expand into.
118
- const sliceRows = slice?.rows ?? null;
119
- const loadExcerptFiles = useMemo<FileDiffContentsLoader | null>(() => {
120
- if (sliceRows === null || sliceRows.some((row) => row.hunk !== sliceRows[0]?.hunk)) {
121
- return null;
122
- }
123
- return async (fileDiff) => {
124
- const loaded = await loadDiffFiles(fileDiff);
125
- const oldContents = excerptExpansionOldContents(loaded.newFile.contents, sliceRows);
126
- if (oldContents === null) return loaded;
127
- return {
128
- newFile: loaded.newFile,
129
- oldFile: {
130
- name: loaded.oldFile?.name ?? loaded.newFile.name,
131
- contents: oldContents,
132
- cacheKey: `${loaded.newFile.cacheKey ?? loaded.newFile.name}:excerpt:${excerpt.side}:${excerpt.start}-${excerpt.end}`,
133
- },
134
- };
135
- };
136
- }, [excerpt.end, excerpt.side, excerpt.start, loadDiffFiles, sliceRows]);
137
-
138
- const annotations = useMemo(() => {
146
+ const annotations = (() => {
147
const groups = new Map<string, DiffLineAnnotation<ExcerptAnnotation>>();
148
const at = (side: "additions" | "deletions", line: number) => {
149
const key = `${side}:${line}`;
@@ -179,17 +187,14 @@ export function GuideExcerpt({
187
update(toViewerSide(anchor.side), anchor.line, (metadata) => ({ ...metadata, draft: true }));
188
}
189
return [...groups.values()];
182
- }, [commenting?.pending, draft, excerpt.path, excerpt.side, notes, slice]);
190
+ })();
191
184
- const beginComment = useCallback(
185
- (range: SelectedLineRange | null) => {
186
- if (range === null || file === null) return;
187
- const position = resolveDiffReviewPosition(file, range.end, range.endSide ?? range.side);
188
- if (position === null) return;
189
- setDraft({ range, position });
190
- },
191
- [file],
192
- );
192
+ const beginComment = (range: SelectedLineRange | null) => {
193
+ if (range === null || file === null) return;
194
+ const position = resolveDiffReviewPosition(file, range.end, range.endSide ?? range.side);
195
+ if (position === null) return;
196
+ setDraft({ range, position });
197
+ };
198
const interactive = commenting !== null && draft === null;
199
const onAskAgent = commenting?.onAskAgent ?? null;
200
const askAgent = (request: string) => {
@@ -207,25 +212,22 @@ export function GuideExcerpt({
212
if (comment !== null) onAskAgent(comment, request);
213
};
214
210
- const options = useMemo(
211
- () => ({
212
- diffStyle: "unified" as const,
213
- lineDiffType: "none" as const,
214
- overflow: "scroll" as const,
215
- theme: resolveDiffThemeName(resolvedTheme),
216
- themeType: resolvedTheme,
217
- preferredHighlighter: PREFERRED_HIGHLIGHTER,
218
- unsafeCSS: EXCERPT_UNSAFE_CSS,
219
- enableGutterUtility: interactive,
220
- enableLineSelection: interactive,
221
- onGutterUtilityClick: beginComment,
222
- onLineSelectionEnd: beginComment,
223
- ...(loadExcerptFiles === null
224
- ? { hunkSeparators: "simple" as const }
225
- : { loadDiffFiles: loadExcerptFiles }),
226
- }),
227
- [resolvedTheme, interactive, beginComment, loadExcerptFiles],
228
- );
215
+ const options = {
216
+ diffStyle: "unified" as const,
217
+ lineDiffType: "none" as const,
218
+ overflow: "scroll" as const,
219
+ theme: resolveDiffThemeName(resolvedTheme),
220
+ themeType: resolvedTheme,
221
+ preferredHighlighter: PREFERRED_HIGHLIGHTER,
222
+ unsafeCSS: EXCERPT_UNSAFE_CSS,
223
+ enableGutterUtility: interactive,
224
+ enableLineSelection: interactive,
225
+ onGutterUtilityClick: beginComment,
226
+ onLineSelectionEnd: beginComment,
227
+ ...(loadExcerptFiles === null
228
+ ? { hunkSeparators: "simple" as const }
229
+ : { loadDiffFiles: loadExcerptFiles }),
230
+ };
231
232
return (
233
<figure className="flex min-w-0 flex-col gap-1.5">