Skip to content
Closed
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
17 changes: 9 additions & 8 deletions src/hooks/useSearchHighlightAndScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans
// Ref to track if the search was triggered by this hook
const triggeredByHookRef = useRef(false);
const searchTriggeredRef = useRef(false);
const hasNewItemsRef = useRef(false);
const hasNewTransactionsRef = useRef(false);
const previousSearchResults = usePrevious(searchResults?.data);
const [newSearchResultKey, setNewSearchResultKey] = useState<string | null>(null);
const highlightedIDs = useRef<Set<string>>(new Set());
Expand All @@ -54,11 +54,12 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans

// Check if there is a change in the transactions or report actions list
if ((!isChat && hasTransactionsIDsChange) || hasReportActionsIDsChange) {
// 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.
hasNewItemsRef.current = isChat ? reportActionsIDs.length > previousReportActionsIDs.length : transactionsIDs.length > previousTransactionsIDs.length;

// We only want to highlight new transactions if the addition of transactions triggered the search.
// This is because, on deletion of transactions, the backend sometimes returns old transactions in place of the deleted ones
// but we don't want to highlight these old transactions, even if they appear as new in the current search results.
if (!isChat && hasTransactionsIDsChange) {
hasNewTransactionsRef.current = transactionsIDs.length > previousTransactionsIDs.length;
}
// Set the flag indicating the search is triggered by the hook
triggeredByHookRef.current = true;

Expand Down Expand Up @@ -98,7 +99,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans
// Find new report action IDs that are not in the previousReportActionIDs and not already highlighted
const newReportActionIDs = currentReportActionIDs.filter((id) => !previousReportActionIDs.includes(id) && !highlightedIDs.current.has(id));

if (!triggeredByHookRef.current || newReportActionIDs.length === 0 || !hasNewItemsRef.current) {
if (!triggeredByHookRef.current || newReportActionIDs.length === 0) {
return;
}

Expand All @@ -114,7 +115,7 @@ function useSearchHighlightAndScroll({searchResults, transactions, previousTrans
// Find new transaction IDs that are not in the previousTransactionIDs and not already highlighted
const newTransactionIDs = currentTransactionIDs.filter((id) => !previousTransactionIDs.includes(id) && !highlightedIDs.current.has(id));

if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasNewItemsRef.current) {
if (!triggeredByHookRef.current || newTransactionIDs.length === 0 || !hasNewTransactionsRef.current) {
return;
}

Expand Down