diff --git a/lib/hooks/primary-device-hooks.js b/lib/hooks/primary-device-hooks.js new file mode 100644 index 000000000..2e9606f1a --- /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 index 2760dc990..66dc2f756 100644 --- 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 @@ -1,96 +1,89 @@ // @flow import { useNavigation } from '@react-navigation/native'; import invariant from 'invariant'; 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, } from 'lib/types/alert-types.js'; import { shouldSkipCreateSIWEBackupMessageAlert } from 'lib/utils/push-alerts.js'; import { useDispatch } from 'lib/utils/redux-utils.js'; import { commCoreModule } from '../../../native-modules.js'; import { CreateMissingSIWEBackupMessageRouteName } from '../../../navigation/route-names.js'; import { useSelector } from '../../../redux/redux-utils.js'; import { RegistrationContext } from '../registration-context.js'; function MissingRegistrationDataHandler(): React.Node { 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], ); const dispatch = useDispatch(); React.useEffect(() => { if ( !loggedIn || accountHasPassword(currentUserInfo) || cachedSelections.siweBackupSecrets || shouldSkipCreateSIWEBackupMessageAlert(createSIWEBAckupMessageAlertInfo) ) { return; } 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; } const nativeSIWEBackupSecrets = await commCoreModule.getSIWEBackupSecrets(); if (nativeSIWEBackupSecrets) { return; } navigation.navigate<'CreateMissingSIWEBackupMessage'>({ name: CreateMissingSIWEBackupMessageRouteName, }); const payload: RecordAlertActionPayload = { alertType: alertTypes.SIWE_BACKUP_MESSAGE, time: Date.now(), }; dispatch({ type: recordAlertActionType, payload, }); })(); }, [ - currentUserInfo, - loggedIn, cachedSelections.siweBackupSecrets, - navigation, + checkIfPrimaryDevice, createSIWEBAckupMessageAlertInfo, + currentUserInfo, dispatch, - userDevicesInfos, - getAuthMetadata, + loggedIn, + navigation, ]); return null; } export { MissingRegistrationDataHandler };