FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

98 lines
2.9 KiB

  1. "use client";
  2. import { QrCodeInfo } from "@/app/api/qrcode";
  3. import { Box, Button, Modal, ModalProps } from "@mui/material";
  4. import { useCallback, useEffect, useMemo, useState } from "react";
  5. import BarcodeScanner, { BarcodeStringFormat } from "react-qr-barcode-scanner";
  6. import { BarcodeFormat, Result } from "@zxing/library";
  7. interface Props {
  8. scannerConfig: ScannerConfig;
  9. }
  10. // interface Props extends Omit<ModalProps, "children"> {
  11. // scannerConfig: ScannerConfig;
  12. // }
  13. const style = {
  14. position: "absolute",
  15. top: "50%",
  16. left: "50%",
  17. transform: "translate(-50%, -50%)",
  18. bgcolor: "background.paper",
  19. pt: 5,
  20. px: 5,
  21. pb: 10,
  22. width: { xs: "80%", sm: "80%", md: "80%" },
  23. };
  24. export var defaultScannerConfig: ScannerConfig = {
  25. onUpdate: (err, result) => {
  26. if (result) {
  27. const data = JSON.parse(result.getText());
  28. console.log(data);
  29. } else return;
  30. },
  31. width: 500,
  32. height: 500,
  33. facingMode: "environment",
  34. // torch: false
  35. };
  36. export interface ScannerConfig {
  37. onUpdate: (arg0: unknown, arg1?: Result) => void;
  38. onError?: (arg0: string | DOMException) => void;
  39. width?: number | string;
  40. height?: number | string;
  41. facingMode?: "environment" | "user"; // default environment
  42. delay?: number; // Delay between scans in milliseconds. Default is 500ms.
  43. videoConstraints?: MediaTrackConstraints; // Video constraints to pass to the webcam. If not provided, the default constraints will be used.
  44. formats?: BarcodeFormat[] | BarcodeStringFormat[]; // Array of barcode formats to decode. If not provided, all formats will be used. A smaller list may improve the speed of the scan.
  45. stopStream?: boolean;
  46. }
  47. const ReactQrCodeScanner: React.FC<Props> = ({ scannerConfig }) => {
  48. const [stopStream, setStopStream] = useState(
  49. scannerConfig.stopStream || defaultScannerConfig.stopStream || false
  50. );
  51. const [torchEnabled, setTorchEnabled] = useState<boolean>(false);
  52. // const _scannerConfig = useMemo(() => ({
  53. // ...defaultScannerConfig,
  54. // ...scannerConfig,
  55. // }),[])
  56. const [_scannerConfig, setScannerConfig] = useState<ScannerConfig>({
  57. ...defaultScannerConfig
  58. });
  59. useEffect(() => {
  60. setScannerConfig({
  61. ...defaultScannerConfig,
  62. ...scannerConfig,
  63. });
  64. }, []);
  65. const SwitchOnOffScanner = useCallback(() => {
  66. // Stop the QR Reader stream (fixes issue where the browser freezes when closing the modal) and then dismiss the modal one tick later
  67. setStopStream((prev) => !prev);
  68. }, []);
  69. const SwitchOnOffTorch = useCallback(() => {
  70. setTorchEnabled((prev) => !prev);
  71. }, []);
  72. return (
  73. <>
  74. {!stopStream ? (
  75. <BarcodeScanner
  76. stopStream={stopStream}
  77. torch={torchEnabled}
  78. {..._scannerConfig}
  79. />
  80. ) : undefined}
  81. <Button onClick={SwitchOnOffTorch}>{torchEnabled ? "off" : "on"}</Button>
  82. <Button onClick={SwitchOnOffScanner}>
  83. {stopStream ? "start" : "stop"}
  84. </Button>
  85. </>
  86. );
  87. };
  88. export default ReactQrCodeScanner;