diff --git a/src/libs/actions/IOU/SearchUpdate.ts b/src/libs/actions/IOU/SearchUpdate.ts index cf9d238ba918..3afdd085089b 100644 --- a/src/libs/actions/IOU/SearchUpdate.ts +++ b/src/libs/actions/IOU/SearchUpdate.ts @@ -1,4 +1,4 @@ -import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import type {SearchQueryJSON} from '@components/Search/types'; @@ -10,9 +10,18 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; import type {Participant} from '@src/types/onyx/IOU'; import type {OnyxData} from '@src/types/onyx/Request'; -import type {SearchResultDataType} from '@src/types/onyx/SearchResults'; +import type {SearchMemberGroup, SearchResultDataType} from '@src/types/onyx/SearchResults'; import {getCurrentUserPersonalDetails} from './index'; +let allSnapshots: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.SNAPSHOT, + waitForCollectionCallback: true, + callback: (value) => { + allSnapshots = value ?? {}; + }, +}); + type ExpenseReportStatusPredicate = (expenseReport: OnyxEntry, transactionReportID?: string) => boolean; const expenseReportStatusFilterMapping: Record = { @@ -202,6 +211,16 @@ function getSearchOnyxUpdate({ ]; if (currentSearchQueryJSON.groupBy === CONST.SEARCH.GROUP_BY.FROM) { + const groupKey = `${CONST.SEARCH.GROUP_PREFIX}${fromAccountID}`; + const currentSnapshot = allSnapshots[`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchQueryJSON.hash}`]; + const existingGroup = currentSnapshot?.data?.[groupKey] as SearchMemberGroup | undefined; + optimisticSnapshotData[groupKey] = { + accountID: fromAccountID, + count: (existingGroup?.count ?? 0) + 1, + total: (existingGroup?.total ?? 0) + (transaction.amount ?? 0), + currency: existingGroup?.currency ?? transaction.currency ?? CONST.CURRENCY.USD, + }; + const newFlatFilters = currentSearchQueryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM); newFlatFilters.push({ key: CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM,