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 @@ -36,6 +36,7 @@ import { useStyles } from '../../themes/colors.js'; import { defaultURLPrefix } from '../../utils/url-utils.js'; import EthereumLogoDark from '../../vectors/ethereum-logo-dark.react.js'; +import { useSIWEPanelState } from '../siwe-hooks.js'; import SIWEPanel from '../siwe-panel.react.js'; const exactSearchUserLoadingStatusSelector = createLoadingStatusSelector( @@ -50,8 +51,6 @@ }, }; -type PanelState = 'closed' | 'opening' | 'open' | 'closing'; - type Props = { +navigation: RegistrationNavigationProp<'ConnectEthereum'>, +route: NavigationRoute<'ConnectEthereum'>, @@ -108,27 +107,6 @@ ); } - const [panelState, setPanelState] = React.useState('closed'); - const openPanel = React.useCallback(() => { - setPanelState('opening'); - }, []); - const onPanelClosed = React.useCallback(() => { - setPanelState('closed'); - }, []); - const onPanelClosing = React.useCallback(() => { - setPanelState('closing'); - }, []); - - const siwePanelSetLoading = React.useCallback( - (loading: boolean) => { - if (panelState === 'closing' || panelState === 'closed') { - return; - } - setPanelState(loading ? 'opening' : 'open'); - }, - [panelState], - ); - const { navigate } = props.navigation; const onSkip = React.useCallback(() => { navigate<'UsernameSelection'>({ @@ -202,6 +180,14 @@ ], ); + const { + panelState, + onPanelClosed, + onPanelClosing, + siwePanelSetLoading, + openPanel, + } = useSIWEPanelState(); + let siwePanel; if (panelState !== 'closed') { siwePanel = ( diff --git a/native/account/registration/siwe-backup-message-creation.react.js b/native/account/registration/siwe-backup-message-creation.react.js --- a/native/account/registration/siwe-backup-message-creation.react.js +++ b/native/account/registration/siwe-backup-message-creation.react.js @@ -24,10 +24,9 @@ } from '../../navigation/route-names.js'; import { useStyles } from '../../themes/colors.js'; import Alert from '../../utils/alert.js'; +import { useSIWEPanelState } from '../siwe-hooks.js'; import SIWEPanel from '../siwe-panel.react.js'; -type PanelState = 'closed' | 'opening' | 'open' | 'closing'; - type CreateSIWEBackupMessageBaseProps = { +onSuccessfulWalletSignature: (result: SIWEResult) => void, +onExistingWalletSignature?: () => void, @@ -43,27 +42,13 @@ props; const styles = useStyles(unboundStyles); - const [panelState, setPanelState] = React.useState('closed'); - - const openPanel = React.useCallback(() => { - setPanelState('opening'); - }, []); - const onPanelClosed = React.useCallback(() => { - setPanelState('closed'); - }, []); - const onPanelClosing = React.useCallback(() => { - setPanelState('closing'); - }, []); - - const siwePanelSetLoading = React.useCallback( - (loading: boolean) => { - if (panelState === 'closing' || panelState === 'closed') { - return; - } - setPanelState(loading ? 'opening' : 'open'); - }, - [panelState], - ); + const { + panelState, + onPanelClosed, + onPanelClosing, + openPanel, + siwePanelSetLoading, + } = useSIWEPanelState(); let siwePanel; if (panelState !== 'closed') { diff --git a/native/account/siwe-hooks.js b/native/account/siwe-hooks.js --- a/native/account/siwe-hooks.js +++ b/native/account/siwe-hooks.js @@ -124,4 +124,52 @@ ); } -export { useLegacySIWEServerCall, useIdentityWalletRegisterCall }; +type PanelState = 'closed' | 'opening' | 'open' | 'closing'; + +function useSIWEPanelState(): { + +panelState: PanelState, + +openPanel: () => void, + +onPanelClosed: () => void, + +onPanelClosing: () => void, + +siwePanelSetLoading: (loading: boolean) => void, +} { + const [panelState, setPanelState] = React.useState('closed'); + + const openPanel = React.useCallback(() => { + setPanelState('opening'); + }, []); + + const onPanelClosed = React.useCallback(() => { + setPanelState('closed'); + }, []); + const onPanelClosing = React.useCallback(() => { + setPanelState('closing'); + }, []); + + const siwePanelSetLoading = React.useCallback( + (loading: boolean) => { + if (panelState === 'closing' || panelState === 'closed') { + return; + } + setPanelState(loading ? 'opening' : 'open'); + }, + [panelState], + ); + + return React.useMemo( + () => ({ + panelState, + openPanel, + onPanelClosed, + onPanelClosing, + siwePanelSetLoading, + }), + [panelState, openPanel, onPanelClosed, onPanelClosing, siwePanelSetLoading], + ); +} + +export { + useLegacySIWEServerCall, + useIdentityWalletRegisterCall, + useSIWEPanelState, +};