// Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import type { ChangeEvent } from 'react'; import React, { useState, useEffect, useCallback, useRef } from 'react'; import type { Plugin } from 'intl-tel-input'; import intlTelInput from 'intl-tel-input'; import { strictAssert } from '../util/assert'; import { parseNumber } from '../util/libphonenumberUtil'; import { missingCaseError } from '../util/missingCaseError'; import { VerificationTransport } from '../types/VerificationTransport'; function PhoneInput({ initialValue, onValidation, onNumberChange, }: { initialValue: string | undefined; onValidation: (isValid: boolean) => void; onNumberChange: (number?: string) => void; }): JSX.Element { const [isValid, setIsValid] = useState(false); const pluginRef = useRef(); const elemRef = useRef(null); const onRef = useCallback( (elem: HTMLInputElement | null) => { elemRef.current = elem; if (!elem) { return; } if (initialValue !== undefined) { // eslint-disable-next-line no-param-reassign elem.value = initialValue; } pluginRef.current?.destroy(); const plugin = intlTelInput(elem); pluginRef.current = plugin; }, [initialValue] ); const validateNumber = useCallback( (number: string) => { const { current: plugin } = pluginRef; if (!plugin) { return; } const regionCode = plugin.getSelectedCountryData().iso2; const parsedNumber = parseNumber(number, regionCode); setIsValid(parsedNumber.isValidNumber); onValidation(parsedNumber.isValidNumber); onNumberChange( parsedNumber.isValidNumber ? parsedNumber.e164 : undefined ); }, [setIsValid, onNumberChange, onValidation] ); const onChange = useCallback( (_: ChangeEvent) => { if (elemRef.current) { validateNumber(elemRef.current.value); } }, [validateNumber] ); const onKeyDown = useCallback( (event: React.KeyboardEvent) => { // Pacify TypeScript and handle events bubbling up if (event.target instanceof HTMLInputElement) { validateNumber(event.target.value); } }, [validateNumber] ); return (
); } enum Stage { PhoneNumber, VerificationCode, ProfileName, } type StageData = | { stage: Stage.PhoneNumber; initialNumber: string | undefined; } | { stage: Stage.VerificationCode; number: string; sessionId: string; } | { stage: Stage.ProfileName; }; function PhoneNumberStage({ initialNumber, getCaptchaToken, requestVerification, onNext, }: { initialNumber: string | undefined; getCaptchaToken: () => Promise; requestVerification: ( number: string, captcha: string, transport: VerificationTransport ) => Promise<{ sessionId: string }>; onNext: (result: { number: string; sessionId: string }) => void; }): JSX.Element { const [number, setNumber] = useState(initialNumber); const [isValidNumber, setIsValidNumber] = useState(false); const [error, setError] = useState(undefined); const onRequestCode = useCallback( async (transport: VerificationTransport) => { if (!isValidNumber) { return; } if (!number) { setIsValidNumber(false); setError(undefined); return; } try { const token = await getCaptchaToken(); const result = await requestVerification(number, token, transport); setError(undefined); onNext({ number, sessionId: result.sessionId }); } catch (err) { setError(err.message); } }, [ getCaptchaToken, isValidNumber, setIsValidNumber, setError, requestVerification, number, onNext, ] ); const onSMSClick = useCallback( (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); void onRequestCode(VerificationTransport.SMS); }, [onRequestCode] ); const onVoiceClick = useCallback( (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); void onRequestCode(VerificationTransport.Voice); }, [onRequestCode] ); return (
Create your Signal Account
{error}
); } export function VerificationCodeStage({ number, sessionId, registerSingleDevice, onNext, onBack, }: { number: string; sessionId: string; registerSingleDevice: ( number: string, code: string, sessionId: string ) => Promise; onNext: () => void; onBack: () => void; }): JSX.Element { const [code, setCode] = useState(''); const [isValidCode, setIsValidCode] = useState(false); const [error, setError] = useState(undefined); const onChangeCode = useCallback( (event: ChangeEvent) => { const { value } = event.target; setIsValidCode(value.length === 6); setCode(value); }, [setIsValidCode, setCode] ); const onBackClick = useCallback( (event: React.MouseEvent) => { event.preventDefault(); event.stopPropagation(); onBack(); }, [onBack] ); const onVerifyCode = useCallback( async (event: React.MouseEvent) => { event.preventDefault(); event.stopPropagation(); if (!isValidCode || !sessionId) { return; } strictAssert(number != null && code.length > 0, 'Missing number or code'); try { await registerSingleDevice(number, code, sessionId); onNext(); } catch (err) { setError(err.message); } }, [ registerSingleDevice, onNext, number, code, sessionId, setError, isValidCode, ] ); return ( <>
Create your Signal Account
{error}
); } export function ProfileNameStage({ uploadProfile, onNext, }: { uploadProfile: (opts: { firstName: string; lastName: string; }) => Promise; onNext: () => void; }): JSX.Element { const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [error, setError] = useState(undefined); const onChangeFirstName = useCallback( (event: ChangeEvent) => setFirstName(event.target.value), [] ); const onChangeLastName = useCallback( (event: ChangeEvent) => setLastName(event.target.value), [] ); const onNextClick = useCallback( async (event: React.MouseEvent) => { event.preventDefault(); event.stopPropagation(); try { await uploadProfile({ firstName, lastName }); onNext(); } catch (err) { setError(err.message); } }, [onNext, firstName, lastName, uploadProfile] ); return ( <>
Select Profile Name
{/* TODO(indutny): highlight error */}
{error}
); } export type PropsType = Readonly<{ onComplete: () => void; getCaptchaToken: () => Promise; requestVerification: ( number: string, captcha: string, transport: VerificationTransport ) => Promise<{ sessionId: string }>; registerSingleDevice: ( number: string, code: string, sessionId: string ) => Promise; uploadProfile: (opts: { firstName: string; lastName: string; }) => Promise; readyForUpdates: () => void; }>; export function StandaloneRegistration({ onComplete, getCaptchaToken, requestVerification, registerSingleDevice, uploadProfile, readyForUpdates, }: PropsType): JSX.Element { useEffect(() => { readyForUpdates(); }, [readyForUpdates]); const [stageData, setStageData] = useState({ stage: Stage.PhoneNumber, initialNumber: undefined, }); const onPhoneNumber = useCallback( ({ number, sessionId }: { number: string; sessionId: string }) => { setStageData({ stage: Stage.VerificationCode, number, sessionId, }); }, [] ); const onBackToPhoneNumber = useCallback(() => { setStageData(data => { if (data.stage !== Stage.VerificationCode) { return data; } return { stage: Stage.PhoneNumber, initialNumber: data.number, }; }); }, []); const onRegistered = useCallback(() => { setStageData({ stage: Stage.ProfileName, }); }, []); let body: JSX.Element; if (stageData.stage === Stage.PhoneNumber) { body = ( ); } else if (stageData.stage === Stage.VerificationCode) { body = ( ); } else if (stageData.stage === Stage.ProfileName) { body = ( ); } else { throw missingCaseError(stageData); } return (
{body}
); }