-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathResource.jsx
More file actions
47 lines (41 loc) · 1.03 KB
/
Resource.jsx
File metadata and controls
47 lines (41 loc) · 1.03 KB
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
import React from 'react';
let Resource = React.memo((props) => {
const EXTENSION = /\.[\w{3,4}]$/;
const URI_SEGMENT = /[^/]+/g;
let getMediaType = (uri) => {
try {
if (/picture/.test(uri)) return 'image';
switch (uri.match(EXTENSION)) {
case '.gif':
case '.jpg':
case '.jpeg':
case '.png':
case '.svg':
case '.webp':
return 'image';
default: return null;
}
} catch (e) {
return null;
}
};
let uri = props.href && props.href[0];
if (!uri) return null;
let fileName = uri.match(URI_SEGMENT).slice(-1)[0];
switch (getMediaType(uri)) {
case 'image': return (
<div className="p-3">
<a href={uri} target="_blank" className="cursor-zoom"
rel="noopener noreferrer">
<img src={uri} alt="fileName" />
</a>
</div>
);
default: return (
<div className="p-3">
<a href={uri} className="btn btn-light" > {fileName} </a>
</div>
);
}
});
export default Resource;