@@ -466,6 +466,34 @@ function MoneyRequestReportPreviewContent({
466466 carouselTransactionsRef . current = carouselTransactions ;
467467 } , [ carouselTransactions ] ) ;
468468
469+ useEffect ( ( ) => {
470+ const prevLength = prevCarouselTransactionLength . current ;
471+ const currentLength = carouselTransactions . length ;
472+
473+ // Reset to beginning if currentIndex is out of bounds for the new transactions list
474+ if ( currentIndex > 0 && ( currentLength === 0 || currentIndex >= currentLength ) ) {
475+ setCurrentIndex ( 0 ) ;
476+ setOptimisticIndex ( undefined ) ;
477+ setCurrentVisibleItems ( [ 0 ] ) ;
478+ // Scroll the FlatList back to the beginning
479+ if ( carouselRef . current && currentLength > 0 ) {
480+ carouselRef . current . scrollToIndex ( { index : 0 , animated : false } ) ;
481+ } else if ( carouselRef . current ) {
482+ // If there are no transactions, scroll to offset 0
483+ carouselRef . current . scrollToOffset ( { offset : 0 , animated : false } ) ;
484+ }
485+ }
486+ // Reset when transactions go from empty to having items (to ensure we start at the beginning)
487+ else if ( prevLength === 0 && currentLength > 0 && currentIndex !== 0 ) {
488+ setCurrentIndex ( 0 ) ;
489+ setOptimisticIndex ( undefined ) ;
490+ setCurrentVisibleItems ( [ 0 ] ) ;
491+ if ( carouselRef . current ) {
492+ carouselRef . current . scrollToIndex ( { index : 0 , animated : false } ) ;
493+ }
494+ }
495+ } , [ carouselTransactions . length , currentIndex ] ) ;
496+
469497 useFocusEffect (
470498 useCallback ( ( ) => {
471499 const index = carouselTransactions . findIndex ( ( transaction ) => newTransactionIDs ?. includes ( transaction . transactionID ) ) ;
0 commit comments