This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
filename_overlay.jsx
123 lines (108 loc) · 3.63 KB
/
filename_overlay.jsx
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import PropTypes from 'prop-types';
import React from 'react';
import {OverlayTrigger, Tooltip} from 'react-bootstrap';
import {getFileUrl} from 'mattermost-redux/utils/file_utils';
import AttachmentIcon from 'components/svg/attachment_icon';
import {trimFilename} from 'utils/file_utils';
import {localizeMessage} from 'utils/utils.jsx';
export default class FilenameOverlay extends React.PureComponent {
static propTypes = {
/*
* File detailed information
*/
fileInfo: PropTypes.object.isRequired,
/*
* The index of this attachment preview in the parent FileAttachmentList
*/
index: PropTypes.number.isRequired,
/*
* Handler for when the thumbnail is clicked passed the index above
*/
handleImageClick: PropTypes.func,
/*
* Display in compact format
*/
compactDisplay: PropTypes.bool,
/*
* If it should display link to download on file name
*/
canDownload: PropTypes.bool,
/**
* Optional children like download icon
*/
children: PropTypes.element,
/**
* Optional class like for icon
*/
iconClass: PropTypes.string
};
onAttachmentClick = (e) => {
e.preventDefault();
this.props.handleImageClick(this.props.index);
}
render() {
const {
canDownload,
children,
compactDisplay,
fileInfo,
iconClass
} = this.props;
const fileName = fileInfo.name;
const trimmedFilename = trimFilename(fileName);
const fileUrl = getFileUrl(fileInfo.id);
let filenameOverlay;
if (compactDisplay) {
filenameOverlay = (
<OverlayTrigger
trigger={['hover', 'focus']}
delayShow={1000}
placement='top'
overlay={<Tooltip id='file-name__tooltip'>{fileName}</Tooltip>}
>
<a
href='#'
onClick={this.onAttachmentClick}
className='post-image__name'
rel='noopener noreferrer'
>
<AttachmentIcon className='icon'/>
{trimmedFilename}
</a>
</OverlayTrigger>
);
} else if (canDownload) {
filenameOverlay = (
<a
href={fileUrl}
download={fileName}
className={iconClass || 'post-image__name'}
target='_blank'
rel='noopener noreferrer'
>
<OverlayTrigger
trigger={['hover', 'focus']}
delayShow={1000}
placement='top'
overlay={
<Tooltip id='file-name__tooltip'>
{localizeMessage('file_attachment.download', 'Download')}
</Tooltip>
}
>
{children || trimmedFilename}
</OverlayTrigger>
</a>
);
} else {
filenameOverlay = (
<span className='post-image__name'>
{trimmedFilename}
</span>
);
}
return (filenameOverlay);
}
}