Skip to content

Commit

Permalink
feature(setup-wallet): display text when there are not suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
banklesss committed May 3, 2024
1 parent 5a158c4 commit 282934e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const MnemonicInput = ({
onFocus,
mnenonicRefs,
mnemonic,
addInputErrorIndex,
removeInputErrorIndex,
}: {
length: number
isValidPhrase: boolean
Expand All @@ -36,6 +38,8 @@ export const MnemonicInput = ({
onFocus: (index: number) => void
mnenonicRefs: React.RefObject<MnemonicWordInputRef>[]
mnemonic: string
addInputErrorIndex: (index: number) => void
removeInputErrorIndex: (index: number) => void
}) => {
const strings = useStrings()
const {styles} = useStyles()
Expand All @@ -53,6 +57,8 @@ export const MnemonicInput = ({
suggestedWords={suggestedWords}
setSuggestedWords={setSuggestedWords}
onFocus={onFocus}
addInputErrorIndex={addInputErrorIndex}
removeInputErrorIndex={removeInputErrorIndex}
/>

<Space height="l" />
Expand Down Expand Up @@ -106,6 +112,8 @@ type MnemonicWordsInputProps = {
suggestedWords: Array<string>
setSuggestedWords: (suggestedWord: Array<string>) => void
onFocus: (index: number) => void
addInputErrorIndex: (index: number) => void
removeInputErrorIndex: (index: number) => void
}
const MnemonicWordsInput = ({
onSelect,
Expand All @@ -115,6 +123,8 @@ const MnemonicWordsInput = ({
suggestedWords,
setSuggestedWords,
onFocus,
addInputErrorIndex,
removeInputErrorIndex,
}: MnemonicWordsInputProps) => {
const {styles} = useStyles()
const scrollView = useScrollView()
Expand Down Expand Up @@ -156,6 +166,10 @@ const MnemonicWordsInput = ({
mnenonicRefs[index - 1]?.current?.focus()
}
}}
onError={(error: string) => {
if (!isEmptyString(error)) addInputErrorIndex(index)
else removeInputErrorIndex(index)
}}
/>
</View>
))}
Expand All @@ -174,6 +188,7 @@ type MnemonicWordInputProps = {
index: number
suggestedWords: Array<string>
setSuggestedWords: (suggestedWord: Array<string>) => void
onError: (error: string) => void
}

const MnemonicWordInput = React.forwardRef<MnemonicWordInputRef, MnemonicWordInputProps>(
Expand All @@ -187,6 +202,7 @@ const MnemonicWordInput = React.forwardRef<MnemonicWordInputRef, MnemonicWordInp
index,
suggestedWords,
setSuggestedWords,
onError,
},
ref,
) => {
Expand All @@ -199,10 +215,9 @@ const MnemonicWordInput = React.forwardRef<MnemonicWordInputRef, MnemonicWordInp
ref,
() => ({
selectWord: setWord,
word: word,
focus: () => inputRef.current?.focus(),
}),
[word],
[setWord],
)

const onSubmitEditing = React.useCallback(() => {
Expand All @@ -227,10 +242,15 @@ const MnemonicWordInput = React.forwardRef<MnemonicWordInputRef, MnemonicWordInp

if (suggestedWords.length <= 0) {
setError('error')
} else if (error !== '') setError('')
} else setSuggestedWords([])
onError('error')
}
} else {
setSuggestedWords([])
setError('')
onError('')
}
},
[error, onSubmitEditing, setSuggestedWords],
[onError, onSubmitEditing, setSuggestedWords],
)

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {useStrings} from '../../common/useStrings'

export type MnemonicWordInputRef = {
focus: () => void
word: string
selectWord: (matchingWord: string) => void
}

Expand Down Expand Up @@ -57,6 +56,9 @@ export const RestoreWalletScreen = () => {
const [mnemonicSelectedWords, setMnemonicSelectedWords] = React.useState<Array<string>>(
Array.from({length: mnemonicType}).map(() => ''),
)
const [inputErrorsIndexes, setInputErrorsIndexes] = React.useState<Array<number>>([])
const hasFocusedInputError = inputErrorsIndexes[focusedIndex] !== undefined

const mnenonicRefs = React.useRef(mnemonicSelectedWords.map(() => React.createRef<MnemonicWordInputRef>())).current

const onSelect = (index: number, word: string) => {
Expand Down Expand Up @@ -157,6 +159,16 @@ export const RestoreWalletScreen = () => {
walletMetas,
])

const addInputErrorIndex = (indexToAdd: number) => {
const newInputErrors = [...inputErrorsIndexes, indexToAdd]
setInputErrorsIndexes(newInputErrors)
}

const removeInputErrorIndex = (indexToRemove: number) => {
const newInputErrors = inputErrorsIndexes.filter((index) => index !== indexToRemove)
setInputErrorsIndexes(newInputErrors)
}

return (
<SafeAreaView edges={['left', 'right', 'bottom']} style={styles.root}>
<KeyboardAvoidingView style={{flex: 1}}>
Expand All @@ -183,19 +195,21 @@ export const RestoreWalletScreen = () => {
onFocus={onFocus}
mnemonic={mnemonic}
mnenonicRefs={mnenonicRefs}
addInputErrorIndex={addInputErrorIndex}
removeInputErrorIndex={removeInputErrorIndex}
/>
</ScrollView>

{mnemonic !== '' && isValidPhrase && <NextButton onPress={handleOnNext} />}

{suggestedWords.length > 0 ? (
{suggestedWords.length > 0 && !hasFocusedInputError && (
<WordSuggestionList data={suggestedWords} index={focusedIndex} onSelect={onSelect} />
) : (
!isEmptyString(mnenonicRefs[focusedIndex].current?.word) && (
<View style={styles.suggestionArea}>
<Text style={styles.suggestionMessage}>{strings.wordNotFound}</Text>
</View>
)
)}

{suggestedWords.length === 0 && hasFocusedInputError && (
<View style={styles.suggestionArea}>
<Text style={styles.suggestionMessage}>{strings.wordNotFound}</Text>
</View>
)}
</KeyboardAvoidingView>
</SafeAreaView>
Expand Down Expand Up @@ -382,12 +396,12 @@ const useStyles = () => {
borderColor: theme.color.gray[200],
borderTopWidth: 1,
alignItems: 'center',
paddingTop: 30,
paddingBottom: 30,
},
suggestionMessage: {
...theme.typography['body-1-l-regular'],
textAlign: 'center',
paddingTop: 24,
paddingBottom: 24,
},
})

Expand Down

0 comments on commit 282934e

Please sign in to comment.