TypeScript
refactor(web): timeline row derivation, position restore and find reader live in hooks (#22682)
Julius Marmingebef9142f8b96
diff --git a/apps/web/src/components/chat/MessagesTimeline.logic.ts b/apps/web/src/components/chat/MessagesTimeline.logic.tsindex 96c3a36a6e4..361bcdb62db 100644--- a/apps/web/src/components/chat/MessagesTimeline.logic.ts+++ b/apps/web/src/components/chat/MessagesTimeline.logic.ts@@ -8,6 +8,7 @@ import {collectAssistantCitations,} from "@t3tools/shared/assistantCitations";import { shallow } from "zustand/vanilla/shallow";+import type { HandoffTimelineRun } from "@t3tools/client-runtime/handoff";import { renderCodexDirectivesForCopy } from "@t3tools/shared/codexMarkdownDirectives";import {commandDisplayText,@@ -2011,7 +2012,7 @@ function attachCreatedThreadSummaries(const WORKTREE_SETUP_ROW_ID = "worktree-setup-row";-type MessagesTimelineRowsInput = Parameters<typeof deriveMessagesTimelineRows>[0];+export type MessagesTimelineRowsInput = Parameters<typeof deriveMessagesTimelineRows>[0];export interface MessagesTimelineRowsProjection {readonly input: MessagesTimelineRowsInput;@@ -2314,3 +2315,28 @@ export function shouldCollapseUserMessage(text: string): boolean {visible.split("\n").length > MAX_COLLAPSED_USER_MESSAGE_LINES);}++export function handoffRunsSignature(runs: ReadonlyArray<HandoffTimelineRun>): string {+ return runs+ .map(+ (run) =>+ `${run.id}\0${run.providerInstanceId}\0${run.ordinal}\0${run.modelSelection.instanceId}\0${run.modelSelection.model}`,+ )+ .join("\n");+}++// Expanded-id sets can be rebuilt during render; comparing them by content keeps+// the render-time state update in useIncrementalTimelineRows from repeating forever.+export function sameRowsInput(+ left: MessagesTimelineRowsInput,+ right: MessagesTimelineRowsInput,+): boolean {+ for (const key of Object.keys(right) as Array<keyof MessagesTimelineRowsInput>) {+ const previous = left[key];+ const next = right[key];+ if (Object.is(previous, next)) continue;+ if (previous instanceof Set && next instanceof Set && shallow(previous, next)) continue;+ return false;+ }+ return Object.keys(left).length === Object.keys(right).length;+}diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex ae09911bd9a..8382c322484 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -1,5 +1,4 @@import { ThreadFindTimelineContext } from "./ThreadFindProvider";-import { shallow } from "zustand/vanilla/shallow";import { MarkdownFindContext } from "./markdownFindContext";import { useChatCanvas } from "./ChatCanvasContext";import { WorkLogBlock } from "./WorkLog";@@ -60,9 +59,6 @@ import type { AssistantCitationSourceAnchor } from "~/lib/assistantTextSelectionimport { type AssistantCitationRequest } from "./AssistantCitationSource";import { useAssistantCitationTarget, type CitationHistoryPage } from "./useAssistantCitationTarget";import {- computeStableMessagesTimelineRows,- deriveMessagesTimelineRowsWithState,- type MessagesTimelineRowsProjection,resolveTimelineIsAtEnd,resolveTimelineMinimapHasPersistentGutter,resolveTimelineMinimapCurrentIndex,@@ -71,13 +67,11 @@ import {resolveTimelineMinimapHitStripWidth,timelineTurnFoldRunIdsByEntryId,worktreeSetupAgentStarted,- type StableMessagesTimelineRowsState,type MessagesTimelineRow,type TimelineLatestRun,} from "./MessagesTimeline.logic";import { type ThreadFindMatch } from "./threadFind";import { useThreadFindNavigation } from "./useThreadFindNavigation";-import { readThreadFindPosition } from "./threadFindPosition";import type { ThreadFindPositionReader } from "./threadFind";import { TooltipScrollDismissArea } from "../ui/tooltip";import { WorktreeSetupCard } from "./WorktreeSetupCard";@@ -116,6 +110,9 @@ import {McpAppTimelineRow,} from "./TimelineAssistantRows";import { V2EventTimelineRow } from "./TimelineV2EventRow";+import { useTimelinePositionRestore } from "./useTimelinePositionRestore";+import { useThreadFindPositionReader } from "./useThreadFindPositionReader";+import { useStableHandoffRuns, useIncrementalTimelineRows, useStableRows } from "./useTimelineRows";function timelineListStateFor(identity: string): {readonly identity: string;@@ -163,7 +160,6 @@ const TIMELINE_MAINTAIN_SCROLL_AT_END = {layout: true,},} as const satisfies MaintainScrollAtEndOptions;-const EMPTY_TIMELINE_RUNS: ReadonlyArray<HandoffTimelineRun> = [];const EMPTY_RUN_IDS: ReadonlySet<RunId> = new Set();const TIMELINE_MAINTAIN_SCROLL_AT_END_SMOOTH = {...TIMELINE_MAINTAIN_SCROLL_AT_END,@@ -609,145 +605,23 @@ const ConversationTimeline = memo(function ConversationTimeline({const runs = useStableHandoffRuns(runsProp);const minimapItems = deriveTimelineMinimapItems(rows);const minimapRowIndices = minimapItems.map((item) => item.rowIndex);- // 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): -1;const restoringAlwaysRender =restoringThreadPosition && restoreRowIndex >= 0 ? { indices: [restoreRowIndex] } : undefined;- useLayoutEffect(() => {- if (!restoringThreadPosition || rows.length === 0) return;- const list = listRef.current;- if (!list) return;- if (citationRequest !== null) {- // oxlint-disable-next-line react/set-state-in-effect -- The citation scroll owns positioning once the list has mounted.- setPositionedThreadKey(listIdentityKey);- return;- }- let cancelled = false;- let settleFrame: number | null = null;- const viewport: HTMLElement | null = list.getScrollableNode();- const cancelRestoration = () => {- if (cancelled) return;- cancelled = true;- if (settleFrame !== null) cancelAnimationFrame(settleFrame);- if (viewport) void list.scrollToOffset({ offset: viewport.scrollTop, animated: false });- setPositionedThreadKey(listIdentityKey);- };- const cancelForNavigation = () => {- cancelRestoration();- onManualNavigation();- };- const onScrollKey = (event: globalThis.KeyboardEvent) => {- if (- ["ArrowUp", "ArrowDown", "PageUp", "PageDown", "Home", "End", " "].includes(event.key) &&- !(- event.target instanceof Element &&- event.target.closest("input, textarea, [contenteditable=true]")- )- )- cancelForNavigation();- };- viewport?.addEventListener("wheel", cancelForNavigation, { passive: true });- viewport?.addEventListener("touchmove", cancelForNavigation, { passive: true });- viewport?.addEventListener("pointerdown", cancelForNavigation, { passive: true });- viewport?.ownerDocument.addEventListener("keydown", onScrollKey);- const position = rememberedPosition;- const index = position ? rows.findIndex((row) => row.id === position.rowId) : -1;- if (position?.atEnd === false) onManualNavigation();- if (cancelPositionRestoreRef) cancelPositionRestoreRef.current = cancelRestoration;- const scrolling =- position?.atEnd === false- ? index >= 0- ? list.scrollToIndex({- index,- animated: false,- viewPosition: 0,- viewOffset: -position.offsetWithinRow,- })- : list.scrollToOffset({ offset: position.scrollOffset, 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) {- setPositionedThreadKey(listIdentityKey);- return;- }- // Index scrolling starts from estimates. Keep the saved row mounted- // until its measured position and the DOM agree for two layout frames.- let stableFrames = 0;- const reconcile = () => {- if (cancelled) return;- const state = list.getState();- const rowIndex = state.indexByKey(position.rowId);- const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);- const element = list.getScrollableNode();- if (!row || !element) return;- const offset = Math.max(- 0,- Math.min(- element.scrollTop +- row.getBoundingClientRect().top -- element.getBoundingClientRect().top +- position.offsetWithinRow,- element.scrollHeight - element.clientHeight,- ),- );- if (Math.abs(element.scrollTop - offset) > 1) {- stableFrames = 0;- void list.scrollToOffset({ offset, animated: false }).then(() => {- if (!cancelled) settleFrame = requestAnimationFrame(reconcile);- });- return;- }- stableFrames += 1;- if (stableFrames >= 2) {- setPositionedThreadKey(listIdentityKey);- } else {- settleFrame = requestAnimationFrame(reconcile);- }- };- settleFrame = requestAnimationFrame(reconcile);- });- return () => {- cancelled = true;- if (cancelPositionRestoreRef?.current === cancelRestoration) {- cancelPositionRestoreRef.current = null;- }- if (settleFrame !== null) cancelAnimationFrame(settleFrame);- viewport?.removeEventListener("wheel", cancelForNavigation);- viewport?.removeEventListener("touchmove", cancelForNavigation);- viewport?.removeEventListener("pointerdown", cancelForNavigation);- viewport?.ownerDocument.removeEventListener("keydown", onScrollKey);- };- }, [- citationRequest,- cancelPositionRestoreRef,- listIdentityKey,+ useTimelinePositionRestore({listRef,- onManualNavigation,- rememberedPosition,+ listIdentityKey,restoringThreadPosition,+ rememberedPosition,rows,- ]);+ citationRequest,+ cancelPositionRestoreRef,+ onManualNavigation,+ onPositioned: setPositionedThreadKey,+ });const [timelineViewportElement, setTimelineViewportElement] = useState<HTMLDivElement | null>(null,@@ -1112,70 +986,18 @@ const ConversationTimeline = memo(function ConversationTimeline({registerTimeline?.(list?.getScrollableNode() ?? null);};- useLayoutEffect(() => {- if (!findPositionReaderRef || !timelineViewportElement) return;- const read: ThreadFindPositionReader = (query) => {- const foldRunIds =- findFoldRunIds ??- timelineTurnFoldRunIdsByEntryId({- timelineEntries,- latestRun,- runningRunId,- isWorking,- runlessWorkActive,- });- const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));- return readThreadFindPosition(- timelineViewportElement,- query,- (rowId) => {- const rowIndex = rows.findIndex((row) => row.id === rowId);- for (const row of rows.slice(Math.max(0, rowIndex))) {- if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {- const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);- if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))- return entry.id;- continue;- }- const entry = timelineEntries.find((entry) => {- const runId =- row.kind === "turn-fold"- ? foldRunIds.get(entry.id)- : entry.kind === "message"- ? entry.message.runId- : entry.kind === "proposed-plan"- ? entry.proposedPlan.runId- : null;- return (- runId === row.runId &&- (entry.kind !== "message" || entry.message.role === "assistant") &&- (row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)- );- });- if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))- return entry.id;- }- return undefined;- },- contentInsetEndAdjustment,- );- };- findPositionReaderRef.current = read;- return () => {- if (findPositionReaderRef.current === read) findPositionReaderRef.current = null;- };- }, [- contentInsetEndAdjustment,- findFoldRunIds,+ useThreadFindPositionReader({findPositionReaderRef,- isWorking,+ timelineViewportElement,+ findFoldRunIds,+ timelineEntries,latestRun,- rows,- runlessWorkActive,runningRunId,- timelineEntries,- timelineViewportElement,- ]);+ isWorking,+ runlessWorkActive,+ rows,+ contentInsetEndAdjustment,+ });useThreadFindNavigation({listReady: findListReady,@@ -1556,89 +1378,3 @@ function AttemptFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "at</button>);}--function handoffRunsSignature(runs: ReadonlyArray<HandoffTimelineRun>): string {- return runs- .map(- (run) =>- `${run.id}\0${run.providerInstanceId}\0${run.ordinal}\0${run.modelSelection.instanceId}\0${run.modelSelection.model}`,- )- .join("\n");-}--function useStableHandoffRuns(- runs: ReadonlyArray<HandoffTimelineRun>,-): ReadonlyArray<HandoffTimelineRun> {- const [state, setState] = useState<{- readonly source: ReadonlyArray<HandoffTimelineRun> | null;- readonly signature: string;- readonly value: ReadonlyArray<HandoffTimelineRun>;- }>({ source: null, signature: "", value: EMPTY_TIMELINE_RUNS });- if (state.source === runs) return state.value;- const signature = handoffRunsSignature(runs);- const value =- signature === state.signature- ? state.value- : runs.map((run) => ({- id: run.id,- ordinal: run.ordinal,- providerInstanceId: run.providerInstanceId,- modelSelection: run.modelSelection,- }));- setState({ source: runs, signature, value });- return value;-}--type MessagesTimelineRowsInput = MessagesTimelineRowsProjection["input"];--// Expanded-id sets can be rebuilt during render; comparing them by content keeps-// the render-time state update below from repeating forever.-function sameRowsInput(left: MessagesTimelineRowsInput, right: MessagesTimelineRowsInput): boolean {- for (const key of Object.keys(right) as Array<keyof MessagesTimelineRowsInput>) {- const previous = left[key];- const next = right[key];- if (Object.is(previous, next)) continue;- if (previous instanceof Set && next instanceof Set && shallow(previous, next)) continue;- return false;- }- return Object.keys(left).length === Object.keys(right).length;-}--/**- * Rows derived from the previous projection of the same list, so a streamed- * text update patches its message rows instead of deriving every row again.- */-function useIncrementalTimelineRows(- identity: string,- input: MessagesTimelineRowsInput,-): MessagesTimelineRow[] {- const [state, setState] = useState(() => ({- identity,- input,- projection: deriveMessagesTimelineRowsWithState(input),- }));- if (state.identity === identity && sameRowsInput(state.input, input)) {- return state.projection.rows;- }- const projection = deriveMessagesTimelineRowsWithState(- input,- state.identity === identity ? state.projection : null,- );- setState({ identity, input, projection });- return projection.rows;-}--function useStableRows(rows: MessagesTimelineRow[], identity: string): MessagesTimelineRow[] {- const [state, setState] = useState<{- readonly identity: string;- readonly source: MessagesTimelineRow[] | null;- readonly stable: StableMessagesTimelineRowsState;- }>({ identity, source: null, stable: { byId: new Map(), result: [] } });- if (state.identity === identity && state.source === rows) return state.stable.result;- const stable = computeStableMessagesTimelineRows(- rows,- state.identity === identity ? state.stable : { byId: new Map(), result: [] },- );- setState({ identity, source: rows, stable });- return stable.result;-}diff --git a/apps/web/src/components/chat/useThreadFindPositionReader.ts b/apps/web/src/components/chat/useThreadFindPositionReader.tsnew file mode 100644index 00000000000..5afc9ca2548--- /dev/null+++ b/apps/web/src/components/chat/useThreadFindPositionReader.ts@@ -0,0 +1,100 @@+import { useLayoutEffect } from "react";+import type { RunId } from "@t3tools/contracts";+import type { TimelineEntry } from "../../session-logic";+import {+ timelineTurnFoldRunIdsByEntryId,+ type MessagesTimelineRow,+ type TimelineLatestRun,+} from "./MessagesTimeline.logic";+import type { ThreadFindPositionReader } from "./threadFind";+import { readThreadFindPosition } from "./threadFindPosition";++/** Publishes how to read the find position of the timeline's current viewport. */+export function useThreadFindPositionReader({+ findPositionReaderRef,+ timelineViewportElement,+ findFoldRunIds,+ timelineEntries,+ latestRun,+ runningRunId,+ isWorking,+ runlessWorkActive,+ rows,+ contentInsetEndAdjustment,+}: {+ readonly findPositionReaderRef: React.RefObject<ThreadFindPositionReader | null> | undefined;+ readonly timelineViewportElement: HTMLDivElement | null;+ readonly findFoldRunIds: ReadonlyMap<string, RunId> | null;+ readonly timelineEntries: ReadonlyArray<TimelineEntry>;+ readonly latestRun: TimelineLatestRun | null;+ readonly runningRunId: RunId | null;+ readonly isWorking: boolean;+ readonly runlessWorkActive: boolean;+ readonly rows: MessagesTimelineRow[];+ readonly contentInsetEndAdjustment: number;+}) {+ useLayoutEffect(() => {+ if (!findPositionReaderRef || !timelineViewportElement) return;+ const read: ThreadFindPositionReader = (query) => {+ const foldRunIds =+ findFoldRunIds ??+ timelineTurnFoldRunIdsByEntryId({+ timelineEntries,+ latestRun,+ runningRunId,+ isWorking,+ runlessWorkActive,+ });+ const entryById = new Map(timelineEntries.map((entry) => [entry.id, entry]));+ return readThreadFindPosition(+ timelineViewportElement,+ query,+ (rowId) => {+ const rowIndex = rows.findIndex((row) => row.id === rowId);+ for (const row of rows.slice(Math.max(0, rowIndex))) {+ if (row.kind !== "turn-fold" && row.kind !== "attempt-fold") {+ const entry = entryById.get(row.kind === "assistant-meta" ? row.message.id : row.id);+ if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))+ return entry.id;+ continue;+ }+ const entry = timelineEntries.find((entry) => {+ const runId =+ row.kind === "turn-fold"+ ? foldRunIds.get(entry.id)+ : entry.kind === "message"+ ? entry.message.runId+ : entry.kind === "proposed-plan"+ ? entry.proposedPlan.runId+ : null;+ return (+ runId === row.runId &&+ (entry.kind !== "message" || entry.message.role === "assistant") &&+ (row.kind !== "attempt-fold" || entry.attempt?.id === row.attemptId)+ );+ });+ if (entry && (entry.kind === "message" || entry.kind === "proposed-plan"))+ return entry.id;+ }+ return undefined;+ },+ contentInsetEndAdjustment,+ );+ };+ findPositionReaderRef.current = read;+ return () => {+ if (findPositionReaderRef.current === read) findPositionReaderRef.current = null;+ };+ }, [+ contentInsetEndAdjustment,+ findFoldRunIds,+ findPositionReaderRef,+ isWorking,+ latestRun,+ rows,+ runlessWorkActive,+ runningRunId,+ timelineEntries,+ timelineViewportElement,+ ]);+}diff --git a/apps/web/src/components/chat/useTimelinePositionRestore.ts b/apps/web/src/components/chat/useTimelinePositionRestore.tsnew file mode 100644index 00000000000..700a2a41af0--- /dev/null+++ b/apps/web/src/components/chat/useTimelinePositionRestore.ts@@ -0,0 +1,166 @@+import { useLayoutEffect, useRef } from "react";+import type { LegendListRef } from "@legendapp/list/react";+import type { AssistantCitationRequest } from "./AssistantCitationSource";+import type { MessagesTimelineRow } from "./MessagesTimeline.logic";+import type { RememberedTimelinePosition } from "./timelineScrollAnchoring";++/**+ * Scrolls a newly shown thread back to where the user left it, or to its end,+ * and reports the thread positioned once the saved row settles.+ */+export function useTimelinePositionRestore({+ listRef,+ listIdentityKey,+ restoringThreadPosition,+ rememberedPosition,+ rows,+ citationRequest,+ cancelPositionRestoreRef,+ onManualNavigation,+ onPositioned,+}: {+ readonly listRef: React.RefObject<LegendListRef | null>;+ readonly listIdentityKey: string;+ readonly restoringThreadPosition: boolean;+ readonly rememberedPosition: RememberedTimelinePosition | undefined;+ readonly rows: MessagesTimelineRow[];+ readonly citationRequest: AssistantCitationRequest | null;+ readonly cancelPositionRestoreRef: React.RefObject<(() => void) | null> | undefined;+ readonly onManualNavigation: () => void;+ readonly onPositioned: (identity: string) => void;+}) {+ // 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;+ }+ });+ useLayoutEffect(() => {+ if (!restoringThreadPosition || rows.length === 0) return;+ const list = listRef.current;+ if (!list) return;+ if (citationRequest !== null) {+ // The citation scroll owns positioning once the list has mounted.+ onPositioned(listIdentityKey);+ return;+ }+ let cancelled = false;+ let settleFrame: number | null = null;+ const viewport: HTMLElement | null = list.getScrollableNode();+ const cancelRestoration = () => {+ if (cancelled) return;+ cancelled = true;+ if (settleFrame !== null) cancelAnimationFrame(settleFrame);+ if (viewport) void list.scrollToOffset({ offset: viewport.scrollTop, animated: false });+ onPositioned(listIdentityKey);+ };+ const cancelForNavigation = () => {+ cancelRestoration();+ onManualNavigation();+ };+ const onScrollKey = (event: globalThis.KeyboardEvent) => {+ if (+ ["ArrowUp", "ArrowDown", "PageUp", "PageDown", "Home", "End", " "].includes(event.key) &&+ !(+ event.target instanceof Element &&+ event.target.closest("input, textarea, [contenteditable=true]")+ )+ )+ cancelForNavigation();+ };+ viewport?.addEventListener("wheel", cancelForNavigation, { passive: true });+ viewport?.addEventListener("touchmove", cancelForNavigation, { passive: true });+ viewport?.addEventListener("pointerdown", cancelForNavigation, { passive: true });+ viewport?.ownerDocument.addEventListener("keydown", onScrollKey);+ const position = rememberedPosition;+ const index = position ? rows.findIndex((row) => row.id === position.rowId) : -1;+ if (position?.atEnd === false) onManualNavigation();+ if (cancelPositionRestoreRef) cancelPositionRestoreRef.current = cancelRestoration;+ const scrolling =+ position?.atEnd === false+ ? index >= 0+ ? list.scrollToIndex({+ index,+ animated: false,+ viewPosition: 0,+ viewOffset: -position.offsetWithinRow,+ })+ : list.scrollToOffset({ offset: position.scrollOffset, 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) {+ onPositioned(listIdentityKey);+ return;+ }+ // Index scrolling starts from estimates. Keep the saved row mounted+ // until its measured position and the DOM agree for two layout frames.+ let stableFrames = 0;+ const reconcile = () => {+ if (cancelled) return;+ const state = list.getState();+ const rowIndex = state.indexByKey(position.rowId);+ const row = rowIndex === undefined ? undefined : state.elementAtIndex(rowIndex);+ const element = list.getScrollableNode();+ if (!row || !element) return;+ const offset = Math.max(+ 0,+ Math.min(+ element.scrollTop ++ row.getBoundingClientRect().top -+ element.getBoundingClientRect().top ++ position.offsetWithinRow,+ element.scrollHeight - element.clientHeight,+ ),+ );+ if (Math.abs(element.scrollTop - offset) > 1) {+ stableFrames = 0;+ void list.scrollToOffset({ offset, animated: false }).then(() => {+ if (!cancelled) settleFrame = requestAnimationFrame(reconcile);+ });+ return;+ }+ stableFrames += 1;+ if (stableFrames >= 2) {+ onPositioned(listIdentityKey);+ } else {+ settleFrame = requestAnimationFrame(reconcile);+ }+ };+ settleFrame = requestAnimationFrame(reconcile);+ });+ return () => {+ cancelled = true;+ if (cancelPositionRestoreRef?.current === cancelRestoration) {+ cancelPositionRestoreRef.current = null;+ }+ if (settleFrame !== null) cancelAnimationFrame(settleFrame);+ viewport?.removeEventListener("wheel", cancelForNavigation);+ viewport?.removeEventListener("touchmove", cancelForNavigation);+ viewport?.removeEventListener("pointerdown", cancelForNavigation);+ viewport?.ownerDocument.removeEventListener("keydown", onScrollKey);+ };+ }, [+ citationRequest,+ cancelPositionRestoreRef,+ listIdentityKey,+ listRef,+ onManualNavigation,+ onPositioned,+ rememberedPosition,+ restoringThreadPosition,+ rows,+ ]);+}diff --git a/apps/web/src/components/chat/useTimelineRows.ts b/apps/web/src/components/chat/useTimelineRows.tsnew file mode 100644index 00000000000..847dcc594af--- /dev/null+++ b/apps/web/src/components/chat/useTimelineRows.ts@@ -0,0 +1,78 @@+import { useState } from "react";+import {+ computeStableMessagesTimelineRows,+ handoffRunsSignature,+ type MessagesTimelineRowsInput,+ sameRowsInput,+ deriveMessagesTimelineRowsWithState,+ type StableMessagesTimelineRowsState,+ type MessagesTimelineRow,+} from "./MessagesTimeline.logic";+import { type HandoffTimelineRun } from "./V2LifecycleRow";++const EMPTY_TIMELINE_RUNS: ReadonlyArray<HandoffTimelineRun> = [];++export function useStableHandoffRuns(+ runs: ReadonlyArray<HandoffTimelineRun>,+): ReadonlyArray<HandoffTimelineRun> {+ const [state, setState] = useState<{+ readonly source: ReadonlyArray<HandoffTimelineRun> | null;+ readonly signature: string;+ readonly value: ReadonlyArray<HandoffTimelineRun>;+ }>({ source: null, signature: "", value: EMPTY_TIMELINE_RUNS });+ if (state.source === runs) return state.value;+ const signature = handoffRunsSignature(runs);+ const value =+ signature === state.signature+ ? state.value+ : runs.map((run) => ({+ id: run.id,+ ordinal: run.ordinal,+ providerInstanceId: run.providerInstanceId,+ modelSelection: run.modelSelection,+ }));+ setState({ source: runs, signature, value });+ return value;+}++/**+ * Rows derived from the previous projection of the same list, so a streamed+ * text update patches its message rows instead of deriving every row again.+ */+export function useIncrementalTimelineRows(+ identity: string,+ input: MessagesTimelineRowsInput,+): MessagesTimelineRow[] {+ const [state, setState] = useState(() => ({+ identity,+ input,+ projection: deriveMessagesTimelineRowsWithState(input),+ }));+ if (state.identity === identity && sameRowsInput(state.input, input)) {+ return state.projection.rows;+ }+ const projection = deriveMessagesTimelineRowsWithState(+ input,+ state.identity === identity ? state.projection : null,+ );+ setState({ identity, input, projection });+ return projection.rows;+}++export function useStableRows(+ rows: MessagesTimelineRow[],+ identity: string,+): MessagesTimelineRow[] {+ const [state, setState] = useState<{+ readonly identity: string;+ readonly source: MessagesTimelineRow[] | null;+ readonly stable: StableMessagesTimelineRowsState;+ }>({ identity, source: null, stable: { byId: new Map(), result: [] } });+ if (state.identity === identity && state.source === rows) return state.stable.result;+ const stable = computeStableMessagesTimelineRows(+ rows,+ state.identity === identity ? state.stable : { byId: new Map(), result: [] },+ );+ setState({ identity, source: rows, stable });+ return stable.result;+}