Skip to content

Commit efdaa4b

Browse files
refactor(web): the guide excerpt keeps its caching without "use memo" (#20798)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 767da3b commit efdaa4b

1 file changed

Lines changed: 148 additions & 109 deletions

File tree

‎apps/web/src/components/pullRequest/guide/GuideExcerpt.tsx‎

Lines changed: 148 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ interface ExcerptDraft {
7575
readonly position: PullRequestReviewPosition;
7676
}
7777

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) {
8081
if (file === null) return null;
8182
const rows = fileDiffRows(file);
8283
const shown = excerptRows(rows, excerpt);
@@ -97,13 +98,12 @@ function useExcerptSlice(file: FileDiffMetadata | null, excerpt: PullRequestRevi
9798
// The excerpt expands against an old side of its own: the file's real old side differs from
9899
// the new one wherever another hunk changed it, and the renderer needs the two to agree around
99100
// the lines it shows. A slice across hunks has nothing consistent to expand into.
100-
function useExcerptLoader(
101+
function excerptLoader(
101102
excerpt: PullRequestReviewGuideExcerpt,
102-
sliceRows: ReturnType<typeof excerptRows> | null,
103+
sliceRows: ExcerptSlice["rows"],
103104
loadDiffFiles: FileDiffContentsLoader,
104105
): 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)) {
107107
return null;
108108
}
109109
return async (fileDiff) => {
@@ -138,10 +138,72 @@ export function GuideExcerpt({
138138
loadDiffFiles: FileDiffContentsLoader;
139139
onOpenFile: (path: string) => void;
140140
}) {
141+
// Kept here, not in ExcerptDiff, so a selection survives the diff reloading for a new head.
141142
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+
}) {
142205
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);
145207

146208
const annotations = (() => {
147209
const groups = new Map<string, DiffLineAnnotation<ExcerptAnnotation>>();
@@ -176,7 +238,7 @@ export function GuideExcerpt({
176238
if (comment.path !== excerpt.path) continue;
177239
const anchor = getReviewPositionAnchor(comment.position);
178240
const side = toViewerSide(anchor.side);
179-
if (!slice?.drawn[side].has(anchor.line)) continue;
241+
if (!slice.drawn[side].has(anchor.line)) continue;
180242
update(side, anchor.line, (metadata) => ({
181243
...metadata,
182244
pending: [...metadata.pending, comment],
@@ -190,15 +252,15 @@ export function GuideExcerpt({
190252
})();
191253

192254
const beginComment = (range: SelectedLineRange | null) => {
193-
if (range === null || file === null) return;
255+
if (range === null) return;
194256
const position = resolveDiffReviewPosition(file, range.end, range.endSide ?? range.side);
195257
if (position === null) return;
196258
setDraft({ range, position });
197259
};
198260
const interactive = commenting !== null && draft === null;
199261
const onAskAgent = commenting?.onAskAgent ?? null;
200262
const askAgent = (request: string) => {
201-
if (draft === null || file === null || onAskAgent === null) return;
263+
if (draft === null || onAskAgent === null) return;
202264
const comment = buildDiffReviewComment({
203265
id: `pull-request-guide:${excerpt.path}:${draft.range.start}:${draft.range.end}`,
204266
sectionId: `pull-request-guide:${excerpt.path}`,
@@ -230,107 +292,84 @@ export function GuideExcerpt({
230292
};
231293

232294
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>
264310
))}
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}
298311
</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>
327369
</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>
334373
);
335374
}
336375

0 commit comments

Comments
 (0)