forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filename_overlay.jsx
115 lines (102 loc) · 3.59 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
// 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 {Tooltip} from 'react-bootstrap';
import {getFileDownloadUrl} from 'mattermost-redux/utils/file_utils';
import OverlayTrigger from 'components/overlay_trigger';
import AttachmentIcon from 'components/widgets/icons/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,
/*
* 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,
};
render() {
const {
canDownload,
children,
compactDisplay,
fileInfo,
handleImageClick,
iconClass,
} = this.props;
const fileName = fileInfo.name;
const trimmedFilename = trimFilename(fileName);
let filenameOverlay;
if (compactDisplay) {
filenameOverlay = (
<OverlayTrigger
delayShow={1000}
placement='top'
overlay={<Tooltip id='file-name__tooltip'>{fileName}</Tooltip>}
>
<a
id='file-attachment-link'
href='#'
onClick={handleImageClick}
className='post-image__name'
rel='noopener noreferrer'
>
<AttachmentIcon className='icon'/>
{trimmedFilename}
</a>
</OverlayTrigger>
);
} else if (canDownload) {
filenameOverlay = (
<div className={iconClass || 'post-image__name'}>
<a
href={getFileDownloadUrl(fileInfo.id)}
aria-label={localizeMessage('view_image_popover.download', 'Download').toLowerCase()}
download={fileName}
target='_blank'
rel='noopener noreferrer'
>
<OverlayTrigger
delayShow={1000}
placement='top'
overlay={
<Tooltip id='file-name__tooltip'>
{localizeMessage('view_image_popover.download', 'Download').toLowerCase()}
</Tooltip>
}
>
{children || trimmedFilename}
</OverlayTrigger>
</a>
</div>
);
} else {
filenameOverlay = (
<span className='post-image__name'>
{trimmedFilename}
</span>
);
}
return (filenameOverlay);
}
}