-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Optimize /Search call in useSearchHighlightAndScroll hook #60896
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 9 commits into
Expensify:main
from
callstack-internal:optimize-search-calls
Apr 30, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cc51de6
Optimize /Search call in useSearchHighlightAndScroll hook
martasudol 8d63364
Optimize /Search call in useSearchHighlightAndScroll hook
martasudol cd316ec
prettier fixes
martasudol c989d29
replace map with flatmap
martasudol 9223bb9
prettier fix
martasudol 0e8549a
Merge branch 'main' into optimize-search-calls
martasudol 30f8091
Merge branch 'main' into optimize-search-calls
martasudol 1fbbc2a
fix hook generic type
martasudol 58725f6
fix hook generic type
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {useEffect} from 'react'; | ||
import type {OnyxCollection} from 'react-native-onyx'; | ||
import type {SearchQueryJSON} from '@components/Search/types'; | ||
import {search} from '@libs/actions/Search'; | ||
import CONST from '@src/CONST'; | ||
import type {ReportActions, Transaction} from '@src/types/onyx'; | ||
import usePrevious from './usePrevious'; | ||
|
||
/** | ||
* Hook to detect new transactions or report actions from Pusher and trigger a search refresh | ||
*/ | ||
function useSearchPusherUpdates({ | ||
isOffline, | ||
queryJSON, | ||
transactions, | ||
reportActions, | ||
}: { | ||
isOffline: boolean; | ||
queryJSON: SearchQueryJSON; | ||
transactions?: OnyxCollection<Transaction>; | ||
reportActions?: OnyxCollection<ReportActions>; | ||
}) { | ||
const previousTransactions = usePrevious(transactions); | ||
const previousReportActions = usePrevious(reportActions); | ||
const isChat = queryJSON.type === CONST.SEARCH.DATA_TYPES.CHAT; | ||
|
||
// Detect Pusher updates and trigger search | ||
useEffect(() => { | ||
if (isOffline) { | ||
return; | ||
} | ||
|
||
// Only proceed if we have stable references to compare | ||
if (isChat && (!reportActions || !previousReportActions)) { | ||
return; | ||
} | ||
|
||
if (!isChat && (!transactions || !previousTransactions)) { | ||
return; | ||
} | ||
|
||
// For chat, check if there are new report actions | ||
if (isChat) { | ||
const currentReportActionsIDs = Object.values(reportActions ?? {}).flatMap((actions) => Object.keys(actions ?? {})); | ||
|
||
const previousReportActionsIDs = new Set(Object.values(previousReportActions ?? {}).flatMap((actions) => Object.keys(actions ?? {}))); | ||
|
||
// Only trigger search if Pusher added new report actions | ||
const hasNewReportActions = currentReportActionsIDs.length > previousReportActionsIDs.size && currentReportActionsIDs.some((id) => !previousReportActionsIDs.has(id)); | ||
|
||
if (hasNewReportActions) { | ||
search({queryJSON, offset: 0}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
// For expenses/transactions, check if there are new transactions | ||
const currentTransactionIDs = Object.keys(transactions ?? {}); | ||
const previousTransactionIDs = Object.keys(previousTransactions ?? {}); | ||
|
||
// Only trigger search if Pusher added new transactions | ||
const hasNewTransactions = currentTransactionIDs.length > previousTransactionIDs.length && currentTransactionIDs.some((id) => !previousTransactionIDs.includes(id)); | ||
|
||
if (hasNewTransactions) { | ||
search({queryJSON, offset: 0}); | ||
} | ||
}, [isOffline, queryJSON, transactions, previousTransactions, reportActions, previousReportActions, isChat]); | ||
} | ||
|
||
export default useSearchPusherUpdates; |
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.
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.
Actually, could you please add unit tests for this hook? Same as we have unit tests for the original hook, would be great to have that covered for this one too