Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 13 additions & 2 deletions src/hooks/useSearchAutoRefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {search} from '@libs/actions/Search';
import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute';
import TransitionTracker from '@libs/Navigation/TransitionTracker';
import type {SearchKey} from '@libs/SearchKeyUtils';
import {isReportActionEntry} from '@libs/SearchUIUtils';
import {getValidGroupBy, isReportActionEntry} from '@libs/SearchUIUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
Expand Down Expand Up @@ -77,6 +77,7 @@ function useSearchAutoRefetch({
const searchTriggeredRef = useRef(false);
const hasPendingSearchRef = useRef(false);
const isChat = queryJSON.type === CONST.SEARCH.DATA_TYPES.CHAT;
const isGroupedSearch = !!getValidGroupBy(queryJSON.groupBy);
const shouldReturnTripRooms = useShouldReturnTripRooms(queryJSON.type);

const searchResultsData = searchResults?.data;
Expand Down Expand Up @@ -127,7 +128,9 @@ function useSearchAutoRefetch({
// The rows and the footer total are served from the Search snapshot, and the footer total is computed by the
// server, so those edits only become visible after a refetch.
const hasChangedResultTransaction =
!isChat && !hasTransactionsIDsChange && hasChangedTransactionInSearchResults(transactions, transactionsIDs, previousTransactions, previousTransactionsIDsSet, searchResultsData);
!isChat &&
!hasTransactionsIDsChange &&
hasChangedTransactionInSearchResults(transactions, transactionsIDs, previousTransactions, previousTransactionsIDsSet, searchResultsData, isGroupedSearch);
const hasChangedResultMemberCustomField = !isChat && hasChangedMemberCustomField(memberCustomFields, previousMemberCustomFields);

// Check if there is a change in the transactions or report actions list
Expand Down Expand Up @@ -200,6 +203,7 @@ function useSearchAutoRefetch({
memberCustomFields,
previousMemberCustomFields,
isChat,
isGroupedSearch,
searchResultsData,
isOffline,
searchResults?.search?.isLoading,
Expand Down Expand Up @@ -286,13 +290,17 @@ function extractReportActionIDsFromSearchResults(searchResultsData: Partial<Sear
* snapshot keys and never touches the transaction collection.
*
* A move counts too: the report row owes its count and total to the snapshot, so a transaction never on screen still invalidates it.
*
* Grouped results only hold the groups. Their transactions and reports live in each group's child snapshot, so neither
* check can tell whether a change touches a group, and any changed transaction counts.
*/
function hasChangedTransactionInSearchResults(
transactions: OnyxCollection<Transaction>,
transactionKeys: string[],
previousTransactions: OnyxCollection<Transaction>,
previousTransactionKeys: Set<string>,
searchResultsData: Partial<SearchResults['data']> | undefined,
isGroupedSearch: boolean,
): boolean {
if (!searchResultsData) {
return false;
Expand All @@ -307,6 +315,9 @@ function hasChangedTransactionInSearchResults(
if (!transaction?.transactionID || !previousTransactionKeys.has(key) || previousTransaction === transaction) {
continue;
}
if (isGroupedSearch) {
return true;
Comment thread
nkdengineer marked this conversation as resolved.
Resolved
}
if (transaction.reportID !== previousTransaction?.reportID && (isReportInSearchResults(transaction.reportID) || isReportInSearchResults(previousTransaction?.reportID))) {
return true;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/useSearchAutoRefetchTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {UseSearchAutoRefetch} from '@hooks/useSearchAutoRefetch';

import {search} from '@libs/actions/Search';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {MemberCustomFieldsByPolicyID} from '@src/selectors/Policy';
import type {Transaction} from '@src/types/onyx';
Expand Down Expand Up @@ -290,6 +291,29 @@ describe('useSearchAutoRefetch', () => {
expect(search).toHaveBeenCalledWith({queryJSON: baseProps.queryJSON, searchKey: undefined, offset: 0, shouldCalculateTotals: false, isLoading: false});
});

it.each([
['edited', {amount: 250}],
['removed from its report', {reportID: CONST.REPORT.UNREPORTED_REPORT_ID}],
])('should trigger search on a grouped search when a transaction is %s', (_, transactionChange) => {
// Given a search grouped by approver, whose snapshot only holds the groups and not their transactions or reports
const transaction = createMock<Transaction>({transactionID: '1', amount: 100, reportID: '10'});
const initialProps = createMock<UseSearchAutoRefetch>({
...baseProps,
queryJSON: {...baseProps.queryJSON, groupBy: CONST.SEARCH.GROUP_BY.APPROVER},
transactions: {transactions_1: transaction},
previousTransactions: {transactions_1: transaction},
});
const {rerender} = renderHook((props: UseSearchAutoRefetch) => useSearchAutoRefetch(props), {
initialProps,
});

// When the transaction changes
rerender(createMock<UseSearchAutoRefetch>({...initialProps, transactions: {transactions_1: {...transaction, ...transactionChange}}}));

// Then the search runs again, because the snapshot can't show whether the change touches one of its groups
expect(search).toHaveBeenCalledTimes(1);
});

it('should not trigger search when the edited transaction is absent from the results', () => {
const visibleTransaction = createMock<Transaction>({transactionID: '1', amount: 100});
const filteredOutTransaction = createMock<Transaction>({transactionID: '99', amount: 100});
Expand Down
Loading