@@ -593,11 +593,25 @@ export const MessagesTimeline = memo(function MessagesTimeline({
593593 historyControls,
594594 loadEarlier = null ,
595595} : MessagesTimelineProps ) {
596+ const canvas = useChatCanvas ( ) ;
597+ const [ timelineViewportElement , setTimelineViewportElement ] = useState < HTMLDivElement | null > (
598+ null ,
599+ ) ;
596600 const listIdentityKey = displayThreadKey ?? routeThreadKey ;
597601 const rememberedPosition = useMemo (
598602 ( ) => readTimelinePosition ( listIdentityKey ) ,
599603 [ listIdentityKey ] ,
600604 ) ;
605+ const rememberedItemSize = rememberedPosition ?. itemSize ;
606+ const timelineReady =
607+ ! rememberedItemSize ||
608+ timelineViewportElement !== null ||
609+ ! canvas ||
610+ canvas . container . width > 0 ;
611+ const estimatedItemSize =
612+ rememberedItemSize && rememberedItemSize . viewportWidth === canvas ?. container . width
613+ ? rememberedItemSize . average
614+ : 90 ;
601615 const [ expandedRunIds , setExpandedRunIds ] = useState < ReadonlySet < RunId > > (
602616 ( ) => rememberedPosition ?. disclosures ?. runs ?? new Set ( ) ,
603617 ) ;
@@ -844,7 +858,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
844858 [ restoreRowIndex , restoringThreadPosition ] ,
845859 ) ;
846860 useLayoutEffect ( ( ) => {
847- if ( ! restoringThreadPosition || rows . length === 0 ) return ;
861+ if ( ! timelineReady || ! restoringThreadPosition || rows . length === 0 ) return ;
848862 const list = listRef . current ;
849863 if ( ! list ) return ;
850864 if ( citationRequest !== null ) {
@@ -956,11 +970,9 @@ export const MessagesTimeline = memo(function MessagesTimeline({
956970 rememberedPosition ,
957971 restoringThreadPosition ,
958972 rows ,
973+ timelineReady ,
959974 ] ) ;
960975
961- const [ timelineViewportElement , setTimelineViewportElement ] = useState < HTMLDivElement | null > (
962- null ,
963- ) ;
964976 // Re-measure the minimap gutter when the chat column changes width without a viewport resize.
965977 const chatWidth = useClientSettings ( ( settings ) => settings . chatWidth ) ;
966978 const {
@@ -1079,8 +1091,14 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10791091 // it is dropped first.
10801092 useLayoutEffect ( ( ) => {
10811093 cancelContentOverflowFrame ( ) ;
1082- onContentOverflowChange ?.( measureContentOverflow ( ) ) ;
1083- } , [ cancelContentOverflowFrame , measureContentOverflow , onContentOverflowChange , rows . length ] ) ;
1094+ if ( timelineReady ) onContentOverflowChange ?.( measureContentOverflow ( ) ) ;
1095+ } , [
1096+ cancelContentOverflowFrame ,
1097+ measureContentOverflow ,
1098+ onContentOverflowChange ,
1099+ rows . length ,
1100+ timelineReady ,
1101+ ] ) ;
10841102
10851103 const handleScroll = useCallback ( ( ) => {
10861104 const state = listRef . current ?. getState ?.( ) ;
@@ -1092,12 +1110,25 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10921110 const row = index === undefined ? undefined : state . elementAtIndex ( index ) ;
10931111 const element = listRef . current ?. getScrollableNode ( ) ;
10941112 if ( row && element ) {
1113+ const viewportRect = element . getBoundingClientRect ( ) ;
1114+ const itemSizes = Object . values ( state . getAverageItemSizes ( ) ) ;
1115+ const measuredItemCount = itemSizes . reduce ( ( sum , item ) => sum + item . count , 0 ) ;
1116+ const measuredItemSize =
1117+ itemSizes . reduce ( ( sum , item ) => sum + item . average * item . count , 0 ) / measuredItemCount ;
10951118 rememberTimelinePosition ( listIdentityKey , {
10961119 ...position ,
10971120 // DOM geometry includes the header and the virtualizer's layout adjustment.
1098- offsetWithinRow : element . getBoundingClientRect ( ) . top - row . getBoundingClientRect ( ) . top ,
1121+ offsetWithinRow : viewportRect . top - row . getBoundingClientRect ( ) . top ,
10991122 scrollOffset : element . scrollTop ,
11001123 atEnd : isAtEnd ,
1124+ ...( measuredItemCount > 0
1125+ ? {
1126+ itemSize : {
1127+ viewportWidth : Math . round ( viewportRect . width ) ,
1128+ average : measuredItemSize ,
1129+ } ,
1130+ }
1131+ : { } ) ,
11011132 disclosures : {
11021133 runs : paintedExpandedRunIds ,
11031134 workGroups : paintedExpandedWorkGroupIds ,
@@ -1341,7 +1372,6 @@ export const MessagesTimeline = memo(function MessagesTimeline({
13411372 ) ;
13421373 } , [ historyControls , onOpenThread , parentThreadLink , topFadeEnabled ] ) ;
13431374
1344- const canvas = useChatCanvas ( ) ;
13451375 const registerTimeline = canvas ?. registerTimeline ;
13461376 const setTimelineList = useCallback (
13471377 ( list : LegendListRef | null ) => {
@@ -1386,6 +1416,8 @@ export const MessagesTimeline = memo(function MessagesTimeline({
13861416 ) ;
13871417 }
13881418
1419+ if ( ! timelineReady ) return null ;
1420+
13891421 return (
13901422 < TimelineRowCtx value = { sharedState } >
13911423 < TimelineRowActivityCtx value = { activityState } >
@@ -1408,7 +1440,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
14081440 keyExtractor = { keyExtractor }
14091441 getItemType = { getItemType }
14101442 renderItem = { renderItem }
1411- estimatedItemSize = { 90 }
1443+ estimatedItemSize = { estimatedItemSize }
14121444 initialScrollAtEnd = { citationRequest === null && rememberedPosition ?. atEnd !== false }
14131445 // Legend needs a data refresh to mount new pins without a scroll event.
14141446 dataVersion = { readyCitationRequest ?. key ?? listIdentityKey }
0 commit comments