Skip to content

Commit 0ce9ead

Browse files
perf(web): the annotatable diff view compiles without manual memoization
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 70d60e0 commit 0ce9ead

2 files changed

Lines changed: 94 additions & 107 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ export const REACT_COMPILED_FILES = [
9696
"apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx",
9797
"apps/web/src/components/ThreadTerminalDrawer.tsx",
9898
"apps/web/src/components/settings/SettingsPanels.tsx",
99+
"apps/web/src/components/diffs/AnnotatableCodeView.tsx",
99100
] as const;

‎apps/web/src/components/diffs/AnnotatableCodeView.tsx‎

Lines changed: 93 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
} from "@pierre/diffs";
99
import type { CodeViewHandle } from "@pierre/diffs/react";
1010
import type { ScopedThreadRef } from "@t3tools/contracts";
11-
import { useCallback, useMemo, useState, type ReactNode, type Ref } from "react";
11+
import { useState, type ReactNode, type Ref } from "react";
1212

1313
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
1414
import { fnv1a32, resolveFileDiffPath } from "~/lib/diffRendering";
@@ -133,119 +133,105 @@ export function AnnotatableCodeView({
133133
} | null>(null);
134134
const [draftText, setDraftText] = useState("");
135135

136-
const filesByKey = useMemo(() => new Map(files.map((file) => [file.fileKey, file])), [files]);
137-
const items = useMemo<CodeViewDiffItem<DiffCommentAnnotationGroup>[]>(
138-
() =>
139-
files.map(({ fileDiff, filePath, fileKey, fileVersion, collapsed }) => {
140-
const persisted = reviewComments
141-
.filter(
142-
(comment) =>
143-
comment.sectionId === sectionId &&
144-
comment.filePath === filePath &&
145-
(comment.fenceLanguage ?? "diff") === "diff",
146-
)
147-
.reduce<DiffCommentLineAnnotation[]>((annotations, comment) => {
148-
const range = restoreDiffReviewCommentRange(fileDiff, comment);
149-
if (!range) return annotations;
150-
return appendAnnotationEntry(annotations, range, {
151-
id: comment.id,
152-
kind: "comment",
153-
range,
154-
rangeLabel: comment.rangeLabel,
155-
text: comment.text,
156-
});
157-
}, []);
158-
const annotations =
159-
draft?.fileKey === fileKey ? [...persisted, draft.annotation] : persisted;
160-
return {
161-
id: fileKey,
162-
type: "diff",
163-
fileDiff,
164-
annotations,
165-
collapsed,
166-
version: fnv1a32(
167-
`${fileVersion}:${collapsed ? "1" : "0"}:${annotations
168-
.flatMap((annotation) =>
169-
annotation.metadata.entries.map(
170-
(entry) => `${entry.id}:${entry.rangeLabel}:${entry.text}`,
171-
),
172-
)
173-
.join(":")}`,
174-
),
175-
};
176-
}),
177-
[draft, files, reviewComments, sectionId],
178-
);
179-
180-
const removeEntry = useCallback(
181-
(entryId: string) => {
182-
setSelectedLines(null);
183-
if (draft?.annotation.metadata.entries.some((entry) => entry.id === entryId)) {
184-
setDraft(null);
185-
setDraftText("");
186-
} else {
187-
removeReviewComment(composerDraftTarget, entryId);
188-
}
136+
const filesByKey = new Map(files.map((file) => [file.fileKey, file]));
137+
const items: CodeViewDiffItem<DiffCommentAnnotationGroup>[] = files.map(
138+
({ fileDiff, filePath, fileKey, fileVersion, collapsed }) => {
139+
const persisted = reviewComments
140+
.filter(
141+
(comment) =>
142+
comment.sectionId === sectionId &&
143+
comment.filePath === filePath &&
144+
(comment.fenceLanguage ?? "diff") === "diff",
145+
)
146+
.reduce<DiffCommentLineAnnotation[]>((annotations, comment) => {
147+
const range = restoreDiffReviewCommentRange(fileDiff, comment);
148+
if (!range) return annotations;
149+
return appendAnnotationEntry(annotations, range, {
150+
id: comment.id,
151+
kind: "comment",
152+
range,
153+
rangeLabel: comment.rangeLabel,
154+
text: comment.text,
155+
});
156+
}, []);
157+
const annotations = draft?.fileKey === fileKey ? [...persisted, draft.annotation] : persisted;
158+
return {
159+
id: fileKey,
160+
type: "diff",
161+
fileDiff,
162+
annotations,
163+
collapsed,
164+
version: fnv1a32(
165+
`${fileVersion}:${collapsed ? "1" : "0"}:${annotations
166+
.flatMap((annotation) =>
167+
annotation.metadata.entries.map(
168+
(entry) => `${entry.id}:${entry.rangeLabel}:${entry.text}`,
169+
),
170+
)
171+
.join(":")}`,
172+
),
173+
};
189174
},
190-
[composerDraftTarget, draft, removeReviewComment],
191175
);
192176

193-
const submitEntry = useCallback(
194-
(entryId: string, text: string) => {
195-
const entry = draft?.annotation.metadata.entries.find(
196-
(candidate) => candidate.id === entryId,
197-
);
198-
const file = draft ? filesByKey.get(draft.fileKey) : undefined;
199-
if (!entry || !file) return;
200-
const comment = buildDiffReviewComment({
201-
id: entry.id,
202-
sectionId,
203-
sectionTitle,
204-
filePath: file.filePath,
205-
fileDiff: file.fileDiff,
206-
range: entry.range,
207-
text,
208-
});
209-
if (comment) addReviewComment(composerDraftTarget, comment);
210-
setSelectedLines(null);
177+
const removeEntry = (entryId: string) => {
178+
setSelectedLines(null);
179+
if (draft?.annotation.metadata.entries.some((entry) => entry.id === entryId)) {
211180
setDraft(null);
212181
setDraftText("");
213-
},
214-
[addReviewComment, composerDraftTarget, draft, filesByKey, sectionId, sectionTitle],
215-
);
182+
} else {
183+
removeReviewComment(composerDraftTarget, entryId);
184+
}
185+
};
216186

217-
const beginComment = useCallback(
218-
(range: SelectedLineRange | null, context: DiffSelectionContext) => {
219-
if (!range) return;
220-
const item = context.item;
221-
if (item.type !== "diff") return;
222-
// Read from the item, not the file list, so this callback keeps its identity as
223-
// patches arrive; the viewer re-applies its options whenever it changes.
224-
const id = nextFileCommentId();
225-
const comment = buildDiffReviewComment({
226-
id,
227-
sectionId,
228-
sectionTitle,
229-
filePath: resolveFileDiffPath(item.fileDiff),
230-
fileDiff: item.fileDiff,
231-
range,
232-
text: "",
233-
});
234-
if (!comment) return;
235-
setDraftText("");
236-
setDraft({
237-
fileKey: item.id,
238-
annotation: {
239-
side: annotationSide(range),
240-
lineNumber: range.end,
241-
metadata: {
242-
entries: [{ id, kind: "draft", range, rangeLabel: comment.rangeLabel, text: "" }],
243-
},
187+
const submitEntry = (entryId: string, text: string) => {
188+
const entry = draft?.annotation.metadata.entries.find((candidate) => candidate.id === entryId);
189+
const file = draft ? filesByKey.get(draft.fileKey) : undefined;
190+
if (!entry || !file) return;
191+
const comment = buildDiffReviewComment({
192+
id: entry.id,
193+
sectionId,
194+
sectionTitle,
195+
filePath: file.filePath,
196+
fileDiff: file.fileDiff,
197+
range: entry.range,
198+
text,
199+
});
200+
if (comment) addReviewComment(composerDraftTarget, comment);
201+
setSelectedLines(null);
202+
setDraft(null);
203+
setDraftText("");
204+
};
205+
206+
const beginComment = (range: SelectedLineRange | null, context: DiffSelectionContext) => {
207+
if (!range) return;
208+
const item = context.item;
209+
if (item.type !== "diff") return;
210+
// Read from the item, not the file list, so the compiled callback keeps its identity
211+
// as patches arrive; the viewer re-applies its options whenever it changes.
212+
const id = nextFileCommentId();
213+
const comment = buildDiffReviewComment({
214+
id,
215+
sectionId,
216+
sectionTitle,
217+
filePath: resolveFileDiffPath(item.fileDiff),
218+
fileDiff: item.fileDiff,
219+
range,
220+
text: "",
221+
});
222+
if (!comment) return;
223+
setDraftText("");
224+
setDraft({
225+
fileKey: item.id,
226+
annotation: {
227+
side: annotationSide(range),
228+
lineNumber: range.end,
229+
metadata: {
230+
entries: [{ id, kind: "draft", range, rangeLabel: comment.rangeLabel, text: "" }],
244231
},
245-
});
246-
},
247-
[sectionId, sectionTitle],
248-
);
232+
},
233+
});
234+
};
249235

250236
const hasOpenComment = draft !== null;
251237
return (

0 commit comments

Comments
 (0)