Skip to content

Commit e9b07f6

Browse files
committed
fix(web): preserve both patches for file type changes
1 parent bd8c34c commit e9b07f6

3 files changed

Lines changed: 61 additions & 41 deletions

File tree

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ export default function DiffPanel({
599599
retry,
600600
requestFile,
601601
renderableFiles,
602-
settledFileCount,
602+
remainingFileCount,
603603
loadNextFiles,
604604
} = useReviewFilePatches({
605605
environmentId: activeThread?.environmentId,
@@ -672,11 +672,8 @@ export default function DiffPanel({
672672
.updateCollapsedFileKeys(collapseScopeKey, defaultCollapsedDiffFileKeys, update);
673673
};
674674
const renderLoadingBoundary = () =>
675-
settledFileCount < renderableFiles.length ? (
676-
<DiffFileLoadingBoundary
677-
load={loadNextFiles}
678-
count={renderableFiles.length - settledFileCount}
679-
/>
675+
remainingFileCount > 0 ? (
676+
<DiffFileLoadingBoundary load={loadNextFiles} count={remainingFileCount} />
680677
) : null;
681678
const codeViewFiles = shownFileEntries.map(({ fileDiff, fileKey, fileVersion }) => ({
682679
fileDiff,
@@ -704,9 +701,7 @@ export default function DiffPanel({
704701
next.delete(file.fileKey);
705702
return next;
706703
});
707-
if (lazySource && index >= settledFileCount) {
708-
requestFile(index);
709-
}
704+
if (lazySource) requestFile(filePath);
710705
codeViewReveal.request(file.fileKey);
711706
return true;
712707
};
@@ -753,8 +748,8 @@ export default function DiffPanel({
753748
});
754749
};
755750
const toggleDiffFileCollapsed = useStableCallback((fileKey: string) => {
756-
const index = renderableFileEntries.findIndex((file) => file.fileKey === fileKey);
757-
if (lazySource && index >= 0) requestFile(index);
751+
const file = renderableFileEntries.find((file) => file.fileKey === fileKey);
752+
if (lazySource && file) requestFile(resolveFileDiffPath(file.fileDiff));
758753
updateCollapsedDiffFiles((current) => {
759754
const next = new Set(current);
760755
if (next.has(fileKey)) {
@@ -1476,12 +1471,7 @@ export default function DiffPanel({
14761471
onToggle={toggleDiffFileCollapsed}
14771472
{...(fileDiff.cacheKey?.endsWith(":pending")
14781473
? {
1479-
load: () =>
1480-
requestFile(
1481-
renderableFileEntries.findIndex(
1482-
(file) => file.fileKey === fileKey,
1483-
),
1484-
),
1474+
load: () => requestFile(resolveFileDiffPath(fileDiff)),
14851475
}
14861476
: {})}
14871477
/>

‎apps/web/src/components/diffs/useReviewFilePatches.browser.test.tsx‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ function Probe({ source }: { source: ReviewDiffPreviewSource }) {
5757
});
5858
return (
5959
<>
60-
<button onClick={() => files.requestFile(source.files!.findIndex((f) => f.path === "f.txt"))}>
61-
Open f
62-
</button>
60+
<button onClick={() => files.requestFile("f.txt")}>Open f</button>
61+
<output>Pending files: {files.remainingFileCount}</output>
6362
{files.renderableFiles.map((file) => (
64-
<div key={file.name}>
65-
{file.name}:{file.hunks.length ? "loaded" : "pending"}
63+
<div key={`${file.name}:${file.type}`}>
64+
<span>
65+
{file.name}:{file.hunks.length ? "loaded" : "pending"}
66+
</span>
67+
<span>{file.type}</span>
6668
</div>
6769
))}
6870
</>
@@ -116,3 +118,28 @@ it("retains automatic and explicitly opened files through metadata insertion, re
116118
act(() => registry.set(atom, loaded));
117119
await expect.element(screen.getByText("f.txt:loaded")).toBeVisible();
118120
});
121+
122+
it("preserves deletion and addition patches for a file replaced by a symlink", async () => {
123+
state.patches.clear();
124+
const registry = AtomRegistry.make();
125+
using _registry = { [Symbol.dispose]: () => registry.dispose() };
126+
const metadata = source(["entry"], "type-change");
127+
await using screen = await render(
128+
<RegistryContext.Provider value={registry}>
129+
<Probe source={metadata} />
130+
</RegistryContext.Provider>,
131+
);
132+
const atom = [...state.patches.values()][0]!;
133+
act(() =>
134+
registry.set(
135+
atom,
136+
AsyncResult.success({
137+
...metadata,
138+
diff: "diff --git a/entry b/entry\ndeleted file mode 100644\n--- a/entry\n+++ /dev/null\n@@ -1 +0,0 @@\n-before\ndiff --git a/entry b/entry\nnew file mode 120000\n--- /dev/null\n+++ b/entry\n@@ -0,0 +1 @@\n+target\n\\ No newline at end of file\n",
139+
}),
140+
),
141+
);
142+
await expect.element(screen.getByText("deleted", { exact: true })).toBeVisible();
143+
await expect.element(screen.getByText("new", { exact: true })).toBeVisible();
144+
await expect.element(screen.getByText("Pending files: 0")).toBeVisible();
145+
});

‎apps/web/src/components/diffs/useReviewFilePatches.ts‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,34 @@ export function useReviewFilePatches({
101101
if (queries.some(({ index }) => index === settledFileCount)) return;
102102
requestFiles(Array.from({ length: 4 }, (_, index) => settledFileCount + index));
103103
};
104-
const requestFile = (index: number) => requestFiles([index]);
104+
const requestFile = (path: string) =>
105+
requestFiles([files.findIndex((file) => file.path === path)]);
105106
const renderableFiles = source
106-
? files.map((file, index): FileDiffMetadata => {
107+
? files.flatMap((file, index): FileDiffMetadata[] => {
107108
const result = patches.get(index);
108109
const value = result && Option.getOrUndefined(AsyncResult.value(result));
109110
if (value?.patch?.kind === "files") {
110-
const loaded = value.patch.files.find(
111+
const loaded = value.patch.files.filter(
111112
(candidate) => resolveFileDiffPath(candidate) === file.path,
112113
);
113-
if (loaded) return loaded;
114+
if (loaded.length > 0) return loaded;
114115
}
115-
return {
116-
name: file.path,
117-
...(file.previousPath ? { prevName: file.previousPath } : {}),
118-
type: file.previousPath ? "rename-changed" : "change",
119-
hunks: [],
120-
additionLines: [],
121-
deletionLines: [],
122-
additionCharacterCount: 0,
123-
deletionCharacterCount: 0,
124-
splitLineCount: 0,
125-
unifiedLineCount: 0,
126-
isPartial: true,
127-
cacheKey: `${scope}:${file.path}:pending`,
128-
};
116+
return [
117+
{
118+
name: file.path,
119+
...(file.previousPath ? { prevName: file.previousPath } : {}),
120+
type: file.previousPath ? "rename-changed" : "change",
121+
hunks: [],
122+
additionLines: [],
123+
deletionLines: [],
124+
additionCharacterCount: 0,
125+
deletionCharacterCount: 0,
126+
splitLineCount: 0,
127+
unifiedLineCount: 0,
128+
isPartial: true,
129+
cacheKey: `${scope}:${file.path}:pending`,
130+
},
131+
];
129132
})
130133
: (preview?.kind === "files" ? preview.files : []).toSorted((a, b) =>
131134
filePathCollator.compare(resolveFileDiffPath(a), resolveFileDiffPath(b)),
@@ -154,7 +157,7 @@ export function useReviewFilePatches({
154157
retry,
155158
requestFile,
156159
renderableFiles,
157-
settledFileCount,
160+
remainingFileCount: source ? files.length - settledFileCount : 0,
158161
loadNextFiles,
159162
};
160163
}

0 commit comments

Comments
 (0)