-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.js
executable file
·192 lines (172 loc) · 6.83 KB
/
index.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import React, {memo, useState} from 'react';
import {View, ActivityIndicator} from 'react-native';
import _ from 'underscore';
import PropTypes from 'prop-types';
import Str from 'expensify-common/lib/str';
import {withOnyx} from 'react-native-onyx';
import styles from '../../../styles/styles';
import Icon from '../../Icon';
import * as Expensicons from '../../Icon/Expensicons';
import withLocalize, {withLocalizePropTypes} from '../../withLocalize';
import compose from '../../../libs/compose';
import Text from '../../Text';
import Tooltip from '../../Tooltip';
import themeColors from '../../../styles/themes/default';
import variables from '../../../styles/variables';
import AttachmentViewImage from './AttachmentViewImage';
import AttachmentViewPdf from './AttachmentViewPdf';
import addEncryptedAuthTokenToURL from '../../../libs/addEncryptedAuthTokenToURL';
import * as StyleUtils from '../../../styles/StyleUtils';
import {attachmentViewPropTypes, attachmentViewDefaultProps} from './propTypes';
import * as TransactionUtils from '../../../libs/TransactionUtils';
import DistanceEReceipt from '../../DistanceEReceipt';
import useNetwork from '../../../hooks/useNetwork';
import ONYXKEYS from '../../../ONYXKEYS';
const propTypes = {
...attachmentViewPropTypes,
...withLocalizePropTypes,
/** Flag to show/hide download icon */
shouldShowDownloadIcon: PropTypes.bool,
/** Flag to show the loading indicator */
shouldShowLoadingSpinnerIcon: PropTypes.bool,
/** Notify parent that the UI should be modified to accommodate keyboard */
onToggleKeyboard: PropTypes.func,
/** Extra styles to pass to View wrapper */
// eslint-disable-next-line react/forbid-prop-types
containerStyles: PropTypes.arrayOf(PropTypes.object),
/** Denotes whether it is a workspace avatar or not */
isWorkspaceAvatar: PropTypes.bool,
/** The id of the transaction related to the attachment */
// eslint-disable-next-line react/no-unused-prop-types
transactionID: PropTypes.string,
};
const defaultProps = {
...attachmentViewDefaultProps,
shouldShowDownloadIcon: false,
shouldShowLoadingSpinnerIcon: false,
onToggleKeyboard: () => {},
containerStyles: [],
isWorkspaceAvatar: false,
transactionID: '',
};
function AttachmentView({
source,
file,
isAuthTokenRequired,
isUsedInCarousel,
onPress,
shouldShowLoadingSpinnerIcon,
shouldShowDownloadIcon,
containerStyles,
onScaleChanged,
onToggleKeyboard,
translate,
isFocused,
isWorkspaceAvatar,
fallbackSource,
transaction,
}) {
const [loadComplete, setLoadComplete] = useState(false);
const [imageError, setImageError] = useState(false);
useNetwork({onReconnect: () => setImageError(false)});
// Handles case where source is a component (ex: SVG)
if (_.isFunction(source)) {
let iconFillColor = '';
let additionalStyles = [];
if (isWorkspaceAvatar) {
const defaultWorkspaceAvatarColor = StyleUtils.getDefaultWorkspaceAvatarColor(file.name);
iconFillColor = defaultWorkspaceAvatarColor.fill;
additionalStyles = [defaultWorkspaceAvatarColor];
}
return (
<Icon
src={source}
height={variables.defaultAvatarPreviewSize}
width={variables.defaultAvatarPreviewSize}
fill={iconFillColor}
additionalStyles={additionalStyles}
/>
);
}
// Check both source and file.name since PDFs dragged into the text field
// will appear with a source that is a blob
if ((_.isString(source) && Str.isPDF(source)) || (file && Str.isPDF(file.name || translate('attachmentView.unknownFilename')))) {
const encryptedSourceUrl = isAuthTokenRequired ? addEncryptedAuthTokenToURL(source) : source;
return (
<AttachmentViewPdf
source={source}
file={file}
isAuthTokenRequired={isAuthTokenRequired}
encryptedSourceUrl={encryptedSourceUrl}
isUsedInCarousel={isUsedInCarousel}
isFocused={isFocused}
onPress={onPress}
onScaleChanged={onScaleChanged}
onToggleKeyboard={onToggleKeyboard}
onLoadComplete={() => !loadComplete && setLoadComplete(true)}
/>
);
}
if (TransactionUtils.isDistanceRequest(transaction)) {
return <DistanceEReceipt transaction={transaction} />;
}
// For this check we use both source and file.name since temporary file source is a blob
// both PDFs and images will appear as images when pasted into the text field.
// We also check for numeric source since this is how static images (used for preview) are represented in RN.
const isImage = typeof source === 'number' || Str.isImage(source);
if (isImage || (file && Str.isImage(file.name))) {
return (
<AttachmentViewImage
source={imageError ? fallbackSource : source}
file={file}
isAuthTokenRequired={isAuthTokenRequired}
isUsedInCarousel={isUsedInCarousel}
loadComplete={loadComplete}
isFocused={isFocused}
isImage={isImage}
onPress={onPress}
onScaleChanged={onScaleChanged}
onError={() => {
setImageError(true);
}}
/>
);
}
return (
<View style={[styles.defaultAttachmentView, ...containerStyles]}>
<View style={styles.mr2}>
<Icon src={Expensicons.Paperclip} />
</View>
<Text style={[styles.textStrong, styles.flexShrink1, styles.breakAll, styles.flexWrap, styles.mw100]}>{file && file.name}</Text>
{!shouldShowLoadingSpinnerIcon && shouldShowDownloadIcon && (
<Tooltip text={translate('common.download')}>
<View style={styles.ml2}>
<Icon src={Expensicons.Download} />
</View>
</Tooltip>
)}
{shouldShowLoadingSpinnerIcon && (
<View style={styles.ml2}>
<Tooltip text={translate('common.downloading')}>
<ActivityIndicator
size="small"
color={themeColors.textSupporting}
/>
</Tooltip>
</View>
)}
</View>
);
}
AttachmentView.propTypes = propTypes;
AttachmentView.defaultProps = defaultProps;
AttachmentView.displayName = 'AttachmentView';
export default compose(
memo,
withLocalize,
withOnyx({
transaction: {
key: ({transactionID}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`,
},
}),
)(AttachmentView);