|
| 1 | +import React, {useCallback, useEffect, useRef, useState} from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import type {LayoutChangeEvent} from 'react-native'; |
| 4 | +import {View} from 'react-native'; |
| 5 | +import Animated, {FadeIn, FadeOut} from 'react-native-reanimated'; |
| 6 | +import DistanceEReceipt from '@components/DistanceEReceipt'; |
| 7 | +import EReceipt from '@components/EReceipt'; |
| 8 | +import BaseImage from '@components/Image/BaseImage'; |
| 9 | +import type {ImageOnLoadEvent} from '@components/Image/types'; |
| 10 | +import useDebouncedState from '@hooks/useDebouncedState'; |
| 11 | +import useResponsiveLayout from '@hooks/useResponsiveLayout'; |
| 12 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 13 | +import useWindowDimensions from '@hooks/useWindowDimensions'; |
| 14 | +import {isDistanceRequest} from '@libs/TransactionUtils'; |
| 15 | +import variables from '@styles/variables'; |
| 16 | +import CONST from '@src/CONST'; |
| 17 | +import type {Transaction} from '@src/types/onyx'; |
| 18 | + |
| 19 | +const eReceiptAspectRatio = variables.eReceiptBGHWidth / variables.eReceiptBGHeight; |
| 20 | + |
| 21 | +type ReceiptPreviewProps = { |
| 22 | + /** Path to the image to be opened in the preview */ |
| 23 | + source: string; |
| 24 | + |
| 25 | + /** Whether the preview should be shown (e.g. if we are hovered over certain ReceiptCell) */ |
| 26 | + hovered: boolean; |
| 27 | + |
| 28 | + /** Is preview for an e-receipt */ |
| 29 | + isEReceipt: boolean; |
| 30 | + |
| 31 | + /** Transaction object related to the preview */ |
| 32 | + transactionItem: Transaction; |
| 33 | +}; |
| 34 | + |
| 35 | +function ReceiptPreview({source, hovered, isEReceipt = false, transactionItem}: ReceiptPreviewProps) { |
| 36 | + const isDistanceEReceipt = isDistanceRequest(transactionItem); |
| 37 | + const styles = useThemeStyles(); |
| 38 | + const [eReceiptScaleFactor, setEReceiptScaleFactor] = useState(0); |
| 39 | + const [imageAspectRatio, setImageAspectRatio] = useState<string | number | undefined>(undefined); |
| 40 | + const [distanceEReceiptAspectRatio, setDistanceEReceiptAspectRatio] = useState<string | number | undefined>(undefined); |
| 41 | + const [shouldShow, debounceShouldShow, setShouldShow] = useDebouncedState(false, CONST.TIMING.SHOW_HOVER_PREVIEW_DELAY); |
| 42 | + const {shouldUseNarrowLayout} = useResponsiveLayout(); |
| 43 | + const hasMeasured = useRef(false); |
| 44 | + const {windowHeight} = useWindowDimensions(); |
| 45 | + |
| 46 | + const handleDistanceEReceiptLayout = (e: LayoutChangeEvent) => { |
| 47 | + if (hasMeasured.current) { |
| 48 | + return; |
| 49 | + } |
| 50 | + hasMeasured.current = true; |
| 51 | + |
| 52 | + const {height, width} = e.nativeEvent.layout; |
| 53 | + if (height === 0) { |
| 54 | + // on the initial layout, measured height is 0, so we want to set everything on the second one |
| 55 | + hasMeasured.current = false; |
| 56 | + return; |
| 57 | + } |
| 58 | + if (height * eReceiptScaleFactor > windowHeight - CONST.RECEIPT_PREVIEW_TOP_BOTTOM_MARGIN) { |
| 59 | + setDistanceEReceiptAspectRatio(variables.eReceiptBGHWidth / (windowHeight - CONST.RECEIPT_PREVIEW_TOP_BOTTOM_MARGIN)); |
| 60 | + return; |
| 61 | + } |
| 62 | + setDistanceEReceiptAspectRatio(variables.eReceiptBGHWidth / height); |
| 63 | + setEReceiptScaleFactor(width / variables.eReceiptBGHWidth); |
| 64 | + }; |
| 65 | + |
| 66 | + const updateImageAspectRatio = useCallback( |
| 67 | + (width: number, height: number) => { |
| 68 | + if (!source) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + setImageAspectRatio(height ? width / height : 'auto'); |
| 73 | + }, |
| 74 | + [source], |
| 75 | + ); |
| 76 | + |
| 77 | + const handleLoad = useCallback( |
| 78 | + (event: ImageOnLoadEvent) => { |
| 79 | + const {width, height} = event.nativeEvent; |
| 80 | + |
| 81 | + updateImageAspectRatio(width, height); |
| 82 | + }, |
| 83 | + [updateImageAspectRatio], |
| 84 | + ); |
| 85 | + |
| 86 | + const handleEReceiptLayout = (e: LayoutChangeEvent) => { |
| 87 | + const {width} = e.nativeEvent.layout; |
| 88 | + setEReceiptScaleFactor(width / variables.eReceiptBGHWidth); |
| 89 | + }; |
| 90 | + |
| 91 | + useEffect(() => { |
| 92 | + setShouldShow(hovered); |
| 93 | + }, [hovered, setShouldShow]); |
| 94 | + |
| 95 | + if (shouldUseNarrowLayout || !debounceShouldShow || !shouldShow || (!source && !isEReceipt && !isDistanceEReceipt)) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + const shouldShowImage = source && !(isEReceipt || isDistanceEReceipt); |
| 100 | + const shouldShowDistanceEReceipt = isDistanceEReceipt && !isEReceipt; |
| 101 | + |
| 102 | + return ReactDOM.createPortal( |
| 103 | + <Animated.View |
| 104 | + entering={FadeIn.duration(CONST.TIMING.SHOW_HOVER_PREVIEW_ANIMATION_DURATION)} |
| 105 | + exiting={FadeOut.duration(CONST.TIMING.SHOW_HOVER_PREVIEW_ANIMATION_DURATION)} |
| 106 | + style={[styles.receiptPreview, styles.flexColumn, styles.alignItemsCenter, styles.justifyContentStart]} |
| 107 | + > |
| 108 | + {shouldShowImage ? ( |
| 109 | + <View style={[styles.w100]}> |
| 110 | + <BaseImage |
| 111 | + source={{uri: source}} |
| 112 | + style={[styles.w100, {aspectRatio: imageAspectRatio}]} |
| 113 | + onLoad={handleLoad} |
| 114 | + /> |
| 115 | + </View> |
| 116 | + ) : ( |
| 117 | + <View style={styles.receiptPreviewEReceiptsContainer}> |
| 118 | + {shouldShowDistanceEReceipt ? ( |
| 119 | + <View |
| 120 | + onLayout={handleDistanceEReceiptLayout} |
| 121 | + style={[{transformOrigin: 'center', scale: eReceiptScaleFactor, aspectRatio: distanceEReceiptAspectRatio}]} |
| 122 | + > |
| 123 | + <DistanceEReceipt |
| 124 | + transaction={transactionItem} |
| 125 | + hoverPreview |
| 126 | + /> |
| 127 | + </View> |
| 128 | + ) : ( |
| 129 | + <View |
| 130 | + onLayout={handleEReceiptLayout} |
| 131 | + style={[styles.receiptPreviewEReceipt, {aspectRatio: eReceiptAspectRatio, scale: eReceiptScaleFactor}]} |
| 132 | + > |
| 133 | + <EReceipt |
| 134 | + transactionID={transactionItem.transactionID} |
| 135 | + transactionItem={transactionItem} |
| 136 | + /> |
| 137 | + </View> |
| 138 | + )} |
| 139 | + </View> |
| 140 | + )} |
| 141 | + </Animated.View>, |
| 142 | + document.body, |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +export default ReceiptPreview; |
0 commit comments