|
| 1 | +import React from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import {RESULTS} from 'react-native-permissions'; |
| 4 | +import AttachmentPicker from '@components/AttachmentPicker'; |
| 5 | +import Icon from '@components/Icon'; |
| 6 | +import ImageSVG from '@components/ImageSVG'; |
| 7 | +import PressableWithFeedback from '@components/Pressable/PressableWithFeedback'; |
| 8 | +import {useMemoizedLazyExpensifyIcons, useMemoizedLazyIllustrations} from '@hooks/useLazyAsset'; |
| 9 | +import useLocalize from '@hooks/useLocalize'; |
| 10 | +import useTheme from '@hooks/useTheme'; |
| 11 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 12 | +import variables from '@styles/variables'; |
| 13 | +import CONST from '@src/CONST'; |
| 14 | +import type {FileObject} from '@src/types/utils/Attachment'; |
| 15 | + |
| 16 | +type ScannerControlsBarProps = { |
| 17 | + /** Whether the device is currently in landscape orientation */ |
| 18 | + isInLandscapeMode: boolean; |
| 19 | + |
| 20 | + /** Whether multi-scan mode is currently active */ |
| 21 | + isMultiScanEnabled: boolean; |
| 22 | + |
| 23 | + /** Whether the multi-scan feature is available */ |
| 24 | + canUseMultiScan: boolean; |
| 25 | + |
| 26 | + /** Whether the attachment picker should allow selecting multiple files */ |
| 27 | + shouldAcceptMultipleFiles: boolean; |
| 28 | + |
| 29 | + /** Current camera permission status from react-native-permissions */ |
| 30 | + cameraPermissionStatus: string | null; |
| 31 | + |
| 32 | + /** Whether the camera flash is currently on */ |
| 33 | + flash: boolean; |
| 34 | + |
| 35 | + /** Whether the camera device supports flash */ |
| 36 | + hasFlash: boolean; |
| 37 | + |
| 38 | + /** Updater function to toggle flash state */ |
| 39 | + setFlash: (updater: (prev: boolean) => boolean) => void; |
| 40 | + |
| 41 | + /** Sets whether the attachment picker modal is open */ |
| 42 | + setIsAttachmentPickerActive: (value: boolean) => void; |
| 43 | + |
| 44 | + /** Sets visibility of the full-screen loading indicator */ |
| 45 | + setIsLoaderVisible: (value: boolean) => void; |
| 46 | + |
| 47 | + /** Validates picked files and begins the receipt upload flow */ |
| 48 | + validateFiles: (files: FileObject[], items?: DataTransferItem[]) => void; |
| 49 | + |
| 50 | + /** Triggers photo capture from the camera */ |
| 51 | + capturePhoto: () => void; |
| 52 | + |
| 53 | + /** Toggles multi-scan mode on or off */ |
| 54 | + toggleMultiScan: () => void; |
| 55 | +}; |
| 56 | + |
| 57 | +function ScannerControlsBar({ |
| 58 | + isInLandscapeMode, |
| 59 | + isMultiScanEnabled, |
| 60 | + canUseMultiScan, |
| 61 | + shouldAcceptMultipleFiles, |
| 62 | + cameraPermissionStatus, |
| 63 | + flash, |
| 64 | + hasFlash, |
| 65 | + setFlash, |
| 66 | + setIsAttachmentPickerActive, |
| 67 | + setIsLoaderVisible, |
| 68 | + validateFiles, |
| 69 | + capturePhoto, |
| 70 | + toggleMultiScan, |
| 71 | +}: ScannerControlsBarProps) { |
| 72 | + const theme = useTheme(); |
| 73 | + const styles = useThemeStyles(); |
| 74 | + const {translate} = useLocalize(); |
| 75 | + const lazyIllustrations = useMemoizedLazyIllustrations(['Shutter']); |
| 76 | + const lazyIcons = useMemoizedLazyExpensifyIcons(['Bolt', 'Gallery', 'ReceiptMultiple', 'boltSlash']); |
| 77 | + |
| 78 | + return ( |
| 79 | + <View style={[styles.justifyContentAround, styles.alignItemsCenter, styles.p3, !isInLandscapeMode && styles.flexRow]}> |
| 80 | + <AttachmentPicker |
| 81 | + onOpenPicker={() => { |
| 82 | + setIsAttachmentPickerActive(true); |
| 83 | + setIsLoaderVisible(true); |
| 84 | + }} |
| 85 | + fileLimit={shouldAcceptMultipleFiles ? CONST.API_ATTACHMENT_VALIDATIONS.MAX_FILE_LIMIT : 1} |
| 86 | + shouldValidateImage={false} |
| 87 | + > |
| 88 | + {({openPicker}) => ( |
| 89 | + <PressableWithFeedback |
| 90 | + role={CONST.ROLE.BUTTON} |
| 91 | + accessibilityLabel={translate('receipt.gallery')} |
| 92 | + sentryLabel={shouldAcceptMultipleFiles ? CONST.SENTRY_LABEL.REQUEST_STEP.SCAN.CHOOSE_FILES : CONST.SENTRY_LABEL.REQUEST_STEP.SCAN.CHOOSE_FILE} |
| 93 | + style={[styles.alignItemsStart, isMultiScanEnabled && styles.opacity0]} |
| 94 | + onPress={() => { |
| 95 | + openPicker({ |
| 96 | + onPicked: (data) => validateFiles(data), |
| 97 | + onCanceled: () => setIsLoaderVisible(false), |
| 98 | + // makes sure the loader is not visible anymore e.g. when there is an error while uploading a file |
| 99 | + onClosed: () => { |
| 100 | + setIsAttachmentPickerActive(false); |
| 101 | + setIsLoaderVisible(false); |
| 102 | + }, |
| 103 | + }); |
| 104 | + }} |
| 105 | + > |
| 106 | + <Icon |
| 107 | + height={variables.iconSizeMenuItem} |
| 108 | + width={variables.iconSizeMenuItem} |
| 109 | + src={lazyIcons.Gallery} |
| 110 | + fill={theme.textSupporting} |
| 111 | + /> |
| 112 | + </PressableWithFeedback> |
| 113 | + )} |
| 114 | + </AttachmentPicker> |
| 115 | + <PressableWithFeedback |
| 116 | + role={CONST.ROLE.BUTTON} |
| 117 | + accessibilityLabel={translate('receipt.shutter')} |
| 118 | + sentryLabel={CONST.SENTRY_LABEL.REQUEST_STEP.SCAN.SHUTTER} |
| 119 | + style={[styles.alignItemsCenter]} |
| 120 | + onPress={capturePhoto} |
| 121 | + > |
| 122 | + <ImageSVG |
| 123 | + contentFit="contain" |
| 124 | + src={lazyIllustrations.Shutter} |
| 125 | + width={CONST.RECEIPT.SHUTTER_SIZE} |
| 126 | + height={CONST.RECEIPT.SHUTTER_SIZE} |
| 127 | + /> |
| 128 | + </PressableWithFeedback> |
| 129 | + {canUseMultiScan ? ( |
| 130 | + <PressableWithFeedback |
| 131 | + accessibilityRole="button" |
| 132 | + role={CONST.ROLE.BUTTON} |
| 133 | + accessibilityLabel={translate('receipt.multiScan')} |
| 134 | + sentryLabel={CONST.SENTRY_LABEL.REQUEST_STEP.SCAN.MULTI_SCAN} |
| 135 | + style={styles.alignItemsEnd} |
| 136 | + onPress={toggleMultiScan} |
| 137 | + > |
| 138 | + <Icon |
| 139 | + height={variables.iconSizeMenuItem} |
| 140 | + width={variables.iconSizeMenuItem} |
| 141 | + src={lazyIcons.ReceiptMultiple} |
| 142 | + fill={isMultiScanEnabled ? theme.iconMenu : theme.textSupporting} |
| 143 | + /> |
| 144 | + </PressableWithFeedback> |
| 145 | + ) : ( |
| 146 | + <PressableWithFeedback |
| 147 | + role={CONST.ROLE.BUTTON} |
| 148 | + accessibilityLabel={translate('receipt.flash')} |
| 149 | + sentryLabel={CONST.SENTRY_LABEL.REQUEST_STEP.SCAN.FLASH} |
| 150 | + style={[styles.alignItemsEnd, !hasFlash && styles.opacity0]} |
| 151 | + disabled={cameraPermissionStatus !== RESULTS.GRANTED || !hasFlash} |
| 152 | + onPress={() => setFlash((prevFlash) => !prevFlash)} |
| 153 | + > |
| 154 | + <Icon |
| 155 | + height={variables.iconSizeMenuItem} |
| 156 | + width={variables.iconSizeMenuItem} |
| 157 | + src={flash ? lazyIcons.Bolt : lazyIcons.boltSlash} |
| 158 | + fill={theme.textSupporting} |
| 159 | + /> |
| 160 | + </PressableWithFeedback> |
| 161 | + )} |
| 162 | + </View> |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +export default ScannerControlsBar; |
0 commit comments