Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 103 additions & 54 deletions apps/web/src/components/DiffPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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={{
Expand Down
12 changes: 6 additions & 6 deletions apps/web/src/components/diffs/AnnotatableCodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: "",
});
Expand All @@ -242,7 +242,7 @@ export function AnnotatableCodeView({
},
});
},
[filesByKey, sectionId, sectionTitle],
[sectionId, sectionTitle],
);

const hasOpenComment = draft !== null;
Expand Down
55 changes: 36 additions & 19 deletions apps/web/src/components/diffs/useReviewFilePatches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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(
() =>
Expand Down Expand Up @@ -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(
() =>
Expand Down
Loading