diff --git a/native/account/registration/connect-ethereum.react.js b/native/account/registration/connect-ethereum.react.js --- a/native/account/registration/connect-ethereum.react.js +++ b/native/account/registration/connect-ethereum.react.js @@ -1,6 +1,5 @@ // @flow -import invariant from 'invariant'; import * as React from 'react'; import { Text, View } from 'react-native'; @@ -8,7 +7,6 @@ exactSearchUser, exactSearchUserActionTypes, } from 'lib/actions/user-actions.js'; -import { ENSCacheContext } from 'lib/components/ens-cache-provider.react.js'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; import type { SIWEResult } from 'lib/types/siwe-types.js'; import { @@ -16,16 +14,13 @@ useDispatchActionPromise, } from 'lib/utils/action-utils.js'; +import { useGetEthereumAccountFromSIWEResult } from './ethereum-utils.js'; import RegistrationButtonContainer from './registration-button-container.react.js'; import RegistrationButton from './registration-button.react.js'; import RegistrationContainer from './registration-container.react.js'; import RegistrationContentContainer from './registration-content-container.react.js'; -import { RegistrationContext } from './registration-context.js'; import type { RegistrationNavigationProp } from './registration-navigator.react.js'; -import { - type CoolOrNerdMode, - ensAvatarSelection, -} from './registration-types.js'; +import type { CoolOrNerdMode } from './registration-types.js'; import { type NavigationRoute, ExistingEthereumAccountRouteName, @@ -58,10 +53,6 @@ const { params } = props.route; const { userSelections } = props.route.params; - const registrationContext = React.useContext(RegistrationContext); - invariant(registrationContext, 'registrationContext should be set'); - const { setCachedSelections } = registrationContext; - const isNerdMode = userSelections.coolOrNerdMode === 'nerd'; const styles = useStyles(unboundStyles); @@ -149,23 +140,14 @@ ); const dispatchActionPromise = useDispatchActionPromise(); - const cacheContext = React.useContext(ENSCacheContext); - const { ensCache } = cacheContext; + const getEthereumAccountFromSIWEResult = + useGetEthereumAccountFromSIWEResult(); const onSuccessfulWalletSignature = React.useCallback( async (result: SIWEResult) => { const searchPromise = exactSearchUserCall(result.address); void dispatchActionPromise(exactSearchUserActionTypes, searchPromise); - // We want to figure out if the user has an ENS avatar now - // so that we can default to the ENS avatar in AvatarSelection - const avatarURIPromise = (async () => { - if (!ensCache) { - return null; - } - return await ensCache.getAvatarURIForAddress(result.address); - })(); - const { userInfo } = await searchPromise; if (userInfo) { @@ -176,27 +158,7 @@ return; } - const avatarURI = await avatarURIPromise; - - const ethereumAccount = { - accountType: 'ethereum', - ...result, - avatarURI, - }; - - setCachedSelections(oldUserSelections => { - const base = { - ...oldUserSelections, - ethereumAccount, - }; - if (base.avatarData || !avatarURI) { - return base; - } - return { - ...base, - avatarData: ensAvatarSelection, - }; - }); + const ethereumAccount = await getEthereumAccountFromSIWEResult(result); const newUserSelections = { ...userSelections, @@ -211,9 +173,8 @@ userSelections, exactSearchUserCall, dispatchActionPromise, - setCachedSelections, navigate, - ensCache, + getEthereumAccountFromSIWEResult, ], ); diff --git a/native/account/registration/ethereum-utils.js b/native/account/registration/ethereum-utils.js new file mode 100644 --- /dev/null +++ b/native/account/registration/ethereum-utils.js @@ -0,0 +1,60 @@ +// @flow + +import invariant from 'invariant'; +import * as React from 'react'; + +import { ENSCacheContext } from 'lib/components/ens-cache-provider.react.js'; +import type { SIWEResult } from 'lib/types/siwe-types.js'; + +import { RegistrationContext } from './registration-context.js'; +import { + type EthereumAccountSelection, + ensAvatarSelection, +} from './registration-types.js'; + +function useGetEthereumAccountFromSIWEResult(): ( + result: SIWEResult, +) => Promise { + const registrationContext = React.useContext(RegistrationContext); + invariant(registrationContext, 'registrationContext should be set'); + const { setCachedSelections } = registrationContext; + + const cacheContext = React.useContext(ENSCacheContext); + const { ensCache } = cacheContext; + + return React.useCallback( + async result => { + // We want to figure out if the user has an ENS avatar now + // so that we can default to the ENS avatar in AvatarSelection + let avatarURI: ?string = null; + if (ensCache) { + avatarURI = await ensCache.getAvatarURIForAddress(result.address); + } + + const ethereumAccount = { + accountType: 'ethereum', + ...result, + avatarURI, + }; + + setCachedSelections(oldUserSelections => { + const base = { + ...oldUserSelections, + ethereumAccount, + }; + if (base.avatarData || !avatarURI) { + return base; + } + return { + ...base, + avatarData: ensAvatarSelection, + }; + }); + + return ethereumAccount; + }, + [ensCache, setCachedSelections], + ); +} + +export { useGetEthereumAccountFromSIWEResult };