Repository navigation
fix: redundant /Search API calls when opening report in RHN
#62352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mountiny
merged 21 commits into
Expensify:main
from
callstack-internal:fix-redundant-search-api-calls
Jun 2, 2025
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4874dd5
fix: redundant `/Search` API calls when opening report in RHN
martasudol c0c392d
fix: redundant `/Search` API calls when opening report in RHN
martasudol 4e5b6c1
Merge branch 'main' into fix-redundant-search-api-calls
martasudol 6aa2970
Merge branch 'main' into fix-redundant-search-api-calls
martasudol 9ddb65c
fix: redundant `/Search` API calls when opening report in RHN
martasudol 94d0ded
unit test adjustment
martasudol 6ef6ca8
prettier
martasudol 43e6d55
Merge branch 'main' into fix-redundant-search-api-calls
martasudol a59c61a
use useDebouce custom hook for debouncing mechanism
martasudol e6806ca
remove redundant code comments
martasudol 8742487
fix: missing highlight of new expense
martasudol 805b24b
fix: highlighting new expense
martasudol 4957dc0
fix: highlighting
martasudol f08d4ba
prettier & eslint fixes
martasudol f157351
Merge branch 'main' into fix-redundant-search-api-calls
martasudol 51508d4
Merge branch 'main' into fix-redundant-search-api-calls
martasudol bfb5378
addressed minor suggestions
martasudol 2b222bc
eslint
martasudol b2ba015
handle expense deletion scenario
martasudol 4d8b0f3
handle expense deletion scenario
martasudol f99af05
Merge branch 'main' into fix-redundant-search-api-calls
martasudol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import {useIsFocused} from '@react-navigation/native'; | ||
| import isEqual from 'lodash/isEqual'; | ||
| import {useCallback, useEffect, useRef, useState} from 'react'; | ||
| import {useCallback, useEffect, useMemo, useRef, useState} from 'react'; | ||
| import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; | ||
| import type {SearchQueryJSON} from '@components/Search/types'; | ||
| import type {ReportListItemType, SearchListItem, SelectionListHandle, TransactionListItemType} from '@components/SelectionList/types'; | ||
|
|
@@ -24,6 +25,7 @@ type UseSearchHighlightAndScroll = { | |
| * Hook used to trigger a search when a new transaction or report action is added and handle highlighting and scrolling. | ||
| */ | ||
| function useSearchHighlightAndScroll({searchResults, transactions, previousTransactions, reportActions, previousReportActions, queryJSON, offset}: UseSearchHighlightAndScroll) { | ||
| const isFocused = useIsFocused(); | ||
| // Ref to track if the search was triggered by this hook | ||
| const triggeredByHookRef = useRef(false); | ||
| const searchTriggeredRef = useRef(false); | ||
|
|
@@ -34,6 +36,13 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans | |
| const initializedRef = useRef(false); | ||
| const isChat = queryJSON.type === CONST.SEARCH.DATA_TYPES.CHAT; | ||
|
|
||
| const existingSearchResultIDs = useMemo(() => { | ||
| if (!searchResults?.data) { | ||
| return []; | ||
| } | ||
| return isChat ? extractReportActionIDsFromSearchResults(searchResults.data) : extractTransactionIDsFromSearchResults(searchResults.data); | ||
| }, [searchResults?.data, isChat]); | ||
|
|
||
| // Trigger search when a new report action is added while on chat or when a new transaction is added for the other search types. | ||
| useEffect(() => { | ||
| const previousTransactionsIDs = Object.keys(previousTransactions ?? {}); | ||
|
|
@@ -46,19 +55,33 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans | |
| .map((actions) => Object.keys(actions ?? {})) | ||
| .flat(); | ||
|
|
||
| if (searchTriggeredRef.current) { | ||
| // Only proceed if we have previous data to compare against | ||
| // This prevents triggering on initial data load | ||
| if (previousTransactionsIDs.length === 0 && previousReportActionsIDs.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| const hasTransactionsIDsChange = !isEqual(transactionsIDs, previousTransactionsIDs); | ||
| const hasReportActionsIDsChange = !isEqual(reportActionsIDs, previousReportActionsIDs); | ||
|
|
||
| // NOTE: This if statement should NOT assume report actions can only change | ||
| // in one type, i.e.: isChat && hasReportActionsIDsChange | ||
| // because they can also change in other types such as CONST.SEARCH.DATA_TYPES.EXPENSE. | ||
| // Assuming they can only change in one type leads to issues such as | ||
| // https://github.com/Expensify/App/issues/57605 | ||
| // Check if there is a change in the transactions or report actions list | ||
| if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange) { | ||
| // If we're not focused, don't trigger search | ||
| if (!isFocused) { | ||
| return; | ||
| } | ||
|
|
||
| const newIDs = isChat ? reportActionsIDs : transactionsIDs; | ||
| const hasAGenuinelyNewID = newIDs.some((id) => !existingSearchResultIDs.includes(id)); | ||
|
|
||
| // Only skip search if there are no new items AND search results aren't empty | ||
| // This ensures deletions that result in empty data still trigger search | ||
| if (!hasAGenuinelyNewID && existingSearchResultIDs.length > 0) { | ||
| const hasDeletedID = existingSearchResultIDs.some((id) => !newIDs.includes(id)); | ||
|
Comment on lines
+74
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (!hasDeletedID) { | ||
| return; | ||
| } | ||
| } | ||
| // We only want to highlight new items if the addition of transactions or report actions triggered the search. | ||
| // This is because, on deletion of items, the backend sometimes returns old items in place of the deleted ones. | ||
| // We don't want to highlight these old items, even if they appear new in the current search results. | ||
|
|
@@ -73,23 +96,17 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans | |
| // Set the ref to prevent further triggers until reset | ||
| searchTriggeredRef.current = true; | ||
| } | ||
|
|
||
| // Reset the ref when transactions or report actions in chat search type are updated | ||
| return () => { | ||
| searchTriggeredRef.current = false; | ||
| }; | ||
| }, [transactions, previousTransactions, queryJSON, offset, reportActions, previousReportActions, isChat]); | ||
| }, [isFocused, transactions, previousTransactions, queryJSON, offset, reportActions, previousReportActions, isChat, searchResults?.data, existingSearchResultIDs]); | ||
|
|
||
| // Initialize the set with existing IDs only once | ||
| useEffect(() => { | ||
| if (initializedRef.current || !searchResults?.data) { | ||
| return; | ||
| } | ||
|
|
||
| const existingIDs = isChat ? extractReportActionIDsFromSearchResults(searchResults.data) : extractTransactionIDsFromSearchResults(searchResults.data); | ||
| highlightedIDs.current = new Set(existingIDs); | ||
| highlightedIDs.current = new Set(existingSearchResultIDs); | ||
| initializedRef.current = true; | ||
| }, [searchResults?.data, isChat]); | ||
| }, [searchResults?.data, isChat, existingSearchResultIDs]); | ||
|
|
||
| // Detect new items (transactions or report actions) | ||
| useEffect(() => { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The calculation of
hasAGenuinelyNewIDandhasDeletedIDhad O(n²) complexity in the worst case. We optimized them in PR #65727 by using sets.