Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DiffPanel.tsx

659 lines25.1 KB

1import { parsePatchFiles } from "@pierre/diffs";
2import { FileDiff, type FileDiffMetadata, Virtualizer } from "@pierre/diffs/react";
3import { useQuery } from "@tanstack/react-query";
4import { useNavigate, useParams, useSearch } from "@tanstack/react-router";
5import { scopeThreadRef } from "@t3tools/client-runtime";
6import type { TurnId } from "@t3tools/contracts";
7import {
8 ChevronLeftIcon,
9 ChevronRightIcon,
10 Columns2Icon,
11 Rows3Icon,
12 TextWrapIcon,
13} from "lucide-react";
14import {
15 type WheelEvent as ReactWheelEvent,
16 useCallback,
17 useEffect,
18 useMemo,
19 useRef,
20 useState,
21} from "react";
22import { openInPreferredEditor } from "../editorPreferences";
23import { useGitStatus } from "~/lib/gitStatusState";
24import { checkpointDiffQueryOptions } from "~/lib/providerReactQuery";
25import { cn } from "~/lib/utils";
26import { readLocalApi } from "../localApi";
27import { resolvePathLinkTarget } from "../terminal-links";
28import { parseDiffRouteSearch, stripDiffSearchParams } from "../diffRouteSearch";
29import { useTheme } from "../hooks/useTheme";
30import { buildPatchCacheKey } from "../lib/diffRendering";
31import { resolveDiffThemeName } from "../lib/diffRendering";
32import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
33import { selectProjectByRef, useStore } from "../store";
34import { createThreadSelectorByRef } from "../storeSelectors";
35import { buildThreadRouteParams, resolveThreadRouteRef } from "../threadRoutes";
36import { useSettings } from "../hooks/useSettings";
37import { formatShortTimestamp } from "../timestampFormat";
38import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
39import { ToggleGroup, Toggle } from "./ui/toggle-group";
40​
41type DiffRenderMode = "stacked" | "split";
42type DiffThemeType = "light" | "dark";
43​
44const DIFF_PANEL_UNSAFE_CSS = `
45[data-diffs-header],
46[data-diff],
47[data-file],
48[data-error-wrapper],
49[data-virtualizer-buffer] {
50 --diffs-bg: color-mix(in srgb, var(--card) 90%, var(--background)) !important;
51 --diffs-light-bg: color-mix(in srgb, var(--card) 90%, var(--background)) !important;
52 --diffs-dark-bg: color-mix(in srgb, var(--card) 90%, var(--background)) !important;
53 --diffs-token-light-bg: transparent;
54 --diffs-token-dark-bg: transparent;
55​
56 --diffs-bg-context-override: color-mix(in srgb, var(--background) 97%, var(--foreground));
57 --diffs-bg-hover-override: color-mix(in srgb, var(--background) 94%, var(--foreground));
58 --diffs-bg-separator-override: color-mix(in srgb, var(--background) 95%, var(--foreground));
59 --diffs-bg-buffer-override: color-mix(in srgb, var(--background) 90%, var(--foreground));
60​
61 --diffs-bg-addition-override: color-mix(in srgb, var(--background) 92%, var(--success));
62 --diffs-bg-addition-number-override: color-mix(in srgb, var(--background) 88%, var(--success));
63 --diffs-bg-addition-hover-override: color-mix(in srgb, var(--background) 85%, var(--success));
64 --diffs-bg-addition-emphasis-override: color-mix(in srgb, var(--background) 80%, var(--success));
65​
66 --diffs-bg-deletion-override: color-mix(in srgb, var(--background) 92%, var(--destructive));
67 --diffs-bg-deletion-number-override: color-mix(in srgb, var(--background) 88%, var(--destructive));
68 --diffs-bg-deletion-hover-override: color-mix(in srgb, var(--background) 85%, var(--destructive));
69 --diffs-bg-deletion-emphasis-override: color-mix(
70 in srgb,
71 var(--background) 80%,
72 var(--destructive)
73 );
74​
75 background-color: var(--diffs-bg) !important;
77​
78[data-file-info] {
79 background-color: color-mix(in srgb, var(--card) 94%, var(--foreground)) !important;
80 border-block-color: var(--border) !important;
81 color: var(--foreground) !important;
83​
84[data-diffs-header] {
85 position: sticky !important;
86 top: 0;
87 z-index: 4;
88 background-color: color-mix(in srgb, var(--card) 94%, var(--foreground)) !important;
89 border-bottom: 1px solid var(--border) !important;
91​
92[data-title] {
93 cursor: pointer;
94 transition:
95 color 120ms ease,
96 text-decoration-color 120ms ease;
97 text-decoration: underline;
98 text-decoration-color: transparent;
99 text-underline-offset: 2px;
101​
102[data-title]:hover {
103 color: color-mix(in srgb, var(--foreground) 84%, var(--primary)) !important;
104 text-decoration-color: currentColor;
106`;
107​
108type RenderablePatch =
109 | {
110 kind: "files";
111 files: FileDiffMetadata[];
112 }
113 | {
114 kind: "raw";
115 text: string;
116 reason: string;
117 };
118​
119function getRenderablePatch(
120 patch: string | undefined,
121 cacheScope = "diff-panel",
122): RenderablePatch | null {
123 if (!patch) return null;
124 const normalizedPatch = patch.trim();
125 if (normalizedPatch.length === 0) return null;
126​
127 try {
128 const parsedPatches = parsePatchFiles(
129 normalizedPatch,
130 buildPatchCacheKey(normalizedPatch, cacheScope),
131 );
132 const files = parsedPatches.flatMap((parsedPatch) => parsedPatch.files);
133 if (files.length > 0) {
134 return { kind: "files", files };
135 }
136​
137 return {
138 kind: "raw",
139 text: normalizedPatch,
140 reason: "Unsupported diff format. Showing raw patch.",
141 };
142 } catch {
143 return {
144 kind: "raw",
145 text: normalizedPatch,
146 reason: "Failed to parse patch. Showing raw patch.",
147 };
148 }
150​
151function resolveFileDiffPath(fileDiff: FileDiffMetadata): string {
152 const raw = fileDiff.name ?? fileDiff.prevName ?? "";
153 if (raw.startsWith("a/") || raw.startsWith("b/")) {
154 return raw.slice(2);
155 }
156 return raw;
158​
159function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {
160 return fileDiff.cacheKey ?? `${fileDiff.prevName ?? "none"}:${fileDiff.name}`;
162​
163interface DiffPanelProps {
164 mode?: DiffPanelMode;
166​
167export { DiffWorkerPoolProvider } from "./DiffWorkerPoolProvider";
168​
169export default function DiffPanel({ mode = "inline" }: DiffPanelProps) {
170 const navigate = useNavigate();
171 const { resolvedTheme } = useTheme();
172 const settings = useSettings();
173 const [diffRenderMode, setDiffRenderMode] = useState<DiffRenderMode>("stacked");
174 const [diffWordWrap, setDiffWordWrap] = useState(settings.diffWordWrap);
175 const patchViewportRef = useRef<HTMLDivElement>(null);
176 const turnStripRef = useRef<HTMLDivElement>(null);
177 const previousDiffOpenRef = useRef(false);
178 const [canScrollTurnStripLeft, setCanScrollTurnStripLeft] = useState(false);
179 const [canScrollTurnStripRight, setCanScrollTurnStripRight] = useState(false);
180 const routeThreadRef = useParams({
181 strict: false,
182 select: (params) => resolveThreadRouteRef(params),
183 });
184 const diffSearch = useSearch({ strict: false, select: (search) => parseDiffRouteSearch(search) });
185 const diffOpen = diffSearch.diff === "1";
186 const activeThreadId = routeThreadRef?.threadId ?? null;
187 const activeThread = useStore(
188 useMemo(() => createThreadSelectorByRef(routeThreadRef), [routeThreadRef]),
189 );
190 const activeProjectId = activeThread?.projectId ?? null;
191 const activeProject = useStore((store) =>
192 activeThread && activeProjectId
193 ? selectProjectByRef(store, {
194 environmentId: activeThread.environmentId,
195 projectId: activeProjectId,
196 })
197 : undefined,
198 );
199 const activeCwd = activeThread?.worktreePath ?? activeProject?.cwd;
200 const gitStatusQuery = useGitStatus({
201 environmentId: activeThread?.environmentId ?? null,
202 cwd: activeCwd ?? null,
203 });
204 const isGitRepo = gitStatusQuery.data?.isRepo ?? true;
205 const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
206 useTurnDiffSummaries(activeThread);
207 const orderedTurnDiffSummaries = useMemo(
208 () =>
209 [...turnDiffSummaries].toSorted((left, right) => {
210 const leftTurnCount =
211 left.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[left.turnId] ?? 0;
212 const rightTurnCount =
213 right.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[right.turnId] ?? 0;
214 if (leftTurnCount !== rightTurnCount) {
215 return rightTurnCount - leftTurnCount;
216 }
217 return right.completedAt.localeCompare(left.completedAt);
218 }),
219 [inferredCheckpointTurnCountByTurnId, turnDiffSummaries],
220 );
221​
222 const selectedTurnId = diffSearch.diffTurnId ?? null;
223 const selectedFilePath = selectedTurnId !== null ? (diffSearch.diffFilePath ?? null) : null;
224 const selectedTurn =
225 selectedTurnId === null
226 ? undefined
227 : (orderedTurnDiffSummaries.find((summary) => summary.turnId === selectedTurnId) ??
228 orderedTurnDiffSummaries[0]);
229 const selectedCheckpointTurnCount =
230 selectedTurn &&
231 (selectedTurn.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[selectedTurn.turnId]);
232 const selectedCheckpointRange = useMemo(
233 () =>
234 typeof selectedCheckpointTurnCount === "number"
235 ? {
236 fromTurnCount: Math.max(0, selectedCheckpointTurnCount - 1),
237 toTurnCount: selectedCheckpointTurnCount,
238 }
239 : null,
240 [selectedCheckpointTurnCount],
241 );
242 const conversationCheckpointTurnCount = useMemo(() => {
243 const turnCounts = orderedTurnDiffSummaries
244 .map(
245 (summary) =>
246 summary.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[summary.turnId],
247 )
248 .filter((value): value is number => typeof value === "number");
249 if (turnCounts.length === 0) {
250 return undefined;
251 }
252 const latest = Math.max(...turnCounts);
253 return latest > 0 ? latest : undefined;
254 }, [inferredCheckpointTurnCountByTurnId, orderedTurnDiffSummaries]);
255 const conversationCheckpointRange = useMemo(
256 () =>
257 !selectedTurn && typeof conversationCheckpointTurnCount === "number"
258 ? {
259 fromTurnCount: 0,
260 toTurnCount: conversationCheckpointTurnCount,
261 }
262 : null,
263 [conversationCheckpointTurnCount, selectedTurn],
264 );
265 const activeCheckpointRange = selectedTurn
266 ? selectedCheckpointRange
267 : conversationCheckpointRange;
268 const conversationCacheScope = useMemo(() => {
269 if (selectedTurn || orderedTurnDiffSummaries.length === 0) {
270 return null;
271 }
272 return `conversation:${orderedTurnDiffSummaries.map((summary) => summary.turnId).join(",")}`;
273 }, [orderedTurnDiffSummaries, selectedTurn]);
274 const activeCheckpointDiffQuery = useQuery(
275 checkpointDiffQueryOptions({
276 environmentId: activeThread?.environmentId ?? null,
277 threadId: activeThreadId,
278 fromTurnCount: activeCheckpointRange?.fromTurnCount ?? null,
279 toTurnCount: activeCheckpointRange?.toTurnCount ?? null,
280 cacheScope: selectedTurn ? `turn:${selectedTurn.turnId}` : conversationCacheScope,
281 enabled: isGitRepo,
282 }),
283 );
284 const selectedTurnCheckpointDiff = selectedTurn
285 ? activeCheckpointDiffQuery.data?.diff
286 : undefined;
287 const conversationCheckpointDiff = selectedTurn
288 ? undefined
289 : activeCheckpointDiffQuery.data?.diff;
290 const isLoadingCheckpointDiff = activeCheckpointDiffQuery.isLoading;
291 const checkpointDiffError =
292 activeCheckpointDiffQuery.error instanceof Error
293 ? activeCheckpointDiffQuery.error.message
294 : activeCheckpointDiffQuery.error
295 ? "Failed to load checkpoint diff."
296 : null;
297​
298 const selectedPatch = selectedTurn ? selectedTurnCheckpointDiff : conversationCheckpointDiff;
299 const hasResolvedPatch = typeof selectedPatch === "string";
300 const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0;
301 const renderablePatch = useMemo(
302 () => getRenderablePatch(selectedPatch, `diff-panel:${resolvedTheme}`),
303 [resolvedTheme, selectedPatch],
304 );
305 const renderableFiles = useMemo(() => {
306 if (!renderablePatch || renderablePatch.kind !== "files") {
307 return [];
308 }
309 return renderablePatch.files.toSorted((left, right) =>
310 resolveFileDiffPath(left).localeCompare(resolveFileDiffPath(right), undefined, {
311 numeric: true,
312 sensitivity: "base",
313 }),
314 );
315 }, [renderablePatch]);
316​
317 useEffect(() => {
318 if (diffOpen && !previousDiffOpenRef.current) {
319 setDiffWordWrap(settings.diffWordWrap);
320 }
321 previousDiffOpenRef.current = diffOpen;
322 }, [diffOpen, settings.diffWordWrap]);
323​
324 useEffect(() => {
325 if (!selectedFilePath || !patchViewportRef.current) {
326 return;
327 }
328 const target = Array.from(
329 patchViewportRef.current.querySelectorAll<HTMLElement>("[data-diff-file-path]"),
330 ).find((element) => element.dataset.diffFilePath === selectedFilePath);
331 target?.scrollIntoView({ block: "nearest" });
332 }, [selectedFilePath, renderableFiles]);
333​
334 const openDiffFileInEditor = useCallback(
335 (filePath: string) => {
336 const api = readLocalApi();
337 if (!api) return;
338 const targetPath = activeCwd ? resolvePathLinkTarget(filePath, activeCwd) : filePath;
339 void openInPreferredEditor(api, targetPath).catch((error) => {
340 console.warn("Failed to open diff file in editor.", error);
341 });
342 },
343 [activeCwd],
344 );
345​
346 const selectTurn = (turnId: TurnId) => {
347 if (!activeThread) return;
348 void navigate({
349 to: "/$environmentId/$threadId",
350 params: buildThreadRouteParams(scopeThreadRef(activeThread.environmentId, activeThread.id)),
351 search: (previous) => {
352 const rest = stripDiffSearchParams(previous);
353 return { ...rest, diff: "1", diffTurnId: turnId };
354 },
355 });
356 };
357 const selectWholeConversation = () => {
358 if (!activeThread) return;
359 void navigate({
360 to: "/$environmentId/$threadId",
361 params: buildThreadRouteParams(scopeThreadRef(activeThread.environmentId, activeThread.id)),
362 search: (previous) => {
363 const rest = stripDiffSearchParams(previous);
364 return { ...rest, diff: "1" };
365 },
366 });
367 };
368 const updateTurnStripScrollState = useCallback(() => {
369 const element = turnStripRef.current;
370 if (!element) {
371 setCanScrollTurnStripLeft(false);
372 setCanScrollTurnStripRight(false);
373 return;
374 }
375​
376 const maxScrollLeft = Math.max(0, element.scrollWidth - element.clientWidth);
377 setCanScrollTurnStripLeft(element.scrollLeft > 4);
378 setCanScrollTurnStripRight(element.scrollLeft < maxScrollLeft - 4);
379 }, []);
380 const scrollTurnStripBy = useCallback((offset: number) => {
381 const element = turnStripRef.current;
382 if (!element) return;
383 element.scrollBy({ left: offset, behavior: "smooth" });
384 }, []);
385 const onTurnStripWheel = useCallback((event: ReactWheelEvent<HTMLDivElement>) => {
386 const element = turnStripRef.current;
387 if (!element) return;
388 if (element.scrollWidth <= element.clientWidth + 1) return;
389 if (Math.abs(event.deltaY) <= Math.abs(event.deltaX)) return;
390​
391 event.preventDefault();
392 element.scrollBy({ left: event.deltaY, behavior: "auto" });
393 }, []);
394​
395 useEffect(() => {
396 const element = turnStripRef.current;
397 if (!element) return;
398​
399 const frameId = window.requestAnimationFrame(() => updateTurnStripScrollState());
400 const onScroll = () => updateTurnStripScrollState();
401​
402 element.addEventListener("scroll", onScroll, { passive: true });
403​
404 const resizeObserver = new ResizeObserver(() => updateTurnStripScrollState());
405 resizeObserver.observe(element);
406​
407 return () => {
408 window.cancelAnimationFrame(frameId);
409 element.removeEventListener("scroll", onScroll);
410 resizeObserver.disconnect();
411 };
412 }, [updateTurnStripScrollState]);
413​
414 useEffect(() => {
415 const frameId = window.requestAnimationFrame(() => updateTurnStripScrollState());
416 return () => {
417 window.cancelAnimationFrame(frameId);
418 };
419 }, [orderedTurnDiffSummaries, selectedTurnId, updateTurnStripScrollState]);
420​
421 useEffect(() => {
422 const element = turnStripRef.current;
423 if (!element) return;
424​
425 const selectedChip = element.querySelector<HTMLElement>("[data-turn-chip-selected='true']");
426 selectedChip?.scrollIntoView({ block: "nearest", inline: "nearest", behavior: "smooth" });
427 }, [selectedTurn?.turnId, selectedTurnId]);
428​
429 const headerRow = (
430 <>
431 <div className="relative min-w-0 flex-1 [-webkit-app-region:no-drag]">
432 {canScrollTurnStripLeft && (
433 <div className="pointer-events-none absolute inset-y-0 left-8 z-10 w-7 bg-linear-to-r from-card to-transparent" />
434 )}
435 {canScrollTurnStripRight && (
436 <div className="pointer-events-none absolute inset-y-0 right-8 z-10 w-7 bg-linear-to-l from-card to-transparent" />
437 )}
438 <button
439 type="button"
440 className={cn(
441 "absolute left-0 top-1/2 z-20 inline-flex size-6 -translate-y-1/2 items-center justify-center rounded-md border bg-background/90 text-muted-foreground transition-colors",
442 canScrollTurnStripLeft
443 ? "border-border/70 hover:border-border hover:text-foreground"
444 : "cursor-not-allowed border-border/40 text-muted-foreground/40",
445 )}
446 onClick={() => scrollTurnStripBy(-180)}
447 disabled={!canScrollTurnStripLeft}
448 aria-label="Scroll turn list left"
449 >
450 <ChevronLeftIcon className="size-3.5" />
451 </button>
452 <button
453 type="button"
454 className={cn(
455 "absolute right-0 top-1/2 z-20 inline-flex size-6 -translate-y-1/2 items-center justify-center rounded-md border bg-background/90 text-muted-foreground transition-colors",
456 canScrollTurnStripRight
457 ? "border-border/70 hover:border-border hover:text-foreground"
458 : "cursor-not-allowed border-border/40 text-muted-foreground/40",
459 )}
460 onClick={() => scrollTurnStripBy(180)}
461 disabled={!canScrollTurnStripRight}
462 aria-label="Scroll turn list right"
463 >
464 <ChevronRightIcon className="size-3.5" />
465 </button>
466 <div
467 ref={turnStripRef}
468 className="turn-chip-strip flex gap-1 overflow-x-auto px-8 py-0.5"
469 onWheel={onTurnStripWheel}
470 >
471 <button
472 type="button"
473 className="shrink-0 rounded-md"
474 onClick={selectWholeConversation}
475 data-turn-chip-selected={selectedTurnId === null}
476 >
477 <div
478 className={cn(
479 "rounded-md border px-2 py-1 text-left transition-colors",
480 selectedTurnId === null
481 ? "border-border bg-accent text-accent-foreground"
482 : "border-border/70 bg-background/70 text-muted-foreground/80 hover:border-border hover:text-foreground/80",
483 )}
484 >
485 <div className="text-[10px] leading-tight font-medium">All turns</div>
486 </div>
487 </button>
488 {orderedTurnDiffSummaries.map((summary) => (
489 <button
490 key={summary.turnId}
491 type="button"
492 className="shrink-0 rounded-md"
493 onClick={() => selectTurn(summary.turnId)}
494 title={summary.turnId}
495 data-turn-chip-selected={summary.turnId === selectedTurn?.turnId}
496 >
497 <div
498 className={cn(
499 "rounded-md border px-2 py-1 text-left transition-colors",
500 summary.turnId === selectedTurn?.turnId
501 ? "border-border bg-accent text-accent-foreground"
502 : "border-border/70 bg-background/70 text-muted-foreground/80 hover:border-border hover:text-foreground/80",
503 )}
504 >
505 <div className="flex items-center gap-1">
506 <span className="text-[10px] leading-tight font-medium">
507 Turn{" "}
508 {summary.checkpointTurnCount ??
509 inferredCheckpointTurnCountByTurnId[summary.turnId] ??
510 "?"}
511 </span>
512 <span className="text-[9px] leading-tight opacity-70">
513 {formatShortTimestamp(summary.completedAt, settings.timestampFormat)}
514 </span>
515 </div>
516 </div>
517 </button>
518 ))}
519 </div>
520 </div>
521 <div className="flex shrink-0 items-center gap-1 [-webkit-app-region:no-drag]">
522 <ToggleGroup
523 className="shrink-0"
524 variant="outline"
525 size="xs"
526 value={[diffRenderMode]}
527 onValueChange={(value) => {
528 const next = value[0];
529 if (next === "stacked" || next === "split") {
530 setDiffRenderMode(next);
531 }
532 }}
533 >
534 <Toggle aria-label="Stacked diff view" value="stacked">
535 <Rows3Icon className="size-3" />
536 </Toggle>
537 <Toggle aria-label="Split diff view" value="split">
538 <Columns2Icon className="size-3" />
539 </Toggle>
540 </ToggleGroup>
541 <Toggle
542 aria-label={diffWordWrap ? "Disable diff line wrapping" : "Enable diff line wrapping"}
543 title={diffWordWrap ? "Disable line wrapping" : "Enable line wrapping"}
544 variant="outline"
545 size="xs"
546 pressed={diffWordWrap}
547 onPressedChange={(pressed) => {
548 setDiffWordWrap(Boolean(pressed));
549 }}
550 >
551 <TextWrapIcon className="size-3" />
552 </Toggle>
553 </div>
554 </>
555 );
556​
557 return (
558 <DiffPanelShell mode={mode} header={headerRow}>
559 {!activeThread ? (
560 <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
561 Select a thread to inspect turn diffs.
562 </div>
563 ) : !isGitRepo ? (
564 <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
565 Turn diffs are unavailable because this project is not a git repository.
566 </div>
567 ) : orderedTurnDiffSummaries.length === 0 ? (
568 <div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
569 No completed turns yet.
570 </div>
571 ) : (
572 <>
573 <div
574 ref={patchViewportRef}
575 className="diff-panel-viewport min-h-0 min-w-0 flex-1 overflow-hidden"
576 >
577 {checkpointDiffError && !renderablePatch && (
578 <div className="px-3">
579 <p className="mb-2 text-[11px] text-red-500/80">{checkpointDiffError}</p>
580 </div>
581 )}
582 {!renderablePatch ? (
583 isLoadingCheckpointDiff ? (
584 <DiffPanelLoadingState label="Loading checkpoint diff..." />
585 ) : (
586 <div className="flex h-full items-center justify-center px-3 py-2 text-xs text-muted-foreground/70">
587 <p>
588 {hasNoNetChanges
589 ? "No net changes in this selection."
590 : "No patch available for this selection."}
591 </p>
592 </div>
593 )
594 ) : renderablePatch.kind === "files" ? (
595 <Virtualizer
596 className="diff-render-surface h-full min-h-0 overflow-auto px-2 pb-2"
597 config={{
598 overscrollSize: 600,
599 intersectionObserverMargin: 1200,
600 }}
601 >
602 {renderableFiles.map((fileDiff) => {
603 const filePath = resolveFileDiffPath(fileDiff);
604 const fileKey = buildFileDiffRenderKey(fileDiff);
605 const themedFileKey = `${fileKey}:${resolvedTheme}`;
606 return (
607 <div
608 key={themedFileKey}
609 data-diff-file-path={filePath}
610 className="diff-render-file mb-2 rounded-md first:mt-2 last:mb-0"
611 onClickCapture={(event) => {
612 const nativeEvent = event.nativeEvent as MouseEvent;
613 const composedPath = nativeEvent.composedPath?.() ?? [];
614 const clickedHeader = composedPath.some((node) => {
615 if (!(node instanceof Element)) return false;
616 return node.hasAttribute("data-title");
617 });
618 if (!clickedHeader) return;
619 openDiffFileInEditor(filePath);
620 }}
621 >
622 <FileDiff
623 fileDiff={fileDiff}
624 options={{
625 diffStyle: diffRenderMode === "split" ? "split" : "unified",
626 lineDiffType: "none",
627 overflow: diffWordWrap ? "wrap" : "scroll",
628 theme: resolveDiffThemeName(resolvedTheme),
629 themeType: resolvedTheme as DiffThemeType,
630 unsafeCSS: DIFF_PANEL_UNSAFE_CSS,
631 }}
632 />
633 </div>
634 );
635 })}
636 </Virtualizer>
637 ) : (
638 <div className="h-full overflow-auto p-2">
639 <div className="space-y-2">
640 <p className="text-[11px] text-muted-foreground/75">{renderablePatch.reason}</p>
641 <pre
642 className={cn(
643 "max-h-[72vh] rounded-md border border-border/70 bg-background/70 p-3 font-mono text-[11px] leading-relaxed text-muted-foreground/90",
644 diffWordWrap
645 ? "overflow-auto whitespace-pre-wrap wrap-break-word"
646 : "overflow-auto",
647 )}
648 >
649 {renderablePatch.text}
650 </pre>
651 </div>
652 </div>
653 )}
654 </div>
655 </>
656 )}
657 </DiffPanelShell>
658 );