[Size] Auth: Make QR code lib async (#2021)

This commit is contained in:
Alexander Zinchuk 2022-09-12 11:05:56 +02:00
parent 6d0c4e4b66
commit 893e6b4321

View File

@ -1,10 +1,9 @@
import QrCodeStyling from 'qr-code-styling';
import type { FC } from '../../lib/teact/teact';
import React, { import React, {
useEffect, useRef, memo, useCallback, useEffect, useRef, memo, useCallback,
} from '../../lib/teact/teact'; } from '../../lib/teact/teact';
import { getActions, withGlobal } from '../../global'; import { getActions, withGlobal } from '../../global';
import type { FC } from '../../lib/teact/teact';
import type { GlobalState } from '../../global/types'; import type { GlobalState } from '../../global/types';
import type { LangCode } from '../../types'; import type { LangCode } from '../../types';
@ -19,12 +18,14 @@ import useLangString from '../../hooks/useLangString';
import useFlag from '../../hooks/useFlag'; import useFlag from '../../hooks/useFlag';
import useLang from '../../hooks/useLang'; import useLang from '../../hooks/useLang';
import useMediaTransition from '../../hooks/useMediaTransition'; import useMediaTransition from '../../hooks/useMediaTransition';
import useAsync from '../../hooks/useAsync';
import Loading from '../ui/Loading'; import Loading from '../ui/Loading';
import Button from '../ui/Button'; import Button from '../ui/Button';
import blankUrl from '../../assets/blank.png';
import AnimatedIcon from '../common/AnimatedIcon'; import AnimatedIcon from '../common/AnimatedIcon';
import blankUrl from '../../assets/blank.png';
type StateProps = type StateProps =
Pick<GlobalState, 'connectionState' | 'authState' | 'authQrCode'> Pick<GlobalState, 'connectionState' | 'authState' | 'authQrCode'>
& { language?: LangCode }; & { language?: LangCode };
@ -33,26 +34,14 @@ const DATA_PREFIX = 'tg://login?token=';
const QR_SIZE = 280; const QR_SIZE = 280;
const QR_PLANE_SIZE = 54; const QR_PLANE_SIZE = 54;
const QR_CODE = new QrCodeStyling({ let qrCodeStylingPromise: Promise<typeof import('qr-code-styling')>;
width: QR_SIZE,
height: QR_SIZE, function ensureQrCodeStyling() {
image: blankUrl, if (!qrCodeStylingPromise) {
margin: 10, qrCodeStylingPromise = import('qr-code-styling');
type: 'svg', }
dotsOptions: { return qrCodeStylingPromise;
type: 'rounded', }
},
cornersSquareOptions: {
type: 'extra-rounded',
},
imageOptions: {
imageSize: 0.4,
margin: 8,
},
qrOptions: {
errorCorrectionLevel: 'M',
},
});
const AuthCode: FC<StateProps> = ({ const AuthCode: FC<StateProps> = ({
connectionState, connectionState,
@ -73,10 +62,34 @@ const AuthCode: FC<StateProps> = ({
const [isLoading, markIsLoading, unmarkIsLoading] = useFlag(); const [isLoading, markIsLoading, unmarkIsLoading] = useFlag();
const [isQrMounted, markQrMounted, unmarkQrMounted] = useFlag(); const [isQrMounted, markQrMounted, unmarkQrMounted] = useFlag();
const { result: qrCode } = useAsync(async () => {
const QrCodeStyling = (await ensureQrCodeStyling()).default;
return new QrCodeStyling({
width: QR_SIZE,
height: QR_SIZE,
image: blankUrl,
margin: 10,
type: 'svg',
dotsOptions: {
type: 'rounded',
},
cornersSquareOptions: {
type: 'extra-rounded',
},
imageOptions: {
imageSize: 0.4,
margin: 8,
},
qrOptions: {
errorCorrectionLevel: 'M',
},
});
}, []);
const transitionClassNames = useMediaTransition(isQrMounted); const transitionClassNames = useMediaTransition(isQrMounted);
useEffect(() => { useEffect(() => {
if (!authQrCode) { if (!authQrCode || !qrCode) {
return () => { return () => {
unmarkQrMounted(); unmarkQrMounted();
}; };
@ -89,16 +102,16 @@ const AuthCode: FC<StateProps> = ({
const container = qrCodeRef.current!; const container = qrCodeRef.current!;
const data = `${DATA_PREFIX}${authQrCode.token}`; const data = `${DATA_PREFIX}${authQrCode.token}`;
QR_CODE.update({ qrCode.update({
data, data,
}); });
if (!isQrMounted) { if (!isQrMounted) {
QR_CODE.append(container); qrCode.append(container);
markQrMounted(); markQrMounted();
} }
return undefined; return undefined;
}, [connectionState, authQrCode, isQrMounted, markQrMounted, unmarkQrMounted]); }, [connectionState, authQrCode, isQrMounted, markQrMounted, unmarkQrMounted, qrCode]);
useEffect(() => { useEffect(() => {
if (connectionState === 'connectionStateReady') { if (connectionState === 'connectionStateReady') {