forked from Saifullah-dev/react-file-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviewFile.action.jsx
More file actions
109 lines (100 loc) · 3.82 KB
/
PreviewFile.action.jsx
File metadata and controls
109 lines (100 loc) · 3.82 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
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
import React, { useMemo, useState } from "react";
import { getFileExtension } from "../../../utils/getFileExtension";
import Loader from "../../../components/Loader/Loader";
import { useSelection } from "../../../contexts/SelectionContext";
import Button from "../../../components/Button/Button";
import { getDataSize } from "../../../utils/getDataSize";
import { MdOutlineFileDownload } from "react-icons/md";
import { useFileIcons } from "../../../hooks/useFileIcons";
import { FaRegFileAlt } from "react-icons/fa";
import "./PreviewFile.action.scss";
import {injectIntl} from "react-intl";
const imageExtensions = ["jpg", "jpeg", "png"];
const videoExtensions = ["mp4", "mov", "avi"];
const audioExtensions = ["mp3", "wav", "m4a"];
const iFrameExtensions = ["txt", "pdf"];
const PreviewFileAction = ({ filePreviewPath, filePreviewComponent }) => {
const [isLoading, setIsLoading] = useState(true);
const [hasError, setHasError] = useState(false);
const { selectedFiles } = useSelection();
const fileIcons = useFileIcons(73);
const extension = getFileExtension(selectedFiles[0].name)?.toLowerCase();
const filePath = `${filePreviewPath}${selectedFiles[0].path}`;
// Custom file preview component
const customPreview = useMemo(
() => filePreviewComponent?.(selectedFiles[0]),
[filePreviewComponent]
);
const handleImageLoad = () => {
setIsLoading(false); // Loading is complete
setHasError(false); // No error
};
const handleImageError = () => {
setIsLoading(false); // Loading is complete
setHasError(true); // Error occurred
};
const handleDownload = () => {
window.location.href = filePath;
};
if (React.isValidElement(customPreview)) {
return customPreview;
}
return (
<section className={`file-previewer ${extension === "pdf" ? "pdf-previewer" : ""}`}>
{hasError ||
(![
...imageExtensions,
...videoExtensions,
...audioExtensions,
...iFrameExtensions,
].includes(extension) && (
<div className="preview-error">
<span className="error-icon">{fileIcons[extension] ?? <FaRegFileAlt size={73} />}</span>
<span className="error-msg">Sorry! Preview is not available for this file.</span>
<div className="file-info">
<span className="file-name">{selectedFiles[0].name}</span>
{selectedFiles[0].size && <span>-</span>}
<span className="file-size">{getDataSize(selectedFiles[0].size)}</span>
</div>
<Button onClick={handleDownload} padding="0.45rem .9rem">
<div className="download-btn">
<MdOutlineFileDownload size={18} />
<span>{intl.formatMessage({id: `download`})}</span>
</div>
</Button>
</div>
))}
{imageExtensions.includes(extension) && (
<>
<Loader isLoading={isLoading} />
<img
src={filePath}
alt="Preview Unavailable"
className={`photo-popup-image ${isLoading ? "img-loading" : ""}`}
onLoad={handleImageLoad}
onError={handleImageError}
loading="lazy"
/>
</>
)}
{videoExtensions.includes(extension) && (
<video src={filePath} className="video-preview" controls autoPlay />
)}
{audioExtensions.includes(extension) && (
<audio src={filePath} controls autoPlay className="audio-preview" />
)}
{iFrameExtensions.includes(extension) && (
<>
<iframe
src={filePath}
onLoad={handleImageLoad}
onError={handleImageError}
frameBorder="0"
className={`photo-popup-iframe ${isLoading ? "img-loading" : ""}`}
></iframe>
</>
)}
</section>
);
};
export default injectIntl(PreviewFileAction);