TypeScript
fix(web): scrolling a huge thread's history no longer loops the message list (#21269)
Julius Marminge1114c3815ba1
diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsxindex 71e6cf5d042..5c6e62d114f 100644--- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx@@ -8,7 +8,7 @@ import {ThreadId,} from "@t3tools/contracts";import type { LegendListRef } from "@legendapp/list/react";-import { act, createRef, useState, type ComponentProps, type ReactElement } from "react";+import { act, createRef, Profiler, useState, type ComponentProps, type ReactElement } from "react";import { createRoot, type Root } from "react-dom/client";import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";import { userEvent } from "vite-plus/test/browser";@@ -1165,6 +1165,48 @@ describe("MessagesTimeline scroll position", () => {return props.listRef.current!.getScrollableNode()!;}+ it("settles when earlier short replies load above long ones", async () => {+ const props = baseProps();+ const short = Array.from({ length: 80 }, (_, index) =>+ message(`Short reply ${index}`, { role: "assistant" }, `short-${index}`),+ );+ const long = Array.from({ length: 4 }, (_, index) =>+ message(+ `${"Long handbook paragraph. ".repeat(40)}\n\n`.repeat(60),+ { role: "assistant" },+ `long-${index}`,+ ),+ );+ let nestedCommits = 0;+ let maxNestedCommits = 0;+ const onRender = (_id: string, phase: string) => {+ nestedCommits = phase === "nested-update" ? nestedCommits + 1 : 0;+ maxNestedCommits = Math.max(maxNestedCommits, nestedCommits);+ };+ const timeline = (entries: Entry[]) => (+ <Profiler id="timeline" onRender={onRender}>+ <MessagesTimeline {...props} timelineEntries={entries} />+ </Profiler>+ );+ await renderElement(timeline(long));+ const viewport = await waitForList(props);+ await act(async () => {+ viewport.scrollTop = 0;+ viewport.dispatchEvent(new Event("scroll"));+ });++ maxNestedCommits = 0;+ await act(async () => roots.at(-1)!.render(timeline([...short, ...long])));+ await vi.waitFor(async () => {+ await act(async () => {+ viewport.scrollTop = 0;+ viewport.dispatchEvent(new Event("scroll"));+ });+ expect(text()).toContain("Short reply 0");+ });+ expect(maxNestedCommits).toBeLessThan(8);+ });+it("remembers a manual reading position when the chat closes", async () => {const props = baseProps();function Chat() {diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex e8fd2954e2f..f40580f3a60 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -1540,8 +1540,17 @@ function keyExtractor(item: MessagesTimelineRow) {return item.id;}+// LegendList estimates unmeasured rows from the running average size of their+// type. One type for every assistant reply lets a few very long replies, or+// one that is streaming, lift the estimate for short replies to thousands of+// pixels. Rows then mount one at a time, each measurement shrinks the list+// and mounts the next row in the same commit, and scrolling into unmeasured+// history exceeds React's nested update limit. Doubling-length buckets keep+// each average within about 2x of the rows it estimates.function getItemType(item: MessagesTimelineRow) {- return item.kind === "message" ? `message:${item.message.role}` : item.kind;+ if (item.kind !== "message") return item.kind;+ const lengthBucket = Math.max(0, Math.floor(Math.log2(item.message.text.length / 1000)));+ return `message:${item.message.role}:${lengthBucket}`;}interface TimelinePositionState {