fix: preserve staged and unstaged changes per file (#3104)
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import type { GitChangeLayer } from "@/lib/state/slices/session-runtime/types";
|
||||
|
||||
export type DiffSource = "uncommitted" | "committed" | "pr";
|
||||
export type ChangeLayer = GitChangeLayer;
|
||||
|
||||
export type LocalCommitDetailTarget = {
|
||||
source: "local";
|
||||
@@ -23,6 +26,7 @@ export type OpenDiffOptions = {
|
||||
source?: DiffSource;
|
||||
repositoryName?: string;
|
||||
prKey?: string;
|
||||
changeLayer?: ChangeLayer;
|
||||
};
|
||||
|
||||
export type DiffSheetMode =
|
||||
@@ -33,5 +37,6 @@ export type DiffSheetMode =
|
||||
sourceFilter?: "all" | DiffSource;
|
||||
repositoryName?: string;
|
||||
prKey?: string;
|
||||
changeLayer?: ChangeLayer;
|
||||
}
|
||||
| { kind: "commit"; target: CommitDetailTarget };
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useMemo } from "react";
|
||||
import { IconAlertTriangle } from "@tabler/icons-react";
|
||||
import { reviewFileKey } from "@/components/review/types";
|
||||
import type { GitChangeLayer } from "@/lib/state/slices/session-runtime/types";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
/** Composite key for the file currently selected in single-file mode. */
|
||||
@@ -8,13 +9,18 @@ export function useSelectedFileKey(
|
||||
mode: "all" | "file",
|
||||
filePath: string | undefined,
|
||||
fileRepositoryName: string | undefined,
|
||||
changeLayer?: GitChangeLayer,
|
||||
): string | undefined {
|
||||
return useMemo(
|
||||
() =>
|
||||
mode === "file" && filePath
|
||||
? reviewFileKey({ path: filePath, repository_name: fileRepositoryName })
|
||||
? reviewFileKey({
|
||||
path: filePath,
|
||||
repository_name: fileRepositoryName,
|
||||
change_layer: changeLayer,
|
||||
})
|
||||
: undefined,
|
||||
[mode, filePath, fileRepositoryName],
|
||||
[mode, filePath, fileRepositoryName, changeLayer],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,12 @@ describe("FileRow truncation (regression: path overlaps diff stats in narrow pan
|
||||
<TooltipProvider>
|
||||
<ul>
|
||||
<FileRow
|
||||
file={{ ...baseFile, path: "README.md", repositoryName: "frontend" }}
|
||||
file={{
|
||||
...baseFile,
|
||||
path: "README.md",
|
||||
repositoryName: "frontend",
|
||||
changeLayer: "staged",
|
||||
}}
|
||||
isPending={false}
|
||||
onSelect={noopSelect}
|
||||
onOpenDiff={onOpenDiff}
|
||||
@@ -115,6 +120,7 @@ describe("FileRow truncation (regression: path overlaps diff stats in narrow pan
|
||||
expect(onOpenDiff).toHaveBeenCalledWith("README.md", {
|
||||
source: "uncommitted",
|
||||
repositoryName: "frontend",
|
||||
changeLayer: "staged",
|
||||
});
|
||||
});
|
||||
|
||||
@@ -306,6 +312,36 @@ describe("FileRow tree-mode hover stage action", () => {
|
||||
expect(rightActions).not.toBeNull();
|
||||
expect(rightActions!.querySelector("button[title='Unstage file']")).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the tree-mode stage action visible and touch-sized for coarse pointers", () => {
|
||||
responsive.isFinePointer = false;
|
||||
|
||||
const { container } = render(
|
||||
<TooltipProvider>
|
||||
<ul>
|
||||
<FileRow
|
||||
file={{ ...baseFile, path: "src/mobile.go", staged: true }}
|
||||
isPending={false}
|
||||
treeMode
|
||||
onSelect={noopSelect}
|
||||
onOpenDiff={noop}
|
||||
onStage={noop}
|
||||
onUnstage={noop}
|
||||
onDiscard={noop}
|
||||
onEditFile={noop}
|
||||
/>
|
||||
</ul>
|
||||
</TooltipProvider>,
|
||||
);
|
||||
|
||||
const iconSlot = container.querySelector("[data-testid='file-row-icon-action-slot']");
|
||||
const action = iconSlot?.querySelector("button[title='Unstage file']");
|
||||
|
||||
expect(iconSlot?.className).toContain("size-11");
|
||||
expect(action?.className).toContain("min-h-11");
|
||||
expect(action?.className).toContain("min-w-11");
|
||||
expect(action?.parentElement?.className).not.toContain("opacity-0");
|
||||
});
|
||||
});
|
||||
|
||||
describe("FileRow active-tab highlight", () => {
|
||||
|
||||
@@ -21,6 +21,8 @@ import type { OpenDiffOptions } from "./changes-diff-target";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useResponsiveBreakpoint } from "@/hooks/use-responsive-breakpoint";
|
||||
|
||||
const COARSE_POINTER_TARGET_CLASS = "min-h-11 min-w-11";
|
||||
|
||||
const splitPath = (path: string) => {
|
||||
const lastSlash = path.lastIndexOf("/");
|
||||
if (lastSlash === -1) return { folder: "", file: path };
|
||||
@@ -85,6 +87,7 @@ export function FileRow({
|
||||
onOpenDiff(file.path, {
|
||||
source: "uncommitted",
|
||||
repositoryName: file.repositoryName,
|
||||
...(file.changeLayer ? { changeLayer: file.changeLayer } : {}),
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -112,6 +115,7 @@ export function FileRow({
|
||||
<TreeModeFileActionSlot
|
||||
name={name}
|
||||
isPending={isPending}
|
||||
isFinePointer={isFinePointer}
|
||||
staged={file.staged}
|
||||
path={file.path}
|
||||
repo={file.repositoryName}
|
||||
@@ -122,6 +126,7 @@ export function FileRow({
|
||||
<StageButton
|
||||
isPending={isPending}
|
||||
staged={file.staged}
|
||||
touchSized={!isFinePointer}
|
||||
path={file.path}
|
||||
repo={file.repositoryName}
|
||||
onStage={onStage}
|
||||
@@ -170,6 +175,7 @@ function FileRowStats({ file, isFinePointer }: { file: ChangedFile; isFinePointe
|
||||
function TreeModeFileActionSlot({
|
||||
name,
|
||||
isPending,
|
||||
isFinePointer,
|
||||
staged,
|
||||
path,
|
||||
repo,
|
||||
@@ -178,6 +184,7 @@ function TreeModeFileActionSlot({
|
||||
}: {
|
||||
name: string;
|
||||
isPending: boolean;
|
||||
isFinePointer: boolean;
|
||||
staged: boolean;
|
||||
path: string;
|
||||
repo?: string;
|
||||
@@ -187,16 +194,27 @@ function TreeModeFileActionSlot({
|
||||
return (
|
||||
<div
|
||||
data-testid="file-row-icon-action-slot"
|
||||
className="grid size-4 shrink-0 items-center justify-center [&>*]:col-start-1 [&>*]:row-start-1"
|
||||
className={cn(
|
||||
"grid shrink-0 items-center justify-center [&>*]:col-start-1 [&>*]:row-start-1",
|
||||
isFinePointer ? "size-4" : "size-11",
|
||||
)}
|
||||
>
|
||||
<FileIcon
|
||||
fileName={name}
|
||||
className="size-4 transition-opacity group-hover:opacity-0 pointer-events-none"
|
||||
/>
|
||||
<div className="opacity-0 transition-opacity pointer-events-none group-hover:opacity-100 group-hover:pointer-events-auto">
|
||||
{isFinePointer && (
|
||||
<FileIcon
|
||||
fileName={name}
|
||||
className="size-4 transition-opacity group-hover:opacity-0 pointer-events-none"
|
||||
/>
|
||||
)}
|
||||
<div
|
||||
className={cn(
|
||||
isFinePointer &&
|
||||
"opacity-0 transition-opacity pointer-events-none group-hover:opacity-100 group-hover:pointer-events-auto",
|
||||
)}
|
||||
>
|
||||
<StageButton
|
||||
isPending={isPending}
|
||||
staged={staged}
|
||||
touchSized={!isFinePointer}
|
||||
path={path}
|
||||
repo={repo}
|
||||
onStage={onStage}
|
||||
@@ -210,6 +228,7 @@ function TreeModeFileActionSlot({
|
||||
function StageButton({
|
||||
isPending,
|
||||
staged,
|
||||
touchSized,
|
||||
path,
|
||||
repo,
|
||||
onStage,
|
||||
@@ -217,6 +236,7 @@ function StageButton({
|
||||
}: {
|
||||
isPending: boolean;
|
||||
staged: boolean;
|
||||
touchSized?: boolean;
|
||||
path: string;
|
||||
repo?: string;
|
||||
onStage: (path: string, repo?: string) => void;
|
||||
@@ -225,7 +245,12 @@ function StageButton({
|
||||
const { t } = useTranslation();
|
||||
if (isPending) {
|
||||
return (
|
||||
<div className="flex-shrink-0 flex items-center justify-center size-4">
|
||||
<div
|
||||
className={cn(
|
||||
"flex-shrink-0 flex items-center justify-center size-4",
|
||||
touchSized && COARSE_POINTER_TARGET_CLASS,
|
||||
)}
|
||||
>
|
||||
<IconLoader2 className="h-3 w-3 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
);
|
||||
@@ -235,7 +260,10 @@ function StageButton({
|
||||
<button
|
||||
type="button"
|
||||
title={t("task:unstageFile")}
|
||||
className="group/unstage flex-shrink-0 flex items-center justify-center size-4 rounded bg-emerald-500/20 text-emerald-600 hover:bg-rose-500/20 hover:text-rose-600 cursor-pointer"
|
||||
className={cn(
|
||||
"group/unstage flex-shrink-0 flex items-center justify-center size-4 rounded bg-emerald-500/20 text-emerald-600 hover:bg-rose-500/20 hover:text-rose-600 cursor-pointer",
|
||||
touchSized && COARSE_POINTER_TARGET_CLASS,
|
||||
)}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onUnstage(path, repo);
|
||||
@@ -250,7 +278,10 @@ function StageButton({
|
||||
<button
|
||||
type="button"
|
||||
title={t("task:stageFile")}
|
||||
className="flex-shrink-0 flex items-center justify-center size-4 rounded border border-dashed border-muted-foreground/50 text-muted-foreground hover:border-emerald-500 hover:text-emerald-500 hover:bg-emerald-500/10 cursor-pointer"
|
||||
className={cn(
|
||||
"flex-shrink-0 flex items-center justify-center size-4 rounded border border-dashed border-muted-foreground/50 text-muted-foreground hover:border-emerald-500 hover:text-emerald-500 hover:bg-emerald-500/10 cursor-pointer",
|
||||
touchSized && COARSE_POINTER_TARGET_CLASS,
|
||||
)}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
onStage(path, repo);
|
||||
@@ -292,7 +323,7 @@ function FileRowActions({
|
||||
aria-label={t("task:discardChanges2")}
|
||||
className={cn(
|
||||
"text-muted-foreground hover:text-foreground cursor-pointer",
|
||||
!isFinePointer && "min-h-11 min-w-11",
|
||||
!isFinePointer && COARSE_POINTER_TARGET_CLASS,
|
||||
)}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
@@ -311,7 +342,7 @@ function FileRowActions({
|
||||
aria-label={t("common:edit")}
|
||||
className={cn(
|
||||
"text-muted-foreground hover:text-foreground cursor-pointer",
|
||||
!isFinePointer && "min-h-11 min-w-11",
|
||||
!isFinePointer && COARSE_POINTER_TARGET_CLASS,
|
||||
)}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
|
||||
@@ -6,7 +6,11 @@ import { useShallow } from "zustand/react/shallow";
|
||||
import { useAppStore } from "@/components/state-provider";
|
||||
import { useDockviewStore } from "@/lib/state/dockview-store";
|
||||
import type { AppState } from "@/lib/state/store";
|
||||
import type { FileInfo, GitStatusEntry } from "@/lib/state/slices/session-runtime/types";
|
||||
import type {
|
||||
FileChangeFacet,
|
||||
FileInfo,
|
||||
GitStatusEntry,
|
||||
} from "@/lib/state/slices/session-runtime/types";
|
||||
import { djb2Hash } from "@/lib/utils/hash";
|
||||
|
||||
type DockviewPanel = NonNullable<ReturnType<DockviewApi["getPanel"]>>;
|
||||
@@ -54,6 +58,18 @@ export function autoActivateChangesPanel(): ActivateChangesPanelResult {
|
||||
const fileFingerprintCache = new WeakMap<FileInfo, string>();
|
||||
const gitStatusFingerprintCache = new WeakMap<GitStatusEntry, GitStatusFingerprintCacheEntry>();
|
||||
|
||||
function changeFacetFingerprint(facet: FileChangeFacet | undefined): string {
|
||||
if (!facet) return "";
|
||||
return [
|
||||
facet.status,
|
||||
facet.additions ?? 0,
|
||||
facet.deletions ?? 0,
|
||||
facet.old_path ?? "",
|
||||
djb2Hash(facet.diff ?? ""),
|
||||
facet.diff_skip_reason ?? "",
|
||||
].join(":");
|
||||
}
|
||||
|
||||
function fileFingerprint(file: FileInfo): string {
|
||||
const cached = fileFingerprintCache.get(file);
|
||||
if (cached !== undefined) return cached;
|
||||
@@ -67,6 +83,8 @@ function fileFingerprint(file: FileInfo): string {
|
||||
file.old_path ?? "",
|
||||
djb2Hash(file.diff ?? ""),
|
||||
file.diff_skip_reason ?? "",
|
||||
changeFacetFingerprint(file.staged_change),
|
||||
changeFacetFingerprint(file.unstaged_change),
|
||||
file.repository_name ?? "",
|
||||
].join(":");
|
||||
fileFingerprintCache.set(file, fingerprint);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
buildPrByRepoMap,
|
||||
computeReviewProgress,
|
||||
mapToChangedFiles,
|
||||
mapPRFilesToChangedFiles,
|
||||
selectPRFilesForReviewProgress,
|
||||
type ReviewProgressPRFile,
|
||||
@@ -12,6 +13,27 @@ import type { PRDiffFile } from "@/lib/types/github";
|
||||
const PRIMARY_PR_ID = "primary-pr";
|
||||
const RENAMED_PATH = "src/renamed.ts";
|
||||
|
||||
describe("mapToChangedFiles", () => {
|
||||
it("preserves a projected mixed-change layer", () => {
|
||||
expect(
|
||||
mapToChangedFiles([
|
||||
{
|
||||
path: "src/mixed.ts",
|
||||
status: "modified",
|
||||
staged: true,
|
||||
change_layer: "staged",
|
||||
},
|
||||
]),
|
||||
).toEqual([
|
||||
expect.objectContaining({
|
||||
path: "src/mixed.ts",
|
||||
staged: true,
|
||||
changeLayer: "staged",
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
function diffFile(overrides: Partial<PRDiffFile>): PRDiffFile {
|
||||
return {
|
||||
filename: "src/app.ts",
|
||||
|
||||
@@ -10,7 +10,7 @@ import type { FileInfo } from "@/lib/state/store";
|
||||
import type { PRDiffFile } from "@/lib/types/github";
|
||||
import { normalizeFileChangeStatus, type FileChangeStatus } from "@/lib/utils/file-change-status";
|
||||
import type { PRChangedFile } from "./changes-panel-timeline";
|
||||
import type { CommitDetailTarget } from "./changes-diff-target";
|
||||
import type { ChangeLayer, CommitDetailTarget } from "./changes-diff-target";
|
||||
import type { CommitPresentation } from "./commit-row";
|
||||
|
||||
export type ChangedFile = {
|
||||
@@ -22,6 +22,7 @@ export type ChangedFile = {
|
||||
oldPath: string | undefined;
|
||||
/** Repository this file belongs to in multi-repo workspaces; empty for single-repo. */
|
||||
repositoryName?: string;
|
||||
changeLayer?: ChangeLayer;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -62,6 +63,7 @@ export function mapToChangedFiles(files: FileInfo[]): ChangedFile[] {
|
||||
minus: file.deletions,
|
||||
oldPath: file.old_path,
|
||||
repositoryName: file.repository_name,
|
||||
changeLayer: file.change_layer,
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
@@ -118,6 +118,8 @@ function DiffViewerContent({
|
||||
const selectedRepositoryName =
|
||||
panelKind === "file" ? (params?.repositoryName as string | undefined) : undefined;
|
||||
const selectedPRKey = panelKind === "file" ? (params?.prKey as string | undefined) : undefined;
|
||||
const selectedChangeLayer =
|
||||
panelKind === "file" ? (params?.changeLayer as OpenDiffOptions["changeLayer"]) : undefined;
|
||||
const sourceFilter = ((params?.source as string) || "all") as "all" | ReviewSource;
|
||||
const panelSelectedDiff = panelKind === "all" ? selectedDiff : null;
|
||||
const handleClosePanel = useCallback(() => {
|
||||
@@ -132,6 +134,7 @@ function DiffViewerContent({
|
||||
filePath={selectedPath}
|
||||
fileRepositoryName={selectedRepositoryName}
|
||||
prKey={selectedPRKey}
|
||||
changeLayer={selectedChangeLayer}
|
||||
sourceFilter={sourceFilter}
|
||||
selectedDiff={panelSelectedDiff}
|
||||
onClearSelected={() => setSelectedDiff(null)}
|
||||
@@ -171,6 +174,7 @@ function ChangesContent({ panelId }: { panelId: string }) {
|
||||
source: options?.source,
|
||||
repositoryName: options?.repositoryName,
|
||||
prKey: options?.prKey,
|
||||
changeLayer: options?.changeLayer,
|
||||
}),
|
||||
[addFileDiffPanel],
|
||||
);
|
||||
|
||||
@@ -294,6 +294,8 @@ function DiffViewerContent({
|
||||
const selectedRepositoryName =
|
||||
panelKind === "file" ? (params?.repositoryName as string | undefined) : undefined;
|
||||
const selectedPRKey = panelKind === "file" ? (params?.prKey as string | undefined) : undefined;
|
||||
const selectedChangeLayer =
|
||||
panelKind === "file" ? (params?.changeLayer as OpenDiffOptions["changeLayer"]) : undefined;
|
||||
const sourceFilter = ((params?.source as string) || "all") as "all" | ReviewSource;
|
||||
const panelSelectedDiff = panelKind === "all" ? selectedDiff : null;
|
||||
useResyncGitStatusOnTabActivate(panelId, activeSessionId);
|
||||
@@ -309,6 +311,7 @@ function DiffViewerContent({
|
||||
filePath={selectedPath}
|
||||
fileRepositoryName={selectedRepositoryName}
|
||||
prKey={selectedPRKey}
|
||||
changeLayer={selectedChangeLayer}
|
||||
sourceFilter={sourceFilter}
|
||||
selectedDiff={panelSelectedDiff}
|
||||
onClearSelected={() => setSelectedDiff(null)}
|
||||
@@ -352,6 +355,7 @@ function ChangesContent({ panelId }: { panelId: string }) {
|
||||
source: options?.source,
|
||||
repositoryName: options?.repositoryName,
|
||||
prKey: options?.prKey,
|
||||
changeLayer: options?.changeLayer,
|
||||
}),
|
||||
[addFileDiffPanel],
|
||||
);
|
||||
|
||||
@@ -86,6 +86,7 @@ export const MobileChangesPanel = memo(function MobileChangesPanel({
|
||||
sourceFilter: options?.source ?? "all",
|
||||
repositoryName: options?.repositoryName || undefined,
|
||||
prKey: options?.prKey,
|
||||
changeLayer: options?.changeLayer,
|
||||
});
|
||||
}, []);
|
||||
|
||||
|
||||
@@ -178,6 +178,7 @@ function renderPanel(
|
||||
const filePath = mode.kind === "file" ? mode.path : undefined;
|
||||
const fileRepositoryName = mode.kind === "file" ? mode.repositoryName : undefined;
|
||||
const prKey = mode.kind === "file" ? mode.prKey : undefined;
|
||||
const changeLayer = mode.kind === "file" ? mode.changeLayer : undefined;
|
||||
const effectiveSourceFilter = mode.kind === "all" ? activeSource : (mode.sourceFilter ?? "all");
|
||||
return (
|
||||
<TaskChangesPanel
|
||||
@@ -185,6 +186,7 @@ function renderPanel(
|
||||
filePath={filePath}
|
||||
fileRepositoryName={fileRepositoryName}
|
||||
prKey={prKey}
|
||||
changeLayer={changeLayer}
|
||||
selectedDiff={selectedDiff}
|
||||
onClearSelected={onClearSelected}
|
||||
onOpenFile={onOpenFile}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { renderHook } from "@testing-library/react";
|
||||
import { hashDiff, reviewFileKey, type ReviewFile } from "@/components/review/types";
|
||||
import { useSelectedFileKey } from "./changes-panel-banner";
|
||||
import {
|
||||
computeChangesReviewSets,
|
||||
filterVisibleFiles,
|
||||
reviewDiffHashForKey,
|
||||
useVisibleDiffState,
|
||||
} from "./task-changes-panel-state";
|
||||
|
||||
const MIXED_PATH = "mixed.ts";
|
||||
const STAGED_DIFF = "staged diff";
|
||||
const UNSTAGED_DIFF = "unstaged diff";
|
||||
|
||||
function mixedFile(): ReviewFile {
|
||||
return {
|
||||
path: MIXED_PATH,
|
||||
source: "uncommitted",
|
||||
status: "modified",
|
||||
staged: false,
|
||||
additions: 2,
|
||||
deletions: 0,
|
||||
diff: "combined",
|
||||
staged_change: {
|
||||
status: "modified",
|
||||
additions: 1,
|
||||
deletions: 0,
|
||||
diff: STAGED_DIFF,
|
||||
},
|
||||
unstaged_change: {
|
||||
status: "modified",
|
||||
additions: 1,
|
||||
deletions: 0,
|
||||
diff: UNSTAGED_DIFF,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function projectLayer(file: ReviewFile, changeLayer: "staged" | "unstaged"): ReviewFile {
|
||||
const [projected] = filterVisibleFiles([file], {
|
||||
mode: "file",
|
||||
filePath: MIXED_PATH,
|
||||
fileRepositoryName: undefined,
|
||||
sourceFilter: "uncommitted",
|
||||
changeLayer,
|
||||
});
|
||||
return projected;
|
||||
}
|
||||
|
||||
describe("filterVisibleFiles mixed layers", () => {
|
||||
it("projects the requested layer for a mixed uncommitted file", () => {
|
||||
expect(projectLayer(mixedFile(), "staged")).toEqual(
|
||||
expect.objectContaining({
|
||||
diff: STAGED_DIFF,
|
||||
staged: true,
|
||||
change_layer: "staged",
|
||||
additions: 1,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps staged and unstaged review identities independent", () => {
|
||||
const mixed = mixedFile();
|
||||
const staged = projectLayer(mixed, "staged");
|
||||
const unstaged = projectLayer(mixed, "unstaged");
|
||||
|
||||
expect(reviewFileKey(staged)).not.toBe(reviewFileKey(unstaged));
|
||||
});
|
||||
|
||||
it("selects the layer-qualified review row", () => {
|
||||
const { result } = renderHook(() =>
|
||||
useSelectedFileKey("file", MIXED_PATH, undefined, "staged"),
|
||||
);
|
||||
|
||||
expect(result.current).toBe(reviewFileKey({ path: MIXED_PATH, change_layer: "staged" }));
|
||||
});
|
||||
});
|
||||
|
||||
describe("mixed layer review state", () => {
|
||||
it("restores review state against the selected layer diff", () => {
|
||||
const mixed = mixedFile();
|
||||
const staged = projectLayer(mixed, "staged");
|
||||
const stagedKey = reviewFileKey(staged);
|
||||
|
||||
const state = computeChangesReviewSets(
|
||||
[mixed],
|
||||
new Map([[stagedKey, { reviewed: true, diffHash: hashDiff(staged.diff) }]]),
|
||||
);
|
||||
|
||||
expect(state.reviewedFiles).toEqual(new Set([stagedKey]));
|
||||
expect(state.staleFiles).toEqual(new Set());
|
||||
});
|
||||
|
||||
it("persists the selected layer diff hash", () => {
|
||||
const mixed = mixedFile();
|
||||
const staged = projectLayer(mixed, "staged");
|
||||
|
||||
expect(reviewDiffHashForKey([mixed], reviewFileKey(staged))).toBe(hashDiff(staged.diff));
|
||||
});
|
||||
|
||||
it("keys the selected layer ref by its review identity", () => {
|
||||
const mixed = mixedFile();
|
||||
const ref = { current: null };
|
||||
const fileRefs = new Map([[reviewFileKey(mixed), ref]]);
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useVisibleDiffState({
|
||||
allFiles: [mixed],
|
||||
rawPRFiles: [],
|
||||
mode: "file",
|
||||
filePath: MIXED_PATH,
|
||||
fileRepositoryName: undefined,
|
||||
sourceFilter: "uncommitted",
|
||||
prKey: undefined,
|
||||
changeLayer: "staged",
|
||||
fileRefs,
|
||||
reviewedFiles: new Set(),
|
||||
staleFiles: new Set(),
|
||||
}),
|
||||
);
|
||||
const [visible] = result.current.visibleFiles;
|
||||
|
||||
expect(result.current.visibleFileRefs.get(reviewFileKey(visible))).toBe(ref);
|
||||
});
|
||||
});
|
||||
@@ -1,8 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef, type MutableRefObject, type RefObject } from "react";
|
||||
import { reviewFileKey, splitReviewFileKey, type ReviewFile } from "@/components/review/types";
|
||||
import { hashDiff, reviewFileKey, type ReviewFile } from "@/components/review/types";
|
||||
import type { ReviewSource } from "@/hooks/domains/session/use-review-sources";
|
||||
import type { GitChangeLayer } from "@/lib/state/slices/session-runtime/types";
|
||||
|
||||
export function shouldDeferReviewStateForPR(
|
||||
hasSelectedPR: boolean,
|
||||
@@ -104,13 +105,84 @@ type FilterVisibleFilesOpts = {
|
||||
sourceFilter: "all" | ReviewSource;
|
||||
rawPRFiles?: ReviewFile[];
|
||||
prKey?: string;
|
||||
changeLayer?: GitChangeLayer;
|
||||
};
|
||||
|
||||
function projectReviewFileLayer(file: ReviewFile, layer: GitChangeLayer): ReviewFile | null {
|
||||
if (file.source !== "uncommitted") return file;
|
||||
const facet = layer === "staged" ? file.staged_change : file.unstaged_change;
|
||||
if (!facet) {
|
||||
if (file.staged !== (layer === "staged")) return null;
|
||||
return { ...file, change_layer: layer };
|
||||
}
|
||||
return {
|
||||
...file,
|
||||
...facet,
|
||||
staged: layer === "staged",
|
||||
change_layer: layer,
|
||||
};
|
||||
}
|
||||
|
||||
type PersistedReviewState = { reviewed: boolean; diffHash: string };
|
||||
|
||||
function reviewStateFiles(files: ReviewFile[]): ReviewFile[] {
|
||||
const stateFiles: ReviewFile[] = [];
|
||||
for (const file of files) {
|
||||
stateFiles.push(file);
|
||||
if (
|
||||
file.source !== "uncommitted" ||
|
||||
file.change_layer ||
|
||||
!file.staged_change ||
|
||||
!file.unstaged_change
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
for (const layer of ["staged", "unstaged"] as const) {
|
||||
const projected = projectReviewFileLayer(file, layer);
|
||||
if (projected) stateFiles.push(projected);
|
||||
}
|
||||
}
|
||||
return stateFiles;
|
||||
}
|
||||
|
||||
export function computeChangesReviewSets(
|
||||
files: ReviewFile[],
|
||||
reviews: ReadonlyMap<string, PersistedReviewState>,
|
||||
): { reviewedFiles: Set<string>; staleFiles: Set<string> } {
|
||||
const reviewedFiles = new Set<string>();
|
||||
const staleFiles = new Set<string>();
|
||||
for (const file of reviewStateFiles(files)) {
|
||||
const key = reviewFileKey(file);
|
||||
const reviewState = reviews.get(key);
|
||||
if (!reviewState?.reviewed) continue;
|
||||
const currentHash = hashDiff(file.diff);
|
||||
if (reviewState.diffHash && reviewState.diffHash !== currentHash) staleFiles.add(key);
|
||||
else reviewedFiles.add(key);
|
||||
}
|
||||
return { reviewedFiles, staleFiles };
|
||||
}
|
||||
|
||||
export function reviewDiffHashForKey(files: ReviewFile[], key: string): string {
|
||||
const file = reviewStateFiles(files).find((candidate) => reviewFileKey(candidate) === key);
|
||||
return file ? hashDiff(file.diff) : "";
|
||||
}
|
||||
|
||||
function projectRequestedLayer(
|
||||
files: ReviewFile[],
|
||||
mode: FilterVisibleFilesOpts["mode"],
|
||||
changeLayer: GitChangeLayer | undefined,
|
||||
): ReviewFile[] {
|
||||
if (mode !== "file" || !changeLayer) return files;
|
||||
return files
|
||||
.map((file) => projectReviewFileLayer(file, changeLayer))
|
||||
.filter((file): file is ReviewFile => file !== null);
|
||||
}
|
||||
|
||||
export function filterVisibleFiles(
|
||||
allFiles: ReviewFile[],
|
||||
opts: FilterVisibleFilesOpts,
|
||||
): ReviewFile[] {
|
||||
const { mode, filePath, fileRepositoryName, sourceFilter, rawPRFiles, prKey } = opts;
|
||||
const { mode, filePath, fileRepositoryName, sourceFilter, rawPRFiles, prKey, changeLayer } = opts;
|
||||
const repositoryFilter = prKey && sourceFilter === "pr" ? undefined : fileRepositoryName;
|
||||
if (mode === "file" && filePath && sourceFilter === "pr" && rawPRFiles?.length) {
|
||||
let prFiles = rawPRFiles.filter((file) => file.path === filePath && file.source === "pr");
|
||||
@@ -128,7 +200,7 @@ export function filterVisibleFiles(
|
||||
}
|
||||
}
|
||||
if (sourceFilter !== "all") files = files.filter((file) => file.source === sourceFilter);
|
||||
return files;
|
||||
return projectRequestedLayer(files, mode, changeLayer);
|
||||
}
|
||||
|
||||
export function useVisibleDiffState(opts: {
|
||||
@@ -139,6 +211,7 @@ export function useVisibleDiffState(opts: {
|
||||
fileRepositoryName: string | undefined;
|
||||
sourceFilter: "all" | ReviewSource;
|
||||
prKey: string | undefined;
|
||||
changeLayer: GitChangeLayer | undefined;
|
||||
fileRefs: Map<string, RefObject<HTMLDivElement | null>>;
|
||||
reviewedFiles: Set<string>;
|
||||
staleFiles: Set<string>;
|
||||
@@ -151,6 +224,7 @@ export function useVisibleDiffState(opts: {
|
||||
fileRepositoryName,
|
||||
sourceFilter,
|
||||
prKey,
|
||||
changeLayer,
|
||||
fileRefs,
|
||||
reviewedFiles,
|
||||
staleFiles,
|
||||
@@ -164,23 +238,21 @@ export function useVisibleDiffState(opts: {
|
||||
sourceFilter,
|
||||
rawPRFiles,
|
||||
prKey,
|
||||
changeLayer,
|
||||
}),
|
||||
[allFiles, mode, filePath, fileRepositoryName, sourceFilter, rawPRFiles, prKey],
|
||||
[allFiles, mode, filePath, fileRepositoryName, sourceFilter, rawPRFiles, prKey, changeLayer],
|
||||
);
|
||||
const repositoryFilter = prKey && sourceFilter === "pr" ? undefined : fileRepositoryName;
|
||||
const visibleFileRefs = useMemo(() => {
|
||||
if (mode !== "file" || !filePath) return fileRefs;
|
||||
const refs = new Map<string, RefObject<HTMLDivElement | null>>();
|
||||
for (const [key, ref] of fileRefs.entries()) {
|
||||
const split = splitReviewFileKey(key);
|
||||
if (split.path !== filePath) continue;
|
||||
if (repositoryFilter !== undefined && (split.repositoryName || "") !== repositoryFilter) {
|
||||
continue;
|
||||
}
|
||||
refs.set(key, ref);
|
||||
for (const file of visibleFiles) {
|
||||
const visibleKey = reviewFileKey(file);
|
||||
const rawKey = reviewFileKey({ path: file.path, repository_name: file.repository_name });
|
||||
const ref = fileRefs.get(visibleKey) ?? fileRefs.get(rawKey);
|
||||
if (ref) refs.set(visibleKey, ref);
|
||||
}
|
||||
return refs;
|
||||
}, [mode, filePath, fileRefs, repositoryFilter]);
|
||||
}, [mode, filePath, fileRefs, visibleFiles]);
|
||||
const reviewedCount = useMemo(
|
||||
() =>
|
||||
visibleFiles.reduce((count, file) => {
|
||||
|
||||
@@ -17,12 +17,14 @@ import { ReviewDiffList } from "@/components/review/review-diff-list";
|
||||
import { ReviewPRDiffBoundary } from "@/components/review/review-dialog-pr-state";
|
||||
import { DEFAULT_DIFF_WORD_WRAP } from "@/components/diff/diff-defaults";
|
||||
import type { ReviewFile } from "@/components/review/types";
|
||||
import { hashDiff, reviewFileKey, splitReviewFileKey } from "@/components/review/types";
|
||||
import { reviewFileKey, splitReviewFileKey } from "@/components/review/types";
|
||||
import { usePanelActions } from "@/hooks/use-panel-actions";
|
||||
import { useRequestChangesWalkthrough } from "@/hooks/domains/session/use-request-changes-walkthrough";
|
||||
import { ChangesTopBar } from "./changes-top-bar";
|
||||
import {
|
||||
computeChangesReviewSets,
|
||||
resolveSelectedFileRepositoryName,
|
||||
reviewDiffHashForKey,
|
||||
shouldBlockChangesForPR,
|
||||
shouldDeferReviewStateForPR,
|
||||
useAutoCloseWhenEmpty,
|
||||
@@ -31,11 +33,13 @@ import {
|
||||
import type { SelectedDiff } from "./task-layout";
|
||||
import { useIsTaskArchived, ArchivedPanelPlaceholder } from "./task-archived-context";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type { GitChangeLayer } from "@/lib/state/slices/session-runtime/types";
|
||||
|
||||
type TaskChangesPanelProps = {
|
||||
mode?: "all" | "file";
|
||||
filePath?: string;
|
||||
fileRepositoryName?: string;
|
||||
changeLayer?: GitChangeLayer;
|
||||
prKey?: string;
|
||||
selectedDiff: SelectedDiff | null;
|
||||
onClearSelected: () => void;
|
||||
@@ -118,18 +122,7 @@ function useChangesView(
|
||||
if (shouldDeferReviewStateForPR(Boolean(pr), prDiffLoading, sourceFilter)) {
|
||||
return { reviewedFiles: reviewed, staleFiles: stale };
|
||||
}
|
||||
for (const file of allFiles) {
|
||||
const key = reviewFileKey(file);
|
||||
const reviewState = reviews.get(key);
|
||||
if (!reviewState?.reviewed) continue;
|
||||
const currentHash = hashDiff(file.diff);
|
||||
if (reviewState.diffHash && reviewState.diffHash !== currentHash) {
|
||||
stale.add(key);
|
||||
} else {
|
||||
reviewed.add(key);
|
||||
}
|
||||
}
|
||||
return { reviewedFiles: reviewed, staleFiles: stale };
|
||||
return computeChangesReviewSets(allFiles, reviews);
|
||||
}, [allFiles, reviews, pr, prDiffLoading, sourceFilter]);
|
||||
|
||||
const totalCommentCount = useMemo(() => {
|
||||
@@ -210,6 +203,7 @@ function useChangesPRPresentation(opts: {
|
||||
mode: "all" | "file";
|
||||
filePath: string | undefined;
|
||||
fileRepositoryName: string | undefined;
|
||||
changeLayer: GitChangeLayer | undefined;
|
||||
visibleFiles: ReviewFile[];
|
||||
}) {
|
||||
const visibleSelectedFile = opts.visibleFiles.find((file) => file.path === opts.filePath);
|
||||
@@ -222,6 +216,7 @@ function useChangesPRPresentation(opts: {
|
||||
opts.fileRepositoryName,
|
||||
visibleSelectedFile?.repository_name,
|
||||
),
|
||||
opts.changeLayer,
|
||||
);
|
||||
const blockChangesForPR = shouldBlockChangesForPR(opts.sourceFilter, opts.visibleFiles);
|
||||
return { selectedFileKey, blockChangesForPR };
|
||||
@@ -258,8 +253,7 @@ function useChangesActions(
|
||||
const handleToggleReviewed = useCallback(
|
||||
(key: string, reviewed: boolean) => {
|
||||
if (reviewed) {
|
||||
const file = allFiles.find((f) => reviewFileKey(f) === key);
|
||||
markReviewed(key, file ? hashDiff(file.diff) : "");
|
||||
markReviewed(key, reviewDiffHashForKey(allFiles, key));
|
||||
} else {
|
||||
markUnreviewed(key);
|
||||
}
|
||||
@@ -338,6 +332,7 @@ const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
mode = "all",
|
||||
filePath,
|
||||
fileRepositoryName,
|
||||
changeLayer,
|
||||
prKey,
|
||||
selectedDiff,
|
||||
onClearSelected,
|
||||
@@ -355,14 +350,13 @@ const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
const relevantPRLoading = usesPRDiff && view.prDiffLoading;
|
||||
const actions = useChangesActions(view.activeSessionId, view.allFiles, wordWrapProp);
|
||||
const handleRequestWalkthrough = useWalkthroughRequest(view.activeSessionId, view.allFiles);
|
||||
const fileTarget = { filePath, fileRepositoryName, prKey, changeLayer };
|
||||
const visible = useVisibleDiffState({
|
||||
allFiles: view.allFiles,
|
||||
rawPRFiles: view.rawPRFiles,
|
||||
mode,
|
||||
filePath,
|
||||
fileRepositoryName,
|
||||
...fileTarget,
|
||||
sourceFilter,
|
||||
prKey,
|
||||
fileRefs: view.fileRefs,
|
||||
reviewedFiles: view.reviewedFiles,
|
||||
staleFiles: view.staleFiles,
|
||||
@@ -373,6 +367,7 @@ const TaskChangesPanel = memo(function TaskChangesPanel({
|
||||
mode,
|
||||
filePath,
|
||||
fileRepositoryName,
|
||||
changeLayer,
|
||||
visibleFiles: visible.visibleFiles,
|
||||
});
|
||||
useAutoCloseWhenEmpty({
|
||||
|
||||
Reference in New Issue
Block a user