import type { FormEvent } from 'react'; import type { FC } from '../../lib/teact/teact'; import { memo, useCallback, useEffect, useRef, useState, } from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { GlobalState } from '../../global/types'; import { IS_TOUCH_ENV } from '../../util/browser/windowEnvironment'; import { pick } from '../../util/iteratees'; import useHistoryBack from '../../hooks/useHistoryBack'; import useLang from '../../hooks/useLang'; import Icon from '../common/icons/Icon'; import TrackingMonkey from '../common/TrackingMonkey'; import InputText from '../ui/InputText'; import Loading from '../ui/Loading'; type StateProps = Pick; const CODE_LENGTH = 5; const AuthCode: FC = ({ authPhoneNumber, authIsCodeViaApp, authIsLoading, authErrorKey, }) => { const { setAuthCode, returnToAuthPhoneNumber, clearAuthErrorKey, } = getActions(); const lang = useLang(); const inputRef = useRef(); const [code, setCode] = useState(''); const [isTracking, setIsTracking] = useState(false); const [trackingDirection, setTrackingDirection] = useState(1); useEffect(() => { if (!IS_TOUCH_ENV) { inputRef.current!.focus(); } }, []); useHistoryBack({ isActive: true, onBack: returnToAuthPhoneNumber, }); const onCodeChange = useCallback((e: FormEvent) => { if (authErrorKey) { clearAuthErrorKey(); } const { currentTarget: target } = e; target.value = target.value.replace(/[^\d]+/, '').substr(0, CODE_LENGTH); if (target.value === code) { return; } setCode(target.value); if (!isTracking) { setIsTracking(true); } else if (!target.value.length) { setIsTracking(false); } if (code && code.length > target.value.length) { setTrackingDirection(-1); } else { setTrackingDirection(1); } if (target.value.length === CODE_LENGTH) { setAuthCode({ code: target.value }); } }, [authErrorKey, code, isTracking, setAuthCode]); function handleReturnToAuthPhoneNumber() { returnToAuthPhoneNumber(); } return (

{authPhoneNumber}

{lang(authIsCodeViaApp ? 'SentAppCode' : 'LoginJustSentSms', undefined, { withNodes: true, withMarkdown: true, })}

{authIsLoading && }
); }; export default memo(withGlobal( (global): StateProps => pick(global, ['authPhoneNumber', 'authIsCodeViaApp', 'authIsLoading', 'authErrorKey']), )(AuthCode));