Files
t3code/apps/web/src/components/DiffPanel.tsx
T
2026-09-24 10:49:05 -07:00

1206 lines
49 KiB
TypeScript

import { RefreshIcon } from "~/components/ui/refresh-icon";
import { useAtomValue } from "@effect/atom-react";
import type { FileDiffContentsLoader, FileDiffMetadata } from "@pierre/diffs";
import { useParams } from "@tanstack/react-router";
import {
isAtomCommandInterrupted,
squashAtomCommandFailure,
} from "@t3tools/client-runtime/state/runtime";
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
import type { ScopedThreadRef, TurnId } from "@t3tools/contracts";
import {
ArrowRightIcon,
CheckIcon,
ChevronDownIcon,
ChevronRightIcon,
ChevronsDownUpIcon,
ChevronsUpDownIcon,
Columns2Icon,
FolderTreeIcon,
PilcrowIcon,
Rows3Icon,
TextWrapIcon,
} from "lucide-react";
import * as Schema from "effect/Schema";
import * as DateTime from "effect/DateTime";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useCodeViewFileReveal } from "./diffs/useCodeViewFileReveal";
import { useOpenInPreferredEditor } from "../editorPreferences";
import { useFileContextMenuHandler } from "../fileContextMenu";
import { type DraftId } from "../composerDraftStore";
import { openDiffFilePrimaryAction } from "../diffFileActions";
import { useCheckpointDiff } from "~/lib/checkpointDiffState";
import { cn } from "~/lib/utils";
import { selectThreadDiffPanelSelection, useDiffPanelStore } from "../diffPanelStore";
import { useLocalStorage } from "../hooks/useLocalStorage";
import { useTheme } from "../hooks/useTheme";
import {
buildFileDiffContentVersion,
buildFileDiffIdentityKey,
getDiffCollapseIconClassName,
getDiffLineStat,
getRenderablePatch,
resolveDiffThemeName,
resolveFileDiffPath,
} from "../lib/diffRendering";
import { PREFERRED_HIGHLIGHTER } from "../lib/syntaxHighlighting";
import { areAllDiffFilesCollapsed, toggleAllDiffFiles } from "../lib/diffCollapse";
import { useTurnDiffSummaries } from "../hooks/useTurnDiffSummaries";
import { useWorkspaceMutationRefresh } from "../hooks/useWorkspaceMutationRefresh";
import { useProject, useThread } from "../state/entities";
import { resolveThreadRouteRef } from "../threadRoutes";
import { useClientSettings, useUpdateClientSettings } from "../hooks/useSettings";
import { formatShortTimestamp } from "../timestampFormat";
import { DiffFilePathCopyButton } from "./DiffFilePathCopyButton";
import { DiffPanelLoadingState, DiffPanelShell, type DiffPanelMode } from "./DiffPanelShell";
import { DiffStatLabel } from "./chat/DiffStatLabel";
import { AnnotatableCodeView, type AnnotatableCodeViewHandle } from "./diffs/AnnotatableCodeView";
import { DiffFileTree } from "./diffs/DiffFileTree";
import { diffFileTreeEntries } from "./diffs/diffFileTree.logic";
import { Button } from "./ui/button";
import { ToggleGroup, Toggle } from "./ui/toggle-group";
import { Switch } from "./ui/switch";
import {
Combobox,
ComboboxEmpty,
ComboboxSearchInput,
ComboboxItem,
ComboboxList,
ComboboxPopup,
ComboboxTrigger,
} from "./ui/combobox";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "./ui/menu";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { useEnvironmentQuery } from "../state/query";
import { useAtomCommand } from "../state/use-atom-command";
import { serverEnvironment } from "../state/server";
import { reviewEnvironment } from "../state/review";
import { vcsEnvironment } from "../state/vcs";
import { buildBaseRefChoices, filterBaseRefChoices } from "../lib/baseRefChoices";
import { createGitDiffFileContentsLoader } from "../lib/diffFileContents";
import { useReviewFilePatches } from "./diffs/useReviewFilePatches";
import { DiffFileLoadingBoundary } from "./diffs/DiffFileLoadingBoundary";
import { DiffFileStatus } from "./diffs/DiffFileStatus";
type DiffThemeType = "light" | "dark";
const AUTOMATIC_BASE_REF = "__automatic_base_ref__";
const DIFF_FILE_TREE_STORAGE_KEY = "t3code.diffFileTreeOpen";
const fileEntryCache = new WeakMap<
FileDiffMetadata,
{ fileDiff: FileDiffMetadata; fileKey: string; fileVersion: number }
>();
function getCachedFileEntry(fileDiff: FileDiffMetadata) {
const cached = fileEntryCache.get(fileDiff);
if (cached) return cached;
const entry = {
fileDiff,
fileKey: buildFileDiffIdentityKey(fileDiff),
fileVersion: buildFileDiffContentVersion(fileDiff),
};
fileEntryCache.set(fileDiff, entry);
return entry;
}
interface CollapsedDiffFilesState {
readonly scopeKey: string | null;
readonly fileKeys: ReadonlySet<string>;
}
const EMPTY_COLLAPSED_DIFF_FILE_KEYS: ReadonlySet<string> = new Set();
interface DiffPanelProps {
mode?: DiffPanelMode;
composerDraftTarget: ScopedThreadRef | DraftId;
workspaceMutationId: string | null;
}
export default function DiffPanel({
mode = "inline",
composerDraftTarget,
workspaceMutationId,
}: DiffPanelProps) {
const { resolvedTheme } = useTheme();
const settings = useClientSettings();
const diffLayout = settings.diffLayout;
const updateClientSettings = useUpdateClientSettings();
const [wordWrap, setWordWrap] = useState(settings.wordWrap);
const [diffIgnoreWhitespace, setDiffIgnoreWhitespace] = useState(settings.diffIgnoreWhitespace);
const [fileTreeOpen, setFileTreeOpen] = useLocalStorage(
DIFF_FILE_TREE_STORAGE_KEY,
false,
Schema.Boolean,
);
const [baseRefQuery, setBaseRefQuery] = useState("");
const [collapsedDiffFiles, setCollapsedDiffFiles] = useState<CollapsedDiffFilesState>(() => ({
scopeKey: null,
fileKeys: EMPTY_COLLAPSED_DIFF_FILE_KEYS,
}));
const [codeViewRevision, setCodeViewRevision] = useState(0);
const [codeView, setCodeView] = useState<AnnotatableCodeViewHandle | null>(null);
const routeThreadRef = useParams({
strict: false,
select: (params) => resolveThreadRouteRef(params),
});
const activeThreadId = routeThreadRef?.threadId ?? null;
const activeThread = useThread(routeThreadRef);
const activeProjectId = activeThread?.projectId ?? null;
const activeProject = useProject(
activeThread && activeProjectId
? {
environmentId: activeThread.environmentId,
projectId: activeProjectId,
}
: null,
);
const activeCwd = activeThread?.worktreePath ?? activeProject?.workspaceRoot;
const activeRepositoryRoot = activeThread?.worktreePath
? undefined
: activeProject?.repositoryIdentity?.rootPath;
const serverConfig = useAtomValue(
serverEnvironment.configValueAtom(activeThread?.environmentId ?? null),
);
const onFileContextMenu = useFileContextMenuHandler(activeThread?.environmentId ?? null);
const openInPreferredEditor = useOpenInPreferredEditor(
activeThread?.environmentId ?? null,
serverConfig?.availableEditors ?? [],
);
const getDiffFileContents = useAtomCommand(reviewEnvironment.diffFileContents);
const gitStatusQuery = useEnvironmentQuery(
activeThread !== null && activeThread !== undefined && activeCwd != null
? vcsEnvironment.status({
environmentId: activeThread.environmentId,
input: { cwd: activeCwd },
})
: null,
);
const diffSelection = useDiffPanelStore((state) =>
selectThreadDiffPanelSelection(state.byThreadKey, routeThreadRef),
);
const isGitRepo = gitStatusQuery.data?.isRepo ?? true;
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
useTurnDiffSummaries(activeThread);
const orderedTurnDiffSummaries = useMemo(
() =>
[...turnDiffSummaries].toSorted((left, right) => {
const leftTurnCount =
left.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[left.turnId] ?? 0;
const rightTurnCount =
right.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[right.turnId] ?? 0;
if (leftTurnCount !== rightTurnCount) {
return rightTurnCount - leftTurnCount;
}
return right.completedAt.localeCompare(left.completedAt);
}),
[inferredCheckpointTurnCountByTurnId, turnDiffSummaries],
);
useEffect(() => {
if (!routeThreadRef || diffSelection.kind !== "turn") return;
useDiffPanelStore.getState().reconcileTurnSelection(
routeThreadRef,
orderedTurnDiffSummaries.map((summary) => summary.turnId),
);
}, [diffSelection, orderedTurnDiffSummaries, routeThreadRef]);
const selectedTurnId = diffSelection.kind === "turn" ? diffSelection.turnId : null;
const selectedGitScope = diffSelection.kind === "unstaged" ? "unstaged" : "branch";
const selectedBaseRef = diffSelection.kind === "branch" ? diffSelection.baseRef : null;
const selectedFilePath = diffSelection.kind === "turn" ? diffSelection.filePath : null;
const selectedFileRevealRequestId =
diffSelection.kind === "turn" ? diffSelection.revealRequestId : 0;
const selectedTurn =
selectedTurnId === null
? undefined
: (orderedTurnDiffSummaries.find((summary) => summary.turnId === selectedTurnId) ??
orderedTurnDiffSummaries[0]);
const selectedCheckpointTurnCount =
selectedTurn &&
(selectedTurn.checkpointTurnCount ?? inferredCheckpointTurnCountByTurnId[selectedTurn.turnId]);
const latestTurn = orderedTurnDiffSummaries[0];
const selectedScopeLabel =
selectedTurnId === null
? selectedGitScope === "unstaged"
? "Working tree"
: "Branch changes"
: selectedTurn?.turnId === latestTurn?.turnId
? "Latest turn"
: `Turn ${selectedCheckpointTurnCount ?? "?"}`;
const reviewSectionId = selectedTurn ? `turn:${selectedTurn.turnId}` : selectedGitScope;
const collapseScopeKey = routeThreadRef
? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}:${reviewSectionId}`
: null;
const codeViewMountKey = `${collapseScopeKey ?? reviewSectionId}:${codeViewRevision}`;
const reviewSectionTitle = selectedTurn
? `Turn ${selectedCheckpointTurnCount ?? "?"}`
: selectedGitScope === "unstaged"
? "Working tree"
: "Branch changes";
const selectedCheckpointRange = useMemo(
() =>
typeof selectedCheckpointTurnCount === "number"
? {
fromTurnCount: Math.max(0, selectedCheckpointTurnCount - 1),
toTurnCount: selectedCheckpointTurnCount,
}
: null,
[selectedCheckpointTurnCount],
);
const activeCheckpointDiff = useCheckpointDiff(
{
environmentId: activeThread?.environmentId ?? null,
threadId: activeThreadId,
fromTurnCount: selectedCheckpointRange?.fromTurnCount ?? null,
toTurnCount: selectedCheckpointRange?.toTurnCount ?? null,
ignoreWhitespace: diffIgnoreWhitespace,
cacheScope: selectedTurn ? `turn:${selectedTurn.turnId}` : null,
},
{ enabled: isGitRepo && selectedTurn !== undefined },
);
const primaryBranchDiffPreview = useEnvironmentQuery(
selectedTurnId === null && activeThread && activeCwd
? reviewEnvironment.diffPreview({
environmentId: activeThread.environmentId,
input: {
cwd: activeCwd,
...(selectedBaseRef ? { baseRef: selectedBaseRef } : {}),
ignoreWhitespace: diffIgnoreWhitespace,
},
})
: null,
);
const shouldRetryBranchDiffAtEnvironmentCwd =
selectedTurnId === null &&
primaryBranchDiffPreview.error?.includes("configured workspace root") === true &&
serverConfig?.cwd !== undefined &&
serverConfig.cwd !== activeCwd;
const fallbackBranchDiffPreview = useEnvironmentQuery(
shouldRetryBranchDiffAtEnvironmentCwd && activeThread && serverConfig
? reviewEnvironment.diffPreview({
environmentId: activeThread.environmentId,
input: {
cwd: serverConfig.cwd,
...(selectedBaseRef ? { baseRef: selectedBaseRef } : {}),
ignoreWhitespace: diffIgnoreWhitespace,
},
})
: null,
);
const branchDiffPreview = shouldRetryBranchDiffAtEnvironmentCwd
? fallbackBranchDiffPreview
: primaryBranchDiffPreview;
const canRefreshGitDiff =
isGitRepo && selectedTurnId === null && activeThread != null && activeCwd != null;
const activeThreadRefreshKey = routeThreadRef
? `${routeThreadRef.environmentId}:${routeThreadRef.threadId}`
: null;
const selectedGitSource = branchDiffPreview.data?.sources.find(
(source) => source.kind === (selectedGitScope === "unstaged" ? "working-tree" : "branch-range"),
);
const refreshPreviewQuery = branchDiffPreview.refresh;
const refreshDiffFromUserAction = refreshPreviewQuery;
const currentLoadDiffFiles = useMemo<FileDiffContentsLoader | undefined>(() => {
const preview = branchDiffPreview.data;
if (selectedTurnId !== null || !activeThread || !preview || !selectedGitSource) {
return undefined;
}
return createGitDiffFileContentsLoader(getDiffFileContents, {
environmentId: activeThread.environmentId,
cwd: preview.cwd,
sourceKind: selectedGitSource.kind,
baseRef: selectedGitSource.baseRef,
headRef: selectedGitSource.headRef,
cacheKey: selectedGitSource.diffHash,
});
}, [
activeThread,
branchDiffPreview.data,
getDiffFileContents,
selectedGitSource,
selectedTurnId,
]);
const loadDiffFilesRef = useRef(currentLoadDiffFiles);
loadDiffFilesRef.current = currentLoadDiffFiles;
const loadDiffFiles = useCallback<FileDiffContentsLoader>(async (fileDiff) => {
const loader = loadDiffFilesRef.current;
if (!loader) throw new Error("Diff file contents are unavailable for this selection.");
return loader(fileDiff);
}, []);
const localBranchRefs = useEnvironmentQuery(
selectedTurnId === null &&
selectedGitScope === "branch" &&
activeThread &&
branchDiffPreview.data?.cwd
? vcsEnvironment.listRefs({
environmentId: activeThread.environmentId,
input: {
cwd: branchDiffPreview.data.cwd,
includeMatchingRemoteRefs: true,
refKind: "local",
...(baseRefQuery.trim().length > 0 ? { query: baseRefQuery.trim() } : {}),
limit: 100,
},
})
: null,
);
const remoteBranchRefs = useEnvironmentQuery(
selectedTurnId === null &&
selectedGitScope === "branch" &&
activeThread &&
branchDiffPreview.data?.cwd
? vcsEnvironment.listRefs({
environmentId: activeThread.environmentId,
input: {
cwd: branchDiffPreview.data.cwd,
includeMatchingRemoteRefs: true,
refKind: "remote",
...(baseRefQuery.trim().length > 0 ? { query: baseRefQuery.trim() } : {}),
limit: 100,
},
})
: null,
);
const baseRefChoices = buildBaseRefChoices(
localBranchRefs.data?.refs.filter((ref) => ref.name !== selectedGitSource?.headRef) ?? [],
remoteBranchRefs.data?.refs ?? [],
);
const matchingBaseRefChoices = filterBaseRefChoices(baseRefChoices, baseRefQuery);
const valueForBaseRefChoice = (choice: (typeof baseRefChoices)[number]) =>
selectedBaseRef && selectedBaseRef === choice.remote?.name
? selectedBaseRef
: (choice.local?.name ?? choice.remote?.name ?? choice.id);
const baseRefItems = [AUTOMATIC_BASE_REF, ...baseRefChoices.map(valueForBaseRefChoice)];
const filteredBaseRefItems = [
...(baseRefQuery.trim().length === 0 ? [AUTOMATIC_BASE_REF] : []),
...matchingBaseRefChoices.map(valueForBaseRefChoice),
];
const gitDiff = selectedGitSource?.diff;
const selectedPatch = selectedTurn ? activeCheckpointDiff.data?.diff : gitDiff;
const isSelectedPatchTruncated = !selectedTurn && selectedGitSource?.truncated === true;
const isLoadingSelectedPatch = selectedTurn
? activeCheckpointDiff.isPending
: branchDiffPreview.isPending;
const selectedPatchError = selectedTurn ? activeCheckpointDiff.error : branchDiffPreview.error;
const hasResolvedPatch = typeof selectedPatch === "string";
const hasNoNetChanges = hasResolvedPatch && selectedPatch.trim().length === 0;
const lazySource =
!selectedTurn && selectedGitSource?.truncated && selectedGitSource.files
? selectedGitSource
: null;
const renderablePatch = useMemo(
() =>
lazySource
? null
: getRenderablePatch(selectedPatch, `diff-panel:${resolvedTheme}`, {
compactPartialHunkOffsets: selectedTurnId === null,
}),
[lazySource, resolvedTheme, selectedPatch, selectedTurnId],
);
const fileStats = useMemo(
() => new Map(lazySource?.files?.map((file) => [file.path, file])),
[lazySource?.files],
);
const {
scope: filePatchScope,
isPending: areFilePatchesPending,
fileStates,
retry,
requestFile,
readyFilePaths,
renderableFiles,
settledFileCount,
loadNextFiles,
} = useReviewFilePatches({
environmentId: activeThread?.environmentId,
cwd: branchDiffPreview.data?.cwd,
source: lazySource,
baseRef: lazySource?.baseRef ?? selectedBaseRef,
ignoreWhitespace: diffIgnoreWhitespace,
theme: resolvedTheme,
revision: branchDiffPreview.data
? DateTime.formatIso(branchDiffPreview.data.generatedAt)
: undefined,
preview: renderablePatch,
});
const refreshBranchDiffPreview = refreshPreviewQuery;
useEffect(() => {
if (!canRefreshGitDiff) return;
const refreshOnFocus = () => refreshBranchDiffPreview();
window.addEventListener("focus", refreshOnFocus);
return () => window.removeEventListener("focus", refreshOnFocus);
}, [canRefreshGitDiff, refreshBranchDiffPreview]);
useWorkspaceMutationRefresh({
enabled: canRefreshGitDiff,
mutationId: workspaceMutationId,
refresh: refreshBranchDiffPreview,
resourceKey: `diff:${activeThreadRefreshKey ?? ""}`,
});
const isRefreshingDiff = branchDiffPreview.isPending || areFilePatchesPending;
const renderableFileEntries = useMemo(
() => renderableFiles.map(getCachedFileEntry),
[renderableFiles],
);
const defaultCollapsedDiffFileKeys = useMemo(
() =>
settings.diffFilesCollapsed
? new Set(renderableFileEntries.map((file) => file.fileKey))
: EMPTY_COLLAPSED_DIFF_FILE_KEYS,
[renderableFileEntries, settings.diffFilesCollapsed],
);
const collapsedDiffFileKeys =
collapsedDiffFiles.scopeKey === collapseScopeKey
? collapsedDiffFiles.fileKeys
: defaultCollapsedDiffFileKeys;
const renderLoadingBoundary = useCallback(
() =>
settledFileCount < renderableFiles.length ? (
<DiffFileLoadingBoundary
load={loadNextFiles}
count={renderableFiles.length - settledFileCount}
/>
) : null,
[settledFileCount, renderableFiles.length, loadNextFiles],
);
const codeViewFiles = useMemo(
() =>
renderableFileEntries
.filter(({ fileDiff }) => !lazySource || readyFilePaths.has(resolveFileDiffPath(fileDiff)))
.map(({ fileDiff, fileKey, fileVersion }) => {
return {
fileDiff,
filePath: resolveFileDiffPath(fileDiff),
fileKey,
fileVersion,
// Header-only placeholders use the viewer's collapsed geometry until their patch arrives.
collapsed:
collapsedDiffFileKeys.has(fileKey) ||
fileDiff.cacheKey?.endsWith(":pending") === true,
};
}),
[collapsedDiffFileKeys, renderableFileEntries, lazySource, readyFilePaths],
);
const diffFileKeys = useMemo(
() => renderableFileEntries.map((file) => file.fileKey),
[renderableFileEntries],
);
const allDiffFilesCollapsed = areAllDiffFilesCollapsed(diffFileKeys, collapsedDiffFileKeys);
const diffLineStat = useMemo(() => {
if (!selectedTurn && selectedGitSource?.files) {
return selectedGitSource.files.reduce(
(total, file) => ({
additions: total.additions + file.additions,
deletions: total.deletions + file.deletions,
}),
{ additions: 0, deletions: 0 },
);
}
return getDiffLineStat(renderableFiles);
}, [renderableFiles, selectedGitSource, selectedTurn]);
const fileTreeEntries = useMemo(() => diffFileTreeEntries(renderableFiles), [renderableFiles]);
const selectedDiffFileKey = selectedFilePath
? (codeViewFiles.find((candidate) => candidate.filePath === selectedFilePath)?.fileKey ?? null)
: null;
useEffect(() => {
if (!selectedDiffFileKey || !codeView?.getInstance()) return;
codeView.scrollTo({ type: "item", id: selectedDiffFileKey, align: "start" });
}, [codeView, codeViewMountKey, selectedDiffFileKey, selectedFileRevealRequestId]);
const treeRevealScope = useMemo(
() => ({ collapseScopeKey, diffSelection }),
[collapseScopeKey, diffSelection],
);
const requestTreeReveal = useCodeViewFileReveal(
codeView,
treeRevealScope,
codeViewFiles.map((file) => file.fileKey),
);
const revealDiffFile = useCallback(
(filePath: string) => {
const index = renderableFileEntries.findIndex(
(candidate) => resolveFileDiffPath(candidate.fileDiff) === filePath,
);
const file = renderableFileEntries[index];
if (!file) return;
setCollapsedDiffFiles((current) => {
const next = new Set(
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys,
);
next.delete(file.fileKey);
return { scopeKey: collapseScopeKey, fileKeys: next };
});
if (lazySource && index >= settledFileCount) {
requestFile(index);
}
requestTreeReveal(file.fileKey);
},
[
renderableFileEntries,
collapseScopeKey,
defaultCollapsedDiffFileKeys,
requestTreeReveal,
lazySource,
settledFileCount,
requestFile,
],
);
const externalRevealRef = useRef<{ cache: string; key: string } | null>(null);
useEffect(() => {
if (!lazySource || !selectedFilePath) return;
const key = `${selectedFilePath}:${selectedFileRevealRequestId}`;
if (
externalRevealRef.current?.cache === filePatchScope &&
externalRevealRef.current.key === key
)
return;
externalRevealRef.current = { cache: filePatchScope, key };
revealDiffFile(selectedFilePath);
}, [lazySource, selectedFilePath, selectedFileRevealRequestId, filePatchScope, revealDiffFile]);
const openDiffFile = useCallback(
(filePath: string) => {
openDiffFilePrimaryAction({
threadRef: routeThreadRef,
filePath,
activeCwd,
repositoryRoot: activeRepositoryRoot,
openInEditor: (targetPath) => {
void (async () => {
const result = await openInPreferredEditor(targetPath);
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
console.warn("Failed to open diff file in editor.", {
operation: "open-diff-file",
...(routeThreadRef
? {
environmentId: routeThreadRef.environmentId,
threadId: routeThreadRef.threadId,
}
: {}),
...safeErrorLogAttributes(squashAtomCommandFailure(result)),
});
}
})();
},
});
},
[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 toggleDiffFileCollapse = useCallback(() => {
setCodeViewRevision((current) => current + 1);
setCollapsedDiffFiles((current) => {
const currentKeys =
current.scopeKey === collapseScopeKey ? current.fileKeys : defaultCollapsedDiffFileKeys;
return {
scopeKey: collapseScopeKey,
fileKeys: toggleAllDiffFiles(diffFileKeys, currentKeys),
};
});
}, [collapseScopeKey, defaultCollapsedDiffFileKeys, diffFileKeys]);
const selectTurn = (turnId: TurnId) => {
if (!routeThreadRef) return;
useDiffPanelStore.getState().selectTurn(routeThreadRef, turnId);
};
const selectGitScope = (scope: "branch" | "unstaged") => {
if (!routeThreadRef) return;
useDiffPanelStore.getState().selectGitScope(routeThreadRef, scope);
};
const selectBranchBaseRef = (baseRef: string | null) => {
if (!routeThreadRef) return;
useDiffPanelStore.getState().selectBranchBaseRef(routeThreadRef, baseRef);
};
// The scope menu has two radio groups: the top-level one treats the latest
// turn as "latest", while the turn sub-menu keys every turn by id so the
// latest turn is also marked there.
const selectedTurnValue = selectedTurn ? `turn:${selectedTurn.turnId}` : "";
const selectedScopeValue =
selectedTurnId === null
? selectedGitScope
: selectedTurn?.turnId === latestTurn?.turnId
? "latest"
: selectedTurnValue;
const selectScopeValue = (value: string) => {
if (value === "unstaged" || value === "branch") {
selectGitScope(value);
} else if (value === "latest") {
if (latestTurn) selectTurn(latestTurn.turnId);
} else {
const turn = orderedTurnDiffSummaries.find((summary) => `turn:${summary.turnId}` === value);
if (turn) selectTurn(turn.turnId);
}
};
const headerRow = (
<>
<div className="flex min-w-0 flex-1 items-center gap-3 [-webkit-app-region:no-drag]">
<DropdownMenu>
<DropdownMenuTrigger
render={<Button size="xs" variant="secondary" />}
className="max-w-full"
aria-label={`Diff scope: ${selectedScopeLabel}`}
>
<span className="truncate">{selectedScopeLabel}</span>
<ChevronDownIcon className="size-3.5 shrink-0 opacity-70" />
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
<DropdownMenuRadioGroup value={selectedScopeValue} onValueChange={selectScopeValue}>
<DropdownMenuRadioItem value="unstaged" closeOnClick>
<span>Working tree</span>
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="branch" closeOnClick>
<span>Branch changes</span>
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="latest" closeOnClick>
<span>Latest turn</span>
</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
<DropdownMenuSub>
<DropdownMenuSubTrigger>Turn</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
<DropdownMenuRadioGroup value={selectedTurnValue} onValueChange={selectScopeValue}>
{orderedTurnDiffSummaries.map((summary) => {
const turnCount =
summary.checkpointTurnCount ??
inferredCheckpointTurnCountByTurnId[summary.turnId] ??
"?";
return (
<DropdownMenuRadioItem
key={summary.turnId}
value={`turn:${summary.turnId}`}
closeOnClick
>
<span className="flex items-center gap-2">
<span>Turn {turnCount}</span>
<span className="ml-auto text-xs tabular-nums text-muted-foreground">
{formatShortTimestamp(summary.completedAt, settings.timestampFormat)}
</span>
</span>
</DropdownMenuRadioItem>
);
})}
</DropdownMenuRadioGroup>
</DropdownMenuSubContent>
</DropdownMenuSub>
</DropdownMenuContent>
</DropdownMenu>
{selectedTurnId === null && selectedGitScope === "branch" && selectedGitSource?.baseRef && (
<div
className="flex min-w-0 max-w-full items-center gap-2 overflow-hidden text-xs text-muted-foreground"
aria-label={`Comparing ${selectedGitSource.headRef ?? "HEAD"} against ${selectedGitSource.baseRef}`}
>
<Tooltip>
<TooltipTrigger render={<span className="flex min-w-0 items-center gap-2" />}>
<span className="min-w-0 max-w-48 truncate">
{selectedGitSource.headRef ?? "HEAD"}
</span>
<ArrowRightIcon className="size-3.5 shrink-0 opacity-70" />
</TooltipTrigger>
<TooltipPopup side="top">
{`${selectedGitSource.headRef ?? "HEAD"} → ${selectedGitSource.baseRef}`}
</TooltipPopup>
</Tooltip>
<Combobox
items={baseRefItems}
filteredItems={filteredBaseRefItems}
value={selectedBaseRef ?? AUTOMATIC_BASE_REF}
onOpenChange={(open) => {
if (!open) setBaseRefQuery("");
}}
onValueChange={(value) => {
if (!value) return;
selectBranchBaseRef(value === AUTOMATIC_BASE_REF ? null : value);
}}
>
<ComboboxTrigger
render={<Button variant="ghost-muted" size="xs" />}
className="min-w-0 max-w-48"
aria-label={`Change comparison target. Currently ${selectedGitSource.baseRef}`}
>
<span className="min-w-0 truncate">{selectedGitSource.baseRef}</span>
<ChevronDownIcon className="size-3.5 shrink-0 opacity-70" />
</ComboboxTrigger>
<ComboboxPopup
align="start"
className="w-72 min-w-0 max-w-[calc(100vw-1rem)] overflow-hidden"
>
<ComboboxSearchInput
placeholder="Search refs..."
value={baseRefQuery}
onChange={(event) => setBaseRefQuery(event.target.value)}
/>
<div className="grid shrink-0 grid-cols-[1rem_minmax(0,1fr)] items-center gap-2 border-b border-border/70 ps-3 pe-6.5 pt-2 pb-1.5 font-medium text-3xs text-muted-foreground uppercase tracking-wide">
<span aria-hidden="true" />
<div className="grid min-w-0 grid-cols-[minmax(0,1fr)_2rem] items-center">
<span>Branch</span>
<span className="text-right">Remote</span>
</div>
</div>
<ComboboxEmpty>No matching refs.</ComboboxEmpty>
<ComboboxList className="max-h-64 min-w-0 overflow-x-hidden">
<ComboboxItem
className="w-full min-w-0 grid-cols-[1rem_minmax(0,1fr)]"
value={AUTOMATIC_BASE_REF}
>
<span className="block min-w-0 truncate">Automatic</span>
</ComboboxItem>
{baseRefChoices.map((choice) => {
const item = valueForBaseRefChoice(choice);
const hasBoth = choice.local !== null && choice.remote !== null;
const useRemote = choice.remote?.name === item;
return (
<ComboboxItem
key={choice.id}
className="w-full min-w-0 grid-cols-[1rem_minmax(0,1fr)]"
value={item}
>
<div className="grid w-full min-w-0 grid-cols-[minmax(0,1fr)_2rem] items-center overflow-hidden">
<span className="block min-w-0 truncate pe-2">{choice.label}</span>
{hasBoth ? (
<div
className="flex justify-end"
onClick={(event) => event.stopPropagation()}
onPointerDown={(event) => event.stopPropagation()}
>
<Switch
aria-label={`Use remote version of ${choice.label}`}
checked={useRemote}
className="[--thumb-size:--spacing(3)]"
onCheckedChange={(checked) => {
const nextRef = checked
? choice.remote?.name
: choice.local?.name;
if (nextRef) selectBranchBaseRef(nextRef);
}}
/>
</div>
) : choice.remote ? (
<Tooltip>
<TooltipTrigger
render={
<span className="flex justify-end text-muted-foreground">
<CheckIcon
role="img"
aria-label="Remote only"
className="size-3"
/>
</span>
}
/>
<TooltipPopup side="top">Remote only</TooltipPopup>
</Tooltip>
) : null}
</div>
</ComboboxItem>
);
})}
</ComboboxList>
</ComboboxPopup>
</Combobox>
</div>
)}
</div>
<div className="flex shrink-0 items-center gap-1 [-webkit-app-region:no-drag]">
{codeViewFiles.length > 0 || (!selectedTurn && selectedGitSource?.files?.length) ? (
<DiffStatLabel
additions={diffLineStat.additions}
deletions={diffLineStat.deletions}
className="mr-1 text-2xs"
layout="inline"
/>
) : null}
{canRefreshGitDiff && (
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
size="icon-sm"
variant="ghost"
aria-label={isRefreshingDiff ? "Refreshing diff" : "Refresh diff"}
onClick={refreshDiffFromUserAction}
/>
}
>
<RefreshIcon size="sm" refreshing={isRefreshingDiff} />
</TooltipTrigger>
<TooltipPopup side="top">
{isRefreshingDiff ? "Refreshing diff…" : "Refresh diff"}
</TooltipPopup>
</Tooltip>
)}
{diffFileKeys.length > 0 && (
<Tooltip>
<TooltipTrigger
render={
<Button
type="button"
size="icon-sm"
variant="ghost"
aria-label={allDiffFilesCollapsed ? "Expand all files" : "Collapse all files"}
onClick={toggleDiffFileCollapse}
/>
}
>
{allDiffFilesCollapsed ? (
<ChevronsUpDownIcon className="size-3.5" />
) : (
<ChevronsDownUpIcon className="size-3.5" />
)}
</TooltipTrigger>
<TooltipPopup side="top">
{allDiffFilesCollapsed ? "Expand all files" : "Collapse all files"}
</TooltipPopup>
</Tooltip>
)}
<ToggleGroup
aria-label="Diff layout"
className="shrink-0"
variant="segmented"
value={[diffLayout]}
onValueChange={(value) => {
const next = value[0];
if (next === "stacked" || next === "split") {
updateClientSettings({ diffLayout: next });
}
}}
>
<Toggle aria-label="Stacked diff view" value="stacked">
<Rows3Icon className="size-3.5" />
</Toggle>
<Toggle aria-label="Split diff view" value="split">
<Columns2Icon className="size-3.5" />
</Toggle>
</ToggleGroup>
<Tooltip>
<TooltipTrigger
render={
<Toggle
aria-label={wordWrap ? "Disable diff line wrapping" : "Enable diff line wrapping"}
variant="ghost"
size="sm"
pressed={wordWrap}
onPressedChange={(pressed) => {
setWordWrap(Boolean(pressed));
}}
/>
}
>
<TextWrapIcon className="size-3.5" />
</TooltipTrigger>
<TooltipPopup side="top">
{wordWrap ? "Disable line wrapping" : "Enable line wrapping"}
</TooltipPopup>
</Tooltip>
<Tooltip>
<TooltipTrigger
render={
<Toggle
aria-label={
diffIgnoreWhitespace ? "Show whitespace changes" : "Hide whitespace changes"
}
variant="ghost"
size="sm"
pressed={diffIgnoreWhitespace}
onPressedChange={(pressed) => {
setDiffIgnoreWhitespace(Boolean(pressed));
}}
/>
}
>
<PilcrowIcon className="size-3.5" />
</TooltipTrigger>
<TooltipPopup side="top">
{diffIgnoreWhitespace ? "Show whitespace changes" : "Hide whitespace changes"}
</TooltipPopup>
</Tooltip>
{diffFileKeys.length > 0 && (
<Tooltip>
<TooltipTrigger
render={
<Toggle
aria-label={fileTreeOpen ? "Hide file tree" : "Show file tree"}
variant="ghost"
size="sm"
pressed={fileTreeOpen}
onPressedChange={(pressed) => setFileTreeOpen(Boolean(pressed))}
/>
}
>
<FolderTreeIcon className="size-3.5" />
</TooltipTrigger>
<TooltipPopup side="top">
{fileTreeOpen ? "Hide file tree" : "Show file tree"}
</TooltipPopup>
</Tooltip>
)}
</div>
</>
);
return (
<DiffPanelShell mode={mode} header={headerRow}>
{!activeThread ? (
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
Select a thread to inspect turn diffs.
</div>
) : !isGitRepo ? (
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
Turn diffs are unavailable because this project is not a git repository.
</div>
) : selectedTurnId !== null && orderedTurnDiffSummaries.length === 0 ? (
<div className="flex flex-1 items-center justify-center px-5 text-center text-xs text-muted-foreground/70">
No completed turns yet.
</div>
) : (
<>
<div className="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden bg-background">
{isSelectedPatchTruncated && !lazySource && (
<p className="shrink-0 border-b border-border/70 bg-muted/40 px-3 py-1.5 text-2xs text-muted-foreground">
This preview exceeds the size limit. Changes shown are incomplete.
{selectedGitSource?.files ? " Totals include all changes." : ""}
</p>
)}
{selectedPatchError && !renderablePatch && (
<div className="px-3">
<p className="mb-2 text-2xs text-error/80">{selectedPatchError}</p>
</div>
)}
{!renderablePatch && !lazySource ? (
isLoadingSelectedPatch ? (
<DiffPanelLoadingState
label={
selectedTurn
? "Loading checkpoint diff..."
: selectedGitScope === "unstaged"
? "Loading working tree diff..."
: "Loading branch diff..."
}
/>
) : (
<div className="flex h-full items-center justify-center px-3 py-2 text-xs text-muted-foreground/70">
<p>
{hasNoNetChanges
? "No net changes in this selection."
: "No patch available for this selection."}
</p>
</div>
)
) : lazySource || renderablePatch?.kind === "files" ? (
<div className="flex min-h-0 flex-1 overflow-hidden">
<div
className="min-h-0 min-w-0 flex-1"
onClickCapture={(event) => {
const composedPath = event.nativeEvent.composedPath?.() ?? [];
for (const node of composedPath) {
if (!(node instanceof HTMLElement)) continue;
// Header controls keep their own actions. In particular, the chevron must
// not also trigger the row handler or the two toggles cancel each other.
if (node instanceof HTMLButtonElement || node instanceof HTMLAnchorElement) {
return;
}
}
const title = composedPath.find(
(node): node is HTMLElement =>
node instanceof HTMLElement && node.hasAttribute("data-title"),
);
const filePath = title?.textContent;
// The filename remains the explicit "open in editor" affordance.
if (filePath) {
openDiffFile(filePath);
return;
}
const header = composedPath.find(
(node): node is HTMLElement =>
node instanceof HTMLElement && node.hasAttribute("data-diffs-header"),
);
const headerFilePath = header?.querySelector("[data-title]")?.textContent;
if (!headerFilePath) return;
const file = codeViewFiles.find(
(candidate) => candidate.filePath === headerFilePath,
);
if (file) toggleDiffFileCollapsed(file.fileKey);
}}
onContextMenuCapture={(event) => {
const composedPath = event.nativeEvent.composedPath?.() ?? [];
const title = composedPath.find(
(node): node is HTMLElement =>
node instanceof HTMLElement && node.hasAttribute("data-title"),
);
const filePath = title?.textContent?.trim();
if (!filePath) return;
event.preventDefault();
onFileContextMenu(
{
environmentId: activeThread?.environmentId ?? null,
filePath,
workspaceRoot: activeCwd,
repositoryRoot: activeRepositoryRoot,
},
event,
);
}}
>
<AnnotatableCodeView
key={collapseScopeKey ?? reviewSectionId}
viewerRef={setCodeView}
codeViewKey={`${codeViewMountKey}:${lazySource ? filePatchScope : "preview"}`}
className="h-full min-h-0 overflow-auto"
files={codeViewFiles}
renderCodeViewFooter={renderLoadingBoundary}
sectionId={reviewSectionId}
sectionTitle={reviewSectionTitle}
composerDraftTarget={composerDraftTarget}
renderHeaderFilenameSuffix={(fileDiff) => {
const path = resolveFileDiffPath(fileDiff);
const stat = fileStats.get(path);
return (
<>
<DiffFilePathCopyButton filePath={path} />
{stat ? (
<DiffFileStatus {...fileStates.get(path)} retry={() => retry(path)} />
) : null}
</>
);
}}
{...(lazySource
? {
unsafeCSSExtra:
"[data-additions-count], [data-deletions-count] { display: none; }",
renderHeaderMetadata: (fileDiff: FileDiffMetadata) => {
const stat = fileStats.get(resolveFileDiffPath(fileDiff));
return stat ? (
<DiffStatLabel
additions={stat.additions}
deletions={stat.deletions}
/>
) : null;
},
}
: {})}
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);
}}
/>
}
>
{collapsed ? (
<ChevronRightIcon
className={cn("size-4", getDiffCollapseIconClassName(fileDiff))}
/>
) : (
<ChevronDownIcon
className={cn("size-4", getDiffCollapseIconClassName(fileDiff))}
/>
)}
</TooltipTrigger>
<TooltipPopup side="top">
{collapsed ? "Expand diff" : "Collapse diff"}
</TooltipPopup>
</Tooltip>
);
}}
options={{
diffStyle: diffLayout === "split" ? "split" : "unified",
lineDiffType: "none",
overflow: wordWrap ? "wrap" : "scroll",
theme: resolveDiffThemeName(resolvedTheme),
preferredHighlighter: PREFERRED_HIGHLIGHTER,
themeType: resolvedTheme as DiffThemeType,
stickyHeaders: true,
...(currentLoadDiffFiles ? { loadDiffFiles } : {}),
}}
/>
</div>
{fileTreeOpen ? (
<aside className="flex w-[min(16rem,40%)] min-w-40 shrink-0 border-l border-border/60">
<DiffFileTree
ariaLabel={`${reviewSectionTitle} files`}
entries={fileTreeEntries}
selectedPath={selectedFilePath}
revealRequestId={selectedFileRevealRequestId}
onSelectFile={revealDiffFile}
/>
</aside>
) : null}
</div>
) : (
<div className="min-h-0 flex-1 overflow-auto p-2">
<div className="space-y-2">
<p className="text-2xs text-muted-foreground/75">
{renderablePatch?.kind === "raw" ? renderablePatch.reason : null}
</p>
<pre
className={cn(
"max-h-[72vh] rounded-md border border-border/70 bg-background/70 p-3 font-mono text-2xs leading-relaxed text-muted-foreground/90",
wordWrap
? "overflow-auto whitespace-pre-wrap wrap-break-word"
: "overflow-auto",
)}
>
{renderablePatch?.kind === "raw" ? renderablePatch.text : null}
</pre>
</div>
</div>
)}
</div>
</>
)}
</DiffPanelShell>
);
}