Skip to content

Commit 678bd5b

Browse files
committed
refactor(web): simplify timeline state persistence
1 parent fd90b0e commit 678bd5b

3 files changed

Lines changed: 80 additions & 22 deletions

File tree

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6395,13 +6395,6 @@ export default function ChatView(props: ChatViewProps) {
63956395
}
63966396
}
63976397
}, []);
6398-
const cancelTimelineLiveFollowForUserNavigationRef = useRef(
6399-
cancelTimelineLiveFollowForUserNavigation,
6400-
);
6401-
useEffect(() => {
6402-
cancelTimelineLiveFollowForUserNavigationRef.current =
6403-
cancelTimelineLiveFollowForUserNavigation;
6404-
}, [cancelTimelineLiveFollowForUserNavigation]);
64056398
useEffect(() => {
64066399
const previous = observedTimelineActivityRef.current;
64076400
const dispatchedUserItem =
@@ -6502,7 +6495,7 @@ export default function ChatView(props: ChatViewProps) {
65026495
const onTimelineScrollNodeMount = useCallback(
65036496
(scrollNode: HTMLElement) => {
65046497
const handleManualNavigation = () => {
6505-
cancelTimelineLiveFollowForUserNavigationRef.current();
6498+
cancelTimelineLiveFollowForUserNavigation();
65066499
};
65076500
// The gestures below must only break follow when they can actually
65086501
// move the viewport away from the live edge (#5566): a spurious break
@@ -6630,7 +6623,11 @@ export default function ChatView(props: ChatViewProps) {
66306623
document.removeEventListener("keydown", handleKeyDown);
66316624
};
66326625
},
6633-
[isTimelineAtLogicalEnd, timelineRealContentOverflowsViewport],
6626+
[
6627+
cancelTimelineLiveFollowForUserNavigation,
6628+
isTimelineAtLogicalEnd,
6629+
timelineRealContentOverflowsViewport,
6630+
],
66346631
);
66356632

66366633
const onTimelineAnchorReady = useCallback((messageId: MessageId, anchorIndex: number) => {

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,6 +2717,64 @@ describe("MessagesTimeline", () => {
27172717
});
27182718
});
27192719

2720+
describe("disclosure state across thread switches", () => {
2721+
it("remembers an expanded turn without waiting for a scroll or animation frame", async () => {
2722+
vi.unstubAllGlobals();
2723+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
2724+
vi.stubGlobal("requestAnimationFrame", () => 1);
2725+
vi.stubGlobal("cancelAnimationFrame", () => {});
2726+
vi.stubGlobal(
2727+
"ResizeObserver",
2728+
class {
2729+
observe() {}
2730+
unobserve() {}
2731+
disconnect() {}
2732+
},
2733+
);
2734+
const container = document.createElement("div");
2735+
document.body.append(container);
2736+
const root = createRoot(container);
2737+
const runId = RunId.make("disclosure-switch-run");
2738+
const entries = ["Earlier response", "Final response"].map((text, index) => {
2739+
const entry = buildAssistantTimelineEntry(text);
2740+
return {
2741+
...entry,
2742+
id: `disclosure-switch-entry-${index}`,
2743+
message: { ...entry.message, id: MessageId.make(`disclosure-switch-${index}`), runId },
2744+
};
2745+
});
2746+
const renderThread = async (threadKey: string) => {
2747+
await act(async () => {
2748+
root.render(
2749+
<MessagesTimeline
2750+
{...buildProps()}
2751+
routeThreadKey={threadKey}
2752+
timelineEntries={entries}
2753+
/>,
2754+
);
2755+
});
2756+
};
2757+
try {
2758+
await renderThread("disclosure-switch:thread-a");
2759+
expect(container.textContent).not.toContain("Earlier response");
2760+
const toggle = container.querySelector<HTMLButtonElement>(
2761+
'[data-timeline-row-kind="turn-fold"] button',
2762+
);
2763+
expect(toggle).not.toBeNull();
2764+
await act(async () => toggle!.click());
2765+
expect(container.textContent).toContain("Earlier response");
2766+
await renderThread("disclosure-switch:thread-b");
2767+
expect(container.textContent).not.toContain("Earlier response");
2768+
await renderThread("disclosure-switch:thread-a");
2769+
expect(container.textContent).toContain("Earlier response");
2770+
} finally {
2771+
await act(async () => root.unmount());
2772+
container.remove();
2773+
vi.unstubAllGlobals();
2774+
}
2775+
});
2776+
});
2777+
27202778
describe("thread entry with the real virtualizer", () => {
27212779
it("returns a previously scrolled thread to its measured end", async () => {
27222780
vi.unstubAllGlobals();

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
628628
const expandCitedRun = useCallback((runId: RunId) => {
629629
setExpandedRunIds((current) => (current.has(runId) ? current : new Set([...current, runId])));
630630
}, []);
631-
// Nested tool state shares the bounded thread-position cache.
631+
// Nested tool state shares the bounded disclosure cache.
632632
const workGroupViewState = useMemo<WorkGroupViewState>(
633633
() =>
634634
rememberedDisclosures?.workGroupState ?? {
@@ -948,12 +948,6 @@ export const MessagesTimeline = memo(function MessagesTimeline({
948948
const state = listRef.current?.getState?.();
949949
if (state?.data !== rows) return;
950950
const isAtEnd = resolveTimelineIsAtEnd(state);
951-
rememberTimelineDisclosures(listIdentityKey, {
952-
runs: paintedExpandedRunIds,
953-
workGroups: paintedExpandedWorkGroupIds,
954-
attempts: paintedExpandedAttemptIds,
955-
workGroupState: workGroupViewState,
956-
});
957951
if (isAtEnd !== undefined && !citationPositioning) {
958952
onIsAtEndChange(isAtEnd);
959953
}
@@ -997,12 +991,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
997991
);
998992
}, [
999993
citationPositioning,
1000-
paintedExpandedRunIds,
1001-
paintedExpandedWorkGroupIds,
1002-
paintedExpandedAttemptIds,
1003-
workGroupViewState,
1004994
rows,
1005-
listIdentityKey,
1006995
listRef,
1007996
minimapItems,
1008997
minimapStripMap,
@@ -1011,9 +1000,23 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10111000
]);
10121001

10131002
useEffect(() => {
1003+
rememberTimelineDisclosures(listIdentityKey, {
1004+
runs: paintedExpandedRunIds,
1005+
workGroups: paintedExpandedWorkGroupIds,
1006+
attempts: paintedExpandedAttemptIds,
1007+
workGroupState: workGroupViewState,
1008+
});
10141009
const frame = requestAnimationFrame(handleScroll);
10151010
return () => cancelAnimationFrame(frame);
1016-
}, [handleScroll, rows.length]);
1011+
}, [
1012+
handleScroll,
1013+
listIdentityKey,
1014+
paintedExpandedRunIds,
1015+
paintedExpandedWorkGroupIds,
1016+
paintedExpandedAttemptIds,
1017+
workGroupViewState,
1018+
rows.length,
1019+
]);
10171020

10181021
useEffect(() => {
10191022
if (!timelineViewportElement) {

0 commit comments

Comments
 (0)