|
1 | | -import React, { useEffect, useRef, useMemo, useState } from 'react' |
| 1 | +import React, { useEffect, useState, useCallback } from 'react' |
2 | 2 | import { captureScreen } from 'services/remote' |
3 | | -import Button from 'widgets/Button' |
4 | 3 | import { isSuccessResponse } from 'utils' |
5 | 4 | import { useTranslation } from 'react-i18next' |
6 | | -import Dialog from 'widgets/Dialog' |
7 | 5 | import AlertDialog from 'widgets/AlertDialog' |
| 6 | +import LoadingDialog from 'widgets/LoadingDialog' |
8 | 7 | import jsQR from 'jsqr' |
9 | 8 |
|
10 | | -import styles from './screenScanDialog.module.scss' |
11 | | - |
12 | | -interface Point { |
13 | | - x: number |
14 | | - y: number |
15 | | -} |
16 | | - |
17 | | -const ScreenScanDialog = ({ close, onConfirm }: { close: () => void; onConfirm: (result: string) => void }) => { |
| 9 | +const ScreenScanDialog = ({ close, onConfirm }: { close: () => void; onConfirm: (result: string[]) => void }) => { |
18 | 10 | const [t] = useTranslation() |
19 | | - const imgRef = useRef<HTMLImageElement>(null) |
20 | | - const canvasRef = useRef<HTMLCanvasElement>(null) |
21 | | - const canvas2dRef = useRef<CanvasRenderingContext2D>() |
22 | | - const [sources, setSources] = useState<Controller.CaptureScreenSource[]>([]) |
23 | | - const [selectId, setSelectId] = useState('') |
24 | | - const [dialogType, setDialogType] = useState<'' | 'access-fail' | 'scan'>('') |
25 | | - const [uri, setUri] = useState('') |
| 11 | + const [dialogType, setDialogType] = useState<'' | 'scanning' | 'access-fail'>('') |
26 | 12 |
|
27 | | - const drawLine = (begin: Point, end: Point) => { |
28 | | - if (!canvas2dRef.current) return |
29 | | - canvas2dRef.current.beginPath() |
30 | | - canvas2dRef.current.moveTo(begin.x, begin.y) |
31 | | - canvas2dRef.current.lineTo(end.x, end.y) |
32 | | - canvas2dRef.current.lineWidth = 4 |
33 | | - canvas2dRef.current.strokeStyle = '#00c891' |
34 | | - canvas2dRef.current.stroke() |
35 | | - } |
| 13 | + const handleScanResult = useCallback(async (sources: Controller.CaptureScreenSource[]) => { |
| 14 | + const uriList = [] as string[] |
36 | 15 |
|
37 | | - const source = useMemo(() => sources.find(item => item.id === selectId), [selectId, sources]) |
| 16 | + const callArr = sources.map(async item => { |
| 17 | + const image = new Image() |
| 18 | + image.src = item.dataUrl |
| 19 | + await new Promise(resolve => { |
| 20 | + image.addEventListener('load', resolve) |
| 21 | + }) |
38 | 22 |
|
39 | | - useEffect(() => { |
40 | | - if (!source) return |
41 | | - |
42 | | - if (imgRef.current) { |
43 | | - const { width, height } = imgRef.current |
44 | | - canvasRef.current?.setAttribute('height', `${height}px`) |
45 | | - canvasRef.current?.setAttribute('width', `${width}px`) |
46 | | - const canvas2d = canvasRef.current?.getContext('2d') |
47 | | - if (canvas2d) { |
48 | | - canvas2d.drawImage(imgRef.current, 0, 0, width, height) |
49 | | - |
50 | | - canvas2dRef.current = canvas2d |
51 | | - const imageData = canvas2d.getImageData(0, 0, width, height) |
52 | | - const code = jsQR(imageData.data, imageData.width, imageData.height, { |
| 23 | + const canvas = document.createElement('canvas') |
| 24 | + canvas.width = image.width |
| 25 | + canvas.height = image.height |
| 26 | + const context = canvas.getContext('2d') |
| 27 | + if (context) { |
| 28 | + context.imageSmoothingEnabled = false |
| 29 | + context.drawImage(image, 0, 0) |
| 30 | + const imageData = context.getImageData(0, 0, image.width, image.height) |
| 31 | + const code = jsQR(imageData.data, image.width, image.height, { |
53 | 32 | inversionAttempts: 'dontInvert', |
54 | 33 | }) |
55 | | - |
56 | 34 | if (code?.data) { |
57 | | - drawLine(code.location.topLeftCorner, code.location.topRightCorner) |
58 | | - drawLine(code.location.topRightCorner, code.location.bottomRightCorner) |
59 | | - drawLine(code.location.bottomRightCorner, code.location.bottomLeftCorner) |
60 | | - drawLine(code.location.bottomLeftCorner, code.location.topLeftCorner) |
61 | | - setUri(code.data) |
| 35 | + uriList.push(code.data) |
62 | 36 | } |
63 | 37 | } |
64 | | - } |
65 | | - }, [source]) |
| 38 | + }) |
| 39 | + |
| 40 | + await Promise.all(callArr) |
| 41 | + |
| 42 | + onConfirm(uriList) |
| 43 | + }, []) |
66 | 44 |
|
67 | 45 | useEffect(() => { |
68 | 46 | captureScreen().then(res => { |
69 | 47 | if (isSuccessResponse(res)) { |
70 | | - setDialogType('scan') |
| 48 | + setDialogType('scanning') |
71 | 49 | const result = res.result as Controller.CaptureScreenSource[] |
72 | | - setSources(result) |
73 | | - if (result.length) { |
74 | | - setSelectId(result[0].id) |
75 | | - } |
| 50 | + handleScanResult(result) |
76 | 51 | } else { |
77 | 52 | setDialogType('access-fail') |
78 | 53 | } |
79 | 54 | }) |
80 | 55 | }, []) |
81 | 56 |
|
82 | | - const handleSelect = (e: React.SyntheticEvent<HTMLButtonElement>) => { |
83 | | - const { idx = '' } = e.currentTarget.dataset |
84 | | - if (idx !== selectId) { |
85 | | - setSelectId(idx) |
86 | | - setUri('') |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - const handleConfirm = () => { |
91 | | - onConfirm(uri) |
92 | | - } |
93 | | - |
94 | 57 | return ( |
95 | 58 | <> |
96 | | - <Dialog |
97 | | - show={dialogType === 'scan'} |
98 | | - title={t('wallet-connect.scan-qrcode')} |
99 | | - onCancel={close} |
100 | | - disabled={!uri} |
101 | | - onConfirm={handleConfirm} |
102 | | - className={styles.scanDialog} |
103 | | - > |
104 | | - <div className={styles.container}> |
105 | | - <div className={styles.chooseBox}> |
106 | | - {sources.map(({ dataUrl, id }) => ( |
107 | | - <Button |
108 | | - key={id} |
109 | | - className={styles.chooseItem} |
110 | | - data-idx={id} |
111 | | - data-active={selectId === id} |
112 | | - onClick={handleSelect} |
113 | | - > |
114 | | - <img src={dataUrl} alt="" /> |
115 | | - </Button> |
116 | | - ))} |
117 | | - </div> |
118 | | - <div className={styles.scanBox}> |
119 | | - <canvas ref={canvasRef} /> |
120 | | - {source ? <img ref={imgRef} src={source?.dataUrl} alt="" /> : null} |
121 | | - </div> |
122 | | - </div> |
123 | | - </Dialog> |
| 59 | + <LoadingDialog show={dialogType === 'scanning'} message={t('wallet-connect.scanning')} /> |
124 | 60 |
|
125 | 61 | <AlertDialog |
126 | 62 | show={dialogType === 'access-fail'} |
|
0 commit comments