// Copyright 2019-2020 @polkadot/extension-ui authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import styled from 'styled-components'; import { AccountContext, ActionContext, Address, ButtonArea, Checkbox, InputWithLabel, Label, NextStepButton, VerticalSpace } from '../../components'; import { validateAccount } from '../../messaging'; import { DerivationPath } from '../../partials'; import { nextDerivationPath } from '../../util/nextDerivationPath'; import AddressDropdown from './AddressDropdown'; interface Props { isLocked?: boolean; parentAddress: string; onDerivationConfirmed: (derivation: { account: { address: string; suri: string }; parentPassword: string }) => void; } export function SelectParent ({ isLocked, onDerivationConfirmed, parentAddress }: Props): React.ReactElement { const onAction = useContext(ActionContext); const { accounts, hierarchy } = useContext(AccountContext); const [account, setAccount] = useState(null); const [parentPassword, setParentPassword] = useState(''); const [isProperParentPassword, setIsProperParentPassword] = useState(false); const [shouldAccountBeDerived, setShouldAccountBeDerived] = useState(true); const passwordInputRef = useRef(null); const _goCreate = useCallback( (): void => onAction('/account/create'), [onAction] ); const _onParentPasswordEnter = useCallback( async (enteredPassword: string) => { setParentPassword(enteredPassword); setIsProperParentPassword(await validateAccount(parentAddress, enteredPassword)); }, [parentAddress] ); const _onParentChange = useCallback( (address: string) => onAction(`/account/derive/${address}`), [onAction] ); const _onSubmit = useCallback( () => parentPassword && account && onDerivationConfirmed({ account, parentPassword }), [account, parentPassword, onDerivationConfirmed] ); useEffect(() => { setParentPassword(''); setIsProperParentPassword(false); // eslint-disable-next-line no-unused-expressions passwordInputRef.current?.querySelector('input')?.focus(); }, [_onParentPasswordEnter]); return ( <> {!isLocked && ( )} {isLocked ? (
) : ( )}
{isProperParentPassword && ( )} {shouldAccountBeDerived ? ( Create a derived account ) : ( Create account from new seed ) } ); } const CheckboxWithSmallerMargins = styled(Checkbox)` margin-top: 0; margin-bottom: 10px; `; const DisableableArea = styled.div<{ isDisabled: boolean }>` opacity: ${({ isDisabled }): string => isDisabled ? '0.2' : '1'}; ${({ isDisabled }): string | false => isDisabled && 'pointer-events: none'}; `;