Skip to content

Commit 1ea8476

Browse files
PR Batch TesternibarAhmedclaude
committed
fix(web): timeline rows reach the DOM in order before the first paint
LegendList sorted row elements into item order on a 500ms timer, so after a reload or thread switch the browser painted rows out of order first. The pinned first message got its container after the rows at the end, so its element sat after them. NVDA's browse mode keeps the order it reads first, and wrapping landed mid-thread. The list now bumps a counter whenever a row takes a new item or index, and sorts the rows in a layout effect on that counter, before the browser paints. Only the react builds change; mobile's list is unchanged. Co-authored-by: nibarAhmed <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 67885e8 commit 1ea8476

3 files changed

Lines changed: 304 additions & 23 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
55

66
import "~/index.css";
77

8-
import { act, render } from "../../../test/render";
8+
import { act, fakeTimers, render } from "../../../test/render";
99
import { MessagesTimeline } from "./MessagesTimeline";
1010
import { makeStreamingTimelineFixture } from "../../test-fixtures";
1111
import { deriveTimelineEntriesFromVisibleTurnItems } from "../../session-logic";
@@ -225,6 +225,47 @@ describe("MessagesTimeline messages", () => {
225225
await expectHeadingInView("Last turn", "Turn 118:");
226226
});
227227

228+
it("keeps rows in item order after a reload or thread switch, before any timer runs", async () => {
229+
setup();
230+
// NVDA's browse mode keeps the first DOM order it reads, so a timer that sorts rows later is too late.
231+
using _timers = fakeTimers();
232+
const thread = (name: string) =>
233+
Array.from({ length: 120 }, (_, index) =>
234+
message(
235+
`${name} turn ${index}:\n\n${"x ".repeat(200)}`,
236+
{ role: index % 2 === 0 ? "user" : "assistant" },
237+
`${name}-${index}`,
238+
),
239+
);
240+
const props = baseProps();
241+
await using screen = await renderElement(
242+
<MessagesTimeline {...props} timelineEntries={thread("A")} />,
243+
);
244+
const turnsInDomOrder = () =>
245+
[...screen.container.querySelectorAll("h3")].map((heading) =>
246+
Number(/turn (\d+):/.exec(heading.parentElement?.textContent ?? "")?.[1]),
247+
);
248+
const expectItemOrder = () => {
249+
const turns = turnsInDomOrder();
250+
expect(turns).toEqual(turns.toSorted((a, b) => a - b));
251+
};
252+
253+
await vi.waitFor(() => expect(screen.text()).toContain("A turn 118:"));
254+
expect(turnsInDomOrder()).toContain(0);
255+
expectItemOrder();
256+
257+
await screen.rerender(
258+
<MessagesTimeline
259+
{...props}
260+
routeThreadKey={`${props.routeThreadKey}-other`}
261+
timelineEntries={thread("B")}
262+
/>,
263+
);
264+
await vi.waitFor(() => expect(screen.text()).toContain("B turn 118:"));
265+
expect(screen.text()).not.toContain("A turn");
266+
expectItemOrder();
267+
});
268+
228269
it("preserves XML-like tags and comparisons in user messages as text", async () => {
229270
const source = [
230271
'Without reading a file, do you have <global-agent-instructions scope="workspace">',

0 commit comments

Comments
 (0)