Skip to content

Commit 67242be

Browse files
authored
Merge pull request Expensify#66624 from Expensify/revert-65486-perf/searchsidebar-optimize-loading-check
Revert "perf: optimize SearchSidebar loading logic"
2 parents 33fcc31 + 3c9cb6c commit 67242be

5 files changed

Lines changed: 13 additions & 18 deletions

File tree

src/components/Navigation/SearchSidebar.tsx

Lines changed: 7 additions & 10 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 {SearchResultsInfo} from '@src/types/onyx/SearchResults';
19+
import type {SearchResults} from '@src/types/onyx';
2020
import NavigationTabBar from './NavigationTabBar';
2121
import NAVIGATION_TABS from './NavigationTabBar/NAVIGATION_TABS';
2222
import TopBar from './TopBar';
@@ -43,24 +43,21 @@ 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, selector: (snapshot) => snapshot?.search});
47-
const [lastNonEmptySearchResults, setLastNonEmptySearchResults] = useState<SearchResultsInfo | undefined>(undefined);
46+
const [currentSearchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${currentSearchResultsKey}`, {canBeMissing: true});
47+
const [lastNonEmptySearchResults, setLastNonEmptySearchResults] = useState<SearchResults | undefined>(undefined);
4848

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

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

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

6663
if (shouldUseNarrowLayout) {

src/components/Search/index.tsx

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

262262
// 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
263263
// we also need to check that the searchResults matches the type and status of the current search
264-
const isDataLoaded = isSearchDataLoaded(searchResults?.search, queryJSON) ?? false;
264+
const isDataLoaded = isSearchDataLoaded(searchResults, queryJSON);
265265

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

src/libs/SearchUIUtils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import type {
3838
SearchPersonalDetails,
3939
SearchPolicy,
4040
SearchReport,
41-
SearchResultsInfo,
4241
SearchTask,
4342
SearchTransaction,
4443
SearchTransactionAction,
@@ -1720,13 +1719,13 @@ function shouldShowEmptyState(isDataLoaded: boolean, dataLength: number, type: S
17201719
return !isDataLoaded || dataLength === 0 || !Object.values(CONST.SEARCH.DATA_TYPES).includes(type);
17211720
}
17221721

1723-
function isSearchDataLoaded(searchResults: SearchResultsInfo | undefined, queryJSON: SearchQueryJSON | undefined) {
1722+
function isSearchDataLoaded(searchResults: SearchResults | undefined, queryJSON: SearchQueryJSON | undefined) {
17241723
const {status} = queryJSON ?? {};
17251724

17261725
const isDataLoaded =
1727-
(searchResults?.hasResults ?? searchResults?.hasMoreResults) &&
1728-
searchResults?.type === queryJSON?.type &&
1729-
(Array.isArray(status) ? searchResults?.status === status.join(',') : searchResults?.status === status);
1726+
searchResults?.data !== undefined &&
1727+
searchResults?.search?.type === queryJSON?.type &&
1728+
(Array.isArray(status) ? searchResults?.search?.status === status.join(',') : searchResults?.search?.status === status);
17301729

17311730
return isDataLoaded;
17321731
}

src/pages/Search/SearchPageNarrow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function SearchPageNarrow({queryJSON, headerButtonsOptions, searchResults, isMob
135135
);
136136
}
137137

138-
const isDataLoaded = isSearchDataLoaded(searchResults?.search, queryJSON);
138+
const isDataLoaded = isSearchDataLoaded(searchResults, queryJSON);
139139
const shouldShowLoadingState = !isOffline && !isDataLoaded;
140140

141141
return (

src/types/onyx/SearchResults.ts

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

0 commit comments

Comments
 (0)