1+ import type { ListRenderItem } from '@shopify/flash-list' ;
12import type { StoryFn } from '@storybook/react-webpack5' ;
2- import React from 'react' ;
3- import type { ListRenderItem } from 'react-native' ;
3+ import React , { useRef , useState } from 'react' ;
4+ import type { LayoutChangeEvent } from 'react-native' ;
45import { View } from 'react-native' ;
56import MoneyRequestReportPreviewContent from '@components/ReportActionItem/MoneyRequestReportPreview/MoneyRequestReportPreviewContent' ;
67import type { MoneyRequestReportPreviewContentProps } from '@components/ReportActionItem/MoneyRequestReportPreview/types' ;
78import TransactionPreviewContent from '@components/ReportActionItem/TransactionPreview/TransactionPreviewContent' ;
89import ThemeProvider from '@components/ThemeProvider' ;
910import ThemeStylesProvider from '@components/ThemeStylesContextProvider' ;
10- // eslint-disable-next-line no-restricted-imports
11- import getMoneyRequestReportPreviewStyle from '@styles/utils/getMoneyRequestReportPreviewStyle' ;
12- // eslint-disable-next-line no-restricted-imports
13- import sizing from '@styles/utils/sizing' ;
11+ import useStyleUtils from '@hooks/useStyleUtils' ;
12+ import useThemeStyles from '@hooks/useThemeStyles' ;
1413import CONST from '@src/CONST' ;
1514import SCREENS from '@src/SCREENS' ;
1615import type { Transaction } from '@src/types/onyx' ;
@@ -34,30 +33,6 @@ const mockTransactionsBig = Array.from({length: 12}).map((item, index) => {
3433 return { ...transactionR14932 , transactionID : `${ transactionR14932 . transactionID } ${ index } ` } ;
3534} ) ;
3635
37- const mockRenderItem : ListRenderItem < Transaction > = ( { item} ) => (
38- < TransactionPreviewContent
39- action = { actionR14932 }
40- isWhisper = { false }
41- isHovered = { false }
42- chatReport = { chatReportR14932 }
43- personalDetails = { personalDetails }
44- report = { iouReportR14932 }
45- transaction = { item }
46- transactionRawAmount = { item . amount }
47- violations = { item . errors ? violationsR14932 : [ ] }
48- offlineWithFeedbackOnClose = { ( ) => undefined }
49- navigateToReviewFields = { ( ) => undefined }
50- isBillSplit = { false }
51- areThereDuplicates = { false }
52- sessionAccountID = { 11111111 }
53- walletTermsErrors = { undefined }
54- routeName = { SCREENS . TRANSACTION_DUPLICATE . REVIEW }
55- shouldHideOnDelete = { false }
56- transactionPreviewWidth = { 303 }
57- containerStyles = { [ sizing . h100 ] }
58- />
59- ) ;
60-
6136type MoneyRequestReportPreviewStory = StoryFn < typeof MoneyRequestReportPreviewContent > ;
6237
6338export default {
@@ -123,7 +98,6 @@ export default {
12398 violations : violationsR14932 ,
12499 invoiceReceiverPersonalDetail : undefined ,
125100 invoiceReceiverPolicy : undefined ,
126- renderTransactionItem : mockRenderItem ,
127101 } ,
128102 parameters : {
129103 useLightTheme : true ,
@@ -133,17 +107,76 @@ export default {
133107function Template ( props : MoneyRequestReportPreviewContentProps , { parameters} : { parameters : { useLightTheme ?: boolean ; transactionsBig ?: boolean } } ) {
134108 const theme = parameters . useLightTheme ? CONST . THEME . LIGHT : CONST . THEME . DARK ;
135109 const transactions = parameters . transactionsBig ? mockTransactionsBig : props . transactions ;
136- const reportPreviewStyle = getMoneyRequestReportPreviewStyle ( false , transactions . length , 400 , 400 ) ;
110+
111+ const widthsRef = useRef < { currentWidth : number | null ; currentWrapperWidth : number | null } > ( { currentWidth : null , currentWrapperWidth : null } ) ;
112+ const [ widths , setWidths ] = useState ( { currentWidth : 0 , currentWrapperWidth : 0 } ) ;
113+
114+ const updateWidths = ( ) => {
115+ const { currentWidth, currentWrapperWidth} = widthsRef . current ;
116+
117+ if ( currentWidth && currentWrapperWidth ) {
118+ setWidths ( { currentWidth, currentWrapperWidth} ) ;
119+ }
120+ } ;
121+
122+ const onCarouselLayout = ( e : LayoutChangeEvent ) => {
123+ const newWidth = e . nativeEvent . layout . width ;
124+ if ( widthsRef . current . currentWidth !== newWidth ) {
125+ widthsRef . current . currentWidth = newWidth ;
126+ updateWidths ( ) ;
127+ }
128+ } ;
129+
130+ const onWrapperLayout = ( e : LayoutChangeEvent ) => {
131+ const newWrapperWidth = e . nativeEvent . layout . width ;
132+ if ( widthsRef . current . currentWrapperWidth !== newWrapperWidth ) {
133+ widthsRef . current . currentWrapperWidth = newWrapperWidth ;
134+ updateWidths ( ) ;
135+ }
136+ } ;
137+
138+ const StyleUtils = useStyleUtils ( ) ;
139+ const styles = useThemeStyles ( ) ;
140+ const reportPreviewStyles = StyleUtils . getMoneyRequestReportPreviewStyle ( false , transactions . length , widths . currentWidth , widths . currentWrapperWidth ) ;
141+ const transactionPreviewContainerStyles = [ styles . h100 , reportPreviewStyles . transactionPreviewCarouselStyle ] ;
142+
143+ const renderItem : ListRenderItem < Transaction > = ( { item} ) => (
144+ < TransactionPreviewContent
145+ action = { actionR14932 }
146+ isWhisper = { false }
147+ isHovered = { false }
148+ chatReport = { chatReportR14932 }
149+ personalDetails = { personalDetails }
150+ report = { iouReportR14932 }
151+ transaction = { item }
152+ transactionRawAmount = { item . amount }
153+ violations = { item . errors ? violationsR14932 : [ ] }
154+ offlineWithFeedbackOnClose = { ( ) => undefined }
155+ navigateToReviewFields = { ( ) => undefined }
156+ isBillSplit = { false }
157+ areThereDuplicates = { false }
158+ sessionAccountID = { 11111111 }
159+ walletTermsErrors = { undefined }
160+ routeName = { SCREENS . TRANSACTION_DUPLICATE . REVIEW }
161+ shouldHideOnDelete = { false }
162+ transactionPreviewWidth = { reportPreviewStyles . transactionPreviewCarouselStyle . width }
163+ containerStyles = { transactionPreviewContainerStyles }
164+ />
165+ ) ;
137166
138167 return (
139168 < ThemeProvider theme = { theme } >
140169 < ThemeStylesProvider >
141170 < View style = { { maxWidth : '100%' } } >
142171 < MoneyRequestReportPreviewContent
143172 { ...props }
144- reportPreviewStyles = { reportPreviewStyle }
145- containerStyles = { [ reportPreviewStyle . componentStyle , props . containerStyles ] }
173+ reportPreviewStyles = { reportPreviewStyles }
174+ containerStyles = { [ reportPreviewStyles . componentStyle , props . containerStyles ] }
146175 transactions = { transactions }
176+ onCarouselLayout = { onCarouselLayout }
177+ onWrapperLayout = { onWrapperLayout }
178+ currentWidth = { widths . currentWidth }
179+ renderTransactionItem = { renderItem }
147180 />
148181 </ View >
149182 </ ThemeStylesProvider >
0 commit comments