diff --git a/lib/hooks/primary-device-hooks.js b/lib/hooks/primary-device-hooks.js new file mode 100644 --- /dev/null +++ b/lib/hooks/primary-device-hooks.js @@ -0,0 +1,27 @@ +// @flow + +import invariant from 'invariant'; +import * as React from 'react'; + +import { getOwnPeerDevices } from '../selectors/user-selectors.js'; +import { IdentityClientContext } from '../shared/identity-client-context.js'; +import { useSelector } from '../utils/redux-utils.js'; + +function useCheckIfPrimaryDevice(): () => Promise { + const identityContext = React.useContext(IdentityClientContext); + invariant(identityContext, 'identity context not set'); + const { getAuthMetadata } = identityContext; + + const userDevicesInfos = useSelector(getOwnPeerDevices); + + return React.useCallback(async () => { + if (userDevicesInfos.length === 0) { + return false; + } + const primaryDeviceID = userDevicesInfos[0].deviceID; + const { deviceID } = await getAuthMetadata(); + return primaryDeviceID === deviceID; + }, [getAuthMetadata, userDevicesInfos]); +} + +export { useCheckIfPrimaryDevice }; diff --git a/native/account/registration/missing-registration-data/missing-registration-data-handler.react.js b/native/account/registration/missing-registration-data/missing-registration-data-handler.react.js --- a/native/account/registration/missing-registration-data/missing-registration-data-handler.react.js +++ b/native/account/registration/missing-registration-data/missing-registration-data-handler.react.js @@ -5,9 +5,9 @@ import * as React from 'react'; import { recordAlertActionType } from 'lib/actions/alert-actions.js'; -import { getOwnPeerDevices, isLoggedIn } from 'lib/selectors/user-selectors.js'; +import { useCheckIfPrimaryDevice } from 'lib/hooks/primary-device-hooks.js'; +import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { accountHasPassword } from 'lib/shared/account-utils.js'; -import { IdentityClientContext } from 'lib/shared/identity-client-context.js'; import { alertTypes, type RecordAlertActionPayload, @@ -24,14 +24,12 @@ const loggedIn = useSelector(isLoggedIn); const navigation = useNavigation(); const currentUserInfo = useSelector(state => state.currentUserInfo); - const userDevicesInfos = useSelector(getOwnPeerDevices); const registrationContext = React.useContext(RegistrationContext); invariant(registrationContext, 'registrationContext should be set'); const { cachedSelections } = registrationContext; - const identityContext = React.useContext(IdentityClientContext); - invariant(identityContext, 'identity context not set'); - const { getAuthMetadata } = identityContext; + + const checkIfPrimaryDevice = useCheckIfPrimaryDevice(); const createSIWEBAckupMessageAlertInfo = useSelector( state => state.alertStore.alertInfos[alertTypes.SIWE_BACKUP_MESSAGE], @@ -49,12 +47,8 @@ } void (async () => { - if (userDevicesInfos.length === 0) { - return; - } - const primaryDeviceID = userDevicesInfos[0].deviceID; - const { deviceID } = await getAuthMetadata(); - if (primaryDeviceID !== deviceID) { + const isPrimaryDevice = await checkIfPrimaryDevice(); + if (!isPrimaryDevice) { return; } @@ -80,14 +74,13 @@ }); })(); }, [ - currentUserInfo, - loggedIn, cachedSelections.siweBackupSecrets, - navigation, + checkIfPrimaryDevice, createSIWEBAckupMessageAlertInfo, + currentUserInfo, dispatch, - userDevicesInfos, - getAuthMetadata, + loggedIn, + navigation, ]); return null;