fix(vcs): collapsing a file in the all-files diff keeps its unsaved edits (#3739)

Collapsing a block unmounted its diff editor and released the working-tree document, which with autosave off drops its unsaved edits. A loaded block now keeps its diff while collapsed, hidden.
This commit is contained in:
l0ng-ai
2026-11-16 18:43:09 +08:00
committed by GitHub
parent c871c164c1
commit f74bc7abac
3 changed files with 20 additions and 12 deletions
+3
View File
@@ -71,6 +71,9 @@
display: flex;
flex-direction: column;
}
.body[hidden] {
display: none;
}
/* Space a block takes before its diff loads, so the scrollbar is roughly right. */
.placeholder {
height: 160px;
+9 -8
View File
@@ -98,14 +98,15 @@ function Block({
/>
)}
</div>
{!collapsed &&
(near ? (
<div className={styles.body}>
<DiffEditor input={input} fit />
</div>
) : (
<div className={styles.placeholder} />
))}
{/* A collapsed block keeps its loaded diff, hidden: unmounting it would release the
working-tree document and, with autosave off, drop its unsaved edits. */}
{near ? (
<div className={styles.body} hidden={collapsed}>
<DiffEditor input={input} fit />
</div>
) : (
!collapsed && <div className={styles.placeholder} />
)}
</section>
);
}
@@ -83,20 +83,24 @@ describe("the all-in-one diff tab", () => {
expect(screen.getAllByText("src")).toHaveLength(2);
});
it("collapses a file and all files", () => {
it("collapses a file and all files, keeping their diffs (and unsaved edits) loaded", () => {
render(<CombinedDiffTab input={input} />);
const shown = () => screen.queryAllByTestId("block-diff").filter((b) => !b.closest("[hidden]"));
const first = screen.getAllByTestId("block-diff")[0]!;
act(() => {
fireEvent.click(screen.getAllByRole("button", { name: "Collapse File" })[0]!);
});
expect(screen.getAllByTestId("block-diff")).toHaveLength(2);
expect(shown()).toHaveLength(2);
act(() => {
fireEvent.click(screen.getByRole("button", { name: "Collapse All Files" }));
});
expect(screen.queryAllByTestId("block-diff")).toHaveLength(0);
expect(shown()).toHaveLength(0);
act(() => {
fireEvent.click(screen.getByRole("button", { name: "Expand All Files" }));
});
expect(screen.getAllByTestId("block-diff")).toHaveLength(3);
expect(shown()).toHaveLength(3);
// The same diff all along: collapsing never released its document.
expect(screen.getAllByTestId("block-diff")[0]).toBe(first);
});
it("switches every block between side-by-side and unified", () => {