2021-07-08 02:10:12 +02:00
|
|
|
import React, { useState, useMemo } from 'react';
|
|
|
|
import { useHistory } from 'react-router-dom';
|
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Box from '../../../components/ui/box';
|
|
|
|
import Button from '../../../components/ui/button';
|
|
|
|
import Typography from '../../../components/ui/typography';
|
|
|
|
import {
|
|
|
|
TEXT_ALIGN,
|
|
|
|
TYPOGRAPHY,
|
|
|
|
JUSTIFY_CONTENT,
|
|
|
|
FONT_WEIGHT,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
|
|
|
import { INITIALIZE_END_OF_FLOW_ROUTE } from '../../../helpers/constants/routes';
|
2021-10-13 19:41:24 +02:00
|
|
|
import {
|
|
|
|
ThreeStepProgressBar,
|
|
|
|
threeStepStages,
|
|
|
|
} from '../../../components/app/step-progress-bar';
|
2021-07-08 02:10:12 +02:00
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
|
|
|
import RecoveryPhraseChips from './recovery-phrase-chips';
|
|
|
|
|
2021-10-13 19:41:24 +02:00
|
|
|
export default function ConfirmRecoveryPhrase({ secretRecoveryPhrase = '' }) {
|
2021-07-08 02:10:12 +02:00
|
|
|
const history = useHistory();
|
|
|
|
const t = useI18nContext();
|
2021-10-13 19:41:24 +02:00
|
|
|
const splitSecretRecoveryPhrase = secretRecoveryPhrase.split(' ');
|
2021-07-08 02:10:12 +02:00
|
|
|
const indicesToCheck = [2, 3, 7];
|
|
|
|
const [matching, setMatching] = useState(false);
|
|
|
|
|
|
|
|
// Removes seed phrase words from chips corresponding to the
|
|
|
|
// indicesToCheck so that user has to complete the phrase and confirm
|
|
|
|
// they have saved it.
|
|
|
|
const initializePhraseElements = () => {
|
2021-10-13 19:41:24 +02:00
|
|
|
const phraseElements = { ...splitSecretRecoveryPhrase };
|
2021-07-08 02:10:12 +02:00
|
|
|
indicesToCheck.forEach((i) => {
|
|
|
|
phraseElements[i] = '';
|
|
|
|
});
|
|
|
|
return phraseElements;
|
|
|
|
};
|
|
|
|
const [phraseElements, setPhraseElements] = useState(
|
|
|
|
initializePhraseElements(),
|
|
|
|
);
|
|
|
|
|
|
|
|
const validate = useMemo(
|
|
|
|
() =>
|
|
|
|
debounce((elements) => {
|
2021-10-13 19:41:24 +02:00
|
|
|
setMatching(Object.values(elements).join(' ') === secretRecoveryPhrase);
|
2021-07-08 02:10:12 +02:00
|
|
|
}, 500),
|
2021-10-13 19:41:24 +02:00
|
|
|
[setMatching, secretRecoveryPhrase],
|
2021-07-08 02:10:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleSetPhraseElements = (values) => {
|
|
|
|
setPhraseElements(values);
|
|
|
|
validate(values);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2021-10-13 19:41:24 +02:00
|
|
|
<ThreeStepProgressBar stage={threeStepStages.RECOVERY_PHRASE_CONFIRM} />
|
2021-07-08 02:10:12 +02:00
|
|
|
<Box
|
|
|
|
justifyContent={JUSTIFY_CONTENT.CENTER}
|
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
|
|
marginBottom={4}
|
|
|
|
>
|
|
|
|
<Typography variant={TYPOGRAPHY.H2} fontWeight={FONT_WEIGHT.BOLD}>
|
|
|
|
{t('seedPhraseConfirm')}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
justifyContent={JUSTIFY_CONTENT.CENTER}
|
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
|
|
marginBottom={4}
|
|
|
|
>
|
|
|
|
<Typography variant={TYPOGRAPHY.H4}>
|
|
|
|
{t('seedPhraseEnterMissingWords')}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
|
|
|
<RecoveryPhraseChips
|
2021-10-13 19:41:24 +02:00
|
|
|
secretRecoveryPhrase={splitSecretRecoveryPhrase}
|
2021-07-08 02:10:12 +02:00
|
|
|
confirmPhase
|
|
|
|
setInputValue={handleSetPhraseElements}
|
|
|
|
inputValue={phraseElements}
|
|
|
|
indicesToCheck={indicesToCheck}
|
|
|
|
/>
|
|
|
|
<div className="recovery-phrase__footer">
|
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
className="recovery-phrase__footer--button"
|
|
|
|
onClick={() => {
|
|
|
|
history.push(INITIALIZE_END_OF_FLOW_ROUTE);
|
|
|
|
}}
|
|
|
|
disabled={!matching}
|
|
|
|
>
|
|
|
|
{t('confirm')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfirmRecoveryPhrase.propTypes = {
|
2021-10-13 19:41:24 +02:00
|
|
|
secretRecoveryPhrase: PropTypes.string,
|
2021-07-08 02:10:12 +02:00
|
|
|
};
|