From 46d3a81c889363adc5c0f648f775d31cb3d3658c Mon Sep 17 00:00:00 2001 From: MobileMage Date: Mon, 19 Jan 2026 21:39:23 +0100 Subject: [PATCH] fix: derive currentReceipt from page to fix wrong receipt deletion In the multi-scan flow, navigating between receipts with carousel arrows would update the page index but not the currentReceipt state, causing the delete action to remove the originally opened receipt instead of the currently viewed one. This change derives currentReceipt directly from the page index instead of storing it as separate state, ensuring it always reflects the currently displayed receipt. --- .../step/IOURequestStepScan/ReceiptView/index.tsx | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepScan/ReceiptView/index.tsx b/src/pages/iou/request/step/IOURequestStepScan/ReceiptView/index.tsx index b48ded136fef..1bd95d5209df 100644 --- a/src/pages/iou/request/step/IOURequestStepScan/ReceiptView/index.tsx +++ b/src/pages/iou/request/step/IOURequestStepScan/ReceiptView/index.tsx @@ -39,7 +39,6 @@ function ReceiptView({route}: ReceiptViewProps) { const {shouldShowArrows, setShouldShowArrows, autoHideArrows, cancelAutoHideArrows} = useCarouselArrows(); const styles = useThemeStyles(); const expensifyIcons = useMemoizedLazyExpensifyIcons(['Trashcan'] as const); - const [currentReceipt, setCurrentReceipt] = useState(); const [page, setPage] = useState(-1); const {showConfirmModal} = useConfirmModal(); @@ -47,17 +46,20 @@ function ReceiptView({route}: ReceiptViewProps) { selector: transactionDraftReceiptsViewSelector, canBeMissing: true, }); + + // Derive currentReceipt from page - always in sync with carousel position + const currentReceipt = page >= 0 ? receipts.at(page) : undefined; + const secondTransactionID = receipts.at(1)?.transactionID; const [secondTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${secondTransactionID}`, {canBeMissing: true}); + + // Set initial page based on route transactionID useEffect(() => { if (!receipts || receipts.length === 0) { return; } - const activeReceipt = receipts.find((receipt) => receipt.transactionID === route?.params?.transactionID); - const activeReceiptIndex = receipts.findIndex((receipt) => receipt.transactionID === activeReceipt?.transactionID); - - setCurrentReceipt(activeReceipt); + const activeReceiptIndex = receipts.findIndex((receipt) => receipt.transactionID === route?.params?.transactionID); setPage(activeReceiptIndex); }, [receipts, route?.params?.transactionID]);