Skip to content

Commit a39773f

Browse files
committed
readability refactor
1 parent 9c42c02 commit a39773f

4 files changed

Lines changed: 42 additions & 64 deletions

File tree

src/libs/SearchQueryUtils.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,21 @@ function formatDefaultRawFilterSegment(rawFilter: RawQueryFilter, policies: Onyx
16031603
* We try to replace every numeric id value with a display version of this value,
16041604
* So: user IDs get turned into emails, report ids into report names etc.
16051605
*/
1606+
type BuildUserReadableQueryStringParams = {
1607+
queryJSON: SearchQueryJSON;
1608+
PersonalDetails: OnyxTypes.PersonalDetailsList | undefined;
1609+
reports: OnyxCollection<OnyxTypes.Report>;
1610+
taxRates: Record<string, string[]>;
1611+
cardList: OnyxTypes.CardList | undefined;
1612+
cardFeeds: OnyxCollection<OnyxTypes.CardFeeds>;
1613+
policies: OnyxCollection<OnyxTypes.Policy>;
1614+
currentUserAccountID: number;
1615+
autoCompleteWithSpace: boolean;
1616+
translate: LocalizedTranslate;
1617+
feedKeysWithCards?: FeedKeysWithAssignedCards;
1618+
reportAttributes: OnyxTypes.ReportAttributesDerivedValue['reports'] | undefined;
1619+
};
1620+
16061621
function buildUserReadableQueryString({
16071622
queryJSON,
16081623
PersonalDetails,
@@ -1616,20 +1631,7 @@ function buildUserReadableQueryString({
16161631
translate,
16171632
feedKeysWithCards,
16181633
reportAttributes,
1619-
}: {
1620-
queryJSON: SearchQueryJSON;
1621-
PersonalDetails: OnyxTypes.PersonalDetailsList | undefined;
1622-
reports: OnyxCollection<OnyxTypes.Report>;
1623-
taxRates: Record<string, string[]>;
1624-
cardList: OnyxTypes.CardList | undefined;
1625-
cardFeeds: OnyxCollection<OnyxTypes.CardFeeds>;
1626-
policies: OnyxCollection<OnyxTypes.Policy>;
1627-
currentUserAccountID: number;
1628-
autoCompleteWithSpace: boolean;
1629-
translate: LocalizedTranslate;
1630-
feedKeysWithCards?: FeedKeysWithAssignedCards;
1631-
reportAttributes: OnyxTypes.ReportAttributesDerivedValue['reports'] | undefined;
1632-
}) {
1634+
}: BuildUserReadableQueryStringParams) {
16331635
const {type, status, groupBy, view, columns, policyID, rawFilterList, flatFilters: filters = [], limit} = queryJSON;
16341636

16351637
if (rawFilterList && rawFilterList.length > 0) {
@@ -2145,4 +2147,6 @@ export {
21452147
isAmountFilterKey,
21462148
};
21472149

2150+
export type {BuildUserReadableQueryStringParams};
2151+
21482152
export type {SearchDateValues};

src/pages/Search/SavedSearchList.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ function SavedSearchList({hash}: SavedSearchListProps) {
120120

121121
const savedSearchTitles = useSavedSearchTitles({
122122
savedSearches,
123-
personalDetails,
123+
PersonalDetails: personalDetails,
124124
reports,
125125
taxRates,
126-
cardsForSavedSearchDisplay,
127-
allFeeds,
128-
allPolicies,
126+
cardList: cardsForSavedSearchDisplay,
127+
cardFeeds: allFeeds,
128+
policies: allPolicies,
129129
currentUserAccountID,
130130
translate,
131131
feedKeysWithCards,

src/pages/Search/SearchTypeMenuNarrow.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,12 @@ function SearchTypeMenuNarrow({queryJSON, onTabPress}: SearchTypeMenuNarrowProps
9393
const cardsForSavedSearchDisplay = mergeCardListWithWorkspaceFeeds(workspaceCardList ?? CONST.EMPTY_OBJECT, cardList);
9494
const savedSearchTitles = useSavedSearchTitles({
9595
savedSearches,
96-
personalDetails,
96+
PersonalDetails: personalDetails,
9797
reports,
9898
taxRates,
99-
cardsForSavedSearchDisplay,
100-
allFeeds,
101-
allPolicies,
99+
cardList: cardsForSavedSearchDisplay,
100+
cardFeeds: allFeeds,
101+
policies: allPolicies,
102102
currentUserAccountID,
103103
translate,
104104
feedKeysWithCards,

src/pages/Search/hooks/useSavedSearchTitles.ts

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
11
import {useDeferredValue} from 'react';
22
import {buildSearchQueryJSON, buildUserReadableQueryString} from '@libs/SearchQueryUtils';
3+
import type {BuildUserReadableQueryStringParams} from '@libs/SearchQueryUtils';
34
import type {SaveSearchItem} from '@src/types/onyx/SaveSearch';
45

56
type SavedSearchCollection = Record<string, SaveSearchItem>;
6-
type UserReadableQueryParams = Parameters<typeof buildUserReadableQueryString>[0];
77

8-
type SavedSearchTitlesHookParams = {
8+
type SavedSearchTitlesHookParams = Omit<BuildUserReadableQueryStringParams, 'queryJSON' | 'autoCompleteWithSpace'> & {
99
savedSearches: SavedSearchCollection | undefined;
10-
personalDetails: UserReadableQueryParams['PersonalDetails'];
11-
reports: UserReadableQueryParams['reports'];
12-
taxRates: UserReadableQueryParams['taxRates'];
13-
cardsForSavedSearchDisplay: UserReadableQueryParams['cardList'];
14-
allFeeds: UserReadableQueryParams['cardFeeds'];
15-
allPolicies: UserReadableQueryParams['policies'];
16-
currentUserAccountID: number;
17-
translate: UserReadableQueryParams['translate'];
18-
feedKeysWithCards: UserReadableQueryParams['feedKeysWithCards'];
19-
reportAttributes: UserReadableQueryParams['reportAttributes'];
2010
enabled?: boolean;
2111
};
2212

23-
function useSavedSearchTitles({
24-
savedSearches,
25-
personalDetails,
26-
reports,
27-
taxRates,
28-
cardsForSavedSearchDisplay,
29-
allFeeds,
30-
allPolicies,
31-
currentUserAccountID,
32-
translate,
33-
feedKeysWithCards,
34-
reportAttributes,
35-
enabled = true,
36-
}: SavedSearchTitlesHookParams): Map<string, string> {
37-
const deferredReports = useDeferredValue(reports);
38-
const deferredPolicies = useDeferredValue(allPolicies);
39-
const deferredPersonalDetails = useDeferredValue(personalDetails);
40-
const deferredReportAttributes = useDeferredValue(reportAttributes);
41-
const deferredTaxRates = useDeferredValue(taxRates);
42-
const deferredCardsForSavedSearchDisplay = useDeferredValue(cardsForSavedSearchDisplay);
43-
const deferredAllFeeds = useDeferredValue(allFeeds);
44-
const deferredFeedKeysWithCards = useDeferredValue(feedKeysWithCards);
13+
function useSavedSearchTitles({savedSearches, translate, enabled = true, ...rest}: SavedSearchTitlesHookParams): Map<string, string> {
14+
// `savedSearches` and `translate` are intentionally excluded from the deferred object.
15+
// `savedSearches` drives which items appear in the list — deferring it would cause a flash
16+
// `translate` is a stable function reference that never triggers heavy re-computation on its own.
17+
const deferredRest = useDeferredValue(rest);
18+
const {PersonalDetails, reports, taxRates, cardList, cardFeeds, policies, currentUserAccountID, feedKeysWithCards, reportAttributes} = deferredRest;
4519

4620
const titles = new Map<string, string>();
4721

@@ -61,17 +35,17 @@ function useSavedSearchTitles({
6135

6236
const title = buildUserReadableQueryString({
6337
queryJSON: itemJsonQuery,
64-
PersonalDetails: deferredPersonalDetails,
65-
reports: deferredReports,
66-
taxRates: deferredTaxRates,
67-
cardList: deferredCardsForSavedSearchDisplay,
68-
cardFeeds: deferredAllFeeds,
69-
policies: deferredPolicies,
38+
PersonalDetails,
39+
reports,
40+
taxRates,
41+
cardList,
42+
cardFeeds,
43+
policies,
7044
currentUserAccountID,
7145
autoCompleteWithSpace: false,
7246
translate,
73-
feedKeysWithCards: deferredFeedKeysWithCards,
74-
reportAttributes: deferredReportAttributes,
47+
feedKeysWithCards,
48+
reportAttributes,
7549
});
7650
titles.set(item.query, title);
7751
}

0 commit comments

Comments
 (0)