33import type { Options } from "qr-code-styling"
44import QRCodeStyling from "qr-code-styling"
55import type React from "react"
6- import { useEffect , useMemo , useRef } from "react"
7- import logo from "@/assets/logo.svg"
8- import type { QrOptions } from "@/lib/qr-config"
6+ import { useCallback , useEffect , useMemo , useRef } from "react"
7+ import { makeOptions , type QrOptions } from "@/lib/qr/config"
98import { cn } from "@/lib/utils"
109
1110export type QrCodeProps = React . HTMLAttributes < HTMLDivElement > & {
@@ -24,61 +23,20 @@ export function QrCode({
2423} : QrCodeProps ) {
2524 const ref = useRef < HTMLDivElement > ( null )
2625
27- const { style, background } = options
28- const styled = style === "styled"
29-
30- const settings = useMemo < Options > ( ( ) => {
31- const color = styled ? "#1156ae" : "#000000"
32- return {
33- type : "canvas" ,
34- width : size ,
35- height : size ,
36- margin : size / 32 ,
37- data : url ,
38- image : styled ? logo . src : undefined ,
39- imageOptions : { margin : size / 64 } ,
40- qrOptions : {
41- typeNumber : 0 ,
42- mode : "Byte" ,
43- errorCorrectionLevel : styled ? "Q" : "M" ,
44- } ,
45- dotsOptions : {
46- color,
47- type : styled ? "extra-rounded" : "square" ,
48- } ,
49- cornersDotOptions : {
50- color,
51- } ,
52- cornersSquareOptions : {
53- color,
54- type : styled ? "extra-rounded" : "square" ,
55- } ,
56- backgroundOptions : {
57- color : background === "white" ? "#ffffff" : "transparent" ,
58- } ,
59- }
60- } , [ url , styled , background , size ] )
26+ // biome-ignore lint/correctness/useExhaustiveDependencies: biome is wrong
27+ const opts = useCallback ( makeOptions ( options ) , [ options ] )
28+ const settings = useMemo < Options > ( ( ) => opts ( url , size ) , [ opts , url , size ] )
6129
6230 // biome-ignore lint/correctness/useExhaustiveDependencies: manually updated externally
6331 const qr = useMemo ( ( ) => new QRCodeStyling ( settings ) , [ ] )
6432
6533 useEffect ( ( ) => {
6634 qr . update ( settings )
6735 qr . getRawData ( "png" ) . then ( ( data ) => {
68- if ( onImageData ) {
69- let blob : Blob | null = null
70- if ( data instanceof Blob ) {
71- blob = data
72- } else if ( data instanceof Buffer ) {
73- blob = new Blob ( [ new Uint8Array ( data ) ] , { type : "image/png" } )
74- }
75- onImageData ( blob )
76- }
36+ onImageData ?.( data instanceof Blob ? data : null )
7737 } )
7838 return ( ) => {
79- if ( onImageData ) {
80- onImageData ( null )
81- }
39+ onImageData ?.( null )
8240 }
8341 } , [ qr , settings , onImageData ] )
8442
0 commit comments