@@ -1,160 +1,81 @@
1+import React, { useCallback, useEffect, useState } from 'react';
1import { Just, Nothing } from 'folktale/maybe';
2import { Just, Nothing } from 'folktale/maybe';
2−import React from 'react';
3+
3−import { Button } from '../../components/old/Base';
4+import View from '../../components/View';
6 MnemonicInput,
6 MnemonicInput,
7− InnerLabel,
7+ PassphraseInput,
8− InputCaption,
8+ HdPathInput,
9−} from '../../components/old/Base';
9+} from '../../components/Inputs';
10−import { Row, Col } from '../../components/old/Base';
10+import { ForwardButton } from '../../components/Buttons';
12−import { DEFAULT_HD_PATH, walletFromMnemonic } from '../../lib/wallet';
12+import { walletFromMnemonic } from '../../lib/wallet';
13−import { compose } from '../../lib/lib';
14−import { withWallet } from '../../store/wallet';
13+import { useWallet } from '../../store/wallet';
16−class Mnemonic extends React.Component {
15+export default function Mnemonic({ advanced, loginCompleted }) {
17− constructor(props) {
16+ const {
18− super(props);
17+ wallet,
20− this.state = {
19+ authMnemonic,
21− mnemonic: '',
20+ setAuthMnemonic,
22− passphrase: '',
21+ walletHdPath,
23− hdpath: DEFAULT_HD_PATH,
22+ setWalletHdPath,
24− };
23+ } = useWallet();
25−
24+ const [passphrase, setPassphrase] = useState('');
26− this.handleMnemonicInput = this.handleMnemonicInput.bind(this);
25+ const mnemonic = authMnemonic.getOrElse('');
27− this.handlePassphraseInput = this.handlePassphraseInput.bind(this);
26+
28− this.handleHdPathInput = this.handleHdPathInput.bind(this);
27+ // TODO: move this into transformers?
29− }
28+ // transform the result of the mnemonic to Maybe<string>
30−
29+ const _setAuthMnemonic = useCallback(
31− componentDidMount() {
30+ mnemonic => setAuthMnemonic(mnemonic === '' ? Nothing() : Just(mnemonic)),
32− const { mnemonic, hdpath, passphrase } = this.state;
31+ [setAuthMnemonic]
33− this.attemptWalletDerivation(mnemonic, hdpath, passphrase);
32+ );
35−
34+ // when the properties change, re-derive wallet
36− handlePassphraseInput(passphrase) {
35+ useEffect(() => {
37− this.setState((state, _) => {
36+ let mounted = true;
38− const mnemonic = state.mnemonic;
37+ (async () => {
39− const hdpath = state.hdpath;
40− this.attemptWalletDerivation(
42− hdpath === '' ? DEFAULT_HD_PATH : hdpath,
51− handleMnemonicInput(mnemonic) {
52− this.setState((state, _) => {
53− const hdpath = state.hdpath;
54− const passphrase = state.passphrase;
55− this.attemptWalletDerivation(
57− hdpath === '' ? DEFAULT_HD_PATH : hdpath,
66− handleHdPathInput(hdpath) {
67− this.setState((state, _) => {
68− const mnemonic = state.mnemonic;
69− const passphrase = state.passphrase;
70− this.attemptWalletDerivation(
72− hdpath === '' ? DEFAULT_HD_PATH : hdpath,
81− attemptWalletDerivation(mnemonic, hdpath, passphrase) {
82− const { setWallet, setAuthMnemonic, setWalletHdPath } = this.props;
83− const wallet = walletFromMnemonic(mnemonic, hdpath, passphrase);
85− setAuthMnemonic(Just(mnemonic));
86− setWalletHdPath(hdpath);
90− const { wallet } = this.props;
91− const { mnemonic, hdpath, passphrase } = this.state;
38+ const wallet = walletFromMnemonic(mnemonic, walletHdPath, passphrase);
92−
39+ mounted && setWallet(wallet);
41+ return () => (mounted = false);
42+ }, [mnemonic, passphrase, walletHdPath, setWallet]);
95− <Col>
46+ <MnemonicInput
96− <InputCaption>
47+ name="mnemonic"
97− {'Please enter your BIP39 mnemonic here.'}
48+ label="BIP39 Mnemonic"
103− prop-format="innerLabel"
105− name="mnemonic"
49+ initialValue={mnemonic}
106− onChange={this.handleMnemonicInput}
50+ onValue={_setAuthMnemonic}
109− autoFocus>
51+ autoFocus
110− <InnerLabel>{'Mnemonic'}</InnerLabel>
52+ />
113− {!this.props.advanced ? null : (
54+ {!advanced ? null : (
115− <InputCaption>
56+ <PassphraseInput
116− {`If your wallet requires a passphrase, you may enter it below.`}
122− prop-format="innerLabel"
123− name="passphrase"
57+ name="passphrase"
124− type="password"
58+ label="(Optional) Wallet Passphrase"
125− value={passphrase}
59+ initialValue={passphrase}
126− autocomplete="off"
60+ onValue={setPassphrase}
127− onChange={this.handlePassphraseInput}>
61+ />
128− <InnerLabel>{'Passphrase'}</InnerLabel>
62+
129− </Input>
63+ <HdPathInput
132− {`If you'd like to use a custom derivation path, you may enter it below.`}
136− className="pt-8 text-mono"
138− prop-format="innerLabel"
139− name="hdpath"
64+ name="hdpath"
140− value={hdpath}
65+ label="HD Path"
141− autocomplete="off"
66+ initialValue={walletHdPath}
142− onChange={this.handleHdPathInput}>
67+ onValue={setWalletHdPath}
143− <InnerLabel>{'HD Path'}</InnerLabel>
68+ />
148− <Button
72+ <ForwardButton
149− className={'mt-10'}
73+ className="mt3"
150− disabled={Nothing.hasInstance(wallet)}
74+ disabled={Nothing.hasInstance(wallet)}
151− onClick={this.props.loginCompleted}>
75+ onClick={loginCompleted}
153− </Button>
77+ Continue
154− </Col>
78+ </ForwardButton>
160−export default compose(withWallet)(Mnemonic);