diff --git a/src/components/Search/SearchList/ListItem/TransactionGroupListItem.tsx b/src/components/Search/SearchList/ListItem/TransactionGroupListItem.tsx index 4f0c54275053..8891206910bb 100644 --- a/src/components/Search/SearchList/ListItem/TransactionGroupListItem.tsx +++ b/src/components/Search/SearchList/ListItem/TransactionGroupListItem.tsx @@ -1,3 +1,4 @@ +import {useIsFocused} from '@react-navigation/native'; import React, {useEffect, useRef, useState} from 'react'; import {View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; @@ -86,6 +87,7 @@ function TransactionGroupListItem({ const {selectedTransactions} = useSearchStateContext(); const {isLargeScreenWidth} = useResponsiveLayout(); const currentUserDetails = useCurrentUserPersonalDetails(); + const isScreenFocused = useIsFocused(); const oneTransactionItem = groupItem.isOneTransactionReport ? groupItem.transactions.at(0) : undefined; const [parentReport] = originalUseOnyx(`${ONYXKEYS.COLLECTION.REPORT}${getNonEmptyStringOnyxID(oneTransactionItem?.reportID)}`); @@ -155,8 +157,7 @@ function TransactionGroupListItem({ const shouldDisplayEmptyView = isEmpty && isExpenseReportType; const isDisabledOrEmpty = isEmpty || isDisabled; - // Search transactions - handles both refresh (offset 0) and pagination (current offset + pageSize) - const searchTransactions = (pageSize = 0, isRefresh = false) => { + const refreshTransactions = () => { if (!groupItem.transactionsQueryJSON) { return; } @@ -164,7 +165,23 @@ function TransactionGroupListItem({ search({ queryJSON: groupItem.transactionsQueryJSON, searchKey: undefined, - offset: isRefresh ? 0 : (transactionsSnapshot?.search?.offset ?? 0) + pageSize, + offset: 0, + shouldCalculateTotals: false, + isLoading: !!transactionsSnapshot?.search?.isLoading, + isOffline, + }); + }; + + // Search transactions for pagination (current offset + pageSize) + const searchTransactions = (pageSize = 0) => { + if (!groupItem.transactionsQueryJSON) { + return; + } + + search({ + queryJSON: groupItem.transactionsQueryJSON, + searchKey: undefined, + offset: (transactionsSnapshot?.search?.offset ?? 0) + pageSize, shouldCalculateTotals: false, isLoading: !!transactionsSnapshot?.search?.isLoading, isOffline, @@ -189,15 +206,50 @@ function TransactionGroupListItem({ if (!newTransactionID || !isExpanded) { return; } - searchTransactions(0, true); - }, [newTransactionID, isExpanded, searchTransactions]); + if (!groupItem.transactionsQueryJSON) { + return; + } + + search({ + queryJSON: groupItem.transactionsQueryJSON, + searchKey: undefined, + offset: 0, + shouldCalculateTotals: false, + isLoading: !!transactionsSnapshot?.search?.isLoading, + isOffline, + }); + }, [newTransactionID, isExpanded, groupItem.transactionsQueryJSON, isOffline, transactionsSnapshot?.search?.isLoading]); + + const wasScreenFocusedRef = useRef(isScreenFocused); + useEffect(() => { + const didReturnToScreen = wasScreenFocusedRef.current === false && isScreenFocused === true; + wasScreenFocusedRef.current = isScreenFocused; + + if (!didReturnToScreen || !isExpanded || isExpenseReportType) { + return; + } + + // Keep expanded group rows in sync with updated grouped totals after returning from RHP flows. + if (!groupItem.transactionsQueryJSON) { + return; + } + + search({ + queryJSON: groupItem.transactionsQueryJSON, + searchKey: undefined, + offset: 0, + shouldCalculateTotals: false, + isLoading: !!transactionsSnapshot?.search?.isLoading, + isOffline, + }); + }, [isScreenFocused, isExpanded, isExpenseReportType, groupItem.transactionsQueryJSON, isOffline, transactionsSnapshot?.search?.isLoading]); const handleToggle = () => { setIsExpanded((prev) => { const newExpandedState = !prev; if (newExpandedState) { - searchTransactions(0, true); + refreshTransactions(); } else { setTransactionsVisibleLimit(CONST.TRANSACTION.RESULTS_PAGE_SIZE); }