perf(web): diff panel no longer re-renders every file header each time a patch arrives (#16033)

Co-authored-by: Julius Marminge <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-06 13:56:33 -07:00
1 parent 115640ae49
commit f21d6da51c
3 files changed
+145 -79

No files matched your search

+103 -54
View File
@@ -21,7 +21,7 @@ import {
import { ChevronDown, ChevronRight, ChevronsDownUp, ChevronsUpDown } from "lucide";
import * as Schema from "effect/Schema";
import * as DateTime from "effect/DateTime";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal";
import { useOpenInPreferredEditor } from "../editorPreferences";
import { useFileContextMenuHandler } from "../fileContextMenu";
@@ -118,6 +118,74 @@ interface CollapsedDiffFilesState {
const EMPTY_COLLAPSED_DIFF_FILE_KEYS: ReadonlySet<string> = new Set();
/** Collapse control for one file header; re-renders only when its own file changes. */
function DiffFileCollapseToggle({
filePath,
fileKey,
collapsed,
unavailable,
iconClassName,
onToggle,
}: {
filePath: string;
fileKey: string;
collapsed: boolean;
unavailable: boolean;
iconClassName: string;
onToggle: (fileKey: string) => void;
}) {
return (
<Tooltip>
<TooltipTrigger
render={
<Button
size="icon-micro"
variant="ghost"
className="-ms-0.5"
aria-label={collapsed ? `Expand ${filePath}` : `Collapse ${filePath}`}
aria-expanded={!collapsed}
disabled={unavailable}
onClick={(event) => {
event.stopPropagation();
onToggle(fileKey);
}}
/>
}
>
<MorphIcon
className={cn("size-4", iconClassName)}
icon={collapsed ? ChevronRight : ChevronDown}
/>
</TooltipTrigger>
<TooltipPopup side="top">{collapsed ? "Expand diff" : "Collapse diff"}</TooltipPopup>
</Tooltip>
);
}
/** Copy and status controls for one file header; re-renders only when its own file changes. */
function DiffFileHeaderSuffix({
filePath,
hasStat,
error,
truncated,
onRetry,
}: {
filePath: string;
hasStat: boolean;
error: boolean;
truncated: boolean;
onRetry: (path: string) => void;
}) {
return (
<>
<DiffFilePathCopyButton filePath={filePath} />
{hasStat ? (
<DiffFileStatus error={error} truncated={truncated} retry={() => onRetry(filePath)} />
) : null}
</>
);
}
interface DiffPanelProps {
mode?: DiffPanelMode;
composerDraftTarget: ScopedThreadRef | DraftId;
@@ -598,22 +666,24 @@ export default function DiffPanel({
},
[activeCwd, activeRepositoryRoot, openInPreferredEditor, routeThreadRef],
);
const toggleDiffFileCollapsed = useCallback(
(fileKey: string) => {
setCollapsedDiffFiles((current) => {
const next = new Set(
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
);
if (next.has(fileKey)) {
next.delete(fileKey);
} else {
next.add(fileKey);
}
return { scopeKey: collapseScopeKey, fileKeys: next };
});
},
[collapseScopeKey, defaultCollapsedDiffFileKeys],
);
const collapseDefaultsRef = useRef({ collapseScopeKey, defaultCollapsedDiffFileKeys });
useLayoutEffect(() => {
collapseDefaultsRef.current = { collapseScopeKey, defaultCollapsedDiffFileKeys };
}, [collapseScopeKey, defaultCollapsedDiffFileKeys]);
const toggleDiffFileCollapsed = useCallback((fileKey: string) => {
const { collapseScopeKey, defaultCollapsedDiffFileKeys } = collapseDefaultsRef.current;
setCollapsedDiffFiles((current) => {
const next = new Set(
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
);
if (next.has(fileKey)) {
next.delete(fileKey);
} else {
next.add(fileKey);
}
return { scopeKey: collapseScopeKey, fileKeys: next };
});
}, []);
const toggleDiffFileCollapse = useCallback(() => {
setCodeViewRevision((current) => current + 1);
@@ -1082,14 +1152,15 @@ export default function DiffPanel({
composerDraftTarget={composerDraftTarget}
renderHeaderFilenameSuffix={(fileDiff) => {
const path = resolveFileDiffPath(fileDiff);
const stat = fileStats.get(path);
const state = fileStates.get(path);
return (
<>
<DiffFilePathCopyButton filePath={path} />
{stat ? (
<DiffFileStatus {...fileStates.get(path)} retry={() => retry(path)} />
) : null}
</>
<DiffFileHeaderSuffix
filePath={path}
hasStat={fileStats.has(path)}
error={state?.error ?? false}
truncated={state?.truncated ?? false}
onRetry={retry}
/>
);
}}
{...(lazySource
@@ -1109,37 +1180,15 @@ export default function DiffPanel({
: {})}
renderHeaderPrefix={(fileDiff, fileKey) => {
const unavailable = fileDiff.cacheKey?.endsWith(":pending") === true;
const collapsed = unavailable || collapsedDiffFileKeys.has(fileKey);
const filePath = resolveFileDiffPath(fileDiff);
return (
<Tooltip>
<TooltipTrigger
render={
<Button
size="icon-micro"
variant="ghost"
className="-ms-0.5"
aria-label={
collapsed ? `Expand ${filePath}` : `Collapse ${filePath}`
}
aria-expanded={!collapsed}
disabled={unavailable}
onClick={(event) => {
event.stopPropagation();
toggleDiffFileCollapsed(fileKey);
}}
/>
}
>
<MorphIcon
className={cn("size-4", getDiffCollapseIconClassName(fileDiff))}
icon={collapsed ? ChevronRight : ChevronDown}
/>
</TooltipTrigger>
<TooltipPopup side="top">
{collapsed ? "Expand diff" : "Collapse diff"}
</TooltipPopup>
</Tooltip>
<DiffFileCollapseToggle
filePath={resolveFileDiffPath(fileDiff)}
fileKey={fileKey}
collapsed={unavailable || collapsedDiffFileKeys.has(fileKey)}
unavailable={unavailable}
iconClassName={getDiffCollapseIconClassName(fileDiff)}
onToggle={toggleDiffFileCollapsed}
/>
);
}}
options={{
@@ -11,7 +11,7 @@ import type { ScopedThreadRef } from "@t3tools/contracts";
import { useCallback, useMemo, useState, type ReactNode, type Ref } from "react";
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
import { fnv1a32 } from "~/lib/diffRendering";
import { fnv1a32, resolveFileDiffPath } from "~/lib/diffRendering";
import {
buildDiffReviewComment,
restoreDiffReviewCommentRange,
@@ -217,15 +217,15 @@ export function AnnotatableCodeView({
if (!range) return;
const item = context.item;
if (item.type !== "diff") return;
const file = filesByKey.get(item.id);
if (!file) return;
// Read from the item, not the file list, so this callback keeps its identity as
// patches arrive; the viewer re-applies its options whenever it changes.
const id = nextFileCommentId();
const comment = buildDiffReviewComment({
id,
sectionId,
sectionTitle,
filePath: file.filePath,
fileDiff: file.fileDiff,
filePath: resolveFileDiffPath(item.fileDiff),
fileDiff: item.fileDiff,
range,
text: "",
});
@@ -242,7 +242,7 @@ export function AnnotatableCodeView({
},
});
},
[filesByKey, sectionId, sectionTitle],
[sectionId, sectionTitle],
);
const hasOpenComment = draft !== null;
@@ -3,7 +3,15 @@ import type { FileDiffMetadata } from "@pierre/diffs";
import type { EnvironmentId, ReviewDiffPreviewSource } from "@t3tools/contracts";
import * as AsyncResult from "effect/reactivity/AsyncResult";
import * as Atom from "effect/reactivity/Atom";
import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
import {
useCallback,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import { getRenderablePatch, resolveFileDiffPath, type RenderablePatch } from "~/lib/diffRendering";
import { reviewEnvironment } from "~/state/review";
@@ -146,12 +154,17 @@ export function useReviewFilePatches({
[requestFiles, settledFileCount],
);
const requestFile = useCallback((index: number) => requestFiles([index]), [requestFiles]);
const retryInputsRef = useRef({ queries, files });
useLayoutEffect(() => {
retryInputsRef.current = { queries, files };
}, [queries, files]);
const retry = useCallback(
(path: string) => {
const { queries, files } = retryInputsRef.current;
const query = queries.find(({ index }) => files[index]?.path === path)?.query;
if (query) registry.refresh(query);
},
[queries, files, registry],
[registry],
);
const renderableFiles = useMemo(
() =>
@@ -185,23 +198,27 @@ export function useReviewFilePatches({
),
[source, files, patches, scope, preview],
);
const fileStates = new Map(
files.map((file, index) => {
const patch = patches.get(index);
return [
file.path,
{
error:
patch?._tag === "Failure" ||
(patch?._tag === "Success" &&
(patch.value.patch?.kind !== "files" ||
!patch.value.patch.files.some(
(candidate) => resolveFileDiffPath(candidate) === file.path,
))),
truncated: patch?._tag === "Success" && patch.value.source.truncated,
},
] as const;
}),
const fileStates = useMemo(
() =>
new Map(
files.map((file, index) => {
const patch = patches.get(index);
return [
file.path,
{
error:
patch?._tag === "Failure" ||
(patch?._tag === "Success" &&
(patch.value.patch?.kind !== "files" ||
!patch.value.patch.files.some(
(candidate) => resolveFileDiffPath(candidate) === file.path,
))),
truncated: patch?._tag === "Success" && patch.value.source.truncated,
},
] as const;
}),
),
[files, patches],
);
const readyFilePaths = useMemo(
() =>