Repository navigation
Fix - Make it more apparent in the expense chat carousel view when expenses are added to a report #81446
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
Fix - Make it more apparent in the expense chat carousel view when expenses are added to a report #81446
Changes from all commits
965451f
679b291
361ddf8
940adc5
bd59c42
8708854
6f8f9bc
bc6bbb7
72a7b55
2139645
9a93f85
4d74c7c
ac72d5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import {useFocusEffect} from '@react-navigation/native'; | ||
| import {FlashList} from '@shopify/flash-list'; | ||
| import type {FlashListRef, ListRenderItemInfo} from '@shopify/flash-list'; | ||
| import React, {useCallback, useDeferredValue, useEffect, useMemo, useRef, useState} from 'react'; | ||
|
|
@@ -104,6 +105,7 @@ const reportAttributesSelector = (c: OnyxEntry<ReportAttributesDerivedValue>) => | |
|
|
||
| function MoneyRequestReportPreviewContent({ | ||
| iouReportID, | ||
| newTransactionIDs, | ||
| chatReportID, | ||
| action, | ||
| containerStyles, | ||
|
|
@@ -453,7 +455,7 @@ function MoneyRequestReportPreviewContent({ | |
| thumbsUpScale.set(isApprovedAnimationRunning ? withDelay(CONST.ANIMATION_THUMBS_UP_DELAY, withSpring(1, {duration: CONST.ANIMATION_THUMBS_UP_DURATION})) : 1); | ||
| }, [isApproved, isApprovedAnimationRunning, thumbsUpScale]); | ||
|
|
||
| const carouselTransactions = shouldShowAccessPlaceHolder ? [] : transactions.slice(0, 11); | ||
| const carouselTransactions = useMemo(() => (shouldShowAccessPlaceHolder ? [] : transactions.slice(0, 11)), [shouldShowAccessPlaceHolder, transactions]); | ||
| const prevCarouselTransactionLength = useRef(0); | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -480,6 +482,34 @@ function MoneyRequestReportPreviewContent({ | |
| return {itemVisiblePercentThreshold: 100}; | ||
| }, []); | ||
|
|
||
| const carouselTransactionsRef = useRef(carouselTransactions); | ||
|
|
||
| useEffect(() => { | ||
| carouselTransactionsRef.current = carouselTransactions; | ||
| }, [carouselTransactions]); | ||
|
|
||
| useFocusEffect( | ||
| useCallback(() => { | ||
| const index = carouselTransactions.findIndex((transaction) => newTransactionIDs?.has(transaction.transactionID)); | ||
|
|
||
| if (index < 0) { | ||
| return; | ||
| } | ||
| const newTransaction = carouselTransactions.at(index); | ||
|
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. ❌ PERFORMANCE-12 (docs)The Fix: Return a cleanup function from the callback that clears the timeout: useFocusEffect(
useCallback(() => {
const index = carouselTransactions.findIndex((transaction) => newTransactionIDs?.includes(transaction.transactionID));
if (index < 0) {
return;
}
const newTransaction = carouselTransactions.at(index);
const timeoutId = setTimeout(() => {
if (newTransaction?.transactionID !== carouselTransactionsRef.current.at(index)?.transactionID) {
return;
}
numberOfScrollToIndexFailed.current = 0;
carouselRef.current?.scrollToIndex({index, viewOffset: 2 * styles.gap2.gap, animated: true});
}, CONST.ANIMATED_TRANSITION);
return () => {
clearTimeout(timeoutId);
};
}, [newTransactionIDs]),
);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| setTimeout(() => { | ||
|
FitseTLT marked this conversation as resolved.
Resolved
|
||
| // If the new transaction is not available at the index it was on before the delay, avoid the scrolling | ||
| // because we are scrolling to either a wrong or unavailable transaction (which can cause crash). | ||
| if (newTransaction?.transactionID !== carouselTransactionsRef.current.at(index)?.transactionID) { | ||
| return; | ||
| } | ||
|
|
||
| carouselRef.current?.scrollToIndex({index, viewOffset: -2 * styles.gap2.gap, animated: true}); | ||
| }, CONST.ANIMATED_TRANSITION); | ||
|
|
||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [newTransactionIDs]), | ||
| ); | ||
|
|
||
| const onViewableItemsChanged = useRef(({viewableItems}: {viewableItems: ViewToken[]; changed: ViewToken[]}) => { | ||
| const newIndex = viewableItems.at(0)?.index; | ||
| if (typeof newIndex === 'number') { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,7 @@ function useNewTransactions(hasOnceLoadedReportActions: boolean | undefined, tra | |
| return CONST.EMPTY_ARRAY as unknown as Transaction[]; | ||
| } | ||
| return transactions.filter((transaction) => !prevTransactions?.some((prevTransaction) => prevTransaction.transactionID === transaction.transactionID)); | ||
| // Depending only on transactions is enough because prevTransactions is a helper object. | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [transactions]); | ||
| }, [transactions, prevTransactions]); | ||
|
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. Does this fix something?
Contributor
Author
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. Yes. Without |
||
|
|
||
| // In case when we have loaded the report, but there were no transactions in it, then we need to explicitly set skipFirstTransactionsChange to false, as it will be not set in the useMemo above. | ||
| useEffect(() => { | ||
|
|
||
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.
❌ PERF-2 (docs)
The
findIndexcallback performs an expensive operation (array lookup with.includes()) for every transaction without any early filtering. This could be optimized by convertingnewTransactionIDsto a Set for O(1) lookup instead of O(n) lookup with array.includes().Fix: Convert to a Set for faster lookups:
Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.