Skip to content

Commit 41387a1

Browse files
committed
perf: optimize SearchSidebar loading logic
1 parent 56b3b68 commit 41387a1

6 files changed

Lines changed: 14 additions & 12 deletions

File tree

Mobile-Expensify

src/components/Navigation/SearchSidebar.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import SearchTypeMenu from '@pages/Search/SearchTypeMenu';
1616
import CONST from '@src/CONST';
1717
import ONYXKEYS from '@src/ONYXKEYS';
1818
import SCREENS from '@src/SCREENS';
19-
import type {SearchResults} from '@src/types/onyx';
19+
import type {SearchResultsInfo} from '@src/types/onyx/SearchResults';
2020
import NavigationTabBar from './NavigationTabBar';
2121
import NAVIGATION_TABS from './NavigationTabBar/NAVIGATION_TABS';
2222
import TopBar from './TopBar';
@@ -43,16 +43,16 @@ function SearchSidebar({state}: SearchSidebarProps) {
4343
}, [params?.q]);
4444

4545
const currentSearchResultsKey = queryJSON?.hash ?? CONST.DEFAULT_NUMBER_ID;
46-
const [currentSearchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchResultsKey}`, {canBeMissing: true});
47-
const [lastNonEmptySearchResults, setLastNonEmptySearchResults] = useState<SearchResults | undefined>(undefined);
46+
const [currentSearchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchResultsKey}`, {canBeMissing: true, selector: (snapshot) => snapshot?.search});
47+
const [lastNonEmptySearchResults, setLastNonEmptySearchResults] = useState<SearchResultsInfo | undefined>(undefined);
4848

4949
useEffect(() => {
50-
if (!currentSearchResults?.search?.type) {
50+
if (!currentSearchResults?.type) {
5151
return;
5252
}
5353

54-
setLastSearchType(currentSearchResults.search.type);
55-
if (currentSearchResults.data) {
54+
setLastSearchType(currentSearchResults.type);
55+
if (currentSearchResults.hasResults ?? currentSearchResults.hasMoreResults) {
5656
setLastNonEmptySearchResults(currentSearchResults);
5757
}
5858
}, [lastSearchType, queryJSON, setLastSearchType, currentSearchResults]);

src/components/Search/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ function Search({queryJSON, searchResults, onSearchListScroll, contentContainerS
238238

239239
// There's a race condition in Onyx which makes it return data from the previous Search, so in addition to checking that the data is loaded
240240
// we also need to check that the searchResults matches the type and status of the current search
241-
const isDataLoaded = isSearchDataLoaded(searchResults, queryJSON);
241+
const isDataLoaded = isSearchDataLoaded(searchResults?.search, queryJSON);
242242

243243
const shouldShowLoadingState = !isOffline && (!isDataLoaded || (!!searchResults?.search.isLoading && Array.isArray(searchResults?.data) && searchResults?.data.length === 0));
244244
const shouldShowLoadingMoreItems = !shouldShowLoadingState && searchResults?.search?.isLoading && searchResults?.search?.offset > 0;

src/libs/SearchUIUtils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import type {
3838
SearchPersonalDetails,
3939
SearchPolicy,
4040
SearchReport,
41+
SearchResultsInfo,
4142
SearchTask,
4243
SearchTransaction,
4344
SearchTransactionAction,
@@ -1650,9 +1651,9 @@ function isSearchDataLoaded(searchResults: SearchResults | undefined, queryJSON:
16501651
const {status} = queryJSON ?? {};
16511652

16521653
const isDataLoaded =
1653-
searchResults?.data !== undefined &&
1654-
searchResults?.search?.type === queryJSON?.type &&
1655-
(Array.isArray(status) ? searchResults?.search?.status === status.join(',') : searchResults?.search?.status === status);
1654+
(searchResults?.hasResults ?? searchResults?.hasMoreResults) &&
1655+
searchResults?.type === queryJSON?.type &&
1656+
(Array.isArray(status) ? searchResults?.status === status.join(',') : searchResults?.status === status);
16561657

16571658
return isDataLoaded;
16581659
}

src/pages/Search/SearchPageNarrow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function SearchPageNarrow({queryJSON, headerButtonsOptions, searchResults, isMob
132132
);
133133
}
134134

135-
const isDataLoaded = isSearchDataLoaded(searchResults, queryJSON);
135+
const isDataLoaded = isSearchDataLoaded(searchResults?.search, queryJSON);
136136
const shouldShowLoadingState = !isOffline && !isDataLoaded;
137137

138138
return (

src/types/onyx/SearchResults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,5 @@ export type {
515515
SearchReportAction,
516516
SearchPolicy,
517517
SearchCard,
518+
SearchResultsInfo,
518519
};

0 commit comments

Comments
 (0)