-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathindex.tsx
74 lines (69 loc) · 3 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
68
69
70
71
72
73
74
// @ts-expect-error - This line imports a module from 'pdfjs-dist' package which lacks TypeScript typings.
import pdfWorkerSource from 'pdfjs-dist/legacy/build/pdf.worker';
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import {Document, pdfjs, Thumbnail} from 'react-pdf';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL';
import variables from '@styles/variables';
import type PDFThumbnailProps from './types';
if (!pdfjs.GlobalWorkerOptions.workerSrc) {
pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(new Blob([pdfWorkerSource], {type: 'text/javascript'}));
}
function PDFThumbnail({previewSourceURL, style, isAuthTokenRequired = false, enabled = true, onPassword, onLoadError}: PDFThumbnailProps) {
const styles = useThemeStyles();
const theme = useTheme();
const [hasError, setHasError] = useState(false);
const thumbnail = useMemo(
() => (
<Document
loading={<FullScreenLoadingIndicator />}
file={isAuthTokenRequired ? addEncryptedAuthTokenToURL(previewSourceURL) : previewSourceURL}
options={{
cMapUrl: 'cmaps/',
cMapPacked: true,
}}
externalLinkTarget="_blank"
onPassword={onPassword}
onLoad={() => {
setHasError(false);
}}
onLoadError={() => {
if (onLoadError) {
onLoadError();
}
setHasError(true);
}}
error={() => null}
>
<View pointerEvents="none">
<Thumbnail pageIndex={0} />
</View>
</Document>
),
[isAuthTokenRequired, previewSourceURL, onPassword, onLoadError],
);
return (
<View style={[style, styles.h100, styles.overflowHidden]}>
<View style={[styles.w100, styles.h100, !hasError && styles.alignItemsCenter, !hasError && styles.justifyContentCenter]}>
{enabled && thumbnail}
{hasError && (
<View style={[styles.justifyContentCenter, styles.pdfErrorPlaceholder, styles.alignItemsCenter]}>
<Icon
src={Expensicons.ReceiptSlash}
width={variables.receiptPlaceholderIconWidth}
height={variables.receiptPlaceholderIconHeight}
fill={theme.icon}
/>
</View>
)}
</View>
</View>
);
}
PDFThumbnail.displayName = 'PDFThumbnail';
export default React.memo(PDFThumbnail);