Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3393392
D14058.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D14058.diff
View Options
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<boolean> {
+ 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;
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 1, 1:37 PM (20 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2605132
Default Alt Text
D14058.diff (3 KB)
Attached To
Mode
D14058: [lib] create a hook to check if device is primary
Attached
Detach File
Event Timeline
Log In to Comment