diff --git a/lib/hooks/peer-list-hooks.js b/lib/hooks/peer-list-hooks.js --- a/lib/hooks/peer-list-hooks.js +++ b/lib/hooks/peer-list-hooks.js @@ -7,6 +7,7 @@ import { getAllPeerDevices, getAllPeerUserIDAndDeviceIDs, + getPeersPrimaryDeviceIDs, } from '../selectors/user-selectors.js'; import { IdentityClientContext } from '../shared/identity-client-context.js'; import { usePeerToPeerCommunication } from '../tunnelbroker/peer-to-peer-context.js'; @@ -72,6 +73,7 @@ const broadcastDeviceListUpdates = useBroadcastDeviceListUpdates(); const allPeerDevices = useSelector(getAllPeerDevices); + const peerPrimaryDevices = useSelector(getPeersPrimaryDeviceIDs); return React.useCallback( async (userIDs: $ReadOnlyArray, broadcastUpdates: ?boolean) => { @@ -87,6 +89,25 @@ if (Object.keys(deviceLists).length === 0) { return {}; } + + const primaryDeviceChangedUserIDs = userIDs.filter(userID => { + const prevPrimaryDeviceID = peerPrimaryDevices[userID]; + const newPrimaryDeviceID = result.deviceLists[userID]?.devices[0]; + return ( + !!prevPrimaryDeviceID && + !!newPrimaryDeviceID && + newPrimaryDeviceID !== prevPrimaryDeviceID + ); + }); + + if (primaryDeviceChangedUserIDs.length > 0) { + console.log( + 'Primary device ID changed for users', + primaryDeviceChangedUserIDs, + ); + // TODO: implement + } + dispatch({ type: setPeerDeviceListsActionType, payload: { deviceLists, usersPlatformDetails }, @@ -112,6 +133,7 @@ }, [ allPeerDevices, + peerPrimaryDevices, broadcastDeviceListUpdates, dispatch, getDeviceListsForUsers, diff --git a/lib/selectors/user-selectors.js b/lib/selectors/user-selectors.js --- a/lib/selectors/user-selectors.js +++ b/lib/selectors/user-selectors.js @@ -328,6 +328,19 @@ ), ); +const getPeersPrimaryDeviceIDs: (state: BaseAppState<>) => { + +[userID: string]: ?string, +} = createSelector( + (state: BaseAppState<>) => state.auxUserStore.auxUserInfos, + (auxUserInfos: AuxUserInfos) => + Object.fromEntries( + entries(auxUserInfos).map(([userID, { deviceList }]) => [ + userID, + deviceList?.devices?.[0], + ]), + ), +); + const getOwnPrimaryDeviceID: (state: BaseAppState<>) => ?string = createSelector( (state: BaseAppState<>) => state.auxUserStore.auxUserInfos, @@ -353,6 +366,7 @@ getKeyserverDeviceID, getAllPeerDevices, getAllPeerUserIDAndDeviceIDs, + getPeersPrimaryDeviceIDs, getOwnPrimaryDeviceID, getOwnRawDeviceList, };