-
Notifications
You must be signed in to change notification settings - Fork 1
feat: addresses qr code scanning #108
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,7 @@ | ||
| import { createIcon } from '@chakra-ui/icons' | ||
|
|
||
| export const QRCodeIcon = createIcon({ | ||
| displayName: 'QRCodeIcon', | ||
| viewBox: '0 0 24 24', | ||
| d: 'M3 11V3H11V11H3ZM5 9H9V5H5V9ZM3 21V13H11V21H3ZM5 19H9V15H5V19ZM13 11V3H21V11H13ZM15 9H19V5H15V9ZM21 21H19V19H21V21ZM13 17V13H15V17H13ZM17 17V13H21V15H19V17H17ZM17 21V19H19V21H17ZM13 21V19H15V21H13Z', | ||
| }) |
This file contains hidden or 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,79 @@ | ||
| import { Html5Qrcode, Html5QrcodeScannerState, Html5QrcodeSupportedFormats } from 'html5-qrcode' | ||
| import type { | ||
| QrcodeErrorCallback, | ||
| QrcodeSuccessCallback, | ||
| QrDimensions, | ||
| } from 'html5-qrcode/esm/core' | ||
| import React, { useEffect, useState } from 'react' | ||
| import { isMobile } from 'react-device-detect' | ||
|
|
||
| export type DOMExceptionCallback = (errorMessage: string) => void | ||
|
|
||
| const qrcodeRegionId = 'reader' | ||
|
|
||
| type QrCodeReaderProps = { | ||
| fps: number | ||
| qrbox: number | QrDimensions | ||
| qrCodeSuccessCallback: QrcodeSuccessCallback | ||
| qrCodeErrorCallback: QrcodeErrorCallback | DOMExceptionCallback | ||
| } | ||
|
|
||
| export const QrCodeReader: React.FC<QrCodeReaderProps> = ({ | ||
| fps, | ||
| qrbox, | ||
| qrCodeSuccessCallback, | ||
| qrCodeErrorCallback, | ||
| }) => { | ||
| const [cameraId, setCameraId] = useState<string | null>(null) | ||
|
|
||
| useEffect(() => { | ||
| const qrScanner = new Html5Qrcode(qrcodeRegionId, { | ||
| formatsToSupport: [Html5QrcodeSupportedFormats.QR_CODE], | ||
| verbose: false, | ||
| }) | ||
|
|
||
| ;(async () => { | ||
| if (!cameraId) { | ||
| try { | ||
| const devices = await Html5Qrcode.getCameras() | ||
|
|
||
| if (devices?.length) { | ||
| setCameraId(devices[0].id) | ||
| } | ||
| } catch (e) { | ||
| // Errors on getCameras() are caused by browser native APIs exception e.g not supported, permission not granted etc | ||
| const error = e as DOMException['message'] | ||
| ;(qrCodeErrorCallback as DOMExceptionCallback)(error) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| await qrScanner.start( | ||
| isMobile ? { facingMode: 'environment' } : cameraId, | ||
| { | ||
| fps, | ||
| qrbox, | ||
| }, | ||
| (decodedText, result) => { | ||
| qrCodeSuccessCallback(decodedText, result) | ||
| }, | ||
| qrCodeErrorCallback as QrcodeErrorCallback, | ||
| ) | ||
| })() | ||
|
|
||
| return () => { | ||
| ;(async () => { | ||
| const scannerState = qrScanner.getState() | ||
|
|
||
| if (scannerState === Html5QrcodeScannerState.SCANNING) { | ||
| await qrScanner.stop() | ||
| qrScanner.clear() | ||
| } | ||
| })() | ||
| } | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [cameraId]) | ||
|
|
||
| return <div id={qrcodeRegionId} /> | ||
| } |
This file contains hidden or 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,100 @@ | ||
| import { | ||
| Alert, | ||
| AlertIcon, | ||
| Box, | ||
| Button, | ||
| Flex, | ||
| Modal, | ||
| ModalBody, | ||
| ModalCloseButton, | ||
| ModalContent, | ||
| ModalHeader, | ||
| ModalOverlay, | ||
| } from '@chakra-ui/react' | ||
| import type { Html5QrcodeError, Html5QrcodeResult } from 'html5-qrcode/esm/core' | ||
| import { Html5QrcodeErrorTypes } from 'html5-qrcode/esm/core' | ||
| import { useCallback, useState } from 'react' | ||
|
|
||
| import { QrCodeReader } from './QrCodeReader' | ||
|
|
||
| const PERMISSION_ERROR = 'NotAllowedError : Permission denied' | ||
| const isPermissionError = ( | ||
| error: DOMException['message'] | Html5QrcodeError, | ||
| ): error is DOMException['message'] => | ||
| typeof (error as DOMException['message']) === 'string' && error === PERMISSION_ERROR | ||
|
|
||
| const boxStyle = { | ||
| width: '100%', | ||
| minHeight: '298px', | ||
| overflow: 'hidden', | ||
| borderRadius: '1rem', | ||
| } | ||
| const qrBoxStyle = { width: 250, height: 250 } | ||
|
|
||
| export type QrCodeScannerProps = { | ||
| isOpen: boolean | ||
| onClose: () => void | ||
| onSuccess: (address: string) => void | ||
| } | ||
|
|
||
| export const QrCodeScanner = ({ isOpen, onClose, onSuccess }: QrCodeScannerProps) => { | ||
| const [scanError, setScanError] = useState<DOMException['message'] | null>(null) | ||
|
|
||
| const handleScanSuccess = useCallback( | ||
| (decodedText: string, _result: Html5QrcodeResult) => { | ||
| onSuccess(decodedText) | ||
| onClose() | ||
| }, | ||
| [onSuccess, onClose], | ||
| ) | ||
|
|
||
| const handleScanError = useCallback((_errorMessage: string, error?: Html5QrcodeError) => { | ||
| if (error?.type === Html5QrcodeErrorTypes.UNKWOWN_ERROR) { | ||
| // https://github.com/mebjas/html5-qrcode/issues/320 | ||
| // 'NotFoundException: No MultiFormat Readers were able to detect the code' errors are thrown on every frame until a valid QR is detected, don't handle these | ||
| return | ||
| } | ||
|
|
||
| setScanError(_errorMessage) | ||
| }, []) | ||
|
|
||
| const handlePermissionsButtonClick = useCallback(() => setScanError(null), []) | ||
|
|
||
| return ( | ||
| <Modal isOpen={isOpen} onClose={onClose} size='md'> | ||
| <ModalOverlay /> | ||
| <ModalContent> | ||
| <ModalHeader> | ||
| Scan QR Code | ||
| <ModalCloseButton /> | ||
| </ModalHeader> | ||
| <ModalBody pb={6}> | ||
| {scanError ? ( | ||
| <Flex justifyContent='center' alignItems='center' flexDirection='column' pb={4}> | ||
| <Alert status='error' borderRadius='xl'> | ||
| <AlertIcon /> | ||
| {isPermissionError(scanError) | ||
| ? 'Camera permission denied. Please allow camera access to scan QR codes.' | ||
| : 'An error occurred while trying to scan the QR code.'} | ||
| </Alert> | ||
| {isPermissionError(scanError) && ( | ||
| <Button colorScheme='blue' mt='5' onClick={handlePermissionsButtonClick}> | ||
| Try Again | ||
| </Button> | ||
| )} | ||
| </Flex> | ||
| ) : ( | ||
| <Box style={boxStyle}> | ||
| <QrCodeReader | ||
| qrbox={qrBoxStyle} | ||
| fps={10} | ||
| qrCodeSuccessCallback={handleScanSuccess} | ||
| qrCodeErrorCallback={handleScanError} | ||
| /> | ||
| </Box> | ||
| )} | ||
| </ModalBody> | ||
| </ModalContent> | ||
| </Modal> | ||
| ) | ||
| } |
This file contains hidden or 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.
Required for mobile detection for QR scanning