-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseAnchorForAttachmentsOnly.js
97 lines (87 loc) · 3.33 KB
/
BaseAnchorForAttachmentsOnly.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
import React from 'react';
import {Pressable} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import {
propTypes as anchorForAttachmentsOnlyPropTypes,
defaultProps as anchorForAttachmentsOnlyDefaultProps,
} from './anchorForAttachmentsOnlyPropTypes';
import CONST from '../../CONST';
import ONYXKEYS from '../../ONYXKEYS';
import AttachmentView from '../AttachmentView';
import * as Download from '../../libs/actions/Download';
import fileDownload from '../../libs/fileDownload';
import addEncryptedAuthTokenToURL from '../../libs/addEncryptedAuthTokenToURL';
import {ShowContextMenuContext, showContextMenuForReport} from '../ShowContextMenuContext';
const propTypes = {
/** Press in handler for the link */
onPressIn: PropTypes.func,
/** Press out handler for the link */
onPressOut: PropTypes.func,
/** If a file download is happening */
download: PropTypes.shape({
isDownloading: PropTypes.bool.isRequired,
}),
...anchorForAttachmentsOnlyPropTypes,
};
const defaultProps = {
onPressIn: undefined,
onPressOut: undefined,
download: {isDownloading: false},
...anchorForAttachmentsOnlyDefaultProps,
};
const BaseAnchorForAttachmentsOnly = (props) => {
const sourceURL = props.source;
const sourceURLWithAuth = addEncryptedAuthTokenToURL(sourceURL);
const sourceID = (sourceURL.match(CONST.REGEX.ATTACHMENT_ID) || [])[1];
const fileName = props.displayName;
const isDownloading = props.download && props.download.isDownloading;
return (
<ShowContextMenuContext.Consumer>
{({
anchor,
reportID,
action,
checkIfContextMenuActive,
}) => (
<Pressable
style={props.style}
onPress={() => {
if (isDownloading) {
return;
}
Download.setDownload(sourceID, true);
fileDownload(sourceURLWithAuth, fileName).then(() => Download.setDownload(sourceID, false));
}}
onPressIn={props.onPressIn}
onPressOut={props.onPressOut}
onLongPress={event => showContextMenuForReport(
event,
anchor,
reportID,
action,
checkIfContextMenuActive,
)}
>
<AttachmentView
source={sourceURLWithAuth}
file={{name: fileName}}
shouldShowDownloadIcon
shouldShowLoadingSpinnerIcon={isDownloading}
/>
</Pressable>
)}
</ShowContextMenuContext.Consumer>
);
};
BaseAnchorForAttachmentsOnly.displayName = 'BaseAnchorForAttachmentsOnly';
BaseAnchorForAttachmentsOnly.propTypes = propTypes;
BaseAnchorForAttachmentsOnly.defaultProps = defaultProps;
export default withOnyx({
download: {
key: ({source}) => {
const sourceID = (source.match(CONST.REGEX.ATTACHMENT_ID) || [])[1];
return `${ONYXKEYS.COLLECTION.DOWNLOAD}${sourceID}`;
},
},
})(BaseAnchorForAttachmentsOnly);