QRCode.tsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { useEffect, useState } from "react";
  2. import Spinner from "@excalidraw/excalidraw/components/Spinner";
  3. interface QRCodeProps {
  4. value: string;
  5. }
  6. export const QRCode = ({ value }: QRCodeProps) => {
  7. const [svgData, setSvgData] = useState<string | null>(null);
  8. const [error, setError] = useState<boolean>(false);
  9. useEffect(() => {
  10. let mounted = true;
  11. import("./qrcode.chunk")
  12. .then(({ generateQRCodeSVG }) => {
  13. if (mounted) {
  14. try {
  15. setSvgData(generateQRCodeSVG(value));
  16. } catch {
  17. setError(true);
  18. }
  19. }
  20. })
  21. .catch(() => {
  22. if (mounted) {
  23. setError(true);
  24. }
  25. });
  26. return () => {
  27. mounted = false;
  28. };
  29. }, [value]);
  30. if (error) {
  31. return null;
  32. }
  33. if (!svgData) {
  34. return (
  35. <div className="ShareDialog__active__qrcode ShareDialog__active__qrcode--loading">
  36. <Spinner />
  37. </div>
  38. );
  39. }
  40. return (
  41. <div
  42. className="ShareDialog__active__qrcode"
  43. role="img"
  44. aria-label="QR code for collaboration link"
  45. dangerouslySetInnerHTML={{ __html: svgData }}
  46. />
  47. );
  48. };