From f36d909764835fd13ca1a1612d27bf195f8811fc Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 22 Jul 2026 11:23:05 +0800 Subject: [PATCH 1/6] fix empty view shows when sorting --- src/libs/SearchQueryUtils.ts | 1 + src/libs/SearchUIUtils.ts | 14 +++++++++++--- src/types/onyx/SearchResults.ts | 8 +++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libs/SearchQueryUtils.ts b/src/libs/SearchQueryUtils.ts index 0d72081250fd..2b38e0884a06 100644 --- a/src/libs/SearchQueryUtils.ts +++ b/src/libs/SearchQueryUtils.ts @@ -2452,6 +2452,7 @@ export { getDateRangeDisplayValueFromFormValue, getRangeBoundariesFromFormValue, getRangeQueryValue, + getQueryHashes, isSearchDatePreset, getDateRangeForPreset, isFilterSupported, diff --git a/src/libs/SearchUIUtils.ts b/src/libs/SearchUIUtils.ts index dd5ebf6d9ef3..680a44313c4c 100644 --- a/src/libs/SearchUIUtils.ts +++ b/src/libs/SearchUIUtils.ts @@ -184,6 +184,7 @@ import { getDateRangeDisplayValueFromFormValue, getDateRangeForPreset, getFilterFromQuery, + getQueryHashes, isFilterNegatable, isFilterSupported, isSearchDatePreset, @@ -4807,9 +4808,16 @@ function shouldShowEmptyState(isDataLoaded: boolean, dataLength: number, type: S } function isSearchDataLoaded(searchResults: SearchResults | undefined, queryJSON: Readonly | undefined) { - const isDataLoaded = (searchResults?.data != null || searchResults?.errors != null) && searchResults?.search?.type === queryJSON?.type && searchResults.search.hash === queryJSON?.hash; - - return isDataLoaded; + return ( + (searchResults?.data != null || searchResults?.errors != null) && + searchResults.search.type === queryJSON?.type && + searchResults.search.hash === + getQueryHashes({ + ...queryJSON, + sortBy: searchResults.search.sortBy, + sortOrder: searchResults.search.sortOrder, + }).primaryHash + ); } function getValidGroupBy(groupBy: string | undefined): ValueOf | undefined { diff --git a/src/types/onyx/SearchResults.ts b/src/types/onyx/SearchResults.ts index e4d5cc7a9159..95e0af07a80a 100644 --- a/src/types/onyx/SearchResults.ts +++ b/src/types/onyx/SearchResults.ts @@ -5,7 +5,7 @@ import type { TransactionListItemType, TransactionReportGroupListItemType, } from '@components/Search/SearchList/ListItem/types'; -import type {SearchGroupBy} from '@components/Search/types'; +import type {SearchGroupBy, SearchSortBy, SortOrder} from '@components/Search/types'; import type CONST from '@src/CONST'; import type ONYXKEYS from '@src/ONYXKEYS'; @@ -59,6 +59,12 @@ type SearchResultsInfo = { /** Whether the search results are currently loading */ isLoading: boolean; + /** The sort by of the current sesarch */ + sortBy: SearchSortBy; + + /** The sort order of the current search */ + sortOrder: SortOrder; + /** Explicit terminal lifecycle state of the most recent search request for this snapshot. * Optional because snapshots persisted before this field existed (and snapshots written by * non-search actions) may not carry it. From cb1c8055849ec53c478e9862a1b6b8eefccc34e9 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 22 Jul 2026 11:36:31 +0800 Subject: [PATCH 2/6] add and update tests --- tests/unit/Search/SearchUIUtilsTest.ts | 62 ++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/tests/unit/Search/SearchUIUtilsTest.ts b/tests/unit/Search/SearchUIUtilsTest.ts index 56301741229f..3a5b82435d0a 100644 --- a/tests/unit/Search/SearchUIUtilsTest.ts +++ b/tests/unit/Search/SearchUIUtilsTest.ts @@ -32,7 +32,7 @@ import IntlStore from '@src/languages/IntlStore'; import type {CardFeedForDisplay} from '@src/libs/CardFeedUtils'; import {getCardDescriptionForSearchTable} from '@src/libs/CardUtils'; import DateUtils from '@src/libs/DateUtils'; -import {buildSearchQueryJSON, getDateRangeForPreset, getUserFriendlyValue} from '@src/libs/SearchQueryUtils'; +import {buildSearchQueryJSON, getDateRangeForPreset, getQueryHashes, getUserFriendlyValue} from '@src/libs/SearchQueryUtils'; import * as SearchUIUtils from '@src/libs/SearchUIUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; @@ -550,6 +550,8 @@ const searchResults: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', isLoading: false, type: 'expense', }, @@ -607,6 +609,8 @@ const searchResultsGroupByFrom: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 100, isLoading: false, type: 'expense', @@ -657,6 +661,8 @@ const searchResultsGroupByCard: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 60, isLoading: false, type: 'expense', @@ -694,6 +700,8 @@ const searchResultsGroupByWithdrawalID: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 60, isLoading: false, type: 'expense', @@ -726,6 +734,8 @@ const searchResultsGroupByCategory: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 325, isLoading: false, type: 'expense', @@ -1955,6 +1965,8 @@ const searchResultsGroupByMerchant: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 470, isLoading: false, type: 'expense', @@ -2012,6 +2024,8 @@ const searchResultsGroupByTag: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 325, isLoading: false, type: 'expense', @@ -2093,6 +2107,8 @@ const searchResultsGroupByMonth: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 325, isLoading: false, type: 'expense', @@ -2122,6 +2138,8 @@ const searchResultsGroupByYear: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 325, isLoading: false, type: 'expense', @@ -2153,6 +2171,8 @@ const searchResultsGroupByQuarter: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 325, isLoading: false, type: 'expense', @@ -2182,6 +2202,8 @@ const searchResultsGroupByWeek: OnyxTypes.SearchResults = { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', total: 325, isLoading: false, type: 'expense', @@ -8338,6 +8360,8 @@ describe('SearchUIUtils', () => { hash: queryJSON?.hash ?? 0, isLoading: false, type: CONST.SEARCH.DATA_TYPES.EXPENSE, + sortBy: queryJSON?.sortBy ?? 'date', + sortOrder: queryJSON?.sortOrder ?? 'desc', }, ...overrides, }; @@ -8359,18 +8383,46 @@ describe('SearchUIUtils', () => { it('should return false when the search type does not match the query type', () => { const results = makeSearchResults({ - search: {hasMoreResults: false, hasResults: true, offset: 0, hash: queryJSON?.hash ?? 0, isLoading: false, type: CONST.SEARCH.DATA_TYPES.CHAT}, + search: { + hasMoreResults: false, + hasResults: true, + offset: 0, + hash: queryJSON?.hash ?? 0, + sortBy: 'date', + sortOrder: 'desc', + isLoading: false, + type: CONST.SEARCH.DATA_TYPES.CHAT, + }, }); expect(SearchUIUtils.isSearchDataLoaded(results, queryJSON)).toBe(false); }); it('should return false when the search hash does not match the query hash', () => { const results = makeSearchResults({ - search: {hasMoreResults: false, hasResults: true, offset: 0, hash: (queryJSON?.hash ?? 0) + 1, isLoading: false, type: CONST.SEARCH.DATA_TYPES.EXPENSE}, + search: { + hasMoreResults: false, + hasResults: true, + offset: 0, + hash: (queryJSON?.hash ?? 0) + 1, + sortBy: 'date', + sortOrder: 'desc', + isLoading: false, + type: CONST.SEARCH.DATA_TYPES.EXPENSE, + }, }); expect(SearchUIUtils.isSearchDataLoaded(results, queryJSON)).toBe(false); }); + it('should return true even when the search sortBy/sortOrder is different from the query', () => { + const sortBy = CONST.SEARCH.TABLE_COLUMNS.MERCHANT; + const sortOrder = CONST.SEARCH.SORT_ORDER.ASC; + const expectedHash = queryJSON ? getQueryHashes({...queryJSON, sortBy, sortOrder}).primaryHash : 0; + const results = makeSearchResults({ + search: {hasMoreResults: false, hasResults: true, offset: 0, hash: expectedHash, isLoading: false, type: CONST.SEARCH.DATA_TYPES.EXPENSE, sortBy, sortOrder}, + }); + expect(SearchUIUtils.isSearchDataLoaded(results, queryJSON)).toBe(true); + }); + it('should return false when searchResults is undefined', () => { expect(SearchUIUtils.isSearchDataLoaded(undefined, queryJSON)).toBe(false); }); @@ -8417,6 +8469,8 @@ describe('SearchUIUtils', () => { type: 'expense', hash: 0, offset: 0, + sortBy: 'date', + sortOrder: 'desc', hasMoreResults: false, hasResults: true, isLoading: false, @@ -8549,6 +8603,8 @@ describe('SearchUIUtils', () => { type: 'expense', hash: 0, offset: 0, + sortBy: 'date', + sortOrder: 'desc', hasMoreResults: false, hasResults: true, isLoading: false, From 16879b7abaa74366403f5d0b7b61f86543a73b39 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 22 Jul 2026 11:39:36 +0800 Subject: [PATCH 3/6] fix typo --- src/types/onyx/SearchResults.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/onyx/SearchResults.ts b/src/types/onyx/SearchResults.ts index 95e0af07a80a..ec58b1026c52 100644 --- a/src/types/onyx/SearchResults.ts +++ b/src/types/onyx/SearchResults.ts @@ -59,7 +59,7 @@ type SearchResultsInfo = { /** Whether the search results are currently loading */ isLoading: boolean; - /** The sort by of the current sesarch */ + /** The sort by of the current search */ sortBy: SearchSortBy; /** The sort order of the current search */ From be2b37c6f76e929c9edcbd2338e3da9cc0149039 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 22 Jul 2026 11:54:02 +0800 Subject: [PATCH 4/6] fix type --- .../Search/SearchResultsProvider.tsx | 2 + .../HomePage/YourSpendSection/rowStateTest.ts | 4 ++ .../YourSpendSection/useYourSpendDataTest.ts | 58 +++++++++++++++++-- .../unit/Search/getSpendOverTimeStateTest.ts | 2 +- .../hooks/useSearchBulkActionsExportTest.ts | 2 + tests/unit/useSearchHighlightAndScrollTest.ts | 2 + 6 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/components/Search/SearchResultsProvider.tsx b/src/components/Search/SearchResultsProvider.tsx index c4965c2af4b0..1f596b3b1832 100644 --- a/src/components/Search/SearchResultsProvider.tsx +++ b/src/components/Search/SearchResultsProvider.tsx @@ -28,6 +28,8 @@ type SearchResultsProviderProps = { const defaultSearchInfo: SearchResultsInfo = { offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, hasMoreResults: false, hasResults: true, diff --git a/tests/unit/HomePage/YourSpendSection/rowStateTest.ts b/tests/unit/HomePage/YourSpendSection/rowStateTest.ts index 355f80de4f82..630d393611a7 100644 --- a/tests/unit/HomePage/YourSpendSection/rowStateTest.ts +++ b/tests/unit/HomePage/YourSpendSection/rowStateTest.ts @@ -10,6 +10,8 @@ const makeSearchResults = (overrides: Partial = {}): SearchResult search: { offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', type: 'expense', hasMoreResults: false, hasResults: true, @@ -25,6 +27,8 @@ const makeSearchResultsWithCount = (count: number): SearchResults => search: { offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', type: 'expense', hasMoreResults: false, hasResults: count > 0, diff --git a/tests/unit/HomePage/YourSpendSection/useYourSpendDataTest.ts b/tests/unit/HomePage/YourSpendSection/useYourSpendDataTest.ts index c8d594f1ae9c..16aa305f29cc 100644 --- a/tests/unit/HomePage/YourSpendSection/useYourSpendDataTest.ts +++ b/tests/unit/HomePage/YourSpendSection/useYourSpendDataTest.ts @@ -142,6 +142,8 @@ function makeSearchResultsWithCount(count: number): SearchResults { type: 'expense', offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', hasMoreResults: false, hasResults: count > 0, isLoading: false, @@ -591,7 +593,19 @@ describe('useYourSpendData — third-party cardRows', () => { mockedGetDisplayableThirdPartyCards.mockReturnValue(makeThirdPartyCards([{cardID: THIRD_PARTY_CARD_ID_1, lastFourPAN: THIRD_PARTY_LAST_FOUR_1}])); // First render: READY snapshot with count > 0 → row produced and total cached. setupCardSnapshot(THIRD_PARTY_CARD_ID_1, { - search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: 3, total: 1234, currency: 'USD'}, + search: { + type: 'expense', + offset: 0, + hash: 0, + sortBy: 'date', + sortOrder: 'desc', + hasMoreResults: false, + hasResults: true, + isLoading: false, + count: 3, + total: 1234, + currency: 'USD', + }, data: {}, }); const {result, rerender} = renderHook(() => useYourSpendData()); @@ -599,7 +613,19 @@ describe('useYourSpendData — third-party cardRows', () => { // Search screen wipes count/total/currency on the shared snapshot. setupCardSnapshot(THIRD_PARTY_CARD_ID_1, { - search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: undefined, total: undefined, currency: undefined}, + search: { + type: 'expense', + offset: 0, + hash: 0, + sortBy: 'date', + sortOrder: 'desc', + hasMoreResults: false, + hasResults: true, + isLoading: false, + count: undefined, + total: undefined, + currency: undefined, + }, data: {}, }); rerender(undefined); @@ -631,11 +657,35 @@ describe('useYourSpendData — third-party cardRows', () => { ]), ); setupCardSnapshot(THIRD_PARTY_CARD_ID_1, { - search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: 2, total: 500, currency: 'USD'}, + search: { + type: 'expense', + offset: 0, + hash: 0, + sortBy: 'date', + sortOrder: 'desc', + hasMoreResults: false, + hasResults: true, + isLoading: false, + count: 2, + total: 500, + currency: 'USD', + }, data: {}, }); setupCardSnapshot(THIRD_PARTY_CARD_ID_2, { - search: {type: 'expense', offset: 0, hash: 0, hasMoreResults: false, hasResults: true, isLoading: false, count: 3, total: 2200, currency: 'EUR'}, + search: { + type: 'expense', + offset: 0, + hash: 0, + sortBy: 'date', + sortOrder: 'desc', + hasMoreResults: false, + hasResults: true, + isLoading: false, + count: 3, + total: 2200, + currency: 'EUR', + }, data: {}, }); const {result} = renderHook(() => useYourSpendData()); diff --git a/tests/unit/Search/getSpendOverTimeStateTest.ts b/tests/unit/Search/getSpendOverTimeStateTest.ts index f4998f4392f3..c24378fef60c 100644 --- a/tests/unit/Search/getSpendOverTimeStateTest.ts +++ b/tests/unit/Search/getSpendOverTimeStateTest.ts @@ -20,7 +20,7 @@ const queryJSON: SearchQueryJSON = { }; const defaultSearchResults: SearchResults = { - search: {offset: 0, hash: 0, type: queryJSON.type, hasMoreResults: false, hasResults: true, isLoading: false}, + search: {offset: 0, hash: 0, sortBy: 'date', sortOrder: 'desc', type: queryJSON.type, hasMoreResults: false, hasResults: true, isLoading: false}, data: {}, }; diff --git a/tests/unit/hooks/useSearchBulkActionsExportTest.ts b/tests/unit/hooks/useSearchBulkActionsExportTest.ts index e39922e4977e..219ee64ce25c 100644 --- a/tests/unit/hooks/useSearchBulkActionsExportTest.ts +++ b/tests/unit/hooks/useSearchBulkActionsExportTest.ts @@ -323,6 +323,8 @@ function makeSearchResults(reports: Report[]): SearchResults { type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, hash: 0, offset: 0, + sortBy: 'date', + sortOrder: 'desc', hasMoreResults: false, hasResults: true, isLoading: false, diff --git a/tests/unit/useSearchHighlightAndScrollTest.ts b/tests/unit/useSearchHighlightAndScrollTest.ts index dc812f75fa56..2e1c2bd66cc9 100644 --- a/tests/unit/useSearchHighlightAndScrollTest.ts +++ b/tests/unit/useSearchHighlightAndScrollTest.ts @@ -46,6 +46,8 @@ describe('useSearchHighlightAndScroll', () => { hasResults: true, offset: 0, hash: 0, + sortBy: 'date', + sortOrder: 'desc', type: 'expense', isLoading: false, }, From 53601200b9421aac00918d0f518bebfa14b9bcd6 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 22 Jul 2026 12:10:35 +0800 Subject: [PATCH 5/6] fix test --- tests/ui/SearchPageTest.tsx | 2 ++ tests/unit/Search/getSpendOverTimeStateTest.ts | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/ui/SearchPageTest.tsx b/tests/ui/SearchPageTest.tsx index 80f7f450d0de..29971e022474 100644 --- a/tests/ui/SearchPageTest.tsx +++ b/tests/ui/SearchPageTest.tsx @@ -195,6 +195,8 @@ describe('SearchPageNarrow', () => { type: CONST.SEARCH.DATA_TYPES.CHAT, offset: 0, hash: failedQueryJSON?.hash, + sortBy: failedQueryJSON?.sortBy, + sortOrder: failedQueryJSON?.sortOrder, isLoading: false, hasMoreResults: false, }, diff --git a/tests/unit/Search/getSpendOverTimeStateTest.ts b/tests/unit/Search/getSpendOverTimeStateTest.ts index c24378fef60c..c64ee4484451 100644 --- a/tests/unit/Search/getSpendOverTimeStateTest.ts +++ b/tests/unit/Search/getSpendOverTimeStateTest.ts @@ -7,9 +7,9 @@ import type SearchResults from '@src/types/onyx/SearchResults'; const queryJSON: SearchQueryJSON = { inputQuery: '', - hash: 0, - recentSearchHash: 0, - similarSearchHash: 0, + hash: 1261888645, + recentSearchHash: 1239358332, + similarSearchHash: 1141398512, type: CONST.SEARCH.DATA_TYPES.EXPENSE, groupBy: CONST.SEARCH.GROUP_BY.MONTH, view: CONST.SEARCH.VIEW.LINE, @@ -20,7 +20,16 @@ const queryJSON: SearchQueryJSON = { }; const defaultSearchResults: SearchResults = { - search: {offset: 0, hash: 0, sortBy: 'date', sortOrder: 'desc', type: queryJSON.type, hasMoreResults: false, hasResults: true, isLoading: false}, + search: { + offset: 0, + hash: 1261888645, + sortBy: CONST.SEARCH.TABLE_COLUMNS.GROUP_MONTH, + sortOrder: CONST.SEARCH.SORT_ORDER.ASC, + type: queryJSON.type, + hasMoreResults: false, + hasResults: true, + isLoading: false, + }, data: {}, }; From 9f363cf16a572585e3b484ec1614c86d100c69f4 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Thu, 23 Jul 2026 13:18:53 +0800 Subject: [PATCH 6/6] use const --- src/components/Search/SearchResultsProvider.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Search/SearchResultsProvider.tsx b/src/components/Search/SearchResultsProvider.tsx index 1f596b3b1832..4105897f7b81 100644 --- a/src/components/Search/SearchResultsProvider.tsx +++ b/src/components/Search/SearchResultsProvider.tsx @@ -28,8 +28,8 @@ type SearchResultsProviderProps = { const defaultSearchInfo: SearchResultsInfo = { offset: 0, hash: 0, - sortBy: 'date', - sortOrder: 'desc', + sortBy: CONST.SEARCH.TABLE_COLUMNS.DATE, + sortOrder: CONST.SEARCH.SORT_ORDER.DESC, type: CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT, hasMoreResults: false, hasResults: true,