Skip to content

Commit

Permalink
Fix Scanning (#1763)
Browse files Browse the repository at this point in the history
* add progress of scanning QR code

* fix delay of scanning
  • Loading branch information
renfengshi authored Sep 21, 2023
1 parent 5e95b6f commit e5832fe
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
22 changes: 22 additions & 0 deletions src/ui/component/Progress/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.progress {
text-align: center;
&-bar {
width: 100%;
background-color: #dadada;
border-radius: 3px;
overflow: hidden;
&::before {
content: " ";
display: block;
height: 6px;
width: var(--percent);
background-color: #7084ff;
}
}
&-num {
font-size: 12px;
color: #7084ff;
margin-top: 12px;
line-height: 1;
}
}
19 changes: 19 additions & 0 deletions src/ui/component/Progress/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import clsx from 'clsx';
import './index.less';

interface Props {
percent: number;
}

export default function Progress({ percent }: Props) {
return (
<div className={clsx('progress')}>
<div
className={clsx('progress-bar')}
style={{ '--percent': `${percent}%` }}
></div>
<div className={clsx('progress-num')}>{percent}%</div>
</div>
);
}
5 changes: 4 additions & 1 deletion src/ui/component/QRCodeReader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ const QRCodeReader = ({
}: QRCodeReaderProps) => {
const [canplay, setCanplay] = useState(false);
const codeReader = useMemo(() => {
return new BrowserQRCodeReader();
return new BrowserQRCodeReader(undefined, {
delayBetweenScanSuccess: 100,
delayBetweenScanAttempts: 50,
});
}, []);
const videoEl = useRef<HTMLVideoElement>(null);
const checkCameraPermission = async () => {
Expand Down
17 changes: 13 additions & 4 deletions src/ui/views/Approval/components/QRHardWareWaiting/Reader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef } from 'react';
import React, { useRef, useState } from 'react';
import { ETHSignature } from '@keystonehq/bc-ur-registry-eth';
import * as uuid from 'uuid';
import { useTranslation } from 'react-i18next';
Expand All @@ -7,16 +7,19 @@ import { URDecoder } from '@ngraveio/bc-ur';
import { openInternalPageInTab, useWallet } from 'ui/utils';
import { useHistory } from 'react-router-dom';
import { Form } from 'antd';
import Progress from '@/ui/component/Progress';

const Reader = ({ requestId, setErrorMessage, brandName, onScan }) => {
const { t } = useTranslation();
const decoder = useRef(new URDecoder());
const wallet = useWallet();
const history = useHistory();
const [form] = Form.useForm();
const [progress, setProgress] = useState(0);

const handleSuccess = async (data) => {
decoder.current.receivePart(data);
setProgress(Math.floor(decoder.current.estimatedPercentComplete() * 100));
if (decoder.current.isComplete()) {
const ur = decoder.current.resultUR();
if (ur.type === 'eth-signature') {
Expand Down Expand Up @@ -58,9 +61,15 @@ const Reader = ({ requestId, setErrorMessage, brandName, onScan }) => {
onError={handleError}
/>
</div>
<p className="text-13 leading-[18px] mb-0 mt-24 text-gray-subTitle font-medium text-center">
{t('page.signFooterBar.qrcode.afterSignDesc', { brand: brandName })}
</p>
{progress > 0 ? (
<div className="mt-[24px] m-auto w-[130px]">
<Progress percent={progress} />
</div>
) : (
<p className="text-13 leading-[18px] mb-0 mt-24 text-gray-subTitle font-medium text-center">
{t('page.signFooterBar.qrcode.afterSignDesc', { brand: brandName })}
</p>
)}
</div>
);
};
Expand Down
12 changes: 12 additions & 0 deletions src/ui/views/ImportHardware/QRCodeConnect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as Sentry from '@sentry/browser';
import { HARDWARE_KEYRING_TYPES, WALLET_BRAND_CONTENT } from 'consts';
import QRCodeCheckerDetail from 'ui/views/QRCodeCheckerDetail';
import clsx from 'clsx';
import Progress from '@/ui/component/Progress';

type Valueof<T> = T[keyof T];

Expand All @@ -36,13 +37,16 @@ export const QRCodeConnect = () => {
const brandInfo: Valueof<typeof WALLET_BRAND_CONTENT> =
WALLET_BRAND_CONTENT[brand] || WALLET_BRAND_CONTENT.Keystone;

const [progress, setProgress] = useState(0);

const showErrorChecker = useMemo(() => {
return errorMessage !== '';
}, [errorMessage]);

const handleScanQRCodeSuccess = async (data) => {
try {
decoder.current.receivePart(data);
setProgress(Math.floor(decoder.current.estimatedPercentComplete() * 100));
if (decoder.current.isComplete()) {
const result = decoder.current.resultUR();
if (result.type === 'crypto-hdkey') {
Expand Down Expand Up @@ -134,6 +138,8 @@ export const QRCodeConnect = () => {
const handleScan = () => {
setErrorMessage('');
setScan(true);
setProgress(0);
decoder.current = new URDecoder();
};
return (
<div className="bg-gray-bg2 h-full flex">
Expand Down Expand Up @@ -171,6 +177,12 @@ export const QRCodeConnect = () => {
)}
</div>

{progress > 0 && (
<div className="mt-[24px] m-auto w-[130px]">
<Progress percent={progress} />
</div>
)}

{showErrorChecker && (
<QRCodeCheckerDetail
visible={showErrorChecker}
Expand Down

0 comments on commit e5832fe

Please sign in to comment.