@@ -4,6 +4,41 @@ import type { AssistantCitationRequest } from "./AssistantCitationSource";
44import type { MessagesTimelineRow } from "./MessagesTimeline.logic" ;
55import type { RememberedTimelinePosition } from "./timelineScrollAnchoring" ;
66
7+ /**
8+ * Index and end scrolling start from estimated row sizes, so they can stop
9+ * short of a tall row. Corrects against the DOM until the viewport holds the
10+ * target for two layout frames; resolves false when the target disappears.
11+ */
12+ export function settleListScroll (
13+ list : LegendListRef ,
14+ target : ( element : HTMLElement ) => number | undefined ,
15+ isCancelled : ( ) => boolean ,
16+ ) : Promise < boolean > {
17+ return new Promise ( ( resolve ) => {
18+ let stableFrames = 0 ;
19+ const reconcile = ( ) => {
20+ if ( isCancelled ( ) ) return resolve ( false ) ;
21+ const element = list . getScrollableNode ( ) ;
22+ const offset = element ? target ( element ) : undefined ;
23+ if ( ! element || offset === undefined ) return resolve ( false ) ;
24+ if ( Math . abs ( element . scrollTop - offset ) > 1 ) {
25+ stableFrames = 0 ;
26+ void list . scrollToOffset ( { offset, animated : false } ) . then ( ( ) => {
27+ requestAnimationFrame ( reconcile ) ;
28+ } ) ;
29+ return ;
30+ }
31+ stableFrames += 1 ;
32+ if ( stableFrames >= 2 ) resolve ( true ) ;
33+ else requestAnimationFrame ( reconcile ) ;
34+ } ;
35+ requestAnimationFrame ( reconcile ) ;
36+ } ) ;
37+ }
38+
39+ export const measuredEndOffset = ( element : HTMLElement ) =>
40+ element . scrollHeight - element . clientHeight ;
41+
742/**
843 * Scrolls a newly shown thread back to where the user left it, or to its end,
944 * and reports the thread positioned once the saved row settles.
@@ -55,12 +90,10 @@ export function useTimelinePositionRestore({
5590 return ;
5691 }
5792 let cancelled = false ;
58- let settleFrame : number | null = null ;
5993 const viewport : HTMLElement | null = list . getScrollableNode ( ) ;
6094 const cancelRestoration = ( ) => {
6195 if ( cancelled ) return ;
6296 cancelled = true ;
63- if ( settleFrame !== null ) cancelAnimationFrame ( settleFrame ) ;
6497 if ( viewport ) void list . scrollToOffset ( { offset : viewport . scrollTop , animated : false } ) ;
6598 onPositioned ( listIdentityKey ) ;
6699 } ;
@@ -99,54 +132,47 @@ export function useTimelinePositionRestore({
99132 : listOpenedOnRef . current ?. identity === listIdentityKey
100133 ? undefined
101134 : list . scrollToEnd ( { animated : false } ) ;
102- void Promise . resolve ( scrolling ) . then ( ( ) => {
135+ const settlesAtEnd = position ?. atEnd !== false && scrolling !== undefined ;
136+ void Promise . resolve ( scrolling ) . then ( async ( ) => {
103137 if ( cancelled ) return ;
138+ if ( settlesAtEnd ) {
139+ if ( await settleListScroll ( list , measuredEndOffset , ( ) => cancelled ) ) {
140+ onPositioned ( listIdentityKey ) ;
141+ }
142+ return ;
143+ }
104144 if ( position ?. atEnd !== false || index < 0 ) {
105145 onPositioned ( listIdentityKey ) ;
106146 return ;
107147 }
108- // Index scrolling starts from estimates. Keep the saved row mounted
109- // until its measured position and the DOM agree for two layout frames.
110- let stableFrames = 0 ;
111- const reconcile = ( ) => {
112- if ( cancelled ) return ;
113- const state = list . getState ( ) ;
114- const rowIndex = state . indexByKey ( position . rowId ) ;
115- const row = rowIndex === undefined ? undefined : state . elementAtIndex ( rowIndex ) ;
116- const element = list . getScrollableNode ( ) ;
117- if ( ! row || ! element ) return ;
118- const offset = Math . max (
119- 0 ,
120- Math . min (
121- element . scrollTop +
122- row . getBoundingClientRect ( ) . top -
123- element . getBoundingClientRect ( ) . top +
124- position . offsetWithinRow ,
125- element . scrollHeight - element . clientHeight ,
126- ) ,
127- ) ;
128- if ( Math . abs ( element . scrollTop - offset ) > 1 ) {
129- stableFrames = 0 ;
130- void list . scrollToOffset ( { offset, animated : false } ) . then ( ( ) => {
131- if ( ! cancelled ) settleFrame = requestAnimationFrame ( reconcile ) ;
132- } ) ;
133- return ;
134- }
135- stableFrames += 1 ;
136- if ( stableFrames >= 2 ) {
137- onPositioned ( listIdentityKey ) ;
138- } else {
139- settleFrame = requestAnimationFrame ( reconcile ) ;
140- }
141- } ;
142- settleFrame = requestAnimationFrame ( reconcile ) ;
148+ // Keep the saved row mounted until its measured position and the DOM agree.
149+ const settled = await settleListScroll (
150+ list ,
151+ ( element ) => {
152+ const state = list . getState ( ) ;
153+ const rowIndex = state . indexByKey ( position . rowId ) ;
154+ const row = rowIndex === undefined ? undefined : state . elementAtIndex ( rowIndex ) ;
155+ if ( ! row ) return undefined ;
156+ return Math . max (
157+ 0 ,
158+ Math . min (
159+ element . scrollTop +
160+ row . getBoundingClientRect ( ) . top -
161+ element . getBoundingClientRect ( ) . top +
162+ position . offsetWithinRow ,
163+ measuredEndOffset ( element ) ,
164+ ) ,
165+ ) ;
166+ } ,
167+ ( ) => cancelled ,
168+ ) ;
169+ if ( settled ) onPositioned ( listIdentityKey ) ;
143170 } ) ;
144171 return ( ) => {
145172 cancelled = true ;
146173 if ( cancelPositionRestoreRef ?. current === cancelRestoration ) {
147174 cancelPositionRestoreRef . current = null ;
148175 }
149- if ( settleFrame !== null ) cancelAnimationFrame ( settleFrame ) ;
150176 viewport ?. removeEventListener ( "wheel" , cancelForNavigation ) ;
151177 viewport ?. removeEventListener ( "touchmove" , cancelForNavigation ) ;
152178 viewport ?. removeEventListener ( "pointerdown" , cancelForNavigation ) ;
0 commit comments