Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions src/hooks/useSearchHighlightAndScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,26 @@ function useSearchHighlightAndScroll({
const previousTransactionIDsLocal = Object.keys(previousTransactions ?? {});
const transactionsIDs = Object.keys(transactions ?? {});

const reportActionsIDs = Object.values(reportActions ?? {})
.map((actions) => Object.keys(actions ?? {}))
.flat();
const previousReportActionsIDs = Object.values(previousReportActions ?? {})
.map((actions) => Object.keys(actions ?? {}))
.flat();

// Only proceed if we have previous data to compare against
// This prevents triggering on initial data load
const hasPreviousReportActions = Object.values(previousReportActions ?? {}).some((actions) => Object.keys(actions ?? {}).length > 0);
if ((previousTransactionIDsLocal.length === 0 && !hasPreviousReportActions) || searchTriggeredRef.current) {
if ((previousTransactionIDsLocal.length === 0 && previousReportActionsIDs.length === 0) || searchTriggeredRef.current) {
return;
}

// Only chat searches are driven by report actions, so the rest skip walking that collection entirely.
const reportActionsIDs = isChat ? Object.values(reportActions ?? {}).flatMap((actions) => Object.keys(actions ?? {})) : [];
const previousReportActionsIDs = isChat ? Object.values(previousReportActions ?? {}).flatMap((actions) => Object.keys(actions ?? {})) : [];

const previousTransactionsIDsSet = new Set(previousTransactionIDsLocal);
const previousReportActionsIDsSet = new Set(previousReportActionsIDs);
const hasTransactionsIDsChange = transactionsIDs.length !== previousTransactionIDsLocal.length || transactionsIDs.some((id) => !previousTransactionsIDsSet.has(id));
const hasReportActionsIDsChange = reportActionsIDs.some((id) => !previousReportActionsIDsSet.has(id));

// Check if there is a change in the transactions or report actions list
if ((isChat ? hasReportActionsIDsChange : hasTransactionsIDsChange) || hasPendingSearchRef.current) {
if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange || hasPendingSearchRef.current) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Gate report-action changes to chat searches

For a non-chat search this condition still enters when hasReportActionsIDsChange is true. The Search screen subscribes to the global report-actions collection and passes it into this hook (src/components/Search/index.tsx:170, src/components/Search/index.tsx:234-244), so sending or receiving any new chat action while an expense search page is open re-runs the current expense search even though no transaction changed; because newIDs below is the transaction key list, the later filter cannot recognize the report action as irrelevant. Keep report-action changes inside the chat branch.

Useful? React with 👍 / 👎.

// Skip if offline, or if the user has navigated to a different fullscreen page entirely.
// An RHP layered on top of Search makes `isFocused` false but keeps Search as the topmost
// fullscreen route, so we still want to refetch — otherwise the snapshot can't reflect
Expand All @@ -114,32 +116,19 @@ function useSearchHighlightAndScroll({
}
hasPendingSearchRef.current = false;

// Transaction Onyx keys are `transactions_<id>` but search results yield bare IDs, so read the ID off the value.
const addedTransactionIDs: string[] = [];
const currentTransactionIDs: string[] = [];
for (const [key, transaction] of Object.entries(transactions ?? {})) {
const transactionID = transaction?.transactionID;
if (!transactionID) {
continue;
}
currentTransactionIDs.push(transactionID);
if (!previousTransactionsIDsSet.has(key)) {
addedTransactionIDs.push(transactionID);
}
}

const newIDs = isChat ? reportActionsIDs : transactionsIDs;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Compare transaction IDs instead of Onyx keys

In the non-chat branch transactionsIDs are Onyx collection keys from ONYXKEYS.COLLECTION.TRANSACTION (for example transactions_123), while extractTransactionIDsFromSearchResults() returns bare transactionID values (for example 123). Comparing these at line 125 makes every existing local transaction look absent from the results, so adding a transaction that is already in the snapshot—or keeping any filtered-out transaction in Onyx—bypasses the “no genuinely new ID” skip and re-fires an identical query. Build newIDs from transaction.transactionID for added/current transactions instead.

Useful? React with 👍 / 👎.

let currentSearchResultIDs: string[] = [];
if (searchResultsData) {
currentSearchResultIDs = isChat ? extractReportActionIDsFromSearchResults(searchResultsData) : extractTransactionIDsFromSearchResults(searchResultsData);
}
const existingSearchResultIDsSet = new Set(currentSearchResultIDs);
const hasAGenuinelyNewID = (isChat ? reportActionsIDs : addedTransactionIDs).some((id) => !existingSearchResultIDsSet.has(id));
const hasAGenuinelyNewID = newIDs.some((id) => !existingSearchResultIDsSet.has(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 && currentSearchResultIDs.length > 0) {
const currentIDsSet = new Set(isChat ? reportActionsIDs : currentTransactionIDs);
const hasDeletedID = currentSearchResultIDs.some((id) => !currentIDsSet.has(id));
const newIDsSet = new Set(newIDs);
const hasDeletedID = currentSearchResultIDs.some((id) => !newIDsSet.has(id));
if (!hasDeletedID) {
return;
}
Expand Down
Loading
Loading