-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathReplyAttachmentList.js
120 lines (105 loc) · 3.18 KB
/
ReplyAttachmentList.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, { useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import selectors from 'selectors';
import { saveAs } from 'file-saver';
import { getSaveAsHandler } from 'helpers/saveAs';
import Button from 'components/Button';
import Icon from 'components/Icon';
import Tooltip from 'components/Tooltip';
import { getAttachmentIcon, isImage, decompressFileContent } from 'helpers/ReplyAttachmentManager';
import './ReplyAttachmentList.scss';
const ImagePreview = ({ file }) => {
const [src, setSrc] = useState();
useEffect(() => {
if (file instanceof File) {
setSrc(URL.createObjectURL(file));
} else if (!file.url) {
decompressFileContent(file).then((blob) => {
setSrc(URL.createObjectURL(blob));
});
}
}, [file]);
return <img src={src} />;
};
const ReplyAttachmentList = ({ files, isEditing, fileDeleted }) => {
const [tabManager, previewEnabled] = useSelector((state) => [
selectors.getTabManager(state),
selectors.isReplyAttachmentPreviewEnabled(state)
]);
const onClick = async (e, file) => {
e.preventDefault();
e.stopPropagation();
if (!tabManager) {
return console.warn('Can\'t open attachment in non-multi-tab mode');
}
let fileData;
if (file instanceof File) {
fileData = file;
} else if (file.url) {
fileData = file.url;
} else {
fileData = await decompressFileContent(file);
}
fileData && tabManager.addTab(fileData, {
filename: file.name,
setActive: true,
saveCurrentActiveTabState: true
});
};
const onDelete = (e, file) => {
e.preventDefault();
e.stopPropagation();
fileDeleted(file);
};
const onDownload = async (e, file) => {
e.preventDefault();
e.stopPropagation();
const fileData = file.url ? file.url : await decompressFileContent(file);
if (getSaveAsHandler() !== null) {
const handler = getSaveAsHandler();
handler(fileData, file.name);
} else {
saveAs(fileData, file.name);
}
};
return (
<div className="reply-attachment-list">
{files.map((file, i) => (
<div
className="reply-attachment"
key={i}
onClick={(e) => onClick(e, file)}
>
{previewEnabled && isImage(file) && (
<div className="reply-attachment-preview">
<ImagePreview file={file} />
</div>
)}
<div className="reply-attachment-info">
<Icon
className="reply-attachment-icon"
glyph={getAttachmentIcon(file)}
/>
<Tooltip content={file.name}>
<div className="reply-file-name">{file.name}</div>
</Tooltip>
{isEditing ? (
<Button
className="attachment-button"
img="icon-close"
onClick={(e) => onDelete(e, file)}
/>
) : (
<Button
className="attachment-button"
img="icon-download"
onClick={(e) => onDownload(e, file)}
/>
)}
</div>
</div>
))}
</div>
);
};
export default ReplyAttachmentList;