-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionItemImage.tsx
148 lines (128 loc) · 5.69 KB
/
ReportActionItemImage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable react/jsx-props-no-spreading */
import {Str} from 'expensify-common';
import React from 'react';
import type {ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import ConfirmedRoute from '@components/ConfirmedRoute';
import type {IconSize} from '@components/EReceiptThumbnail';
import * as Expensicons from '@components/Icon/Expensicons';
import PressableWithoutFocus from '@components/Pressable/PressableWithoutFocus';
import type {ReceiptImageProps} from '@components/ReceiptImage';
import ReceiptImage from '@components/ReceiptImage';
import {ShowContextMenuContext} from '@components/ShowContextMenuContext';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as TransactionUtils from '@libs/TransactionUtils';
import tryResolveUrlFromApiRoot from '@libs/tryResolveUrlFromApiRoot';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import type {Transaction} from '@src/types/onyx';
type ReportActionItemImageProps = {
/** thumbnail URI for the image */
thumbnail?: string;
/** The file type of the receipt */
fileExtension?: string;
/** whether or not we are going to display a thumbnail */
isThumbnail?: boolean;
/** URI for the image or local numeric reference for the image */
image?: string;
/** whether to enable the image preview modal */
enablePreviewModal?: boolean;
/* The transaction associated with this image, if any. Passed for handling eReceipts. */
transaction?: OnyxEntry<Transaction>;
/** whether thumbnail is refer the local file or not */
isLocalFile?: boolean;
/** Filename of attachment */
filename?: string;
/** Whether there are other images displayed in the same parent container */
isSingleImage?: boolean;
/** Whether the map view should have border radius */
shouldMapHaveBorderRadius?: boolean;
};
/**
* An image with an optional thumbnail that fills its parent container. If the thumbnail is passed,
* we try to resolve both the image and thumbnail from the API. Similar to ImageRenderer, we show
* and optional preview modal as well.
*/
function ReportActionItemImage({
thumbnail,
isThumbnail,
image,
enablePreviewModal = false,
transaction,
isLocalFile = false,
fileExtension,
filename,
isSingleImage = true,
shouldMapHaveBorderRadius,
}: ReportActionItemImageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const isDistanceRequest = !!transaction && TransactionUtils.isDistanceRequest(transaction);
const hasPendingWaypoints = transaction && TransactionUtils.isFetchingWaypointsFromServer(transaction);
const showMapAsImage = isDistanceRequest && hasPendingWaypoints;
if (showMapAsImage) {
return (
<View style={[styles.w100, styles.h100]}>
<ConfirmedRoute
transaction={transaction}
isSmallerIcon={!isSingleImage}
shouldHaveBorderRadius={shouldMapHaveBorderRadius}
interactive={false}
requireRouteToDisplayMap
/>
</View>
);
}
const attachmentModalSource = tryResolveUrlFromApiRoot(image ?? '');
const thumbnailSource = tryResolveUrlFromApiRoot(thumbnail ?? '');
const isEReceipt = transaction && TransactionUtils.hasEReceipt(transaction);
let propsObj: ReceiptImageProps;
if (isEReceipt) {
propsObj = {isEReceipt: true, transactionID: transaction.transactionID, iconSize: isSingleImage ? 'medium' : ('small' as IconSize)};
} else if (thumbnail && !isLocalFile) {
propsObj = {
shouldUseThumbnailImage: true,
source: thumbnailSource,
fallbackIcon: Expensicons.Receipt,
fallbackIconSize: isSingleImage ? variables.iconSizeSuperLarge : variables.iconSizeExtraLarge,
isAuthTokenRequired: true,
shouldUseInitialObjectPosition: isDistanceRequest,
};
} else if (isLocalFile && filename && Str.isPDF(filename) && typeof attachmentModalSource === 'string') {
propsObj = {isPDFThumbnail: true, source: attachmentModalSource};
} else {
propsObj = {
isThumbnail,
...(isThumbnail && {iconSize: (isSingleImage ? 'medium' : 'small') as IconSize, fileExtension}),
shouldUseThumbnailImage: true,
isAuthTokenRequired: false,
source: thumbnail ?? image ?? '',
shouldUseInitialObjectPosition: isDistanceRequest,
};
}
if (enablePreviewModal) {
return (
<ShowContextMenuContext.Consumer>
{({report, transactionThreadReport}) => (
<PressableWithoutFocus
style={[styles.w100, styles.h100, styles.noOutline as ViewStyle]}
onPress={() =>
Navigation.navigate(ROUTES.TRANSACTION_RECEIPT.getRoute(transactionThreadReport?.reportID ?? report?.reportID ?? '-1', transaction?.transactionID ?? '-1'))
}
accessibilityLabel={translate('accessibilityHints.viewAttachment')}
accessibilityRole={CONST.ROLE.BUTTON}
>
<ReceiptImage {...propsObj} />
</PressableWithoutFocus>
)}
</ShowContextMenuContext.Consumer>
);
}
return <ReceiptImage {...propsObj} />;
}
ReportActionItemImage.displayName = 'ReportActionItemImage';
export default ReportActionItemImage;