Skip to content

Commit cd5e9f9

Browse files
fix(web): timeline viewport emits one navigation signal, and find reveals its match after a thread switch
The timeline's scroll input (wheel, touch, scrollbar pointer, scroll keys, embedded-frame scroll) was heard separately by the follow hook, the position restore and MessagesTimeline, each with its own listener effect and its own "manual navigation" callback threaded through ChatView and GuestPage. When the list switched to a thread with find already open, the position restore and find's reveal both scrolled the list and the restore won, so the remembered row showed instead of the match. timelineViewport.ts owns the scroll node's listeners from the list's ref callback and emits one TimelineNavigation signal. Follow subscribes to it (onViewportAttach), the restore stops on it, and follow mode lives in a small store read with useSyncExternalStore. Find reveals its match only after the opening restore settles. Opening find announces navigation from the find store's open action instead of an effect on findOpen. Effects: MessagesTimeline 10 -> 1, useTimelineFollow 6 -> 1, useTimelinePositionRestore 2 -> 0. The kept ones are the layout-effect position restore (must land before paint after LegendList's layout effects) and the anchor release scroll that follows the commit dropping the anchored end space. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0712398 commit cd5e9f9

11 files changed

Lines changed: 1164 additions & 731 deletions

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,9 +1999,7 @@ export default function ChatView(props: ChatViewProps) {
19991999
const {
20002000
showScrollToBottom,
20012001
timelineLiveFollowEnabled,
2002-
cancelPositionRestoreRef,
20032002
isTimelineAtLogicalEndStable,
2004-
cancelTimelineLiveFollowForUserNavigationStable,
20052003
onComposerPageScrollKeyDownStable,
20062004
onComposerPageScrollKeyUpStable,
20072005
onComposerPageScrollReleaseStable,
@@ -2011,14 +2009,14 @@ export default function ChatView(props: ChatViewProps) {
20112009
onToolOutputCollapsedAtEndStable,
20122010
onIsAtEndChangeStable,
20132011
anchorNewTurn,
2012+
attachTimelineViewport,
2013+
attachComposerOverlay: attachFollowComposerOverlay,
20142014
} = useTimelineFollow({
20152015
legendListRef,
20162016
composerRef,
2017-
composerOverlayElement,
20182017
composerTimelineInset,
20192018
routeThreadKey,
20202019
activeThreadKey,
2021-
activeThreadId: activeThread?.id,
20222020
isServerThread,
20232021
serverProjection,
20242022
threadStatus,
@@ -3365,8 +3363,7 @@ export default function ChatView(props: ChatViewProps) {
33653363
onIsAtEndChange={onIsAtEndChangeStable}
33663364
onContentOverflowChange={setTimelineOverflows}
33673365
onToolOutputCollapsedAtEnd={onToolOutputCollapsedAtEndStable}
3368-
onManualNavigation={cancelTimelineLiveFollowForUserNavigationStable}
3369-
cancelPositionRestoreRef={cancelPositionRestoreRef}
3366+
onViewportAttach={attachTimelineViewport}
33703367
hideEmptyPlaceholder={isDraftHeroState || threadDetailLoading}
33713368
emptyPlaceholderInset={composerReservedHeight}
33723369
topFadeEnabled={!hasTimelineTopBanner}
@@ -3387,7 +3384,10 @@ export default function ChatView(props: ChatViewProps) {
33873384
</div>
33883385

33893386
<div
3390-
ref={setComposerOverlayElement}
3387+
ref={(overlay: HTMLDivElement | null) => {
3388+
setComposerOverlayElement(overlay);
3389+
attachFollowComposerOverlay(overlay);
3390+
}}
33913391
inert={isRevertingCheckpoint}
33923392
data-chat-composer-overlay="true"
33933393
className={

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

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { parseMarkdownWithMath } from "@t3tools/shared/markdownMath";
2424
import { collectThreadFindRanges } from "./threadFindHighlights";
2525
import { toastManager } from "../ui/toast";
2626
import type { CitedHistory } from "./citationProcedure";
27+
import type { TimelineNavigation } from "./timelineViewport";
28+
import { rememberTimelinePosition } from "./timelineScrollAnchoring";
2729

2830
const environmentQuery = vi.hoisted(() => ({
2931
error: "Pull request not found",
@@ -91,7 +93,6 @@ function baseProps(): Omit<TimelineProps, "timelineEntries"> {
9193
emptyPlaceholderInset: 0,
9294
liveFollowEnabled: true,
9395
onIsAtEndChange: () => {},
94-
onManualNavigation: () => {},
9596
};
9697
}
9798

@@ -638,6 +639,125 @@ describe("MessagesTimeline attachments", () => {
638639
});
639640
});
640641

642+
describe("MessagesTimeline thread find", () => {
643+
// Opening find is navigation; on a list already showing the thread the open
644+
// action navigates, so the list itself releases follow only when it starts
645+
// showing a thread whose find is already open.
646+
function renderObserved() {
647+
const navigations: TimelineNavigation[] = [];
648+
const base = baseProps();
649+
const element = (next: Partial<TimelineProps>) => (
650+
<MessagesTimeline
651+
{...base}
652+
timelineEntries={[message("Where is the sidebar fix?")]}
653+
onViewportAttach={(viewport) =>
654+
viewport.subscribe((navigation) => navigations.push(navigation))
655+
}
656+
{...next}
657+
/>
658+
);
659+
return { navigations, element };
660+
}
661+
const released = { source: "command", releasesFollow: true };
662+
663+
it("releases follow once when it starts showing a thread whose find is open", async () => {
664+
setup();
665+
const { navigations, element } = renderObserved();
666+
await using screen = await renderElement(element({}));
667+
expect(navigations).toEqual([]);
668+
669+
const other = "environment-local:thread-find-open";
670+
await screen.rerender(element({ routeThreadKey: other, findOpen: true }));
671+
expect(navigations).toEqual([released]);
672+
await screen.rerender(element({ routeThreadKey: other, findOpen: false }));
673+
await screen.rerender(element({ routeThreadKey: other, findOpen: true }));
674+
expect(navigations).toEqual([released]);
675+
});
676+
677+
it("releases follow once when it mounts on a thread whose find is open", async () => {
678+
setup();
679+
const { navigations, element } = renderObserved();
680+
await using screen = await renderElement(element({ findOpen: true }));
681+
await screen.rerender(element({ findOpen: true }));
682+
expect(navigations).toEqual([released]);
683+
});
684+
685+
it("still restores the position of a thread it switches to whose find is open", async () => {
686+
setup();
687+
const base = baseProps();
688+
const other = "environment-local:thread-find-open-positioned";
689+
const replies = Array.from({ length: 40 }, (_, index) =>
690+
message(`Reply ${index}\n\n${"line\n\n".repeat(8)}`, { role: "assistant" }, `r-${index}`),
691+
);
692+
rememberTimelinePosition(
693+
other,
694+
{ rowId: "entry-r-20", offsetWithinRow: 0, scrollOffset: 0, atEnd: false },
695+
false,
696+
);
697+
await using screen = await renderElement(
698+
<MessagesTimeline {...base} timelineEntries={[message("Short thread")]} />,
699+
);
700+
await screen.rerender(
701+
<MessagesTimeline
702+
{...base}
703+
routeThreadKey={other}
704+
timelineEntries={replies}
705+
liveFollowEnabled={false}
706+
findOpen
707+
/>,
708+
);
709+
const viewport = base.listRef.current!.getScrollableNode()!;
710+
await vi.waitFor(() => {
711+
const row = screen.container.querySelector('[data-timeline-row-id="entry-r-20"]');
712+
expect(row).not.toBeNull();
713+
expect(
714+
Math.abs(row!.getBoundingClientRect().top - viewport.getBoundingClientRect().top),
715+
).toBeLessThanOrEqual(2);
716+
});
717+
});
718+
it("reveals find's match, not the remembered row, on a thread it switches to", async () => {
719+
setup();
720+
const base = baseProps();
721+
const other = "environment-local:thread-find-open-match";
722+
const replies = Array.from({ length: 40 }, (_, index) =>
723+
message(
724+
`${index === 5 ? "Needle here" : `Reply ${index}`}\n\n${"line\n\n".repeat(8)}`,
725+
{ role: "assistant" },
726+
`r-${index}`,
727+
),
728+
);
729+
rememberTimelinePosition(
730+
other,
731+
{ rowId: "entry-r-20", offsetWithinRow: 0, scrollOffset: 0, atEnd: false },
732+
false,
733+
);
734+
await using screen = await renderElement(
735+
<MessagesTimeline {...base} timelineEntries={[message("Short thread")]} />,
736+
);
737+
await screen.rerender(
738+
<MessagesTimeline
739+
{...base}
740+
routeThreadKey={other}
741+
timelineEntries={replies}
742+
liveFollowEnabled={false}
743+
findOpen
744+
findExpanded
745+
findQuery="Needle"
746+
activeFindMatch={{ entryId: "entry-r-5", runId: null, occurrence: 0 }}
747+
/>,
748+
);
749+
const viewport = base.listRef.current!.getScrollableNode()!;
750+
await vi.waitFor(() => {
751+
const row = screen.container.querySelector('[data-timeline-row-id="entry-r-5"]');
752+
expect(row).not.toBeNull();
753+
const bounds = viewport.getBoundingClientRect();
754+
const top = row!.getBoundingClientRect().top;
755+
expect(top).toBeGreaterThanOrEqual(bounds.top);
756+
expect(top).toBeLessThan(bounds.bottom);
757+
});
758+
});
759+
});
760+
641761
describe("compact reasoning math", () => {
642762
it("keeps formulas while flattening compact reasoning previews", () => {
643763
const tree = parseMarkdownWithMath("## Reasoning\n\nUse **norm** $|x|$ and \\[x^2\\]");

0 commit comments

Comments
 (0)