diff --git a/lib/hooks/peer-list-hooks.js b/lib/hooks/peer-list-hooks.js new file mode 100644 --- /dev/null +++ b/lib/hooks/peer-list-hooks.js @@ -0,0 +1,40 @@ +// @flow + +import invariant from 'invariant'; +import * as React from 'react'; + +import { setPeerDeviceListsActionType } from '../actions/aux-user-actions.js'; +import { getRelativeUserIDs } from '../selectors/user-selectors.js'; +import { IdentityClientContext } from '../shared/identity-client-context.js'; +import { convertSignedDeviceListsToRawDeviceLists } from '../utils/device-list-utils.js'; +import { useDispatch, useSelector } from '../utils/redux-utils.js'; + +function useCreateInitialPeerList(): () => Promise { + const dispatch = useDispatch(); + const relativeUserIDs = useSelector(getRelativeUserIDs); + + const identityContext = React.useContext(IdentityClientContext); + invariant(identityContext, 'Identity context should be set'); + + return React.useCallback(async () => { + if (!identityContext) { + return; + } + try { + const userDeviceLists = + await identityContext.identityClient.getDeviceListsForUsers( + relativeUserIDs, + ); + const usersRawDeviceLists = + convertSignedDeviceListsToRawDeviceLists(userDeviceLists); + dispatch({ + type: setPeerDeviceListsActionType, + payload: { deviceLists: usersRawDeviceLists }, + }); + } catch (e) { + console.log(`Error creating initial peer list: ${e.message}`); + } + }, [dispatch, identityContext, relativeUserIDs]); +} + +export { useCreateInitialPeerList }; diff --git a/native/profile/tunnelbroker-menu.react.js b/native/profile/tunnelbroker-menu.react.js --- a/native/profile/tunnelbroker-menu.react.js +++ b/native/profile/tunnelbroker-menu.react.js @@ -5,6 +5,7 @@ import { Text, View } from 'react-native'; import { ScrollView } from 'react-native-gesture-handler'; +import { useCreateInitialPeerList } from 'lib/hooks/peer-list-hooks.js'; import { IdentityClientContext } from 'lib/shared/identity-client-context.js'; import { useTunnelbroker } from 'lib/tunnelbroker/tunnelbroker-context.js'; import { @@ -89,6 +90,8 @@ } }, [identityContext, sendMessage]); + const onCreateInitialPeerList = useCreateInitialPeerList(); + const onSendEncryptedMessage = React.useCallback(async () => { try { if (!currentUserID) { @@ -183,6 +186,15 @@ Create session with own devices +