@@ -14,6 +14,8 @@ import {
1414import {
1515 buildVirtualizedItems ,
1616 didPrependVirtualizedTimeline ,
17+ estimateVirtualizedTimelineItemHeight ,
18+ type VirtualizedTimelineItem ,
1719 virtualizedItemKey ,
1820} from "@/features/messages/lib/virtualizedTimelineItems" ;
1921import { THREAD_REPLY_ROW_MARGIN_INLINE_REM } from "@/features/messages/lib/threadTreeLayout" ;
@@ -370,6 +372,33 @@ type VirtualizedTimelineRowsProps = {
370372 renderItem : ( item : TimelineNonDayItem ) => React . ReactNode ;
371373} ;
372374
375+ type VirtualizedTimelineItemShellProps = {
376+ children : React . ReactNode ;
377+ index : number ;
378+ ref ?: React . LegacyRef < HTMLDivElement > ;
379+ style : React . CSSProperties ;
380+ } ;
381+
382+ const PreserveVirtualizedItemVisibilityContext = React . createContext ( false ) ;
383+
384+ function VirtualizedTimelineItemShell ( {
385+ children,
386+ ref,
387+ style,
388+ } : VirtualizedTimelineItemShellProps ) {
389+ const preserveVisibility = React . useContext (
390+ PreserveVirtualizedItemVisibilityContext ,
391+ ) ;
392+ return (
393+ < div
394+ ref = { ref }
395+ style = { preserveVisibility ? style : { ...style , visibility : undefined } }
396+ >
397+ { children }
398+ </ div >
399+ ) ;
400+ }
401+
373402function VirtualizedTimelineRows ( {
374403 dayGroups,
375404 historyExhausted,
@@ -391,6 +420,20 @@ function VirtualizedTimelineRows({
391420 typeof window === "undefined" ? 1_000 : window . innerHeight ,
392421 ) ;
393422 const hasInitialPositionedRef = React . useRef ( false ) ;
423+ const estimateCallCountRef = React . useRef ( 0 ) ;
424+ const estimateItemSize = React . useCallback (
425+ ( item : VirtualizedTimelineItem ) => {
426+ estimateCallCountRef . current += 1 ;
427+ const scroller = hostRef . current ?. firstElementChild ;
428+ if ( scroller instanceof HTMLDivElement ) {
429+ scroller . dataset . virtuaEstimateCallCount = String (
430+ estimateCallCountRef . current ,
431+ ) ;
432+ }
433+ return estimateVirtualizedTimelineItemHeight ( item ) ;
434+ } ,
435+ [ ] ,
436+ ) ;
394437 const items = React . useMemo (
395438 ( ) => buildVirtualizedItems ( dayGroups , leadingContent , historyExhausted ) ,
396439 [ dayGroups , historyExhausted , leadingContent ] ,
@@ -555,6 +598,9 @@ function VirtualizedTimelineRows({
555598 if ( element ) {
556599 element . dataset . buzzConversationScroll = "true" ;
557600 element . dataset . testid = "message-timeline" ;
601+ element . dataset . virtuaEstimateCallCount = String (
602+ estimateCallCountRef . current ,
603+ ) ;
558604 }
559605 onVirtualizerScrollerChange ?.( element ) ;
560606 return ( ) => onVirtualizerScrollerChange ?.( null ) ;
@@ -637,62 +683,66 @@ function VirtualizedTimelineRows({
637683
638684 return (
639685 < div className = "h-full min-h-0 w-full" ref = { hostRef } >
640- < VList
641- ref = { listRef }
642- className = "h-full min-h-0 w-full overflow-y-auto overflow-x-hidden overscroll-contain px-2 pt-[var(--channel-top-chrome-height,4.5rem)]"
643- data = { items }
644- bufferSize = { offscreenBufferSize }
645- keepMounted = { retainedIndices }
646- style = { { overflowAnchor : "none" } }
647- shift = { isPrepend }
648- onScroll = { handleScroll }
649- onScrollEnd = { handleScrollEnd }
650- >
651- { ( item ) => {
652- if ( item . kind === "bottom-spacer" ) {
653- return (
654- < div
655- aria-hidden
656- className = "h-[var(--composer-overlay-height,6rem)]"
657- key = { virtualizedItemKey ( item ) }
658- />
659- ) ;
660- }
661- if ( item . kind === "leading-content" ) {
662- return < div key = { virtualizedItemKey ( item ) } > { item . content } </ div > ;
663- }
664- if ( item . kind === "day-divider" ) {
665- const dayLabel = formatDayHeading ( item . headingTimestamp ) ;
686+ < PreserveVirtualizedItemVisibilityContext value = { isPrepend } >
687+ < VList
688+ ref = { listRef }
689+ className = "h-full min-h-0 w-full overflow-y-auto overflow-x-hidden overscroll-contain px-2 pt-[var(--channel-top-chrome-height,4.5rem)]"
690+ data = { items }
691+ item = { VirtualizedTimelineItemShell }
692+ itemSize = { estimateItemSize }
693+ bufferSize = { offscreenBufferSize }
694+ keepMounted = { retainedIndices }
695+ style = { { overflowAnchor : "none" } }
696+ shift = { isPrepend }
697+ onScroll = { handleScroll }
698+ onScrollEnd = { handleScrollEnd }
699+ >
700+ { ( item ) => {
701+ if ( item . kind === "bottom-spacer" ) {
702+ return (
703+ < div
704+ aria-hidden
705+ className = "h-[var(--composer-overlay-height,6rem)]"
706+ key = { virtualizedItemKey ( item ) }
707+ />
708+ ) ;
709+ }
710+ if ( item . kind === "leading-content" ) {
711+ return < div key = { virtualizedItemKey ( item ) } > { item . content } </ div > ;
712+ }
713+ if ( item . kind === "day-divider" ) {
714+ const dayLabel = formatDayHeading ( item . headingTimestamp ) ;
715+ return (
716+ < div
717+ // The sticky pill needs travel room, but its containing block
718+ // is this item wrapper. The trailing spacer extends the content
719+ // box by 4rem while the matching negative margin keeps the
720+ // measured layout height at exactly the divider's height, so
721+ // row spacing and Virtua's size cache are unaffected. Both the
722+ // spacer and the pill are pointer-events-none, and the later
723+ // (absolutely positioned) row siblings paint above the spacer.
724+ className = "relative -mb-16 flex flex-col before:absolute before:inset-x-0 before:top-4 before:h-px before:bg-border/35 before:content-['']"
725+ data-day-label = { dayLabel }
726+ data-testid = "message-timeline-day-group"
727+ key = { virtualizedItemKey ( item ) }
728+ >
729+ < DayDivider label = { dayLabel } />
730+ < div aria-hidden className = "pointer-events-none h-16" />
731+ </ div >
732+ ) ;
733+ }
666734 return (
667- < div
668- // The sticky pill needs travel room, but its containing block
669- // is this item wrapper. The trailing spacer extends the content
670- // box by 4rem while the matching negative margin keeps the
671- // measured layout height at exactly the divider's height, so
672- // row spacing and Virtua's size cache are unaffected. Both the
673- // spacer and the pill are pointer-events-none, and the later
674- // (absolutely positioned) row siblings paint above the spacer.
675- className = "relative -mb-16 flex flex-col before:absolute before:inset-x-0 before:top-4 before:h-px before:bg-border/35 before:content-['']"
676- data-day-label = { dayLabel }
677- data-testid = "message-timeline-day-group"
735+ < TimelineRowShell
736+ item = { item . item }
678737 key = { virtualizedItemKey ( item ) }
738+ useContentVisibility = { false }
679739 >
680- < DayDivider label = { dayLabel } />
681- < div aria-hidden className = "pointer-events-none h-16" />
682- </ div >
740+ { renderItem ( item . item ) }
741+ </ TimelineRowShell >
683742 ) ;
684- }
685- return (
686- < TimelineRowShell
687- item = { item . item }
688- key = { virtualizedItemKey ( item ) }
689- useContentVisibility = { false }
690- >
691- { renderItem ( item . item ) }
692- </ TimelineRowShell >
693- ) ;
694- } }
695- </ VList >
743+ } }
744+ </ VList >
745+ </ PreserveVirtualizedItemVisibilityContext >
696746 </ div >
697747 ) ;
698748}
0 commit comments