@@ -73,6 +73,7 @@ import {
7373} from "@t3tools/client-runtime/work-log/item-detail" ;
7474import { useTurnItemDetail } from "../../state/queries" ;
7575import {
76+ canCompleteThreadWorkGroupAppend ,
7677 resolveThreadWorkGroupInitialScroll ,
7778 shouldFollowThreadWorkGroupAppend ,
7879 type ThreadWorkGroupScrollPosition ,
@@ -573,7 +574,13 @@ function ThreadWorkGroupList(props: {
573574 const listRef = useRef < LegendListRef > ( null ) ;
574575 const loadedRef = useRef ( false ) ;
575576 const userScrollingRef = useRef ( false ) ;
576- const pendingAppendHeightRef = useRef < number | null > ( null ) ;
577+ const pendingAppendRef = useRef < {
578+ contentHeight : number ;
579+ viewportHeight : number ;
580+ requested : boolean ;
581+ } | null > ( null ) ;
582+ const nativeContentHeightRef = useRef ( 0 ) ;
583+ const measuredViewportHeightRef = useRef ( 0 ) ;
577584 const previousContent = useRef ( {
578585 rows : props . activities ,
579586 height : Math . max ( estimatedRowsHeight , initialPosition ?. contentHeight ?? 0 ) ,
@@ -612,22 +619,30 @@ function ThreadWorkGroupList(props: {
612619 contentHeight : state . contentLength ,
613620 } ) ;
614621 } ;
615- const finishPendingAppend = ( ) => {
616- const targetHeight = pendingAppendHeightRef . current ;
617- const state = listRef . current ?. getState ( ) ;
622+ const requestPendingAppend = ( ) => {
623+ const pending = pendingAppendRef . current ;
624+ const list = listRef . current ;
625+ // JS sizing can advance before native content and viewport geometry catch up.
618626 if (
619- targetHeight !== null &&
620- state &&
621- ! userScrollingRef . current &&
622- Math . abs ( state . scrollLength - targetHeight ) <= 1
627+ pending !== null &&
628+ ! pending . requested &&
629+ list !== null &&
630+ canCompleteThreadWorkGroupAppend ( {
631+ targetContentHeight : pending . contentHeight ,
632+ nativeContentHeight : nativeContentHeightRef . current ,
633+ targetViewportHeight : pending . viewportHeight ,
634+ measuredViewportHeight : measuredViewportHeightRef . current ,
635+ userScrolling : userScrollingRef . current ,
636+ } )
623637 ) {
624- pendingAppendHeightRef . current = null ;
625- void listRef . current ?. scrollToEnd ( { animated : false } ) ;
638+ // The public request queues work; native arrival settles the intent.
639+ pending . requested = true ;
640+ void list . scrollToEnd ( { animated : false } ) ;
626641 }
627642 } ;
628643 // LegendList notifies from child layout effects. Effect Events see the
629644 // committed rows before those effects run, unlike a parent layout-effect ref.
630- const onContentSizeChange = useEffectEvent ( ( nextHeight : number ) => {
645+ const onComputedContentSizeChange = useEffectEvent ( ( nextHeight : number ) => {
631646 const previous = previousContent . current ;
632647 const detailsChanged = previous . expandedRows !== props . expandedRows ;
633648 const followAppend =
@@ -641,6 +656,7 @@ function ThreadWorkGroupList(props: {
641656 scrollOffset : scrollOffset . value ,
642657 detailsChanged,
643658 userScrolling : userScrollingRef . current ,
659+ followPending : pendingAppendRef . current !== null ,
644660 } ) ;
645661 previousContent . current = {
646662 rows : props . activities ,
@@ -653,13 +669,25 @@ function ThreadWorkGroupList(props: {
653669 : { height : nextHeight , rowCount : props . activities . length } ,
654670 ) ;
655671 if ( followAppend ) {
656- pendingAppendHeightRef . current = Math . min ( nextHeight , WORK_GROUP_MAX_HEIGHT ) ;
672+ pendingAppendRef . current = {
673+ contentHeight : nextHeight ,
674+ viewportHeight : Math . min ( nextHeight , WORK_GROUP_MAX_HEIGHT ) ,
675+ requested : false ,
676+ } ;
657677 } else if ( detailsChanged || userScrollingRef . current || previous . rows !== props . activities ) {
658- pendingAppendHeightRef . current = null ;
659- } else if ( pendingAppendHeightRef . current !== null ) {
660- pendingAppendHeightRef . current = Math . min ( nextHeight , WORK_GROUP_MAX_HEIGHT ) ;
678+ pendingAppendRef . current = null ;
679+ } else if (
680+ pendingAppendRef . current !== null &&
681+ ( pendingAppendRef . current . contentHeight !== nextHeight ||
682+ pendingAppendRef . current . viewportHeight !== Math . min ( nextHeight , WORK_GROUP_MAX_HEIGHT ) )
683+ ) {
684+ pendingAppendRef . current = {
685+ contentHeight : nextHeight ,
686+ viewportHeight : Math . min ( nextHeight , WORK_GROUP_MAX_HEIGHT ) ,
687+ requested : false ,
688+ } ;
661689 }
662- finishPendingAppend ( ) ;
690+ requestPendingAppend ( ) ;
663691 rememberPosition ( ) ;
664692 } ) ;
665693 // The native ScrollView reports its content size a frame or more after
@@ -671,9 +699,9 @@ function ThreadWorkGroupList(props: {
671699 const list = listRef . current ;
672700 if ( ! list ) return ;
673701 const unsubscribe = list . getState ( ) . listen ( "totalSize" , ( ) => {
674- onContentSizeChange ( list . getState ( ) . contentLength ) ;
702+ onComputedContentSizeChange ( list . getState ( ) . contentLength ) ;
675703 } ) ;
676- onContentSizeChange ( list . getState ( ) . contentLength ) ;
704+ onComputedContentSizeChange ( list . getState ( ) . contentLength ) ;
677705 return unsubscribe ;
678706 } , [ ] ) ;
679707 const getFixedItemSize = ( row : ThreadFeedActivity , index : number ) =>
@@ -704,23 +732,50 @@ function ThreadWorkGroupList(props: {
704732 extraData = { props . renderRow }
705733 renderItem = { renderItem }
706734 sharedValues = { sharedValues }
707- onLayout = { finishPendingAppend }
735+ onLayout = { ( event ) => {
736+ measuredViewportHeightRef . current = event . nativeEvent . layout . height ;
737+ requestPendingAppend ( ) ;
738+ } }
739+ onContentSizeChange = { ( _width , height ) => {
740+ nativeContentHeightRef . current = height ;
741+ requestPendingAppend ( ) ;
742+ } }
708743 onLoad = { ( ) => {
709744 loadedRef . current = true ;
710745 setRestoringPosition ( false ) ;
711746 rememberPosition ( ) ;
712747 } }
713- onScroll = { rememberPosition }
748+ onScroll = { ( event ) => {
749+ const pending = pendingAppendRef . current ;
750+ const { contentOffset, contentSize, layoutMeasurement } = event . nativeEvent ;
751+ if (
752+ pending !== null &&
753+ canCompleteThreadWorkGroupAppend ( {
754+ targetContentHeight : pending . contentHeight ,
755+ nativeContentHeight : contentSize . height ,
756+ targetViewportHeight : pending . viewportHeight ,
757+ measuredViewportHeight : layoutMeasurement . height ,
758+ userScrolling : userScrollingRef . current ,
759+ } ) &&
760+ Number . isFinite ( contentOffset . y ) &&
761+ Math . abs (
762+ contentOffset . y - Math . max ( 0 , contentSize . height - layoutMeasurement . height ) ,
763+ ) <= 1
764+ ) {
765+ pendingAppendRef . current = null ;
766+ }
767+ rememberPosition ( ) ;
768+ } }
714769 onScrollBeginDrag = { ( ) => {
715770 userScrollingRef . current = true ;
716- pendingAppendHeightRef . current = null ;
771+ pendingAppendRef . current = null ;
717772 } }
718773 onScrollEndDrag = { ( ) => {
719774 userScrollingRef . current = false ;
720775 } }
721776 onMomentumScrollBegin = { ( ) => {
722777 userScrollingRef . current = true ;
723- pendingAppendHeightRef . current = null ;
778+ pendingAppendRef . current = null ;
724779 } }
725780 onMomentumScrollEnd = { ( ) => {
726781 userScrollingRef . current = false ;
0 commit comments