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
7 changes: 5 additions & 2 deletions src/components/MoneyReportHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import usePermissions from '@hooks/usePermissions';
import usePolicy from '@hooks/usePolicy';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSearchShouldCalculateTotals from '@hooks/useSearchShouldCalculateTotals';
import useSelectedTransactionsActions from '@hooks/useSelectedTransactionsActions';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -316,6 +317,7 @@ function MoneyReportHeader({
const [isRejectEducationalModalVisible, setIsRejectEducationalModalVisible] = useState(false);

const {selectedTransactionIDs, removeTransaction, clearSelectedTransactions, currentSearchQueryJSON, currentSearchKey} = useSearchContext();
const shouldCalculateTotals = useSearchShouldCalculateTotals(currentSearchKey, currentSearchQueryJSON?.similarSearchHash, true);

const {wideRHPRouteKeys} = useContext(WideRHPContext);
const shouldDisplayNarrowMoreButton = !shouldDisplayNarrowVersion || (wideRHPRouteKeys.length > 0 && !isSmallScreenWidth);
Expand Down Expand Up @@ -427,7 +429,7 @@ function MoneyReportHeader({
if (currentSearchQueryJSON) {
search({
searchKey: currentSearchKey,
shouldCalculateTotals: true,
shouldCalculateTotals,
offset: 0,
queryJSON: currentSearchQueryJSON,
isOffline,
Expand All @@ -445,6 +447,7 @@ function MoneyReportHeader({
moneyRequestReport,
introSelected,
existingB2BInvoiceReport,
shouldCalculateTotals,
activePolicy,
currentSearchQueryJSON,
currentSearchKey,
Expand Down Expand Up @@ -753,7 +756,7 @@ function MoneyReportHeader({
if (currentSearchQueryJSON) {
search({
searchKey: currentSearchKey,
shouldCalculateTotals: true,
shouldCalculateTotals,
offset: 0,
queryJSON: currentSearchQueryJSON,
isOffline,
Expand Down
38 changes: 2 additions & 36 deletions src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import useOnyx from '@hooks/useOnyx';
import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSearchHighlightAndScroll from '@hooks/useSearchHighlightAndScroll';
import useSearchShouldCalculateTotals from '@hooks/useSearchShouldCalculateTotals';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {openSearch, setOptimisticDataForTransactionThreadPreview} from '@libs/actions/Search';
Expand All @@ -31,7 +32,6 @@ import Performance from '@libs/Performance';
import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils';
import {canEditFieldOfMoneyRequest, selectArchivedReportsIdSet, selectFilteredReportActions} from '@libs/ReportUtils';
import {buildCannedSearchQuery, buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils';
import type {SearchKey} from '@libs/SearchUIUtils';
import {
createAndOpenSearchTransactionThread,
getColumnsToShow,
Expand Down Expand Up @@ -230,7 +230,6 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: true});
const previousTransactions = usePrevious(transactions);
const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {canBeMissing: true});
const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES, {canBeMissing: true});
const [outstandingReportsByPolicyID] = useOnyx(ONYXKEYS.DERIVED.OUTSTANDING_REPORTS_BY_POLICY_ID, {canBeMissing: true});
const [violations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true});

Expand Down Expand Up @@ -286,41 +285,8 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
const suggestedSearches = useMemo(() => getSuggestedSearches(accountID, defaultCardFeed?.id), [defaultCardFeed?.id, accountID]);

const searchKey = useMemo(() => Object.values(suggestedSearches).find((search) => search.similarSearchHash === similarSearchHash)?.key, [suggestedSearches, similarSearchHash]);
const shouldCalculateTotals = useMemo(() => {
if (offset !== 0) {
return false;
}

const savedSearchValues = Object.values(savedSearches ?? {});

if (!savedSearchValues.length && !searchKey) {
return false;
}

const eligibleSearchKeys: Partial<SearchKey[]> = [
CONST.SEARCH.SEARCH_KEYS.SUBMIT,
CONST.SEARCH.SEARCH_KEYS.APPROVE,
CONST.SEARCH.SEARCH_KEYS.PAY,
CONST.SEARCH.SEARCH_KEYS.EXPORT,
CONST.SEARCH.SEARCH_KEYS.STATEMENTS,
CONST.SEARCH.SEARCH_KEYS.UNAPPROVED_CASH,
CONST.SEARCH.SEARCH_KEYS.UNAPPROVED_CARD,
CONST.SEARCH.SEARCH_KEYS.RECONCILIATION,
];

if (eligibleSearchKeys.includes(searchKey)) {
return true;
}

for (const savedSearch of savedSearchValues) {
const searchData = buildSearchQueryJSON(savedSearch.query);
if (searchData && searchData.similarSearchHash === similarSearchHash) {
return true;
}
}

return false;
}, [offset, savedSearches, searchKey, similarSearchHash]);
const shouldCalculateTotals = useSearchShouldCalculateTotals(searchKey, similarSearchHash, offset === 0);

const previousReportActions = usePrevious(reportActions);
const reportActionsArray = useMemo(
Expand Down
50 changes: 50 additions & 0 deletions src/hooks/useSearchShouldCalculateTotals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {useMemo} from 'react';
import {buildSearchQueryJSON} from '@libs/SearchQueryUtils';
import type {SearchKey} from '@libs/SearchUIUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import useOnyx from './useOnyx';

function useSearchShouldCalculateTotals(searchKey: SearchKey | undefined, similarSearchHash: number | undefined, enabled: boolean) {
const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES, {canBeMissing: true});

const shouldCalculateTotals = useMemo(() => {
if (!enabled) {
return false;
}

const savedSearchValues = Object.values(savedSearches ?? {});

if (!savedSearchValues.length && !searchKey) {
return false;
}

const eligibleSearchKeys: Partial<SearchKey[]> = [
CONST.SEARCH.SEARCH_KEYS.SUBMIT,
CONST.SEARCH.SEARCH_KEYS.APPROVE,
CONST.SEARCH.SEARCH_KEYS.PAY,
CONST.SEARCH.SEARCH_KEYS.EXPORT,
CONST.SEARCH.SEARCH_KEYS.STATEMENTS,
CONST.SEARCH.SEARCH_KEYS.UNAPPROVED_CASH,
CONST.SEARCH.SEARCH_KEYS.UNAPPROVED_CARD,
CONST.SEARCH.SEARCH_KEYS.RECONCILIATION,
];

if (eligibleSearchKeys.includes(searchKey)) {
return true;
}

for (const savedSearch of savedSearchValues) {
const searchData = buildSearchQueryJSON(savedSearch.query);
if (searchData && searchData.similarSearchHash === similarSearchHash) {
return true;
}
}

return false;
}, [enabled, savedSearches, searchKey, similarSearchHash]);

return shouldCalculateTotals;
}

export default useSearchShouldCalculateTotals;
Loading