-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
611 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Text, View } from '@avalabs/k2-alpine' | ||
import React from 'react' | ||
|
||
export default function ScreenHeader({ | ||
title, | ||
description | ||
}: { | ||
title: string | ||
description?: string | JSX.Element | ||
}): JSX.Element { | ||
return ( | ||
<View> | ||
<Text | ||
sx={{ marginRight: 10, marginTop: 8, marginBottom: 10 }} | ||
variant="heading2"> | ||
{title} | ||
</Text> | ||
{description !== undefined && ( | ||
<View sx={{ marginTop: 8 }}> | ||
{typeof description === 'string' ? ( | ||
<Text variant="subtitle1">{description}</Text> | ||
) : ( | ||
description | ||
)} | ||
</View> | ||
)} | ||
</View> | ||
) | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/core-mobile/app/new/components/onboarding/WordSelection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import React, { Dispatch } from 'react' | ||
import { Button, Text, View } from '@avalabs/k2-alpine' | ||
|
||
type Props = { | ||
title?: string | ||
wordIndex: number | ||
wordOptions: string[] | ||
testID?: string | ||
selectedWordIndex?: number | ||
setSelectedWordIndex: Dispatch<number> | ||
} | ||
|
||
export default function WordSelection({ | ||
title, | ||
wordOptions, | ||
selectedWordIndex, | ||
setSelectedWordIndex | ||
}: Props): JSX.Element { | ||
const onSelection = (word: string, index: number): void => { | ||
setSelectedWordIndex(index) | ||
} | ||
|
||
const [word0, word1, word2] = [ | ||
wordOptions[0] ?? '', | ||
wordOptions[1] ?? '', | ||
wordOptions[2] ?? '' | ||
] | ||
|
||
return ( | ||
<View sx={{ gap: 10 }}> | ||
<Text variant="subtitle1" testID="word_selection__word_number"> | ||
{title ?? ' '} | ||
</Text> | ||
<View | ||
sx={{ | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
gap: 8 | ||
}}> | ||
<Word | ||
testID="word_selection__word_one" | ||
selected={selectedWordIndex === 0} | ||
word={word0} | ||
onSelected={word => onSelection(word, 0)} | ||
/> | ||
<Word | ||
testID="word_selection__word_two" | ||
selected={selectedWordIndex === 1} | ||
word={word1} | ||
onSelected={word => onSelection(word, 1)} | ||
/> | ||
<Word | ||
testID="word_selection__word_three" | ||
selected={selectedWordIndex === 2} | ||
word={word2} | ||
onSelected={word => onSelection(word, 2)} | ||
/> | ||
</View> | ||
</View> | ||
) | ||
} | ||
|
||
function Word({ | ||
word, | ||
onSelected, | ||
selected, | ||
testID | ||
}: { | ||
word: string | ||
onSelected: (word: string) => void | ||
selected: boolean | ||
testID?: string | ||
}): JSX.Element { | ||
return ( | ||
<View testID={testID} style={{ flexGrow: 1 }}> | ||
<Button | ||
testID="word_selection__select_word_button" | ||
type={selected ? 'primary' : 'secondary'} | ||
size="large" | ||
onPress={() => onSelected(word)}> | ||
{word} | ||
</Button> | ||
</View> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
type WordSelection = { | ||
index: number | ||
wordOptions: string[] | ||
} | ||
|
||
export type UseCheckMnemonicData = { | ||
firstWordSelection: WordSelection | ||
secondWordSelection: WordSelection | ||
thirdWordSelection: WordSelection | ||
verify: ( | ||
first: string | undefined, | ||
second: string | undefined, | ||
third: string | undefined | ||
) => boolean | ||
} | ||
|
||
export function useCheckMnemonic(mnemonics: string[]): UseCheckMnemonicData { | ||
const [firstWordSelection, setFirstWordSelection] = useState<WordSelection>({ | ||
index: 0, | ||
wordOptions: [] | ||
}) | ||
const [secondWordSelection, setSecondWordSelection] = useState<WordSelection>( | ||
{ | ||
index: 0, | ||
wordOptions: [] | ||
} | ||
) | ||
const [thirdWordSelection, setThirdWordSelection] = useState<WordSelection>({ | ||
index: 0, | ||
wordOptions: [] | ||
}) | ||
|
||
useEffect(() => { | ||
const pool = [ | ||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | ||
21, 22, 23 | ||
] | ||
const indices = selectXRandNumbers(3, pool) | ||
|
||
const firstWordOptions = selectXRandWordsIncluding({ | ||
included: indices[0] as number, | ||
numOfNumbers: 3, | ||
pool, | ||
mnemonics | ||
}) | ||
|
||
const secondWordOptions = selectXRandWordsIncluding({ | ||
included: indices[1] as number, | ||
numOfNumbers: 3, | ||
pool, | ||
mnemonics | ||
}) | ||
|
||
const thirdWordOptions = selectXRandWordsIncluding({ | ||
included: indices[2] as number, | ||
numOfNumbers: 3, | ||
pool, | ||
mnemonics | ||
}) | ||
|
||
setFirstWordSelection({ | ||
index: indices[0] as number, | ||
wordOptions: firstWordOptions | ||
}) | ||
setSecondWordSelection({ | ||
index: indices[1] as number, | ||
wordOptions: secondWordOptions | ||
}) | ||
setThirdWordSelection({ | ||
index: indices[2] as number, | ||
wordOptions: thirdWordOptions | ||
}) | ||
}, [mnemonics]) | ||
|
||
const verify = ( | ||
first: string | undefined, | ||
second: string | undefined, | ||
third: string | undefined | ||
): boolean => { | ||
return ( | ||
mnemonics[firstWordSelection.index] === first && | ||
mnemonics[secondWordSelection.index] === second && | ||
mnemonics[thirdWordSelection.index] === third | ||
) | ||
} | ||
|
||
return <UseCheckMnemonicData>{ | ||
firstWordSelection, | ||
secondWordSelection, | ||
thirdWordSelection, | ||
verify | ||
} | ||
} | ||
|
||
function selectXRandNumbers(numOfNumbers: number, pool: number[]): number[] { | ||
for (let i = 0; i < numOfNumbers; i++) { | ||
const randomIndex = Math.floor(Math.random() * (pool.length - i)) + i //pick random from pool | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
;[pool[i], pool[randomIndex]] = [pool[randomIndex]!, pool[i]!] //put it on front of pool | ||
} | ||
return pool.splice(0, numOfNumbers) //first [numOfNumbers] nums are now non-repeating random from pool and won't be duplicated in UI causing test failures | ||
} | ||
|
||
function selectXRandWordsIncluding({ | ||
included, | ||
numOfNumbers, | ||
pool, | ||
mnemonics | ||
}: { | ||
included: number | ||
numOfNumbers: number | ||
pool: number[] | ||
mnemonics: string[] | ||
}): string[] { | ||
const randoms = selectXRandNumbers(numOfNumbers, pool) | ||
const indexToRandomInject = Math.floor(Math.random() * numOfNumbers) | ||
randoms[indexToRandomInject] = included | ||
return randoms.map(wordIndex => mnemonics[wordIndex] as string) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.