Skip to content

Commit a0fd206

Browse files
t3dotggbompusclaudejuliusmarminge
authored
fix(web): threads open at the newest message, not thousands of pixels above it (pingdotgg#19663)
Co-authored-by: Aaron Queen <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent bab2613 commit a0fd206

2 files changed

Lines changed: 106 additions & 2 deletions

File tree

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

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,72 @@ describe("MessagesTimeline history and thread links", () => {
930930
expect(onOpenThread).toHaveBeenCalledWith("thread-parent");
931931
});
932932

933+
it("opens a thread at its newest message after an empty pane and restores it on return", async () => {
934+
const props = baseProps();
935+
const entries = Array.from({ length: 12 }, (_, index) =>
936+
message(
937+
Array.from({ length: 60 }, (_, line) => `Paragraph ${index}.${line}`).join("\n\n"),
938+
{ role: "assistant" },
939+
`message-${index}`,
940+
),
941+
);
942+
await renderTimeline([], {
943+
...props,
944+
routeThreadKey: `${props.routeThreadKey}-loading`,
945+
hideEmptyPlaceholder: true,
946+
});
947+
948+
// Each row is much taller than LegendList's estimate.
949+
await act(async () =>
950+
roots.at(-1)!.render(<MessagesTimeline {...props} timelineEntries={entries} />),
951+
);
952+
const viewport = props.listRef.current!.getScrollableNode();
953+
await vi.waitFor(() => {
954+
expect(viewport.scrollHeight).toBeGreaterThan(viewport.clientHeight);
955+
expect(text()).toContain("Paragraph 11.59");
956+
expect(
957+
viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop,
958+
).toBeLessThanOrEqual(1);
959+
});
960+
961+
const otherThreadKey = `${props.routeThreadKey}-other`;
962+
await act(async () =>
963+
roots
964+
.at(-1)!
965+
.render(
966+
<MessagesTimeline
967+
{...props}
968+
routeThreadKey={otherThreadKey}
969+
timelineEntries={entries}
970+
liveFollowEnabled={false}
971+
/>,
972+
),
973+
);
974+
await vi.waitFor(() =>
975+
expect(
976+
viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop,
977+
).toBeLessThanOrEqual(1),
978+
);
979+
// Move beyond LegendList's one-viewport follow threshold so it cannot mask restoration.
980+
await userEvent.wheel(viewport, { delta: { y: -2000 } });
981+
await vi.waitFor(() => {
982+
expect(readTimelinePosition(otherThreadKey)?.atEnd).toBe(false);
983+
expect(viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop).toBeGreaterThan(
984+
viewport.clientHeight,
985+
);
986+
});
987+
988+
await act(async () =>
989+
roots.at(-1)!.render(<MessagesTimeline {...props} timelineEntries={entries} />),
990+
);
991+
await vi.waitFor(() => {
992+
expect(text()).toContain("Paragraph 11.59");
993+
expect(
994+
viewport.scrollHeight - viewport.clientHeight - viewport.scrollTop,
995+
).toBeLessThanOrEqual(1);
996+
});
997+
});
998+
933999
it("does not render the transient V2 interruption request", async () => {
9341000
await renderTimeline([
9351001
{
@@ -1299,7 +1365,7 @@ describe("MessagesTimeline scroll position", () => {
12991365
expect(maxNestedCommits).toBeLessThan(8);
13001366
});
13011367

1302-
it("remembers a manual reading position when the chat closes", async () => {
1368+
it("restores a manual reading position after reopening and switching threads", async () => {
13031369
const props = baseProps();
13041370
function Chat() {
13051371
const [liveFollowEnabled, setLiveFollowEnabled] = useState(true);
@@ -1326,6 +1392,26 @@ describe("MessagesTimeline scroll position", () => {
13261392
atEnd: false,
13271393
scrollOffset: offset,
13281394
});
1395+
1396+
const timeline = (routeThreadKey: string) => (
1397+
<MessagesTimeline {...props} routeThreadKey={routeThreadKey} timelineEntries={tallEntries} />
1398+
);
1399+
await renderElement(timeline(props.routeThreadKey));
1400+
const reopenedViewport = await waitForList(props);
1401+
await vi.waitFor(() =>
1402+
expect(Math.abs(reopenedViewport.scrollTop - offset)).toBeLessThanOrEqual(1),
1403+
);
1404+
1405+
await act(async () => roots.at(-1)!.render(timeline(`${props.routeThreadKey}-other`)));
1406+
await vi.waitFor(() =>
1407+
expect(
1408+
reopenedViewport.scrollHeight - reopenedViewport.clientHeight - reopenedViewport.scrollTop,
1409+
).toBeLessThanOrEqual(1),
1410+
);
1411+
await act(async () => roots.at(-1)!.render(timeline(props.routeThreadKey)));
1412+
await vi.waitFor(() =>
1413+
expect(Math.abs(reopenedViewport.scrollTop - offset)).toBeLessThanOrEqual(1),
1414+
);
13291415
});
13301416

13311417
it("restores the composer after closing tool output only at the end", async () => {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,22 @@ const ConversationTimeline = memo(function ConversationTimeline({
862862
// must not change with them or every timeline row re-renders per event.
863863
const runs = useStableHandoffRuns(runsProp);
864864
const minimapItems = deriveTimelineMinimapItems(rows);
865+
// Preserve initialScrollAtEnd's measured target until the list is reused
866+
// for another thread; scrollToEnd would replace it with an estimated offset.
867+
const listOpenedOnRef = useRef<{
868+
readonly list: LegendListRef;
869+
identity: string | null;
870+
} | null>(null);
871+
useLayoutEffect(() => {
872+
const list = listRef.current;
873+
const opened = listOpenedOnRef.current;
874+
if (!list) return;
875+
if (opened?.list !== list) {
876+
listOpenedOnRef.current = { list, identity: listIdentityKey };
877+
} else if (opened.identity !== listIdentityKey) {
878+
opened.identity = null;
879+
}
880+
});
865881
const restoreRowIndex =
866882
restoringThreadPosition && rememberedPosition?.atEnd === false
867883
? rows.findIndex((row) => row.id === rememberedPosition.rowId)
@@ -919,7 +935,9 @@ const ConversationTimeline = memo(function ConversationTimeline({
919935
viewOffset: -position.offsetWithinRow,
920936
})
921937
: list.scrollToOffset({ offset: position.scrollOffset, animated: false })
922-
: list.scrollToEnd({ animated: false });
938+
: listOpenedOnRef.current?.identity === listIdentityKey
939+
? undefined
940+
: list.scrollToEnd({ animated: false });
923941
void Promise.resolve(scrolling).then(() => {
924942
if (cancelled) return;
925943
if (position?.atEnd !== false || index < 0) {

0 commit comments

Comments
 (0)