Skip to content

Commit 364b893

Browse files
refactor(web): the guide excerpt keeps its caching without "use memo"
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent af39520 commit 364b893

1 file changed

Lines changed: 141 additions & 109 deletions

File tree

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

Lines changed: 141 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) => {
@@ -137,11 +137,66 @@ export function GuideExcerpt({
137137
commenting: ExcerptCommenting | null;
138138
loadDiffFiles: FileDiffContentsLoader;
139139
onOpenFile: (path: string) => void;
140+
}) {
141+
const slice = excerptSlice(file, excerpt);
142+
143+
return (
144+
<figure className="flex min-w-0 flex-col gap-1.5">
145+
{excerpt.caption.trim() !== "" ? (
146+
<figcaption className="text-xs text-muted-foreground">
147+
<GuideProse text={excerpt.caption} plain />
148+
</figcaption>
149+
) : null}
150+
{slice !== null && file !== null ? (
151+
<ExcerptDiff
152+
excerpt={excerpt}
153+
file={file}
154+
slice={slice}
155+
loadDiffFiles={loadDiffFiles}
156+
notes={notes}
157+
commenting={commenting}
158+
onOpenFile={onOpenFile}
159+
/>
160+
) : loading ? (
161+
<p className="rounded-lg border border-border/60 px-3 py-2 text-xs text-muted-foreground">
162+
Loading {excerpt.path}...
163+
</p>
164+
) : (
165+
<div className="flex items-center justify-between gap-3 rounded-lg border border-border/60 px-3 py-2 text-xs text-muted-foreground">
166+
<span className="min-w-0 truncate">
167+
<span className="font-mono text-foreground">{excerpt.path}</span> lines {excerpt.start}-
168+
{excerpt.end} are not in the loaded diff.
169+
</span>
170+
<Button size="xs" variant="outline" onClick={() => onOpenFile(excerpt.path)}>
171+
Open in Code
172+
</Button>
173+
</div>
174+
)}
175+
</figure>
176+
);
177+
}
178+
179+
// Owns the comment draft so draft edits re-render the diff without re-slicing the patch.
180+
function ExcerptDiff({
181+
excerpt,
182+
file,
183+
slice,
184+
loadDiffFiles,
185+
notes,
186+
commenting,
187+
onOpenFile,
188+
}: {
189+
excerpt: PullRequestReviewGuideExcerpt;
190+
file: FileDiffMetadata;
191+
slice: ExcerptSlice;
192+
loadDiffFiles: FileDiffContentsLoader;
193+
notes: ReadonlyArray<ExcerptNoteMarker>;
194+
commenting: ExcerptCommenting | null;
195+
onOpenFile: (path: string) => void;
140196
}) {
141197
const [draft, setDraft] = useState<ExcerptDraft | null>(null);
142198
const { resolvedTheme } = useTheme();
143-
const slice = useExcerptSlice(file, excerpt);
144-
const loadExcerptFiles = useExcerptLoader(excerpt, slice?.rows ?? null, loadDiffFiles);
199+
const loadExcerptFiles = excerptLoader(excerpt, slice.rows, loadDiffFiles);
145200

146201
const annotations = (() => {
147202
const groups = new Map<string, DiffLineAnnotation<ExcerptAnnotation>>();
@@ -176,7 +231,7 @@ export function GuideExcerpt({
176231
if (comment.path !== excerpt.path) continue;
177232
const anchor = getReviewPositionAnchor(comment.position);
178233
const side = toViewerSide(anchor.side);
179-
if (!slice?.drawn[side].has(anchor.line)) continue;
234+
if (!slice.drawn[side].has(anchor.line)) continue;
180235
update(side, anchor.line, (metadata) => ({
181236
...metadata,
182237
pending: [...metadata.pending, comment],
@@ -190,15 +245,15 @@ export function GuideExcerpt({
190245
})();
191246

192247
const beginComment = (range: SelectedLineRange | null) => {
193-
if (range === null || file === null) return;
248+
if (range === null) return;
194249
const position = resolveDiffReviewPosition(file, range.end, range.endSide ?? range.side);
195250
if (position === null) return;
196251
setDraft({ range, position });
197252
};
198253
const interactive = commenting !== null && draft === null;
199254
const onAskAgent = commenting?.onAskAgent ?? null;
200255
const askAgent = (request: string) => {
201-
if (draft === null || file === null || onAskAgent === null) return;
256+
if (draft === null || onAskAgent === null) return;
202257
const comment = buildDiffReviewComment({
203258
id: `pull-request-guide:${excerpt.path}:${draft.range.start}:${draft.range.end}`,
204259
sectionId: `pull-request-guide:${excerpt.path}`,
@@ -230,107 +285,84 @@ export function GuideExcerpt({
230285
};
231286

232287
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-
/>
288+
<div className="min-w-0 overflow-hidden rounded-lg border border-border/60">
289+
<FileDiff<ExcerptAnnotation>
290+
fileDiff={slice.fileDiff}
291+
options={options}
292+
lineAnnotations={annotations}
293+
selectedLines={draft?.range ?? null}
294+
renderAnnotation={(annotation) => (
295+
<div className="font-sans">
296+
{annotation.metadata.markers.length > 0 ? (
297+
<div className="flex flex-wrap items-center gap-x-3 gap-y-1 px-3 py-1 text-xs">
298+
{annotation.metadata.markers.map((entry) => (
299+
<span key={entry.marker} className="inline-flex items-center gap-1.5">
300+
<GuideNoteMarker marker={entry.marker} />
301+
<span className="text-foreground">{entry.title}</span>
302+
</span>
264303
))}
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}
298304
</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.
305+
) : null}
306+
{annotation.metadata.pending.map((comment) => (
307+
<PendingReviewCommentCard
308+
key={comment.id}
309+
comment={comment}
310+
onRemove={() => commenting?.onRemove(comment.id)}
311+
/>
312+
))}
313+
{annotation.metadata.draft && draft !== null && commenting !== null ? (
314+
<DiffCommentAnnotation
315+
kind="draft"
316+
rangeLabel={`${excerpt.path}:${getReviewPositionAnchor(draft.position).line}`}
317+
text=""
318+
placeholder={
319+
commenting.canComment ? "Add a comment…" : "Ask the agent about these lines…"
320+
}
321+
submitLabel={commenting.canComment ? "Add to review" : "Ask agent"}
322+
{...(commenting.canComment && onAskAgent !== null
323+
? {
324+
secondaryAction: {
325+
label: "Ask agent",
326+
onAction: (text: string) => askAgent(text),
327+
},
328+
}
329+
: {})}
330+
onCancel={() => setDraft(null)}
331+
onComment={(body) => {
332+
if (!commenting.canComment) {
333+
askAgent(body);
334+
return;
335+
}
336+
commenting.onComment({
337+
path: excerpt.path,
338+
oldPath: previousPathOf(file, excerpt.path),
339+
position: draft.position,
340+
body,
341+
});
342+
setDraft(null);
343+
}}
344+
/>
345+
) : null}
346+
</div>
347+
)}
348+
renderHeaderMetadata={() => (
349+
<span className="flex items-center gap-2 text-xs text-muted-foreground">
350+
<span className="tabular-nums">
351+
{excerpt.side === "old" ? "removed code, " : ""}showing {slice.shown} of {slice.total}{" "}
352+
diff lines
353+
</span>
354+
<Button
355+
size="icon-xs"
356+
variant="ghost-muted"
357+
aria-label={`Open ${excerpt.path} in Code`}
358+
onClick={() => onOpenFile(excerpt.path)}
359+
>
360+
<ArrowUpRightIcon aria-hidden />
361+
</Button>
327362
</span>
328-
<Button size="xs" variant="outline" onClick={() => onOpenFile(excerpt.path)}>
329-
Open in Code
330-
</Button>
331-
</div>
332-
)}
333-
</figure>
363+
)}
364+
/>
365+
</div>
334366
);
335367
}
336368

0 commit comments

Comments
 (0)