Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -86,6 +87,7 @@ function TransactionGroupListItem<TItem extends ListItem>({
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)}`);
Expand Down Expand Up @@ -155,16 +157,31 @@ function TransactionGroupListItem<TItem extends ListItem>({
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;
}

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,
Expand All @@ -189,15 +206,50 @@ function TransactionGroupListItem<TItem extends ListItem>({
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);
}
Expand Down
Loading