-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathindex.js
96 lines (90 loc) · 3.6 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
import _ from 'underscore';
import React from 'react';
import {Pressable, StyleSheet} from 'react-native';
import lodashGet from 'lodash/get';
import Text from '../../Text';
import {propTypes, defaultProps} from '../anchorForCommentsOnlyPropTypes';
import PressableWithSecondaryInteraction from '../../PressableWithSecondaryInteraction';
import * as ReportActionContextMenu from '../../../pages/home/report/ContextMenu/ReportActionContextMenu';
import * as ContextMenuActions from '../../../pages/home/report/ContextMenu/ContextMenuActions';
import AttachmentView from '../../AttachmentView';
import fileDownload from '../../../libs/fileDownload';
/*
* This is a default anchor component for regular links.
*/
class BaseAnchorForCommentsOnly extends React.Component {
constructor(props) {
super(props);
this.state = {
isDownloading: false,
};
this.processDownload = this.processDownload.bind(this);
}
/**
* Initiate file downloading and update downloading flags
*
* @param {String} href
* @param {String} fileName
*/
processDownload(href, fileName) {
this.setState({isDownloading: true});
fileDownload(href, fileName).then(() => this.setState({isDownloading: false}));
}
render() {
let linkRef;
const rest = _.omit(this.props, _.keys(propTypes));
return (
this.props.isAttachment
? (
<Pressable onPress={() => {
if (this.state.isDownloading) {
return;
}
this.processDownload(this.props.href, this.props.fileName);
}}
>
<AttachmentView
sourceURL={this.props.href}
file={{name: this.props.fileName}}
shouldShowDownloadIcon
shouldShowLoadingSpinnerIcon={this.state.isDownloading}
/>
</Pressable>
)
: (
<PressableWithSecondaryInteraction
inline
onSecondaryInteraction={
(event) => {
ReportActionContextMenu.showContextMenu(
ContextMenuActions.CONTEXT_MENU_TYPES.LINK,
event,
this.props.href,
lodashGet(linkRef, 'current'),
);
}
}
>
<Text
ref={el => linkRef = el}
style={StyleSheet.flatten(this.props.style)}
accessibilityRole="link"
href={this.props.href}
hrefAttrs={{
rel: this.props.rel,
target: this.props.target,
}}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
>
{this.props.children}
</Text>
</PressableWithSecondaryInteraction>
)
);
}
}
BaseAnchorForCommentsOnly.propTypes = propTypes;
BaseAnchorForCommentsOnly.defaultProps = defaultProps;
BaseAnchorForCommentsOnly.displayName = 'BaseAnchorForCommentsOnly';
export default BaseAnchorForCommentsOnly;