-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
67 lines (59 loc) · 1.49 KB
/
index.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
import React from "react";
import { ImageRenderer } from "./renderers/ImageRenderer";
import { VideoRenderer } from "./renderers/VideoRenderer";
import { PDFRenderer } from "./renderers/PDFRenderer";
export type IDocumentViewer<C = any> = React.FC<{
fileUrl: string;
fileName: string;
config: C;
}>;
type DocumentRenderer = Record<string, IDocumentViewer>;
export const DocumentRenderer = {
png: ImageRenderer,
jpg: ImageRenderer,
jpeg: ImageRenderer,
gif: ImageRenderer,
svg: ImageRenderer,
webp: ImageRenderer,
bmp: ImageRenderer,
ico: ImageRenderer,
mp4: VideoRenderer,
pdf: PDFRenderer,
} as const;
type DocumentViewerProps = {
fileExt: string;
fileUrl: string;
fileName: string;
config?: { props: any };
docRenderer: DocumentRenderer;
};
export const DocumentViewer: React.FC<DocumentViewerProps> = ({
config,
fileExt,
fileName,
fileUrl,
docRenderer,
}) => {
const canPreview = (): boolean => {
return Object.keys(docRenderer).includes(fileExt);
};
const DocumentViewer: IDocumentViewer = docRenderer[fileExt];
return (
<>
{!canPreview() ? (
<p style={{ textTransform: "capitalize" }}>
no document viewer found for –{" "}
<span style={{ fontWeight: "bold", textTransform: "lowercase" }}>
.{fileExt}
</span>
</p>
) : (
<DocumentViewer
fileUrl={fileUrl}
fileName={fileName}
config={config ?? { props: {} }}
/>
)}
</>
);
};