Skip to content

Commit a4154c6

Browse files
fix(web): distinguish pending rename drafts by both paths
Clear replaced rename drafts when a new rename shares the destination. Preserve valid lazy reloads across provisional placeholder change types.
1 parent 2cdc7f2 commit a4154c6

3 files changed

Lines changed: 77 additions & 20 deletions

File tree

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ export default function DiffPanel({
559559
lazySource ? readyFilePaths : null,
560560
collapsedDiffFileKeys,
561561
);
562-
const pendingFilePaths = renderableFileEntries
562+
const pendingFiles = renderableFileEntries
563563
.filter(({ fileDiff }) => fileDiff.cacheKey?.endsWith(":pending"))
564-
.map(({ fileDiff }) => resolveFileDiffPath(fileDiff));
564+
.map(({ fileDiff }) => fileDiff);
565565
const allDiffFilesCollapsed = areAllDiffFilesCollapsed(diffFileKeys, collapsedDiffFileKeys);
566566
const diffLineStat =
567567
!selectedTurn && selectedGitSource?.files
@@ -1138,7 +1138,7 @@ export default function DiffPanel({
11381138
codeViewKey={`${codeViewMountKey}:${lazySource ? filePatchScope : "preview"}`}
11391139
className="h-full min-h-0 overflow-auto"
11401140
files={codeViewFiles}
1141-
pendingFilePaths={pendingFilePaths}
1141+
pendingFiles={pendingFiles}
11421142
renderCodeViewFooter={renderLoadingBoundary}
11431143
sectionId={reviewSectionId}
11441144
sectionTitle={reviewSectionTitle}

‎apps/web/src/components/diffs/AnnotatableCodeView.test.tsx‎

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import type { CodeViewDiffItem, DiffLineAnnotation, SelectedLineRange } from "@pierre/diffs";
1+
import type {
2+
CodeViewDiffItem,
3+
DiffLineAnnotation,
4+
FileDiffMetadata,
5+
SelectedLineRange,
6+
} from "@pierre/diffs";
27
import { parsePatchFiles } from "@pierre/diffs/utils/parsePatchFiles";
38
import { act, StrictMode, type ReactNode } from "react";
49
import { create, type ReactTestRenderer } from "react-test-renderer";
510
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
611

712
import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
13+
import { buildFileDiffIdentityKey } from "~/lib/diffRendering";
814

915
type Selection = { id: string; range: SelectedLineRange } | null;
1016
interface ViewerProps {
@@ -44,25 +50,38 @@ const target = DraftId.make("diff-draft-test");
4450
const range: SelectedLineRange = { start: 1, end: 1, side: "additions" };
4551
let renderer: ReactTestRenderer;
4652

47-
function file(name: string, fileVersion = 1) {
53+
function file(
54+
name: string,
55+
fileVersion = 1,
56+
type: FileDiffMetadata["type"] = "change",
57+
previousName?: string,
58+
) {
4859
const fileDiff = parsePatchFiles(
4960
`diff --git a/${name} b/${name}\n--- a/${name}\n+++ b/${name}\n@@ -1 +1 @@\n-old\n+new\n`,
5061
name,
5162
)[0]!.files[0]!;
52-
return { fileDiff, filePath: name, fileKey: name, fileVersion, collapsed: false };
63+
fileDiff.type = type;
64+
if (previousName) fileDiff.prevName = previousName;
65+
return {
66+
fileDiff,
67+
filePath: name,
68+
fileKey: buildFileDiffIdentityKey(fileDiff),
69+
fileVersion,
70+
collapsed: false,
71+
};
5372
}
5473

5574
function view(
5675
files: ReturnType<typeof file>[],
5776
codeViewKey = "scope:initial",
58-
pendingFilePaths: string[] = [],
77+
pendingFiles: FileDiffMetadata[] = [],
5978
) {
6079
return (
6180
<StrictMode>
6281
<AnnotatableCodeView
6382
codeViewKey={codeViewKey}
6483
files={files}
65-
pendingFilePaths={pendingFilePaths}
84+
pendingFiles={pendingFiles}
6685
sectionId="unstaged"
6786
sectionTitle="Uncommitted"
6887
composerDraftTarget={target}
@@ -136,19 +155,51 @@ describe("diff comment drafts", () => {
136155
act(() => renderer.update(view([file("a.ts", 2)], "scope:refreshed")));
137156

138157
expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("Keep this draft");
139-
expect(viewer().selectedLines).toEqual({ id: "a.ts", range });
158+
expect(viewer().selectedLines).toEqual({ id: file("a.ts").fileKey, range });
140159
expect(viewer().options.enableLineSelection).toBe(false);
141160
expect(viewer().options.enableGutterUtility).toBe(false);
142161
});
143162

144-
it("preserves a draft while its file's patch reloads", () => {
163+
it.each(["change", "new", "deleted", "rename-pure", "rename-changed"] as const)(
164+
"preserves a %s draft while its patch reloads",
165+
(type) => {
166+
const previousName = type.startsWith("rename") ? "old.ts" : undefined;
167+
const original = file("a.ts", 1, type, previousName);
168+
act(() => renderer.update(view([original, file("b.ts")])));
169+
beginComment();
170+
const pending = {
171+
...original.fileDiff,
172+
type: previousName ? ("rename-changed" as const) : ("change" as const),
173+
hunks: [],
174+
cacheKey: "scope:a.ts:pending",
175+
};
176+
177+
act(() => renderer.update(view([file("b.ts")], "scope:loading", [pending])));
178+
act(() =>
179+
renderer.update(view([file("a.ts", 2, type, previousName), file("b.ts")], "scope:loaded")),
180+
);
181+
182+
expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("Keep this draft");
183+
expect(viewer().selectedLines).toEqual({ id: original.fileKey, range });
184+
expect(viewer().options.enableGutterUtility).toBe(false);
185+
},
186+
);
187+
188+
it("releases a replaced rename's draft when the replacement patch is unavailable", () => {
189+
const original = file("shared.ts", 1, "rename-changed", "old.ts");
190+
act(() => renderer.update(view([original, file("b.ts")])));
145191
beginComment();
192+
const replacement = file("shared.ts", 2, "rename-changed", "other.ts");
193+
const pending = { ...replacement.fileDiff, hunks: [], cacheKey: "scope:shared.ts:pending" };
146194

147-
act(() => renderer.update(view([file("b.ts")], "scope:loading", ["a.ts"])));
148-
act(() => renderer.update(view([file("a.ts", 2), file("b.ts")], "scope:loaded")));
195+
// Failed lazy queries retain their placeholder metadata in the pending list.
196+
act(() => renderer.update(view([file("b.ts")], "scope:failed", [pending])));
149197

150-
expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("Keep this draft");
151-
expect(viewer().selectedLines).toEqual({ id: "a.ts", range });
152-
expect(viewer().options.enableGutterUtility).toBe(false);
198+
expect(renderer.root.findAllByType(DiffCommentAnnotation)).toHaveLength(0);
199+
expect(viewer().selectedLines).toBeNull();
200+
expect(viewer().options.enableLineSelection).toBe(true);
201+
expect(viewer().options.enableGutterUtility).toBe(true);
202+
act(() => viewer().options.onGutterUtilityClick(range, { item: viewer().items[0]! }));
203+
expect(renderer.root.findByType(DiffCommentAnnotation).props.text).toBe("");
153204
});
154205
});

‎apps/web/src/components/diffs/AnnotatableCodeView.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { ScopedThreadRef } from "@t3tools/contracts";
1111
import { useState, type ReactNode, type Ref } from "react";
1212

1313
import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";
14-
import { fnv1a32, resolveFileDiffPath } from "~/lib/diffRendering";
14+
import { fnv1a32, resolveFileDiffPath, resolveFileDiffPreviousPath } from "~/lib/diffRendering";
1515
import {
1616
buildDiffReviewComment,
1717
restoreDiffReviewCommentRange,
@@ -80,8 +80,8 @@ interface AnnotatableCodeViewProps {
8080
fileVersion: number;
8181
collapsed: boolean;
8282
}>;
83-
/** Files still in the diff whose patches have not loaded. */
84-
pendingFilePaths: ReadonlyArray<string>;
83+
/** Files with unavailable patches; placeholder change types are provisional. */
84+
pendingFiles: ReadonlyArray<FileDiffMetadata>;
8585
sectionId: string;
8686
sectionTitle: string;
8787
composerDraftTarget: ScopedThreadRef | DraftId;
@@ -107,7 +107,7 @@ interface DiffSelectionContext {
107107
export function AnnotatableCodeView({
108108
codeViewKey,
109109
files,
110-
pendingFilePaths,
110+
pendingFiles,
111111
sectionId,
112112
sectionTitle,
113113
composerDraftTarget,
@@ -133,6 +133,7 @@ export function AnnotatableCodeView({
133133
const [draft, setDraft] = useState<{
134134
fileKey: string;
135135
filePath: string;
136+
previousFilePath: string;
136137
annotation: DiffCommentLineAnnotation;
137138
} | null>(null);
138139
const [draftText, setDraftText] = useState("");
@@ -141,7 +142,11 @@ export function AnnotatableCodeView({
141142
if (
142143
draft !== null &&
143144
!filesByKey.has(draft.fileKey) &&
144-
!pendingFilePaths.includes(draft.filePath)
145+
!pendingFiles.some(
146+
(file) =>
147+
resolveFileDiffPath(file) === draft.filePath &&
148+
resolveFileDiffPreviousPath(file) === draft.previousFilePath,
149+
)
145150
) {
146151
setDraft(null);
147152
setDraftText("");
@@ -238,6 +243,7 @@ export function AnnotatableCodeView({
238243
setDraft({
239244
fileKey: item.id,
240245
filePath,
246+
previousFilePath: resolveFileDiffPreviousPath(item.fileDiff),
241247
annotation: {
242248
side: annotationSide(range),
243249
lineNumber: range.end,

0 commit comments

Comments
 (0)