@@ -96,7 +96,8 @@ type TimelineMessageListProps = {
9696 leadingContent ?: React . ReactNode ;
9797 /** The virtualized timeline owns its scroll node when enabled. */
9898 useVirtualizer ?: boolean ;
99- onStartReached ?: ( ) => void ;
99+ onStartReached ?: ( ) => boolean ;
100+ isFetchingOlder ?: boolean ;
100101 onAtBottomStateChange ?: ( atBottom : boolean ) => void ;
101102 onVirtualizerApiChange ?: ( api : TimelineVirtualizerApi | null ) => void ;
102103 onVirtualizerRangeChanged ?: ( ) => void ;
@@ -137,6 +138,7 @@ export const TimelineMessageList = React.memo(function TimelineMessageList({
137138 leadingContent,
138139 useVirtualizer = false ,
139140 onStartReached,
141+ isFetchingOlder = false ,
140142 onAtBottomStateChange,
141143 onVirtualizerApiChange,
142144 onVirtualizerRangeChanged,
@@ -289,6 +291,7 @@ export const TimelineMessageList = React.memo(function TimelineMessageList({
289291 leadingContent = { leadingContent }
290292 onAtBottomStateChange = { onAtBottomStateChange }
291293 onStartReached = { onStartReached }
294+ isFetchingOlder = { isFetchingOlder }
292295 onVirtualizerApiChange = { onVirtualizerApiChange }
293296 onVirtualizerRangeChanged = { onVirtualizerRangeChanged }
294297 onVirtualizerScrollerChange = { onVirtualizerScrollerChange }
@@ -371,7 +374,8 @@ type VirtualizedTimelineRowsProps = {
371374 dayGroups : TimelineDayGroup [ ] ;
372375 leadingContent ?: React . ReactNode ;
373376 onAtBottomStateChange ?: ( atBottom : boolean ) => void ;
374- onStartReached ?: ( ) => void ;
377+ onStartReached ?: ( ) => boolean ;
378+ isFetchingOlder ?: boolean ;
375379 onVirtualizerApiChange ?: ( api : TimelineVirtualizerApi | null ) => void ;
376380 onVirtualizerRangeChanged ?: ( ) => void ;
377381 onVirtualizerScrollerChange ?: ( element : HTMLDivElement | null ) => void ;
@@ -390,11 +394,34 @@ function didPrependVirtualizedTimeline(
390394 ) ;
391395}
392396
397+ type PrependAnchor = {
398+ messageId : string ;
399+ topOffset : number ;
400+ } ;
401+
402+ function capturePrependAnchor ( scroller : HTMLElement ) : PrependAnchor | null {
403+ const scrollerTop = scroller . getBoundingClientRect ( ) . top ;
404+ const rows = Array . from (
405+ scroller . querySelectorAll < HTMLElement > ( "[data-message-id]" ) ,
406+ ) ;
407+ const row =
408+ rows . find (
409+ ( candidate ) => candidate . getBoundingClientRect ( ) . top >= scrollerTop ,
410+ ) ?? rows [ rows . length - 1 ] ;
411+ const messageId = row ?. dataset . messageId ;
412+ if ( ! row || ! messageId ) return null ;
413+ return {
414+ messageId,
415+ topOffset : row . getBoundingClientRect ( ) . top - scrollerTop ,
416+ } ;
417+ }
418+
393419function VirtualizedTimelineRows ( {
394420 dayGroups,
395421 leadingContent,
396422 onAtBottomStateChange,
397423 onStartReached,
424+ isFetchingOlder,
398425 onVirtualizerApiChange,
399426 onVirtualizerRangeChanged,
400427 onVirtualizerScrollerChange,
@@ -411,6 +438,9 @@ function VirtualizedTimelineRows({
411438 [ dayGroups , leadingContent ] ,
412439 ) ;
413440 const previousFirstTimelineKeyRef = React . useRef < string | null > ( null ) ;
441+ const pendingPrependAnchorRef = React . useRef < PrependAnchor | null > ( null ) ;
442+ const isLoadingOlderRef = React . useRef ( false ) ;
443+ const observedOlderFetchRef = React . useRef ( false ) ;
414444 const [ prependShiftEpoch , clearPrependShift ] = React . useReducer (
415445 ( version : number ) => version + 1 ,
416446 0 ,
@@ -433,14 +463,58 @@ function VirtualizedTimelineRows({
433463 keys ,
434464 ) ;
435465 } , [ firstTimelineKey , keys , prependShiftEpoch ] ) ;
466+ React . useEffect ( ( ) => {
467+ if ( ! isLoadingOlderRef . current ) return ;
468+ if ( isFetchingOlder ) {
469+ observedOlderFetchRef . current = true ;
470+ } else if ( observedOlderFetchRef . current ) {
471+ // No prepend committed before the request settled (error, cancellation,
472+ // or an empty page). Re-arm top-edge loading for a later retry.
473+ observedOlderFetchRef . current = false ;
474+ isLoadingOlderRef . current = false ;
475+ pendingPrependAnchorRef . current = null ;
476+ }
477+ } , [ isFetchingOlder ] ) ;
436478 React . useLayoutEffect ( ( ) => {
437479 previousFirstTimelineKeyRef . current = firstTimelineKey ;
438- if ( isPrepend ) clearPrependShift ( ) ;
480+ if ( isPrepend ) {
481+ clearPrependShift ( ) ;
482+ isLoadingOlderRef . current = false ;
483+ observedOlderFetchRef . current = false ;
484+ const anchor = pendingPrependAnchorRef . current ;
485+ pendingPrependAnchorRef . current = null ;
486+ const scroller = hostRef . current ?. firstElementChild ;
487+ if ( anchor && scroller instanceof HTMLElement ) {
488+ // Virtua normally preserves a prepend with `shift`, but a transient
489+ // end-anchored cache can occasionally win and jump near the tail. The
490+ // pre-fetch message identity is the semantic invariant; restore it once
491+ // after the prepended rows commit, then leave all later measurement
492+ // correction to Virtua.
493+ const anchorIndex = items . findIndex (
494+ ( item ) =>
495+ item . kind === "timeline-item" &&
496+ timelineItemMessageId ( item . item ) === anchor . messageId ,
497+ ) ;
498+ if ( anchorIndex >= 0 ) {
499+ listRef . current ?. scrollToIndex ( anchorIndex , { align : "start" } ) ;
500+ }
501+ window . requestAnimationFrame ( ( ) => {
502+ const row = scroller . querySelector < HTMLElement > (
503+ `[data-message-id="${ CSS . escape ( anchor . messageId ) } "]` ,
504+ ) ;
505+ if ( ! row ) return ;
506+ const currentTopOffset =
507+ row . getBoundingClientRect ( ) . top -
508+ scroller . getBoundingClientRect ( ) . top ;
509+ listRef . current ?. scrollBy ( currentTopOffset - anchor . topOffset ) ;
510+ } ) ;
511+ }
512+ }
439513 if ( ! hasInitialPositionedRef . current && items . length > 0 ) {
440514 hasInitialPositionedRef . current = true ;
441515 listRef . current ?. scrollToIndex ( items . length - 1 , { align : "end" } ) ;
442516 }
443- } , [ firstTimelineKey , isPrepend , items . length ] ) ;
517+ } , [ firstTimelineKey , isPrepend , items ] ) ;
444518
445519 const messageItemIndexById = React . useMemo ( ( ) => {
446520 const byId = new Map < string , number > ( ) ;
@@ -504,7 +578,18 @@ function VirtualizedTimelineRows({
504578 onAtBottomStateChange ?.(
505579 list . scrollSize - list . viewportSize - offset <= 32 ,
506580 ) ;
507- if ( offset <= 200 ) onStartReached ?.( ) ;
581+ if ( offset <= 200 && ! isLoadingOlderRef . current ) {
582+ const scroller = hostRef . current ?. firstElementChild ;
583+ pendingPrependAnchorRef . current =
584+ scroller instanceof HTMLElement
585+ ? capturePrependAnchor ( scroller )
586+ : null ;
587+ if ( onStartReached ?.( ) ) {
588+ isLoadingOlderRef . current = true ;
589+ } else {
590+ pendingPrependAnchorRef . current = null ;
591+ }
592+ }
508593 } ,
509594 [ onAtBottomStateChange , onStartReached , onVirtualizerRangeChanged ] ,
510595 ) ;
0 commit comments