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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
isTripRoom as isTripRoomReportUtils,
isWaitingForSubmissionFromCurrentUser as isWaitingForSubmissionFromCurrentUserReportUtils,
} from '@libs/ReportUtils';
import shouldAdjustScroll from '@libs/shouldAdjustScroll';
import {hasPendingUI, isCardTransaction, isPending} from '@libs/TransactionUtils';
import colors from '@styles/theme/colors';
import variables from '@styles/variables';
Expand Down Expand Up @@ -330,6 +331,14 @@ function MoneyRequestReportPreviewContent({
}, [isApproved, isApprovedAnimationRunning, thumbsUpScale]);

const carouselTransactions = transactions.slice(0, 11);
const prevCarouselTransactionLength = useRef(0);

useEffect(() => {
return () => {
prevCarouselTransactionLength.current = carouselTransactions.length;
};
}, [carouselTransactions.length]);

const [currentIndex, setCurrentIndex] = useState(0);
const [currentVisibleItems, setCurrentVisibleItems] = useState([0]);
const [footerWidth, setFooterWidth] = useState(0);
Expand Down Expand Up @@ -570,6 +579,18 @@ function MoneyRequestReportPreviewContent({
),
};

const adjustScroll = useCallback(() => {
Comment thread
MonilBhavsar marked this conversation as resolved.
// Workaround for a known React Native bug on Android (https://github.com/facebook/react-native/issues/27504):
// When the FlatList is scrolled to the end and the last item is deleted, a blank space is left behind.
// To fix this, we detect when onEndReached is triggered due to an item deletion,
// and programmatically scroll to the end to fill the space.
if (carouselTransactions.length >= prevCarouselTransactionLength.current || !shouldAdjustScroll) {
return;
}
prevCarouselTransactionLength.current = carouselTransactions.length;
carouselRef.current?.scrollToEnd();
}, [carouselTransactions.length]);

return (
<View onLayout={onWrapperLayout}>
<OfflineWithFeedback
Expand Down Expand Up @@ -698,6 +719,7 @@ function MoneyRequestReportPreviewContent({
showsHorizontalScrollIndicator={false}
renderItem={renderFlatlistItem}
onViewableItemsChanged={onViewableItemsChanged}
onEndReached={adjustScroll}
viewabilityConfig={viewabilityConfig}
ListFooterComponent={<View style={styles.pl2} />}
ListHeaderComponent={<View style={styles.pr2} />}
Expand Down
1 change: 1 addition & 0 deletions src/libs/shouldAdjustScroll/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default true;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I decided to apply the fix for Android only because on Android mWeb, calling the scrollToEnd scrolls it to the start.

Also, on Web/Desktop, even without the fix, it's scrolled to the start.

1 change: 1 addition & 0 deletions src/libs/shouldAdjustScroll/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default false;