-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add PDFThumbnail to preview PDF receipt #35255
Merged
luacmartins
merged 13 commits into
Expensify:main
from
eh2077:31432-smart-scan-pdf-thumbnail
Mar 5, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
835619b
feat: add PDFThumbnail to preview PDF receipt
eh2077 cb360d3
popup alert and return back when uploading a protected PDF
eh2077 8d514bc
skip loading protected PDF and address PR comments
eh2077 ebc5180
solve conflicts
eh2077 f8d33e3
use two props to control loading protected PDF
eh2077 2b15512
fix props naming
eh2077 3a350fd
solve conflicts
eh2077 878be89
update alert messages of confirm modal
eh2077 ecc5960
fix pointer style for not clickable thumbnail and address PR comments
eh2077 43b70c1
use pointerEvents='none' to remove default pointer style added by bro…
eh2077 6f4a890
solve conflicts
eh2077 b18c364
improve pdf worker import and ts-expect-error comments
eh2077 c999e10
Update src/languages/es.ts
eh2077 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming for this issue #39356 , since we are not passing the error prop to the Document, the Document will display the message 'Failed to load PDF file.' text in case of an error. This, however, causes a UI issue in the dark theme of our app. we should have passed a custom error component here to avoid UI issue.