// Copyright 2019-2020 @polkadot/extension-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 import { PASSWORD_EXPIRY_MIN } from '@polkadot/extension-base/defaults'; import React, { useCallback, useContext, useEffect, useState } from 'react'; import styled from 'styled-components'; import { ActionBar, ActionContext, Button, ButtonArea, Checkbox, Link } from '../../../components'; import useTranslation from '../../../hooks/useTranslation'; import { approveSignPassword, cancelSignRequest, isSignLocked } from '../../../messaging'; import Unlock from '../Unlock'; interface Props { buttonText: string; className?: string; error: string | null; isExternal?: boolean; isFirst: boolean; setError: (value: string | null) => void; signId: string; } function SignArea ({ buttonText, className, error, isExternal, isFirst, setError, signId } : Props): JSX.Element { const [savePass, setSavePass] = useState(false); const [isLocked, setIsLocked] = useState(null); const [password, setPassword] = useState(''); const [isBusy, setIsBusy] = useState(false); const onAction = useContext(ActionContext); const { t } = useTranslation(); useEffect(() => { setIsLocked(null); let timeout: number; !isExternal && isSignLocked(signId) .then(({ isLocked, remainingTime }) => { setIsLocked(isLocked); timeout = setTimeout(() => { setIsLocked(true); }, remainingTime); // if the account was unlocked check the remember me // automatically to prolong the unlock period !isLocked && setSavePass(true); }) .catch((error: Error) => console.error(error)); return () => { !!timeout && clearTimeout(timeout); }; }, [isExternal, signId]); const _onSign = useCallback( (): Promise => { setIsBusy(true); return approveSignPassword(signId, savePass, password) .then((): void => { setIsBusy(false); onAction(); }) .catch((error: Error): void => { setIsBusy(false); setError(error.message); console.error(error); }); }, [onAction, password, savePass, setError, setIsBusy, signId] ); const _onCancel = useCallback( (): Promise => cancelSignRequest(signId) .then(() => onAction()) .catch((error: Error) => console.error(error)), [onAction, signId] ); const RememberPasswordCheckbox = () => ( ( 'Remember my password for the next {{expiration}} minutes', { replace: { expiration: PASSWORD_EXPIRY_MIN } } ) : t( 'Extend the period without password by {{expiration}} minutes', { replace: { expiration: PASSWORD_EXPIRY_MIN } } ) } onChange={setSavePass} /> ); return ( {isFirst && !isExternal && ( <> { isLocked && ( )} )} {t('Cancel')} ); } export default styled(SignArea)` flex-direction: column; padding: 6px 24px; .cancelButton { margin-top: 4px; margin-bottom: 4px; text-decoration: underline; a { margin: auto; } } `;