-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentViewer.tsx
More file actions
114 lines (102 loc) · 4.24 KB
/
DocumentViewer.tsx
File metadata and controls
114 lines (102 loc) · 4.24 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
110
111
112
113
114
import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { TIFFViewer } from 'react-tiff';
import './DocumentViewer.styles.scss';
import Zoom from "react-medium-image-zoom";
import "react-medium-image-zoom/dist/styles.css";
interface IIFrameComponentProps {
className?: string;
metadata?: any;
urlWithSasToken: string | undefined;
iframeKey: number;
}
const DocumentViewer = ({ className, metadata, urlWithSasToken, iframeKey }: IIFrameComponentProps) => {
const { t } = useTranslation();
const [imgError, setImageError] = useState(false);
useEffect(() => {
setImageError(false)
}, [urlWithSasToken])
const getContentComponent = () => {
if (!metadata || !urlWithSasToken) {
return <div className={"noDataDocContainer"}><p>{t("components.document.none", "No document available")}</p></div>;
}
switch (metadata.mimeType) {
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
case "application/vnd.ms-excel.sheet.macroEnabled.12":
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
case "application/vnd.openxmlformats-officedocument.presentationml.presentation": {
return (
<iframe
key={iframeKey}
src={`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(
urlWithSasToken
)}`}
width="100%"
height="100%"
title={getTitle(metadata.mimeType)}
/>
);
}
case "application/pdf": {
return <iframe style={{ border: '1px solid lightgray' }} title="PDF Viewer" key={iframeKey} src={urlWithSasToken.toString()} width="100%" height="100%" />;
}
case "image/jpeg":
case "image/png":
case "image/gif":
case "image/bmp":
case "image/svg+xml": {
return <div className="imageContainer">
<Zoom>
<img src={urlWithSasToken} alt={"Document"} onError={() => setImageError(true)} width="100%" height="100%" className="document-image" />
</Zoom>
</div>;
}
case "image/tiff": {
return (
<div
style={{
width: "100%",
height: "100%",
objectFit: "contain",
overflowX: "scroll",
overflowY: "auto",
}}
>
<TIFFViewer tiff={urlWithSasToken} style={{ width: 100, height: 100, objectFit: "contain" }} />
</div>
);
}
default: {
return (
<iframe key={iframeKey} src={urlWithSasToken} width="100%" height="100%" title="Doc visualizer" />
);
}
}
};
const getTitle = (mimeType: string) => {
switch (mimeType) {
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
case "application/vnd.ms-excel.sheet.macroEnabled.12":
return "Excel viewer";
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
return "PowerPoint viewer";
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return "Word viewer";
case "application/pdf":
return "PDF Viewer";
default:
return "Doc visualizer";
}
};
return <> <div className={`${className} ${imgError ? 'imageErrorContainer' : ''}`}>
{imgError ?
<div className={"invalidImagePopup"}>
<span className="imgEH">We can't open this file</span>
<p className="imgCtn">Something went wrong.</p>
</div>
: getContentComponent()
}
</div>
</>;
}
export default DocumentViewer;