Skip to content

Commit 31dc747

Browse files
Merge pull request Expensify#82636 from TaduJR/chore-DRY-up-search-group-by-logic
[No-QA] chore: DRY up search group-by logic
2 parents 5d14d28 + 6470b92 commit 31dc747

3 files changed

Lines changed: 126 additions & 624 deletions

File tree

src/components/Search/index.tsx

Lines changed: 4 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ import {openOldDotLink} from '@libs/actions/Link';
3737
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
3838
import type {TransactionPreviewData} from '@libs/actions/Search';
3939
import {openSearch, setOptimisticDataForTransactionThreadPreview} from '@libs/actions/Search';
40-
import DateUtils from '@libs/DateUtils';
4140
import {canUseTouchScreen} from '@libs/DeviceCapabilities';
4241
import Log from '@libs/Log';
4342
import isSearchTopmostFullScreenRoute from '@libs/Navigation/helpers/isSearchTopmostFullScreenRoute';
4443
import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNavigation/types';
4544
import Performance from '@libs/Performance';
4645
import {isSplitAction} from '@libs/ReportSecondaryActionUtils';
4746
import {canEditFieldOfMoneyRequest, canHoldUnholdReportAction, canRejectReportAction, isOneTransactionReport, selectFilteredReportActions} from '@libs/ReportUtils';
48-
import {buildCannedSearchQuery, buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils';
47+
import {buildCannedSearchQuery, buildSearchQueryString} from '@libs/SearchQueryUtils';
4948
import {
50-
adjustTimeRangeToDateFilters,
5149
createAndOpenSearchTransactionThread,
5250
getColumnsToShow,
5351
getListItem,
@@ -60,19 +58,9 @@ import {
6058
isSearchDataLoaded,
6159
isSearchResultsEmpty as isSearchResultsEmptyUtil,
6260
isTaskListItemType,
63-
isTransactionCardGroupListItemType,
64-
isTransactionCategoryGroupListItemType,
6561
isTransactionGroupListItemType,
6662
isTransactionListItemType,
67-
isTransactionMemberGroupListItemType,
68-
isTransactionMerchantGroupListItemType,
69-
isTransactionMonthGroupListItemType,
70-
isTransactionQuarterGroupListItemType,
7163
isTransactionReportGroupListItemType,
72-
isTransactionTagGroupListItemType,
73-
isTransactionWeekGroupListItemType,
74-
isTransactionWithdrawalIDGroupListItemType,
75-
isTransactionYearGroupListItemType,
7664
shouldShowEmptyState,
7765
shouldShowYear as shouldShowYearUtil,
7866
} from '@libs/SearchUIUtils';
@@ -936,190 +924,11 @@ function Search({
936924
}
937925
}
938926

939-
if (isTransactionMemberGroupListItemType(item)) {
940-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM);
941-
newFlatFilters.push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM, filters: [{operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, value: item.accountID}]});
942-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
943-
const newQuery = buildSearchQueryString(newQueryJSON);
944-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
945-
if (!newQueryJSONWithHash) {
946-
return;
947-
}
948-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
949-
return;
950-
}
951-
952-
if (isTransactionCardGroupListItemType(item)) {
953-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID);
954-
newFlatFilters.push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID, filters: [{operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, value: item.cardID}]});
955-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
956-
const newQuery = buildSearchQueryString(newQueryJSON);
957-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
958-
if (!newQueryJSONWithHash) {
959-
return;
960-
}
961-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
962-
return;
963-
}
964-
965-
if (isTransactionWithdrawalIDGroupListItemType(item)) {
966-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.WITHDRAWAL_ID);
967-
newFlatFilters.push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.WITHDRAWAL_ID, filters: [{operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, value: item.entryID}]});
968-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
969-
const newQuery = buildSearchQueryString(newQueryJSON);
970-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
971-
if (!newQueryJSONWithHash) {
972-
return;
973-
}
974-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
975-
return;
976-
}
977-
978-
if (isTransactionCategoryGroupListItemType(item)) {
979-
const categoryValue = item.category === '' ? CONST.SEARCH.CATEGORY_EMPTY_VALUE : item.category;
980-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY);
981-
newFlatFilters.push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY, filters: [{operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, value: categoryValue}]});
982-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
983-
const newQuery = buildSearchQueryString(newQueryJSON);
984-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
985-
if (!newQueryJSONWithHash) {
986-
return;
987-
}
988-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
989-
return;
990-
}
991-
992-
if (isTransactionMerchantGroupListItemType(item)) {
993-
const merchantValue = item.merchant === '' ? CONST.SEARCH.MERCHANT_EMPTY_VALUE : item.merchant;
994-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT);
995-
newFlatFilters.push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT, filters: [{operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, value: merchantValue}]});
996-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
997-
const newQuery = buildSearchQueryString(newQueryJSON);
998-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
999-
if (!newQueryJSONWithHash) {
1000-
return;
1001-
}
1002-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
927+
if (isTransactionGroupListItemType(item) && !isTransactionReportGroupListItemType(item) && item.transactionsQueryJSON) {
928+
handleSearch({queryJSON: item.transactionsQueryJSON, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
1003929
return;
1004930
}
1005931

1006-
if (isTransactionTagGroupListItemType(item)) {
1007-
const tagValue = item.tag === '' || item.tag === '(untagged)' ? CONST.SEARCH.TAG_EMPTY_VALUE : item.tag;
1008-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG);
1009-
newFlatFilters.push({key: CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG, filters: [{operator: CONST.SEARCH.SYNTAX_OPERATORS.EQUAL_TO, value: tagValue}]});
1010-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
1011-
const newQuery = buildSearchQueryString(newQueryJSON);
1012-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
1013-
if (!newQueryJSONWithHash) {
1014-
return;
1015-
}
1016-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
1017-
return;
1018-
}
1019-
1020-
if (isTransactionMonthGroupListItemType(item)) {
1021-
// Extract the existing date filter to check for year-to-date or other date limits
1022-
const existingDateFilters = queryJSON.flatFilters.filter((filter) => filter.key === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1023-
const {start: monthStart, end: monthEnd} = adjustTimeRangeToDateFilters(DateUtils.getMonthDateRange(item.year, item.month), existingDateFilters);
1024-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1025-
newFlatFilters.push({
1026-
key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE,
1027-
filters: [
1028-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.GREATER_THAN_OR_EQUAL_TO, value: monthStart},
1029-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.LOWER_THAN_OR_EQUAL_TO, value: monthEnd},
1030-
],
1031-
});
1032-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
1033-
const newQuery = buildSearchQueryString(newQueryJSON);
1034-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
1035-
if (!newQueryJSONWithHash) {
1036-
return;
1037-
}
1038-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
1039-
return;
1040-
}
1041-
1042-
if (isTransactionWeekGroupListItemType(item)) {
1043-
if (!item.week) {
1044-
return;
1045-
}
1046-
// Extract the existing date filter to check for year-to-date or other date limits
1047-
const existingDateFilters = queryJSON.flatFilters.filter((filter) => filter.key === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1048-
const {start: weekStart, end: weekEnd} = adjustTimeRangeToDateFilters(DateUtils.getWeekDateRange(item.week), existingDateFilters);
1049-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1050-
newFlatFilters.push({
1051-
key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE,
1052-
filters: [
1053-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.GREATER_THAN_OR_EQUAL_TO, value: weekStart},
1054-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.LOWER_THAN_OR_EQUAL_TO, value: weekEnd},
1055-
],
1056-
});
1057-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
1058-
const newQuery = buildSearchQueryString(newQueryJSON);
1059-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
1060-
if (!newQueryJSONWithHash) {
1061-
return;
1062-
}
1063-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
1064-
return;
1065-
}
1066-
1067-
if (isTransactionYearGroupListItemType(item)) {
1068-
const yearGroupItem = item;
1069-
if (yearGroupItem.year === undefined) {
1070-
return;
1071-
}
1072-
// Extract the existing date filter to check for year-to-date or other date limits
1073-
const existingDateFilters = queryJSON.flatFilters.filter((filter) => filter.key === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1074-
const {start: yearStart, end: yearEnd} = adjustTimeRangeToDateFilters(DateUtils.getYearDateRange(yearGroupItem.year), existingDateFilters);
1075-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1076-
newFlatFilters.push({
1077-
key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE,
1078-
filters: [
1079-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.GREATER_THAN_OR_EQUAL_TO, value: yearStart},
1080-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.LOWER_THAN_OR_EQUAL_TO, value: yearEnd},
1081-
],
1082-
});
1083-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
1084-
const newQuery = buildSearchQueryString(newQueryJSON);
1085-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
1086-
if (!newQueryJSONWithHash) {
1087-
return;
1088-
}
1089-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
1090-
return;
1091-
}
1092-
1093-
if (isTransactionQuarterGroupListItemType(item)) {
1094-
const quarterGroupItem = item;
1095-
if (quarterGroupItem.year === undefined || quarterGroupItem.quarter === undefined) {
1096-
return;
1097-
}
1098-
// Extract the existing date filter to check for year-to-date or other date limits
1099-
const existingDateFilters = queryJSON.flatFilters.filter((filter) => filter.key === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1100-
const {start: quarterStart, end: quarterEnd} = adjustTimeRangeToDateFilters(
1101-
DateUtils.getQuarterDateRange(quarterGroupItem.year, quarterGroupItem.quarter),
1102-
existingDateFilters,
1103-
);
1104-
const newFlatFilters = queryJSON.flatFilters.filter((filter) => filter.key !== CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE);
1105-
newFlatFilters.push({
1106-
key: CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE,
1107-
filters: [
1108-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.GREATER_THAN_OR_EQUAL_TO, value: quarterStart},
1109-
{operator: CONST.SEARCH.SYNTAX_OPERATORS.LOWER_THAN_OR_EQUAL_TO, value: quarterEnd},
1110-
],
1111-
});
1112-
const newQueryJSON: SearchQueryJSON = {...queryJSON, groupBy: undefined, flatFilters: newFlatFilters};
1113-
const newQuery = buildSearchQueryString(newQueryJSON);
1114-
const newQueryJSONWithHash = buildSearchQueryJSON(newQuery);
1115-
if (!newQueryJSONWithHash) {
1116-
return;
1117-
}
1118-
handleSearch({queryJSON: newQueryJSONWithHash, searchKey, offset: 0, shouldCalculateTotals: false, isLoading: false});
1119-
return;
1120-
}
1121-
1122-
// After handling all group types, item should be TransactionListItemType, ReportActionListItemType, TaskListItemType, or TransactionGroupListItemType
1123932
if (!isTransactionItem && !isReportActionListItemType(item) && !isTaskListItemType(item) && !isTransactionGroupListItemType(item)) {
1124933
return;
1125934
}
@@ -1186,16 +995,7 @@ function Search({
1186995

1187996
requestAnimationFrame(() => Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID, backTo})));
1188997
},
1189-
[
1190-
isMobileSelectionModeEnabled,
1191-
markReportIDAsExpense,
1192-
toggleTransaction,
1193-
queryJSON,
1194-
handleSearch,
1195-
searchKey,
1196-
markReportIDAsMultiTransactionExpense,
1197-
unmarkReportIDAsMultiTransactionExpense,
1198-
],
998+
[isMobileSelectionModeEnabled, markReportIDAsExpense, toggleTransaction, handleSearch, searchKey, markReportIDAsMultiTransactionExpense, unmarkReportIDAsMultiTransactionExpense],
1199999
);
12001000

12011001
const currentColumns = useMemo(() => {

0 commit comments

Comments
 (0)