|
1
|
import { Text } from "@medusajs/ui" |
|
2
|
import QRCode from "qrcode" |
|
3
|
import { useEffect, useRef, useState } from "react" |
|
4
|
import { useTranslation } from "react-i18next" |
|
5
|
|
|
6
|
type MfaQrCodeProps = { |
|
7
|
value: string |
|
8
|
} |
|
9
|
|
|
10
|
export const MfaQrCode = ({ value }: MfaQrCodeProps) => { |
|
11
|
const { t } = useTranslation() |
|
12
|
const canvasRef = useRef<HTMLCanvasElement>(null) |
|
13
|
const [error, setError] = useState(false) |
|
14
|
|
|
15
|
useEffect(() => { |
|
16
|
const canvas = canvasRef.current |
|
17
|
|
|
18
|
if (!canvas) { |
|
19
|
return |
|
20
|
} |
|
21
|
|
|
22
|
setError(false) |
|
23
|
|
|
24
|
QRCode.toCanvas(canvas, value, { |
|
25
|
margin: 2, |
|
26
|
width: 220, |
|
27
|
errorCorrectionLevel: "M", |
|
28
|
}).catch(() => { |
|
29
|
setError(true) |
|
30
|
}) |
|
31
|
}, [value]) |
|
32
|
|
|
33
|
if (error) { |
|
34
|
return ( |
|
35
|
<div className="border-ui-border-base bg-ui-bg-subtle flex size-[220px] items-center justify-center rounded-md border"> |
|
36
|
<Text size="small" className="text-ui-fg-subtle text-center"> |
|
37
|
{t("profile.mfa.qrError")} |
|
38
|
</Text> |
|
39
|
</div> |
|
40
|
) |
|
41
|
} |
|
42
|
|
|
43
|
return ( |
|
44
|
<canvas |
|
45
|
ref={canvasRef} |
|
46
|
className="border-ui-border-base bg-ui-bg-base rounded-md border" |
|
47
|
height={220} |
|
48
|
width={220} |
|
49
|
/> |
|
50
|
) |
|
51
|
} |