@@ -13,6 +13,7 @@ import jsQR from 'jsqr'
1313
1414import { showErrorMessage } from 'services/remote'
1515import { drawPolygon } from 'utils/canvasActions'
16+ import { verifyAddress } from 'utils/validators'
1617
1718interface QRScannerProps {
1819 title : string
@@ -37,6 +38,21 @@ const QRScanner = ({ title, label, onConfirm, styles }: QRScannerProps) => {
3738 const [ t ] = useTranslation ( )
3839 const canvasRef = useRef < HTMLCanvasElement > ( null )
3940 const video = useMemo ( ( ) => document . createElement ( 'video' ) , [ ] )
41+
42+ const onOpen = useCallback ( ( ) => {
43+ setData ( '' )
44+ setOpen ( true )
45+ } , [ setOpen ] )
46+
47+ const onDismiss = useCallback ( ( ) => {
48+ setOpen ( false )
49+ } , [ setOpen ] )
50+
51+ const onSubmit = useCallback ( ( ) => {
52+ onConfirm ( data )
53+ onDismiss ( )
54+ } , [ data , onConfirm , onDismiss ] )
55+
4056 const scan = useCallback ( ( ) => {
4157 navigator . mediaDevices
4258 . getUserMedia ( {
@@ -70,7 +86,12 @@ const QRScanner = ({ title, label, onConfirm, styles }: QRScannerProps) => {
7086 drawPolygon ( canvas , [ topLeftCorner , topRightCorner , bottomRightCorner , bottomLeftCorner ] , {
7187 color,
7288 } )
73- setData ( code . data )
89+ if ( verifyAddress ( code . data ) ) {
90+ onConfirm ( code . data )
91+ onDismiss ( )
92+ } else {
93+ setData ( code . data )
94+ }
7495 }
7596 }
7697 }
@@ -82,7 +103,7 @@ const QRScanner = ({ title, label, onConfirm, styles }: QRScannerProps) => {
82103 showErrorMessage ( t ( 'messages.camera-not-available-or-disabled' ) , err . message )
83104 setOpen ( false )
84105 } )
85- } , [ video , t ] )
106+ } , [ video , t , onConfirm , onDismiss ] )
86107
87108 useEffect ( ( ) => {
88109 if ( open ) {
@@ -104,10 +125,7 @@ const QRScanner = ({ title, label, onConfirm, styles }: QRScannerProps) => {
104125 outline : 'none' ,
105126 }
106127 }
107- onClick = { ( ) => {
108- setOpen ( true )
109- } }
110- onKeyPress = { ( ) => { } }
128+ onClick = { onOpen }
111129 type = "button"
112130 >
113131 < IconButton
@@ -118,30 +136,16 @@ const QRScanner = ({ title, label, onConfirm, styles }: QRScannerProps) => {
118136 } }
119137 />
120138 </ button >
121- < Dialog
122- hidden = { ! open }
123- onDismiss = { ( ) => {
124- setOpen ( false )
125- } }
126- maxWidth = "900px"
127- minWidth = "500xp"
128- >
139+ < Dialog hidden = { ! open } onDismiss = { onDismiss } maxWidth = "900px" minWidth = "500xp" >
129140 < h1 > { title } </ h1 >
130141 < div >
131142 < canvas ref = { canvasRef } />
132143 </ div >
133144 < DialogFooter >
134145 < TextField readOnly value = { data } label = { label } underlined />
135146 < Stack horizontal horizontalAlign = "end" tokens = { { childrenGap : 10 } } >
136- < DefaultButton onClick = { ( ) => setOpen ( false ) } > { t ( 'common.cancel' ) } </ DefaultButton >
137- < PrimaryButton
138- onClick = { ( ) => {
139- onConfirm ( data )
140- setOpen ( false )
141- } }
142- >
143- { t ( 'common.confirm' ) }
144- </ PrimaryButton >
147+ < DefaultButton onClick = { onDismiss } > { t ( 'common.cancel' ) } </ DefaultButton >
148+ < PrimaryButton onClick = { onSubmit } > { t ( 'common.confirm' ) } </ PrimaryButton >
145149 </ Stack >
146150 </ DialogFooter >
147151 </ Dialog >
0 commit comments