Skip to content
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

[TS migration] Migrate 'ReportAttachments' page to TypeScript #33840

Merged
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import PropTypes from 'prop-types';
import {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback} from 'react';
import _ from 'underscore';
import AttachmentModal from '@components/AttachmentModal';
import ComposerFocusManager from '@libs/ComposerFocusManager';
import Navigation from '@libs/Navigation/Navigation';
import {AuthScreensParamList} from '@libs/Navigation/types';
import * as ReportUtils from '@libs/ReportUtils';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';

const propTypes = {
/** Navigation route context info provided by react navigation */
route: PropTypes.shape({
/** Route specific parameters used on this screen */
params: PropTypes.shape({
/** The report ID which the attachment is associated with */
reportID: PropTypes.string.isRequired,
/** The uri encoded source of the attachment */
source: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
type File = {
name: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

I just wonder if this type shouldn't be global

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you are right , I remember that in other Attachments files we should use the same type , I will move it to other place , thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

There is a type for File defined globally, but it has a different structure:

    interface File {
        source: string;
        uri: string;
    }

};

function ReportAttachments(props) {
const reportID = _.get(props, ['route', 'params', 'reportID']);
type Attachment = {
file: File;
hasBeenFlagged: boolean;
isAuthTokenRequired: boolean;
isReceipt: boolean;
reportActionID: string;
source: string;
};

type ReportAttachmentsProps = StackScreenProps<AuthScreensParamList, typeof SCREENS.REPORT_ATTACHMENTS>;

function ReportAttachments({route}: ReportAttachmentsProps) {
const reportID = route.params?.reportID;
const report = ReportUtils.getReport(reportID);

// In native the imported images sources are of type number. Ref: https://reactnative.dev/docs/image#imagesource
const decodedSource = decodeURI(_.get(props, ['route', 'params', 'source']));
const decodedSource = decodeURI(route.params.source);
const source = Number(decodedSource) || decodedSource;
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't route.params nullish? This is inconsistent with above line - const reportID = route.params?.reportID;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actaully its not nullish , I removed optional chaining from const reportID = route.params?.reportID;


const onCarouselAttachmentChange = useCallback(
(attachment) => {
const route = ROUTES.REPORT_ATTACHMENTS.getRoute(reportID, attachment.source);
Navigation.navigate(route);
(attachment: Attachment) => {
const routeToNavigate = ROUTES.REPORT_ATTACHMENTS.getRoute(reportID, attachment.source);
Navigation.navigate(routeToNavigate);
},
[reportID],
);

return (
<AttachmentModal
// @ts-expect-error TODO: Remove this once AttachmentModal (https://github.com/Expensify/App/issues/25130) is migrated to TypeScript.
allowDownload
defaultOpen
report={report}
Expand All @@ -52,7 +56,6 @@ function ReportAttachments(props) {
);
}

ReportAttachments.propTypes = propTypes;
ReportAttachments.displayName = 'ReportAttachments';

export default ReportAttachments;
Loading