Skip to content

Commit

Permalink
Merge branch 'main' into ts/enable-no-unsafe-member-access
Browse files Browse the repository at this point in the history
  • Loading branch information
bgawkuc committed Jun 18, 2024
2 parents c47a908 + b278b8f commit ece8190
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
35 changes: 5 additions & 30 deletions src/components/OptionListContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,49 +53,24 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp

const personalDetails = usePersonalDetails() || CONST.EMPTY_OBJECT;
const prevPersonalDetails = usePrevious(personalDetails);
const prevReports = usePrevious(reports);

/**
* This effect is used to update the options list when a report is updated.
* This effect is used to update the options list when reports change.
*/
useEffect(() => {
// there is no need to update the options if the options are not initialized
if (!areOptionsInitialized.current) {
return;
}

const newReports = OptionsListUtils.createOptionList(personalDetails, reports).reports;

setOptions((prevOptions) => {
const newOptions = {...prevOptions};
newOptions.reports = newReports;
return newOptions;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reports]);

/**
* This effect is used to add a new report option or remove a report option from the list of options when a new report is added to/removed from the collection.
*/
useEffect(() => {
if (!areOptionsInitialized.current || !reports) {
return;
}
const missingReportIds = Object.keys(reports).filter((key) => prevReports && !(key in prevReports));
// Since reports updates can happen in bulk, and some reports depend on other reports, we need to recreate the whole list from scratch.
const newReports = OptionsListUtils.createOptionList(personalDetails, reports).reports;

setOptions((prevOptions) => {
const newOptions = {
...prevOptions,
reports: prevOptions.reports.filter((report) => reports[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] !== null),
reports: newReports,
};
missingReportIds.forEach((missingReportId) => {
const report = missingReportId ? reports[missingReportId] : null;
if (!missingReportId || !report) {
return;
}
const reportOption = OptionsListUtils.createOptionFromReport(report, personalDetails);
newOptions.reports.push(reportOption);
});

return newOptions;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
4 changes: 1 addition & 3 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,7 @@ function openReport(
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
errorFields: {
notFound: null,
},
errorFields: null,
},
},
{
Expand Down
39 changes: 39 additions & 0 deletions src/libs/isPdfFilePasswordProtected/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as pdfjsLib from 'pdfjs-dist';
import type {FileObject} from '@components/AttachmentModal';

const isPdfFilePasswordProtected = (file: FileObject): Promise<boolean> =>
new Promise((resolve) => {
const reader = new FileReader();

reader.onload = (event) => {
const arrayBuffer = event.target?.result;
if (!arrayBuffer) {
resolve(false);
return;
}
try {
const loadingTask = pdfjsLib.getDocument({data: arrayBuffer});
loadingTask.promise.then(
() => {
resolve(false);
},
(error) => {
if (error.name === 'PasswordException') {
resolve(true);
return;
}
resolve(false);
},
);
} catch (error) {
resolve(false);
}
};

reader.onerror = () => {
resolve(false);
};

reader.readAsArrayBuffer(file as Blob);
});
export default isPdfFilePasswordProtected;
10 changes: 10 additions & 0 deletions src/pages/iou/request/step/IOURequestStepScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import useWindowDimensions from '@hooks/useWindowDimensions';
import * as Browser from '@libs/Browser';
import * as FileUtils from '@libs/fileDownload/FileUtils';
import getCurrentPosition from '@libs/getCurrentPosition';
import isPdfFilePasswordProtected from '@libs/isPdfFilePasswordProtected';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
Expand Down Expand Up @@ -212,6 +213,15 @@ function IOURequestStepScan({
return false;
}

if (fileExtension === 'pdf') {
return isPdfFilePasswordProtected(file).then((isProtected: boolean) => {
if (isProtected) {
setUploadReceiptError(true, 'attachmentPicker.wrongFileType', 'attachmentPicker.protectedPDFNotSupported');
return false;
}
return true;
});
}
return true;
})
.catch(() => {
Expand Down

0 comments on commit ece8190

Please sign in to comment.