Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import React, {useRef, useState} from 'react';
import {PanResponder, View} from 'react-native';
import AttachmentPicker from '@components/AttachmentPicker';
import Button from '@components/Button';
import DragAndDropConsumer from '@components/DragAndDrop/Consumer';
import {useDragAndDropState} from '@components/DragAndDrop/Provider';
import DropZoneUI from '@components/DropZone/DropZoneUI';
import Icon from '@components/Icon';
import ReceiptAlternativeMethods from '@components/ReceiptAlternativeMethods';
import Text from '@components/Text';
import {useMemoizedLazyExpensifyIcons, useMemoizedLazyIllustrations} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import type {FileObject} from '@src/types/utils/Attachment';
import type {CameraProps} from './types';

/**
* FileUpload — desktop web capture variant.
* Renders a drag-and-drop zone + file picker button + receipt alternative methods.
*/
function FileUpload({onDrop, shouldAcceptMultipleFiles = false, onLayout}: CameraProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
const lazyIllustrations = useMemoizedLazyIllustrations(['ReceiptStack']);
const lazyIcons = useMemoizedLazyExpensifyIcons(['SmartScan']);

const panResponderRef = useRef(
PanResponder.create({
onPanResponderTerminationRequest: () => false,
}),
);

const {isDraggingOver} = useDragAndDropState();

// Height-based hiding for ReceiptAlternativeMethods
const [containerHeight, setContainerHeight] = useState(0);
const [uploadViewHeight, setUploadViewHeight] = useState(0);
const [altMethodsHeight, setAltMethodsHeight] = useState(0);
const chooseFilesPaddingVertical = Number(styles.chooseFilesView(false).paddingVertical);
const shouldHideAlternativeMethods = altMethodsHeight + uploadViewHeight + chooseFilesPaddingVertical * 2 > containerHeight;

const handleDrop = (e: DragEvent) => {
const rawFiles = Array.from(e?.dataTransfer?.files ?? []);
if (rawFiles.length === 0) {
return;
}
const files: FileObject[] = rawFiles.map((file) => {
// eslint-disable-next-line no-param-reassign
file.uri = URL.createObjectURL(file);
return file;
});

if (onDrop) {
onDrop(files, Array.from(e.dataTransfer?.items ?? []));
}
};

return (
<View
onLayout={(event) => {
setContainerHeight(event.nativeEvent.layout.height);
onLayout?.();
}}
style={[styles.flex1, styles.chooseFilesView(false)]}
>
<View style={[styles.flex1, styles.alignItemsCenter, styles.justifyContentCenter]}>
{!isDraggingOver && (
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We previously also used isDraggingOverWrapper from the StepScreenDragAndDropWrapper.

{!(isDraggingOver ?? isDraggingOverWrapper) && (

So, in prod, we get the isDraggingOver state from 2 providers.

1 from IOURequestStartPage, and 1 from the StepScreenDragAndDropWrapper. In this PR, we only get it from IOURequestStartPage.

<View
style={[styles.alignItemsCenter, styles.justifyContentCenter]}
onLayout={(e) => setUploadViewHeight(e.nativeEvent.layout.height)}
>
<Icon
src={lazyIllustrations.ReceiptStack}
width={CONST.RECEIPT.ICON_SIZE}
height={CONST.RECEIPT.ICON_SIZE}
/>
<View
style={[styles.uploadFileViewTextContainer, styles.userSelectNone]}
// eslint-disable-next-line react/jsx-props-no-spreading, react-hooks/refs
{...panResponderRef.current.panHandlers}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ CONSISTENCY-5 (docs)

The eslint-disable-next-line react/jsx-props-no-spreading, react-hooks/refs comment lacks a justification explaining why these rules are disabled.

Add a comment explaining the reason, for example:

{/* PanResponder handlers must be spread onto the View for gesture recognition */}
{/* eslint-disable-next-line react/jsx-props-no-spreading, react-hooks/refs */}
{...panResponderRef.current.panHandlers}

Reviewed at: 81d47b7 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.

>
<Text style={[styles.textFileUpload, styles.mb2]}>{translate(shouldAcceptMultipleFiles ? 'receipt.uploadMultiple' : 'receipt.upload')}</Text>
<Text style={[styles.textLabelSupporting, styles.textAlignCenter, styles.lineHeightLarge]}>
{translate(shouldAcceptMultipleFiles ? 'receipt.desktopSubtitleMultiple' : 'receipt.desktopSubtitleSingle')}
</Text>
</View>

<AttachmentPicker allowMultiple={shouldAcceptMultipleFiles}>
{({openPicker}) => (
<Button
success
text={translate(shouldAcceptMultipleFiles ? 'common.chooseFiles' : 'common.chooseFile')}
accessibilityLabel={translate(shouldAcceptMultipleFiles ? 'common.chooseFiles' : 'common.chooseFile')}
style={[styles.p5]}
onPress={() => {
openPicker({
onPicked: (data) => onDrop?.(data, []),
});
}}
sentryLabel={CONST.SENTRY_LABEL.IOU_REQUEST_STEP.SCAN_SUBMIT_BUTTON}
/>
)}
</AttachmentPicker>
</View>
)}
</View>
<DragAndDropConsumer onDrop={handleDrop}>
<DropZoneUI
icon={lazyIcons.SmartScan}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the plan for the "replace" case here?

icon={isReplacingReceipt ? lazyIcons.ReplaceReceipt : lazyIcons.SmartScan}

Maybe we can pass the icon and dropTitle as props?

dropStyles={styles.receiptDropOverlay(true)}
dropTitle={translate(shouldAcceptMultipleFiles ? 'dropzone.scanReceipts' : 'quickAction.scanReceipt')}
dropTextStyles={styles.receiptDropText}
dashedBorderStyles={[styles.dropzoneArea, styles.easeInOpacityTransition, styles.activeDropzoneDashedBorder(theme.receiptDropBorderColorActive, true)]}
/>
</DragAndDropConsumer>
{!shouldHideAlternativeMethods && <ReceiptAlternativeMethods onLayout={(e) => setAltMethodsHeight(e.nativeEvent.layout.height)} />}
</View>
);
}

FileUpload.displayName = 'FileUpload';

export default FileUpload;
Loading
Loading