1 import { useEffect, useState } from 'react';
3 import { c } from 'ttag';
5 import InputFieldTwo from '@proton/components/components/v2/field/InputField';
6 import TextAreaTwo from '@proton/components/components/v2/input/TextArea';
7 import { validateMnemonic } from '@proton/shared/lib/mnemonic';
9 import type { InputFieldProps } from '../../components/v2/field/InputField';
11 export const useMnemonicInputValidation = (
14 invalidFormatError = c('Error').t`Wrong recovery phrase. Try again or use another recovery method.`,
15 }: { invalidFormatError?: string } = {}
17 const [mnemonicError, setMnemonicError] = useState('');
20 validateMnemonic(mnemonic)
21 .then((isValid) => setMnemonicError(!isValid ? invalidFormatError : ''))
22 .catch(() => setMnemonicError(''));
27 const splitWords = mnemonic.split(/\s+/);
28 return splitWords.length !== 12 ? invalidFormatError : '';
36 interface Props extends Omit<InputFieldProps<typeof TextAreaTwo>, 'as'> {}
38 const MnemonicInputField = ({
42 label = c('Label').t`Recovery phrase`,
43 assistiveText = c('Label').t`Phrase consists of 12 unique words in a specific order`,
54 assistiveText={assistiveText}
55 onValue={(newValue: string) => {
56 const splitWords = newValue.split(/\s+/);
57 const mnemonic = splitWords.slice(0, 12).join(' ');
58 return onValue?.(mnemonic);
65 export default MnemonicInputField;