From acb7bd1dfb42f9ba11be916b63e8d34e6fe6a444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1rio=20Guimar=C3=A3es?= <49988070+Darguima@users.noreply.github.com> Date: Tue, 27 Feb 2024 17:40:50 +0000 Subject: [PATCH] feat: permissions button on the QR Scanner (#729) --- components/Layout/Layout.tsx | 2 +- .../QRScanner/BarebonesQRScanner/index.jsx | 136 ------------------ .../QRScanner/BarebonesQRScanner/index.tsx | 104 ++++++++++++++ .../BarebonesQRScanner/useQRScanner.ts | 97 +++++++++++++ .../BarebonesQRScanner/useWebcam.tsx | 110 ++++++++++++++ components/QRScanner/index.jsx | 65 --------- components/QRScanner/index.tsx | 89 ++++++++++++ layout/SignUp/components/SignUpForm/index.tsx | 22 +-- layout/Sponsor/Scanner/Scanner.tsx | 43 +++--- layout/Staff/Badges/Badges.tsx | 54 +++---- layout/Staff/Identifier/Identifier.tsx | 71 +++++---- layout/Staff/Prizes/Prizes.tsx | 25 ++-- 12 files changed, 497 insertions(+), 321 deletions(-) delete mode 100644 components/QRScanner/BarebonesQRScanner/index.jsx create mode 100644 components/QRScanner/BarebonesQRScanner/index.tsx create mode 100644 components/QRScanner/BarebonesQRScanner/useQRScanner.ts create mode 100644 components/QRScanner/BarebonesQRScanner/useWebcam.tsx delete mode 100644 components/QRScanner/index.jsx create mode 100644 components/QRScanner/index.tsx diff --git a/components/Layout/Layout.tsx b/components/Layout/Layout.tsx index 0cc900ce..2003b3c9 100644 --- a/components/Layout/Layout.tsx +++ b/components/Layout/Layout.tsx @@ -130,7 +130,7 @@ export default function Layout({ title, description, children }: LayoutProps) { {/* CONTENT */} -
+

{title}

diff --git a/components/QRScanner/BarebonesQRScanner/index.jsx b/components/QRScanner/BarebonesQRScanner/index.jsx deleted file mode 100644 index d6b5682e..00000000 --- a/components/QRScanner/BarebonesQRScanner/index.jsx +++ /dev/null @@ -1,136 +0,0 @@ -import { useEffect, useRef, useState } from "react"; -import jsQR from "jsqr"; - -function BarebonesQRScanner({ handleCode, pauseRef }) { - const canvasRef = useRef(null); - const videoRef = useRef(null); - const wrapperRef = useRef(null); - const animationFrameRef = useRef(); - const [error, setError] = useState(""); - - useEffect(() => { - const video = videoRef.current; - - navigator.mediaDevices - .getUserMedia({ video: { facingMode: "environment" } }) - .then(function (stream) { - //to prevent AbortError on Firefox in strict mode - if (!video.srcObject) { - setError(""); - video.srcObject = stream; - video.setAttribute("playsinline", true); // required to tell iOS safari we don't want fullscreen - video.play(); - animationFrameRef.current = requestAnimationFrame(tick); - } - }) - .catch((e) => { - setError( - "We couldn't access your camera. Check if your camera is being used by another app and if you gave us permission to use it." - ); - video.srcObject = undefined; - }); - - return () => { - if (video.srcObject) { - video.srcObject.getTracks().forEach((track) => track.stop()); - video.srcObject = undefined; - } - }; - }, []); - - function drawLine(canvas, begin, end, color) { - canvas.beginPath(); - canvas.moveTo(begin.x, begin.y); - canvas.lineTo(end.x, end.y); - canvas.lineWidth = 4; - canvas.strokeStyle = color; - canvas.stroke(); - } - - function parseURL(url) { - try { - const url_obj = new URL(url); - if (url_obj.host !== process.env.NEXT_PUBLIC_QRCODE_HOST) return null; - return url_obj.pathname.split("/").at(-1); - } catch { - return null; - } - } - - function tick() { - const video = videoRef.current; - const canvas = canvasRef.current; - - if (!canvas) { - cancelAnimationFrame(animationFrameRef.current); - return null; - } - - const canvas2D = canvas.getContext("2d"); - - if (video.readyState === video.HAVE_ENOUGH_DATA) { - canvas.hidden = false; - - canvas.height = video.videoHeight; - canvas.width = video.videoWidth; - canvas2D.drawImage(video, 0, 0, canvas.width, canvas.height); - var imageData = canvas2D.getImageData(0, 0, canvas.width, canvas.height); - var code = jsQR(imageData.data, imageData.width, imageData.height, { - inversionAttempts: "dontInvert", - }); - - if (code) { - drawLine( - canvas2D, - code.location.topLeftCorner, - code.location.topRightCorner, - "#78f400" - ); - drawLine( - canvas2D, - code.location.topRightCorner, - code.location.bottomRightCorner, - "#78f400" - ); - drawLine( - canvas2D, - code.location.bottomRightCorner, - code.location.bottomLeftCorner, - "#78f400" - ); - drawLine( - canvas2D, - code.location.bottomLeftCorner, - code.location.topLeftCorner, - "#78f400" - ); - - if (!pauseRef.current) { - const uuid = parseURL(code.data); - - if (uuid) { - pauseRef.current = true; - handleCode(uuid); - } - } - } - } - animationFrameRef.current = requestAnimationFrame(tick); - } - - return ( - <> -