Skip to content
TypeScript

fix(web): collapsing a changed-files folder no longer leaves a one-frame gap (#22311)

Yuruzuue1e60080b99f
diff --git a/apps/web/src/components/chat/ChangedFilesTree.tsx b/apps/web/src/components/chat/ChangedFilesTree.tsx
index 868484f37b4..6b49b81401f 100644
--- a/apps/web/src/components/chat/ChangedFilesTree.tsx
+++ b/apps/web/src/components/chat/ChangedFilesTree.tsx
@@ -1,3 +1,4 @@
+import { useSyncLayout } from "@legendapp/list/react";
import { type OrchestrationV2CheckpointFileTotals, type RunId } from "@t3tools/contracts";
import { type MouseEvent, type ReactNode, memo, useLayoutEffect, useRef, useState } from "react";
import { type TurnDiffFileChange } from "../../types";
@@ -45,6 +46,7 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
onOpenTurnDiff,
onFileContextMenu,
} = props;
+ const syncTimelineLayout = useSyncLayout();
const hiddenFileCount = totals.count - files.length;
const hasDirectories = files.some((file) => /[/\\]/.test(file.path));
@@ -83,7 +85,10 @@ export const ChangedFilesCard = memo(function ChangedFilesCard(props: {
allDirectoriesExpanded ? "Collapse all folders" : "Expand all folders"
}
data-scroll-anchor-ignore
- onClick={onToggleAllDirectories}
+ onClick={() => {
+ onToggleAllDirectories();
+ syncTimelineLayout();
+ }}
/>
}
>
@@ -157,6 +162,7 @@ const ChangedFilesTree = memo(function ChangedFilesTree(props: {
key: string | null;
overrides: Record<string, boolean>;
}>({ key: null, overrides: {} });
+ const syncTimelineLayout = useSyncLayout();
const treeNodes = buildTurnDiffTree(files);
const directoryPathsKey = collectDirectoryPaths(treeNodes).join("\u0000");
const hasDirectoryNodes = directoryPathsKey.length > 0;
@@ -177,6 +183,8 @@ const ChangedFilesTree = memo(function ChangedFilesTree(props: {
},
};
});
+ // Measure before paint instead of waiting for LegendList's deferred shrink measurement.
+ syncTimelineLayout();
};
const rows = collectExpandedRows(treeNodes, expandedDirectories, allDirectoriesExpanded);
diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
index 212f3e253ce..7fd73739d91 100644
--- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
+++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx
@@ -1,3 +1,4 @@
+import { flushSync } from "react-dom";
import { AudioPreview } from "../files/AudioPreview";
import { page } from "vite-plus/test/browser";
import {
@@ -15,8 +16,15 @@ import {
ThreadId,
type OrchestrationV2Subagent,
} from "@t3tools/contracts";
-import type { LegendListRef } from "@legendapp/list/react";
-import { createRef, Profiler, StrictMode, type ComponentProps, type ReactElement } from "react";
+import { LegendList, type LegendListRef } from "@legendapp/list/react";
+import {
+ createRef,
+ Profiler,
+ StrictMode,
+ useState,
+ type ComponentProps,
+ type ReactElement,
+} from "react";
import * as DateTime from "effect/DateTime";
import { Atom } from "effect/reactivity";
import { describe, expect, it, onTestFinished, vi } from "vite-plus/test";
@@ -25,6 +33,7 @@ import "~/index.css";
import { environmentThreadDetails } from "../../state/threads";
import { act, fakeTimers, render, stubGlobal } from "../../../test/render";
+import { ChangedFilesCard } from "./ChangedFilesTree";
import { MessagesTimeline } from "./MessagesTimeline";
import { makeStreamingTimelineFixture } from "../../test-fixtures";
import {
@@ -1192,6 +1201,73 @@ it("renders automatic compaction as one live divider that finishes with its reas
expect(container.textContent).not.toContain("Compacting before");
});
+function TimelineChangedFilesCard() {
+ const [expanded, setExpanded] = useState(true);
+ return (
+ <ChangedFilesCard
+ runId={RunId.make("run-1")}
+ files={["src/a.ts", "src/b.ts", "src/c.ts"].map((path) => ({
+ path,
+ kind: "modified",
+ additions: 1,
+ deletions: 0,
+ }))}
+ totals={{ count: 3, additions: 3, deletions: 0 }}
+ allDirectoriesExpanded={expanded}
+ resolvedTheme="light"
+ onToggleAllDirectories={() => setExpanded((value) => !value)}
+ onOpenTurnDiff={() => {}}
+ />
+ );
+}
+
+function ChangedFilesTimeline() {
+ return (
+ <LegendList
+ data={["files", "after"]}
+ keyExtractor={(item) => item}
+ estimatedItemSize={100}
+ recycleItems={false}
+ className="h-[600px]"
+ renderItem={({ item }) =>
+ item === "files" ? <TimelineChangedFilesCard /> : <div data-testid="after">After</div>
+ }
+ />
+ );
+}
+
+describe("ChangedFilesCard", () => {
+ it.each(["folder", "all folders"])(
+ "measures %s collapse and expansion in the same commit",
+ async (toggle) => {
+ await using screen = await render(<ChangedFilesTimeline />);
+ const after = () =>
+ screen.container.querySelector('[data-testid="after"]')!.getBoundingClientRect().top;
+ const cardBottom = () =>
+ screen.container.querySelector("[data-changed-files-state]")!.getBoundingClientRect()
+ .bottom;
+ await vi.waitFor(() => expect(after()).toBe(cardBottom()));
+ const topBefore = after();
+ const button = () =>
+ screen.container.querySelector<HTMLButtonElement>(
+ toggle === "folder"
+ ? '[data-changed-file-row="src"] button'
+ : 'button[aria-label$="all folders"]',
+ )!;
+ await act(() => {
+ flushSync(() => button().click());
+ expect(screen.container.textContent).not.toContain("a.ts");
+ expect(after()).toBe(topBefore - 3 * 28);
+ });
+ await act(() => {
+ flushSync(() => button().click());
+ expect(screen.container.textContent).toContain("a.ts");
+ expect(after()).toBe(topBefore);
+ });
+ },
+ );
+});
+
it("keeps concurrent approvals actionable outside collapsed work and restores settled rows", async () => {
setup();
const fixture = makeStreamingTimelineFixture();