TypeScript
fix(web): threads open at the newest message, not thousands of pixels above it (#19663)
Theo Brownea0fd2063868d
diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsxindex 8b251da3be2..79013513dc1 100644--- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx@@ -930,6 +930,72 @@ describe("MessagesTimeline history and thread links", () => {expect(onOpenThread).toHaveBeenCalledWith("thread-parent");});+ it("opens a thread at its newest message after an empty pane and restores it on return", async () => {+ const props = baseProps();+ const entries = Array.from({ length: 12 }, (_, index) =>+ message(+ Array.from({ length: 60 }, (_, line) => `Paragraph ${index}.${line}`).join("\n\n"),+ { role: "assistant" },+ `message-${index}`,+ ),+ );+ await renderTimeline([], {+ ...props,+ routeThreadKey: `${props.routeThreadKey}-loading`,+ hideEmptyPlaceholder: true,+ });++ // Each row is much taller than LegendList's estimate.+ await act(async () =>+ roots.at(-1)!.render(<MessagesTimeline {...props} timelineEntries={entries} />),+ );+ const viewport = props.listRef.current!.getScrollableNode();+ await vi.waitFor(() => {+ expect(viewport.scrollHeight).toBeGreaterThan(viewport.clientHeight);+ expect(text()).toContain("Paragraph 11.59");+ expect(+ viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop,+ ).toBeLessThanOrEqual(1);+ });++ const otherThreadKey = `${props.routeThreadKey}-other`;+ await act(async () =>+ roots+ .at(-1)!+ .render(+ <MessagesTimeline+ {...props}+ routeThreadKey={otherThreadKey}+ timelineEntries={entries}+ liveFollowEnabled={false}+ />,+ ),+ );+ await vi.waitFor(() =>+ expect(+ viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop,+ ).toBeLessThanOrEqual(1),+ );+ // Move beyond LegendList's one-viewport follow threshold so it cannot mask restoration.+ await userEvent.wheel(viewport, { delta: { y: -2000 } });+ await vi.waitFor(() => {+ expect(readTimelinePosition(otherThreadKey)?.atEnd).toBe(false);+ expect(viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop).toBeGreaterThan(+ viewport.clientHeight,+ );+ });++ await act(async () =>+ roots.at(-1)!.render(<MessagesTimeline {...props} timelineEntries={entries} />),+ );+ await vi.waitFor(() => {+ expect(text()).toContain("Paragraph 11.59");+ expect(+ viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop,+ ).toBeLessThanOrEqual(1);+ });+ });+it("does not render the transient V2 interruption request", async () => {await renderTimeline([{@@ -1299,7 +1365,7 @@ describe("MessagesTimeline scroll position", () => {expect(maxNestedCommits).toBeLessThan(8);});- it("remembers a manual reading position when the chat closes", async () => {+ it("restores a manual reading position after reopening and switching threads", async () => {const props = baseProps();function Chat() {const [liveFollowEnabled, setLiveFollowEnabled] = useState(true);@@ -1326,6 +1392,26 @@ describe("MessagesTimeline scroll position", () => {atEnd: false,scrollOffset: offset,});++ const timeline = (routeThreadKey: string) => (+ <MessagesTimeline {...props} routeThreadKey={routeThreadKey} timelineEntries={tallEntries} />+ );+ await renderElement(timeline(props.routeThreadKey));+ const reopenedViewport = await waitForList(props);+ await vi.waitFor(() =>+ expect(Math.abs(reopenedViewport.scrollTop - offset)).toBeLessThanOrEqual(1),+ );++ await act(async () => roots.at(-1)!.render(timeline(`${props.routeThreadKey}-other`)));+ await vi.waitFor(() =>+ expect(+ reopenedViewport.scrollHeight - reopenedViewport.clientHeight - reopenedViewport.scrollTop,+ ).toBeLessThanOrEqual(1),+ );+ await act(async () => roots.at(-1)!.render(timeline(props.routeThreadKey)));+ await vi.waitFor(() =>+ expect(Math.abs(reopenedViewport.scrollTop - offset)).toBeLessThanOrEqual(1),+ );});it("restores the composer after closing tool output only at the end", async () => {diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex aed65120ea0..02ba86e463d 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -862,6 +862,22 @@ const ConversationTimeline = memo(function ConversationTimeline({// must not change with them or every timeline row re-renders per event.const runs = useStableHandoffRuns(runsProp);const minimapItems = deriveTimelineMinimapItems(rows);+ // Preserve initialScrollAtEnd's measured target until the list is reused+ // for another thread; scrollToEnd would replace it with an estimated offset.+ const listOpenedOnRef = useRef<{+ readonly list: LegendListRef;+ identity: string | null;+ } | null>(null);+ useLayoutEffect(() => {+ const list = listRef.current;+ const opened = listOpenedOnRef.current;+ if (!list) return;+ if (opened?.list !== list) {+ listOpenedOnRef.current = { list, identity: listIdentityKey };+ } else if (opened.identity !== listIdentityKey) {+ opened.identity = null;+ }+ });const restoreRowIndex =restoringThreadPosition && rememberedPosition?.atEnd === false? rows.findIndex((row) => row.id === rememberedPosition.rowId)@@ -919,7 +935,9 @@ const ConversationTimeline = memo(function ConversationTimeline({viewOffset: -position.offsetWithinRow,}): list.scrollToOffset({ offset: position.scrollOffset, animated: false })- : list.scrollToEnd({ animated: false });+ : listOpenedOnRef.current?.identity === listIdentityKey+ ? undefined+ : list.scrollToEnd({ animated: false });void Promise.resolve(scrolling).then(() => {if (cancelled) return;if (position?.atEnd !== false || index < 0) {