|
|
@@ -0,0 +1,99 @@ |
|
|
|
import { Button, Card, CardContent, Modal, ModalProps, SxProps } from "@mui/material"; |
|
|
|
import { Html5QrcodeResult, Html5QrcodeScanner, QrcodeErrorCallback, QrcodeSuccessCallback } from "html5-qrcode"; |
|
|
|
import { Html5QrcodeError } from "html5-qrcode/esm/core"; |
|
|
|
import { Html5QrcodeScannerConfig } from "html5-qrcode/esm/html5-qrcode-scanner"; |
|
|
|
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
|
|
|
|
|
|
|
const scannerSx: React.CSSProperties = { |
|
|
|
position: "absolute", |
|
|
|
// top: "50%", |
|
|
|
// left: "50%", |
|
|
|
// transform: "translate(-50%, -50%)", |
|
|
|
width: "90%", |
|
|
|
maxHeight: "10%", |
|
|
|
maxWidth: 1400, |
|
|
|
}; |
|
|
|
|
|
|
|
type QrCodeScannerProps = { |
|
|
|
onScanSuccess: (result: string) => void, |
|
|
|
onScanError?: (error: string) => void |
|
|
|
} |
|
|
|
|
|
|
|
const scannerConfig: Html5QrcodeScannerConfig = { |
|
|
|
fps: 10, |
|
|
|
qrbox: { width: 400, height: 400 }, |
|
|
|
aspectRatio: 2.5, |
|
|
|
} |
|
|
|
|
|
|
|
const QrCodeScanner: React.FC<QrCodeScannerProps> = ({ |
|
|
|
onScanSuccess, |
|
|
|
onScanError |
|
|
|
}) => { |
|
|
|
|
|
|
|
const [isScanned, setIsScanned] = useState<boolean>(false) |
|
|
|
const [scanner, setScanner] = useState<Html5QrcodeScanner | null>(null) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
setScanner(new Html5QrcodeScanner( |
|
|
|
"qr-reader", |
|
|
|
scannerConfig, |
|
|
|
false |
|
|
|
)) |
|
|
|
}, []) |
|
|
|
|
|
|
|
const handleStartScan = useCallback(() => { |
|
|
|
setIsScanned(false) |
|
|
|
scanner?.resume(); |
|
|
|
}, [scanner]) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
if (scanner) { |
|
|
|
console.log("Scanner Instance:", scanner); |
|
|
|
const success: QrcodeSuccessCallback = (decodedText, result) => { |
|
|
|
console.log(`Decoded text: ${decodedText}`); |
|
|
|
// Handle the decoded text as needed |
|
|
|
setIsScanned(true) |
|
|
|
scanner.pause(); |
|
|
|
onScanSuccess(decodedText) |
|
|
|
}; |
|
|
|
|
|
|
|
const error: QrcodeErrorCallback = (errorMessage, error) => { |
|
|
|
console.log(`Error: ${errorMessage}`); |
|
|
|
|
|
|
|
if (onScanError) { |
|
|
|
onScanError(errorMessage) |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
try { |
|
|
|
scanner.render(success, error); |
|
|
|
console.log("Scanner render called"); |
|
|
|
} catch (err) { |
|
|
|
console.error("Failed to render scanner:", err); |
|
|
|
} |
|
|
|
|
|
|
|
return () => { |
|
|
|
console.log("Cleaning up scanner..."); |
|
|
|
scanner.clear().catch((error) => { |
|
|
|
console.error("Failed to clear html5QrcodeScanner. ", error); |
|
|
|
}); |
|
|
|
}; |
|
|
|
} |
|
|
|
}, [scanner]); |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
<> |
|
|
|
<div id="qr-reader" hidden={isScanned} /> |
|
|
|
<Button |
|
|
|
size="small" |
|
|
|
onClick={handleStartScan} |
|
|
|
variant="contained" |
|
|
|
> |
|
|
|
{isScanned ? "Re-Scan" : "Stop-Scanning"} |
|
|
|
</Button> |
|
|
|
</> |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
export default QrCodeScanner |