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 @@ -276,6 +276,15 @@ ), ); +const getOwnPrimaryDeviceID: (state: BaseAppState<>) => ?string = + createSelector( + (state: BaseAppState<>) => state.auxUserStore.auxUserInfos, + (state: BaseAppState<>) => + state.currentUserInfo && state.currentUserInfo.id, + (auxUserInfos: AuxUserInfos, currentUserID: ?string): ?string => + currentUserID && auxUserInfos[currentUserID]?.deviceList?.devices[0], + ); + export { userIDsToRelativeUserInfos, getRelativeMemberInfos, @@ -290,4 +299,5 @@ getForeignPeerDevices, getAllPeerDevices, getAllPeerUserIDAndDeviceIDs, + getOwnPrimaryDeviceID, }; diff --git a/native/profile/profile-screen.react.js b/native/profile/profile-screen.react.js --- a/native/profile/profile-screen.react.js +++ b/native/profile/profile-screen.react.js @@ -11,9 +11,11 @@ } from 'lib/actions/user-actions.js'; import { useStringForUser } from 'lib/hooks/ens-cache.js'; import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js'; +import { getOwnPrimaryDeviceID } from 'lib/selectors/user-selectors.js'; import { accountHasPassword } from 'lib/shared/account-utils.js'; import type { LogOutResult } from 'lib/types/account-types.js'; import { type CurrentUserInfo } from 'lib/types/user-types.js'; +import { getContentSigningKey } from 'lib/utils/crypto-utils.js'; import { useDispatchActionPromise, type DispatchActionPromise, @@ -155,6 +157,7 @@ type Props = { ...BaseProps, +currentUserInfo: ?CurrentUserInfo, + +primaryDeviceID: ?string, +logOutLoading: boolean, +colors: Colors, +styles: $ReadOnly, @@ -259,13 +262,8 @@ <> - ); @@ -380,44 +378,42 @@ ); }; - onPressPrimaryDeviceLogout = () => { - if (this.loggedOutOrLoggingOut) { - return; - } - // TODO: Add check for primary device - Alert.alert( - 'Log out primary device', - 'Are you sure you want to log out all devices?', - [ - { text: 'No', style: 'cancel' }, - { - text: 'Yes', - onPress: this.logOutPrimaryDevice, - style: 'destructive', - }, - ], - { cancelable: true }, - ); - }; + onPressNewLogout = () => { + void (async () => { + if (this.loggedOutOrLoggingOut) { + return; + } + const { primaryDeviceID } = this.props; + const currentDeviceID = await getContentSigningKey(); + const isPrimaryDevice = currentDeviceID === primaryDeviceID; + + let alertTitle, alertMessage, onPressAction; + if (isPrimaryDevice) { + alertTitle = 'Log out all devices?'; + alertMessage = + 'This device is your primary device, ' + + 'so logging out will cause all of your other devices to log out too.'; + onPressAction = this.logOutPrimaryDevice; + } else { + alertTitle = 'Log out?'; + alertMessage = 'Are you sure you want to log out of this device?'; + onPressAction = this.logOutSecondaryDevice; + } - onPressSecondaryDeviceLogout = () => { - if (this.loggedOutOrLoggingOut) { - return; - } - // TODO: Add check for secondary device - Alert.alert( - 'Log out secondary device', - 'Are you sure you want to log out this device?', - [ - { text: 'No', style: 'cancel' }, - { - text: 'Yes', - onPress: this.logOutSecondaryDevice, - style: 'destructive', - }, - ], - { cancelable: true }, - ); + Alert.alert( + alertTitle, + alertMessage, + [ + { text: 'No', style: 'cancel' }, + { + text: 'Yes', + onPress: onPressAction, + style: 'destructive', + }, + ], + { cancelable: true }, + ); + })(); }; logOutWithoutDeletingNativeCredentialsWrapper = () => { @@ -531,6 +527,7 @@ const ConnectedProfileScreen: React.ComponentType = React.memo(function ConnectedProfileScreen(props: BaseProps) { const currentUserInfo = useSelector(state => state.currentUserInfo); + const primaryDeviceID = useSelector(getOwnPrimaryDeviceID); const logOutLoading = useSelector(logOutLoadingStatusSelector) === 'loading'; const colors = useColors(); @@ -549,6 +546,7 @@