-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix: Receipt validation not working in drag&drop section of report screen (AFTER REVERT) #73238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
df855f0
6478e8b
e866487
ac733c7
5499059
ee106ad
c09e14d
5b1d6a6
d09d3fd
14d18aa
92435d6
864a678
68ccdc9
a5b538b
653fd23
49f80f4
fd4d7ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,29 +16,41 @@ import { | |
| validateAttachment, | ||
| validateImageForCorruption, | ||
| } from '@libs/fileDownload/FileUtils'; | ||
| import type {ValidateAttachmentOptions} from '@libs/fileDownload/FileUtils'; | ||
| import convertHeicImage from '@libs/fileDownload/heicConverter'; | ||
| import Log from '@libs/Log'; | ||
| import CONST from '@src/CONST'; | ||
| import type {FileObject} from '@src/types/utils/Attachment'; | ||
| import useLocalize from './useLocalize'; | ||
| import useThemeStyles from './useThemeStyles'; | ||
|
|
||
| const DEFAULT_IS_VALIDATING_RECEIPTS = true; | ||
|
|
||
| type ErrorObject = { | ||
| error: ValueOf<typeof CONST.FILE_VALIDATION_ERRORS>; | ||
| fileExtension?: string; | ||
| }; | ||
|
|
||
| type ValidationOptions = { | ||
| isValidatingReceipts?: boolean; | ||
| }; | ||
|
|
||
| const sortFilesByOriginalOrder = (files: FileObject[], orderMap: Map<string, number>) => { | ||
| return files.sort((a, b) => (orderMap.get(a.uri ?? '') ?? 0) - (orderMap.get(b.uri ?? '') ?? 0)); | ||
| }; | ||
|
|
||
| function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTransferItems: DataTransferItem[]) => void, isValidatingReceipts = true) { | ||
| function useFilesValidation(onFilesValidated: (files: FileObject[], dataTransferItems: DataTransferItem[]) => void) { | ||
| const styles = useThemeStyles(); | ||
| const {translate} = useLocalize(); | ||
|
|
||
| const [isValidatingFiles, setIsValidatingFiles] = useState(false); | ||
| const [isValidatingReceipts, setIsValidatingReceipts] = useState<boolean>(); | ||
| const [isValidatingMultipleFiles, setIsValidatingMultipleFiles] = useState(false); | ||
|
|
||
| const [isErrorModalVisible, setIsErrorModalVisible] = useState(false); | ||
| const [fileError, setFileError] = useState<ValueOf<typeof CONST.FILE_VALIDATION_ERRORS> | null>(null); | ||
| const [pdfFilesToRender, setPdfFilesToRender] = useState<FileObject[]>([]); | ||
| const [validFilesToUpload, setValidFilesToUpload] = useState([] as FileObject[]); | ||
| const [isValidatingMultipleFiles, setIsValidatingMultipleFiles] = useState(false); | ||
| const [invalidFileExtension, setInvalidFileExtension] = useState(''); | ||
| const [errorQueue, setErrorQueue] = useState<ErrorObject[]>([]); | ||
| const [currentErrorIndex, setCurrentErrorIndex] = useState(0); | ||
|
|
@@ -71,11 +83,13 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| }, []); | ||
|
|
||
| const resetValidationState = useCallback(() => { | ||
| setIsValidatingFiles(false); | ||
| setIsValidatingReceipts(undefined); | ||
| setIsValidatingMultipleFiles(false); | ||
| setIsErrorModalVisible(false); | ||
| setPdfFilesToRender([]); | ||
| setIsLoaderVisible(false); | ||
| setValidFilesToUpload([]); | ||
| setIsValidatingMultipleFiles(false); | ||
| setFileError(null); | ||
| setInvalidFileExtension(''); | ||
| setErrorQueue([]); | ||
|
|
@@ -101,7 +115,7 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| setIsErrorModalVisible(true); | ||
| }; | ||
|
|
||
| const isValidFile = (originalFile: FileObject, item: DataTransferItem | undefined, isCheckingMultipleFiles?: boolean) => { | ||
| const isValidFile = (originalFile: FileObject, item: DataTransferItem | undefined, validationOptions: ValidateAttachmentOptions) => { | ||
| if (item && item.kind === 'file' && 'webkitGetAsEntry' in item) { | ||
| const entry = item.webkitGetAsEntry(); | ||
|
|
||
|
|
@@ -114,7 +128,7 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| return normalizeFileObject(originalFile) | ||
| .then((normalizedFile) => | ||
| validateImageForCorruption(normalizedFile).then(() => { | ||
| const error = validateAttachment(normalizedFile, isCheckingMultipleFiles, isValidatingReceipts); | ||
| const error = validateAttachment(normalizedFile, validationOptions); | ||
| if (error) { | ||
| const errorData = { | ||
| error, | ||
|
|
@@ -170,12 +184,12 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| } | ||
| } else if (validFiles.current.length > 0) { | ||
| const sortedFiles = sortFilesByOriginalOrder(validFiles.current, originalFileOrder.current); | ||
| proceedWithFilesAction(sortedFiles, dataTransferItemList.current); | ||
| onFilesValidated(sortedFiles, dataTransferItemList.current); | ||
| resetValidationState(); | ||
| } | ||
| }, [deduplicateErrors, pdfFilesToRender.length, proceedWithFilesAction, resetValidationState]); | ||
| }, [deduplicateErrors, pdfFilesToRender.length, onFilesValidated, resetValidationState]); | ||
|
|
||
| const validateAndResizeFiles = (files: FileObject[], items: DataTransferItem[]) => { | ||
| const validateAndResizeFiles = (files: FileObject[], items: DataTransferItem[], validationOptions?: ValidationOptions) => { | ||
| // Early return for empty files | ||
| if (files.length === 0) { | ||
| return; | ||
|
|
@@ -188,7 +202,13 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| originalFileOrder.current.set(file.uri ?? '', index); | ||
| }); | ||
|
|
||
| Promise.all(files.map((file, index) => isValidFile(file, items.at(index), files.length > 1).then((isValid) => (isValid ? file : null)))) | ||
| Promise.all( | ||
| files.map((file, index) => | ||
| isValidFile(file, items.at(index), {isValidatingMultipleFiles: files.length > 1, isValidatingReceipts: validationOptions?.isValidatingReceipts ?? isValidatingReceipts}).then( | ||
| (isValid) => (isValid ? file : null), | ||
| ), | ||
| ), | ||
| ) | ||
| .then((validationResults) => { | ||
| const filteredResults = validationResults.filter((result): result is FileObject => result !== null); | ||
| const pdfsToLoad = filteredResults.filter((file) => Str.isPDF(file.name ?? '')); | ||
|
|
@@ -258,14 +278,27 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| } | ||
| } else if (processedFiles.length > 0) { | ||
| const sortedFiles = sortFilesByOriginalOrder(processedFiles, originalFileOrder.current); | ||
| proceedWithFilesAction(sortedFiles, dataTransferItemList.current); | ||
| onFilesValidated(sortedFiles, dataTransferItemList.current); | ||
| resetValidationState(); | ||
| } | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| const validateFiles = (files: FileObject[], items?: DataTransferItem[]) => { | ||
| const validateFiles = (files: FileObject[], items?: DataTransferItem[], validationOptions?: ValidationOptions) => { | ||
| if (isValidatingFiles) { | ||
| Log.warn('Files are already being validated. Please wait for the current validation to complete before calling `validateFiles` again.'); | ||
| return; | ||
| } | ||
|
|
||
| setIsValidatingFiles(true); | ||
|
|
||
| const validationOptionsWithDefaults = { | ||
| ...validationOptions, | ||
| isValidatingReceipts: validationOptions?.isValidatingReceipts ?? DEFAULT_IS_VALIDATING_RECEIPTS, | ||
| }; | ||
| setIsValidatingReceipts(validationOptionsWithDefaults.isValidatingReceipts); | ||
|
|
||
| if (files.length > 1) { | ||
| setIsValidatingMultipleFiles(true); | ||
| } | ||
|
|
@@ -276,11 +309,11 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| } | ||
| setErrorAndOpenModal(CONST.FILE_VALIDATION_ERRORS.MAX_FILE_LIMIT_EXCEEDED); | ||
| } else { | ||
| validateAndResizeFiles(files, items ?? []); | ||
| validateAndResizeFiles(files, items ?? [], validationOptionsWithDefaults); | ||
| } | ||
| }; | ||
|
|
||
| const onConfirm = () => { | ||
| const onConfirmError = () => { | ||
| if (fileError === CONST.FILE_VALIDATION_ERRORS.MAX_FILE_LIMIT_EXCEEDED) { | ||
| setIsErrorModalVisible(false); | ||
| validateAndResizeFiles(filesToValidate.current, dataTransferItemList.current); | ||
|
|
@@ -304,18 +337,18 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| const sortedFiles = sortFilesByOriginalOrder(validFilesToUpload, originalFileOrder.current); | ||
| // If we're validating attachments we need to use InteractionManager to ensure | ||
| // the error modal is dismissed before opening the attachment modal | ||
| if (!isValidatingReceipts && fileError) { | ||
| if (isValidatingReceipts === false && fileError) { | ||
| setIsErrorModalVisible(false); | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| InteractionManager.runAfterInteractions(() => { | ||
| if (sortedFiles.length !== 0) { | ||
| proceedWithFilesAction(sortedFiles, dataTransferItemList.current); | ||
| onFilesValidated(sortedFiles, dataTransferItemList.current); | ||
| } | ||
| resetValidationState(); | ||
| }); | ||
| } else { | ||
| if (sortedFiles.length !== 0) { | ||
| proceedWithFilesAction(sortedFiles, dataTransferItemList.current); | ||
| onFilesValidated(sortedFiles, dataTransferItemList.current); | ||
| } | ||
| hideModalAndReset(); | ||
| } | ||
|
|
@@ -334,7 +367,7 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| }} | ||
| onPassword={() => { | ||
| validatedPDFs.current.push(file); | ||
| if (isValidatingReceipts) { | ||
| if (isValidatingReceipts === true) { | ||
| collectedErrors.current.push({error: CONST.FILE_VALIDATION_ERRORS.PROTECTED_FILE}); | ||
| } else { | ||
| validFiles.current.push(file); | ||
|
|
@@ -350,11 +383,11 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| )) | ||
| : undefined; | ||
|
|
||
| const getModalPrompt = useCallback(() => { | ||
| const getModalPrompt = () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove the useCallback here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the file can be successfully compiled with React Compiler and therefore we can remove manual memoization like in other callbacks. Now on a second look, there are still some usages of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok that makes sense. Thanks! @adamgrzybowski @kacper-mikolajczak Seems like we will need to tweak the AI reviewer to acknoledge this though I am not sure how easy it will be so the reviewer knows if the file is compilable with the react compiler |
||
| if (!fileError) { | ||
| return ''; | ||
| } | ||
| const prompt = getFileValidationErrorText(fileError, {fileType: invalidFileExtension}, isValidatingReceipts).reason; | ||
| const prompt = getFileValidationErrorText(fileError, {fileType: invalidFileExtension}, isValidatingReceipts === true).reason; | ||
| if (fileError === CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE_MULTIPLE || fileError === CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE) { | ||
| return ( | ||
| <Text> | ||
|
|
@@ -364,12 +397,12 @@ function useFilesValidation(proceedWithFilesAction: (files: FileObject[], dataTr | |
| ); | ||
| } | ||
| return prompt; | ||
| }, [fileError, invalidFileExtension, isValidatingReceipts, translate]); | ||
| }; | ||
|
|
||
| const ErrorModal = ( | ||
| <ConfirmModal | ||
| title={getFileValidationErrorText(fileError, {fileType: invalidFileExtension}, isValidatingReceipts).title} | ||
| onConfirm={onConfirm} | ||
| title={getFileValidationErrorText(fileError, {fileType: invalidFileExtension}, isValidatingReceipts === true).title} | ||
| onConfirm={onConfirmError} | ||
| onCancel={hideModalAndReset} | ||
| isVisible={isErrorModalVisible} | ||
| prompt={getModalPrompt()} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| /* eslint-disable @typescript-eslint/no-deprecated */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need this now.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the catch!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @parasharrajat Actually we do need this, my bad. I don't see that usages of |
||
| import {Str} from 'expensify-common'; | ||
| import {Alert, Linking, Platform} from 'react-native'; | ||
| import type {ReactNativeBlobUtilReadStream} from 'react-native-blob-util'; | ||
|
|
@@ -549,18 +550,23 @@ const normalizeFileObject = (file: FileObject): Promise<FileObject> => { | |
| }); | ||
| }; | ||
|
|
||
| const validateAttachment = (file: FileObject, isCheckingMultipleFiles?: boolean, isValidatingReceipt?: boolean) => { | ||
| const maxFileSize = isValidatingReceipt ? CONST.API_ATTACHMENT_VALIDATIONS.RECEIPT_MAX_SIZE : CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE; | ||
| type ValidateAttachmentOptions = { | ||
| isValidatingReceipts?: boolean; | ||
| isValidatingMultipleFiles?: boolean; | ||
| }; | ||
|
|
||
| const validateAttachment = (file: FileObject, validationOptions?: ValidateAttachmentOptions) => { | ||
| const maxFileSize = validationOptions?.isValidatingReceipts ? CONST.API_ATTACHMENT_VALIDATIONS.RECEIPT_MAX_SIZE : CONST.API_ATTACHMENT_VALIDATIONS.MAX_SIZE; | ||
|
|
||
| if (isValidatingReceipt && !isValidReceiptExtension(file)) { | ||
| return isCheckingMultipleFiles ? CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE_MULTIPLE : CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE; | ||
| if (validationOptions?.isValidatingReceipts && !isValidReceiptExtension(file)) { | ||
| return validationOptions?.isValidatingMultipleFiles ? CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE_MULTIPLE : CONST.FILE_VALIDATION_ERRORS.WRONG_FILE_TYPE; | ||
| } | ||
|
|
||
| if (!Str.isImage(file.name ?? '') && !hasHeicOrHeifExtension(file) && (file?.size ?? 0) > maxFileSize) { | ||
| return isCheckingMultipleFiles ? CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE_MULTIPLE : CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE; | ||
| return validationOptions?.isValidatingMultipleFiles ? CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE_MULTIPLE : CONST.FILE_VALIDATION_ERRORS.FILE_TOO_LARGE; | ||
| } | ||
|
|
||
| if (isValidatingReceipt && (file?.size ?? 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { | ||
| if (validationOptions?.isValidatingReceipts && (file?.size ?? 0) < CONST.API_ATTACHMENT_VALIDATIONS.MIN_SIZE) { | ||
| return CONST.FILE_VALIDATION_ERRORS.FILE_TOO_SMALL; | ||
| } | ||
|
|
||
|
|
@@ -777,3 +783,5 @@ export { | |
| cleanFileObject, | ||
| cleanFileObjectName, | ||
| }; | ||
|
|
||
| export type {ValidateAttachmentOptions}; | ||
Uh oh!
There was an error while loading. Please reload this page.