// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { ReactElement, ReactNode } from 'react'; import React, { useEffect, useRef } from 'react'; import classNames from 'classnames'; import { noop } from 'lodash'; import type { LocalizerType } from '../../types/Util'; import { missingCaseError } from '../../util/missingCaseError'; import type { Loadable } from '../../util/loadable'; import { LoadingState } from '../../util/loadable'; import { Intl } from '../Intl'; import { Spinner } from '../Spinner'; import { TitlebarDragArea } from '../TitlebarDragArea'; import { InstallScreenSignalLogo } from './InstallScreenSignalLogo'; import { getClassNamesFor } from '../../util/getClassNamesFor'; // We can't always use destructuring assignment because of the complexity of this props // type. /* eslint-disable react/destructuring-assignment */ type PropsType = { i18n: LocalizerType; provisioningUrl: Loadable; }; // This should match the size in the CSS. const QR_CODE_SIZE = 256; const QR_CODE_FAILED_LINK = 'https://support.signal.org/hc/articles/360007320451#desktop_multiple_device'; const getQrCodeClassName = getClassNamesFor( 'module-InstallScreenQrCodeNotScannedStep__qr-code' ); export const InstallScreenQrCodeNotScannedStep = ({ i18n, provisioningUrl, }: Readonly): ReactElement => (

{i18n('Install__scan-this-code')}

  1. {i18n('Install__instructions__1')}
  2. {i18n('Install__instructions__2__settings')} ), linkedDevices: {i18n('linkedDevices')}, }} />
  3. ), linkNewDevice: {i18n('linkNewDevice')}, }} />
); function QrCode( props: Loadable & { i18n: LocalizerType } ): ReactElement { const { i18n } = props; const qrCodeElRef = useRef(null); const valueToRender = props.loadingState === LoadingState.Loaded ? props.value : undefined; useEffect(() => { const qrCodeEl = qrCodeElRef.current; if (!qrCodeEl || !valueToRender) { return noop; } const qrCode = new window.QRCode(qrCodeEl, { text: valueToRender, width: QR_CODE_SIZE * window.devicePixelRatio, height: QR_CODE_SIZE * window.devicePixelRatio, }); return qrCode.clear.bind(qrCode); }, [valueToRender]); let contents: ReactNode; switch (props.loadingState) { case LoadingState.Loading: contents = ; break; case LoadingState.LoadFailed: contents = ( {i18n('Install__qr-failed__learn-more')} , ]} /> ); break; case LoadingState.Loaded: contents = (
); break; default: throw missingCaseError(props); } return (
{contents}
); }