|
1 | | -import React, {useCallback, useMemo, useState} from 'react'; |
| 1 | +import React, {useCallback, useMemo, useRef, useState} from 'react'; |
2 | 2 | import type {LayoutChangeEvent, ListRenderItem} from 'react-native'; |
3 | 3 | import {usePersonalDetails} from '@components/OnyxListItemProvider'; |
4 | 4 | import TransactionPreview from '@components/ReportActionItem/TransactionPreview'; |
@@ -52,11 +52,42 @@ function MoneyRequestReportPreview({ |
52 | 52 | const lastTransactionViolations = useTransactionViolations(lastTransaction?.transactionID); |
53 | 53 | const isTrackExpenseAction = isTrackExpenseActionReportActionsUtils(action); |
54 | 54 | const isSplitBillAction = isSplitBillActionReportActionsUtils(action); |
55 | | - const [currentWidth, setCurrentWidth] = useState<number>(0); |
56 | | - const [currentWrapperWidth, setCurrentWrapperWidth] = useState<number>(0); |
| 55 | + |
| 56 | + const widthsRef = useRef<{currentWidth: number | null; currentWrapperWidth: number | null}>({currentWidth: null, currentWrapperWidth: null}); |
| 57 | + const [widths, setWidths] = useState({currentWidth: 0, currentWrapperWidth: 0}); |
| 58 | + |
| 59 | + const updateWidths = useCallback(() => { |
| 60 | + const {currentWidth, currentWrapperWidth} = widthsRef.current; |
| 61 | + |
| 62 | + if (currentWidth && currentWrapperWidth) { |
| 63 | + setWidths({currentWidth, currentWrapperWidth}); |
| 64 | + } |
| 65 | + }, []); |
| 66 | + |
| 67 | + const onCarouselLayout = useCallback( |
| 68 | + (e: LayoutChangeEvent) => { |
| 69 | + const newWidth = e.nativeEvent.layout.width; |
| 70 | + if (widthsRef.current.currentWidth !== newWidth) { |
| 71 | + widthsRef.current.currentWidth = newWidth; |
| 72 | + updateWidths(); |
| 73 | + } |
| 74 | + }, |
| 75 | + [updateWidths], |
| 76 | + ); |
| 77 | + const onWrapperLayout = useCallback( |
| 78 | + (e: LayoutChangeEvent) => { |
| 79 | + const newWrapperWidth = e.nativeEvent.layout.width; |
| 80 | + if (widthsRef.current.currentWrapperWidth !== newWrapperWidth) { |
| 81 | + widthsRef.current.currentWrapperWidth = newWrapperWidth; |
| 82 | + updateWidths(); |
| 83 | + } |
| 84 | + }, |
| 85 | + [updateWidths], |
| 86 | + ); |
| 87 | + |
57 | 88 | const reportPreviewStyles = useMemo( |
58 | | - () => StyleUtils.getMoneyRequestReportPreviewStyle(shouldUseNarrowLayout, transactions.length, currentWidth, currentWrapperWidth), |
59 | | - [StyleUtils, currentWidth, currentWrapperWidth, shouldUseNarrowLayout, transactions.length], |
| 89 | + () => StyleUtils.getMoneyRequestReportPreviewStyle(shouldUseNarrowLayout, transactions.length, widths.currentWidth, widths.currentWrapperWidth), |
| 90 | + [StyleUtils, widths, shouldUseNarrowLayout, transactions.length], |
60 | 91 | ); |
61 | 92 | const shouldShowPayerAndReceiver = useMemo(() => { |
62 | 93 | if (!isIOUReport(iouReport) && action.childType !== CONST.REPORT.TYPE.IOU) { |
@@ -121,13 +152,9 @@ function MoneyRequestReportPreview({ |
121 | 152 | invoiceReceiverPolicy={invoiceReceiverPolicy} |
122 | 153 | lastTransactionViolations={lastTransactionViolations} |
123 | 154 | renderTransactionItem={renderItem} |
124 | | - onCarouselLayout={(e: LayoutChangeEvent) => { |
125 | | - setCurrentWidth(e.nativeEvent.layout.width); |
126 | | - }} |
127 | | - onWrapperLayout={(e: LayoutChangeEvent) => { |
128 | | - setCurrentWrapperWidth(e.nativeEvent.layout.width); |
129 | | - }} |
130 | | - currentWidth={currentWidth} |
| 155 | + onCarouselLayout={onCarouselLayout} |
| 156 | + onWrapperLayout={onWrapperLayout} |
| 157 | + currentWidth={widths.currentWidth} |
131 | 158 | reportPreviewStyles={reportPreviewStyles} |
132 | 159 | shouldDisplayContextMenu={shouldDisplayContextMenu} |
133 | 160 | isInvoice={isInvoice} |
|
0 commit comments