forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_search_result_item.tsx
203 lines (184 loc) · 7.73 KB
/
file_search_result_item.tsx
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
193
194
195
196
197
198
199
200
201
202
203
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {Tooltip} from 'react-bootstrap';
import {FormattedMessage} from 'react-intl';
import {FileInfo} from 'mattermost-redux/types/files';
import {FileDropdownPluginComponent} from 'types/store/plugins';
import {fileSizeToString, copyToClipboard, localizeMessage} from 'utils/utils';
import {browserHistory} from 'utils/browser_history';
import {getSiteURL} from 'utils/url';
import Constants from 'utils/constants';
import OverlayTrigger from 'components/overlay_trigger';
import Menu from 'components/widgets/menu/menu';
import Badge from 'components/widgets/badges/badge';
import MenuWrapper from 'components/widgets/menu/menu_wrapper';
import FileThumbnail from 'components/file_attachment/file_thumbnail';
import Timestamp, {RelativeRanges} from 'components/timestamp';
import './file_search_result_item.scss';
import FilePreviewModal from 'components/file_preview_modal';
type Props = {
fileInfo: FileInfo;
channelDisplayName: string;
channelType: string;
teamName: string;
pluginMenuItems?: FileDropdownPluginComponent[];
};
type State = {
keepOpen: boolean;
showPreview: boolean;
}
const FILE_TOOLTIP_RANGES = [
RelativeRanges.TODAY_TITLE_CASE,
RelativeRanges.YESTERDAY_TITLE_CASE,
];
export default class FileSearchResultItem extends React.PureComponent<Props, State> {
public constructor(props: Props) {
super(props);
this.state = {keepOpen: false, showPreview: false};
}
private jumpToConv = (e: MouseEvent) => {
e.stopPropagation();
browserHistory.push(`/${this.props.teamName}/pl/${this.props.fileInfo.post_id}`);
}
private copyLink = () => {
copyToClipboard(`${getSiteURL()}/${this.props.teamName}/pl/${this.props.fileInfo.post_id}`);
}
private stopPropagation = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
e.stopPropagation();
}
private keepOpen = (open: boolean) => {
this.setState({keepOpen: open});
}
private renderPluginItems = () => {
const {fileInfo} = this.props;
const pluginItems = this.props.pluginMenuItems?.filter((item) => item?.match(fileInfo)).map((item) => {
return (
<Menu.ItemAction
id={item.id + '_pluginmenuitem'}
key={item.id + '_pluginmenuitem'}
onClick={() => item.action?.(fileInfo)}
text={item.text}
/>
);
});
if (!pluginItems?.length) {
return null;
}
return (
<>
<li
id={`divider_file_${this.props.fileInfo.id}_plugins`}
className='MenuItem__divider'
role='menuitem'
/>
{pluginItems}
</>
);
}
public render(): React.ReactNode {
const {fileInfo, channelDisplayName, channelType} = this.props;
let channelName: React.ReactNode = channelDisplayName;
if (channelType === Constants.DM_CHANNEL) {
channelName = (
<FormattedMessage
id='search_item.file_badge.direct_message'
defaultMessage='Direct Message'
/>
);
} else if (channelType === Constants.GM_CHANNEL) {
channelName = (
<FormattedMessage
id='search_item.file_badge.group_message'
defaultMessage='Group Message'
/>
);
}
return (
<div
data-testid='search-item-container'
className='search-item__container'
>
<button
className={'FileSearchResultItem' + (this.state.keepOpen ? ' keep-open' : '')}
onClick={() => this.setState({showPreview: true})}
>
<FileThumbnail fileInfo={fileInfo}/>
<div className='fileData'>
<div className='fileDataName'>{fileInfo.name}</div>
<div className='fileMetadata'>
{channelName && <Badge className='file-search-channel-name'>{channelName}</Badge>}
<span>{fileSizeToString(fileInfo.size)}</span>
<span>{' • '}</span>
<Timestamp
value={fileInfo.create_at}
ranges={FILE_TOOLTIP_RANGES}
/>
</div>
</div>
<OverlayTrigger
delayShow={1000}
placement='top'
overlay={
<Tooltip id='file-name__tooltip'>
{localizeMessage('file_search_result_item.more_actions', 'More Actions')}
</Tooltip>
}
>
<MenuWrapper
onToggle={this.keepOpen}
stopPropagationOnToggle={true}
>
<a
href='#'
className='action-icon dots-icon'
>
<i className='icon icon-dots-vertical'/>
</a>
<Menu
ariaLabel={'file menu'}
openLeft={true}
>
<Menu.ItemAction
onClick={this.jumpToConv}
ariaLabel={localizeMessage('file_search_result_item.open_in_channel', 'Open in channel')}
text={localizeMessage('file_search_result_item.open_in_channel', 'Open in channel')}
/>
<Menu.ItemAction
onClick={this.copyLink}
ariaLabel={localizeMessage('file_search_result_item.copy_link', 'Copy link')}
text={localizeMessage('file_search_result_item.copy_link', 'Copy link')}
/>
{this.renderPluginItems()}
</Menu>
</MenuWrapper>
</OverlayTrigger>
<OverlayTrigger
delayShow={1000}
placement='top'
overlay={
<Tooltip id='file-name__tooltip'>
{localizeMessage('file_search_result_item.download', 'Download')}
</Tooltip>
}
>
<a
className='action-icon download-icon'
href={`/api/v4/files/${fileInfo.id}?download=1`}
onClick={this.stopPropagation}
>
<i className='icon icon-download-outline'/>
</a>
</OverlayTrigger>
</button>
<FilePreviewModal
show={this.state.showPreview}
onModalDismissed={() => this.setState({showPreview: false})}
startIndex={0}
fileInfos={[this.props.fileInfo]}
postId={this.props.fileInfo.post_id}
/>
</div>
);
}
}