Skip to content

Commit 8add48d

Browse files
authored
Merge pull request Expensify#65486 from callstack-internal/perf/searchsidebar-optimize-loading-check
perf: optimize SearchSidebar loading logic
2 parents 5375a90 + 74827ee commit 8add48d

5 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/components/Navigation/SearchSidebar.tsx

Lines changed: 10 additions & 7 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,21 +43,24 @@ 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]);
5959

60-
const isDataLoaded = isSearchDataLoaded(currentSearchResults?.data ? currentSearchResults : lastNonEmptySearchResults, queryJSON);
60+
const searchResultsToUse = (currentSearchResults?.hasResults ?? currentSearchResults?.hasMoreResults) ? currentSearchResults : lastNonEmptySearchResults;
61+
62+
const isDataLoaded = isSearchDataLoaded(searchResultsToUse, queryJSON);
63+
6164
const shouldShowLoadingState = route?.name === SCREENS.SEARCH.MONEY_REQUEST_REPORT ? false : !isOffline && !isDataLoaded;
6265

6366
if (shouldUseNarrowLayout) {

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) ?? false;
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: 5 additions & 4 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,
@@ -1648,13 +1649,13 @@ function shouldShowEmptyState(isDataLoaded: boolean, dataLength: number, type: S
16481649
return !isDataLoaded || dataLength === 0 || !Object.values(CONST.SEARCH.DATA_TYPES).includes(type);
16491650
}
16501651

1651-
function isSearchDataLoaded(searchResults: SearchResults | undefined, queryJSON: SearchQueryJSON | undefined) {
1652+
function isSearchDataLoaded(searchResults: SearchResultsInfo | undefined, queryJSON: SearchQueryJSON | undefined) {
16521653
const {status} = queryJSON ?? {};
16531654

16541655
const isDataLoaded =
1655-
searchResults?.data !== undefined &&
1656-
searchResults?.search?.type === queryJSON?.type &&
1657-
(Array.isArray(status) ? searchResults?.search?.status === status.join(',') : searchResults?.search?.status === status);
1656+
(searchResults?.hasResults ?? searchResults?.hasMoreResults) &&
1657+
searchResults?.type === queryJSON?.type &&
1658+
(Array.isArray(status) ? searchResults?.status === status.join(',') : searchResults?.status === status);
16581659

16591660
return isDataLoaded;
16601661
}

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)