-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35255 from eh2077/31432-smart-scan-pdf-thumbnail
feat: add PDFThumbnail to preview PDF receipt
- Loading branch information
Showing
9 changed files
with
190 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import {View} from 'react-native'; | ||
import Pdf from 'react-native-pdf'; | ||
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL'; | ||
import type PDFThumbnailProps from './types'; | ||
|
||
function PDFThumbnail({previewSourceURL, style, isAuthTokenRequired = false, enabled = true, onPassword = () => {}}: PDFThumbnailProps) { | ||
const styles = useThemeStyles(); | ||
const sizeStyles = [styles.w100, styles.h100]; | ||
|
||
return ( | ||
<View style={[style, styles.overflowHidden]}> | ||
<View style={[sizeStyles, styles.alignItemsCenter, styles.justifyContentCenter]}> | ||
{enabled && ( | ||
fitPolicy={0} | ||
trustAllCerts={false} | ||
renderActivityIndicator={() => <FullScreenLoadingIndicator />} | ||
source={{uri: isAuthTokenRequired ? addEncryptedAuthTokenToURL(previewSourceURL) : previewSourceURL}} | ||
singlePage | ||
style={sizeStyles} | ||
onError={(error) => { | ||
if (!('message' in error && typeof error.message === 'string' && error.message.match(/password/i))) { | ||
return; | ||
} | ||
onPassword(); | ||
}} | ||
/> | ||
)} | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
PDFThumbnail.displayName = 'PDFThumbnail'; | ||
export default React.memo(PDFThumbnail); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// @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} from 'react'; | ||
import {View} from 'react-native'; | ||
import {Document, pdfjs, Thumbnail} from 'react-pdf'; | ||
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import addEncryptedAuthTokenToURL from '@libs/addEncryptedAuthTokenToURL'; | ||
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 = () => {}}: PDFThumbnailProps) { | ||
const styles = useThemeStyles(); | ||
|
||
const thumbnail = useMemo( | ||
() => ( | ||
<Document | ||
loading={<FullScreenLoadingIndicator />} | ||
file={isAuthTokenRequired ? addEncryptedAuthTokenToURL(previewSourceURL) : previewSourceURL} | ||
options={{ | ||
cMapUrl: 'cmaps/', | ||
cMapPacked: true, | ||
}} | ||
externalLinkTarget="_blank" | ||
onPassword={() => { | ||
onPassword(); | ||
}} | ||
> | ||
<View pointerEvents="none"> | ||
<Thumbnail pageIndex={0} /> | ||
</View> | ||
</Document> | ||
), | ||
[isAuthTokenRequired, previewSourceURL, onPassword], | ||
); | ||
|
||
return ( | ||
<View style={[style, styles.overflowHidden]}> | ||
<View style={[styles.w100, styles.h100, styles.alignItemsCenter, styles.justifyContentCenter]}>{enabled && thumbnail}</View> | ||
</View> | ||
); | ||
} | ||
|
||
PDFThumbnail.displayName = 'PDFThumbnail'; | ||
export default React.memo(PDFThumbnail); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type {StyleProp, ViewStyle} from 'react-native'; | ||
|
||
type PDFThumbnailProps = { | ||
/** Source URL for the preview PDF */ | ||
previewSourceURL: string; | ||
|
||
/** Any additional styles to apply */ | ||
style?: StyleProp<ViewStyle>; | ||
|
||
/** Whether the PDF thumbnail requires an authToken */ | ||
isAuthTokenRequired?: boolean; | ||
|
||
/** Whether the PDF thumbnail can be loaded */ | ||
enabled?: boolean; | ||
|
||
/** Callback to call if PDF is password protected */ | ||
onPassword?: () => void; | ||
}; | ||
|
||
export default PDFThumbnailProps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters