diff --git a/native/components/version-supported.react.js b/native/components/version-supported.react.js --- a/native/components/version-supported.react.js +++ b/native/components/version-supported.react.js @@ -3,31 +3,19 @@ import * as React from 'react'; import { - useLogOut, - logOutActionTypes, useVersionSupportedByIdentity, versionSupportedByIdentityActionTypes, } from 'lib/actions/user-actions.js'; -import { isLoggedIn } from 'lib/selectors/user-selectors.js'; import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; -import { useSelector } from '../redux/redux-utils.js'; -import { appOutOfDateAlertDetails } from '../utils/alert-messages.js'; -import Alert from '../utils/alert.js'; +import { useShowVersionUnsupportedAlert } from '../utils/hooks.js'; function VersionSupportedChecker(): React.Node { const hasRun = React.useRef(false); - const loggedIn = useSelector(isLoggedIn); const dispatchActionPromise = useDispatchActionPromise(); - const callLogOut = useLogOut(); const callVersionSupportedByIdentity = useVersionSupportedByIdentity(); - - const onUsernameAlertAcknowledged = React.useCallback(() => { - if (loggedIn) { - void dispatchActionPromise(logOutActionTypes, callLogOut()); - } - }, [callLogOut, dispatchActionPromise, loggedIn]); + const showVersionUnsupportedAlert = useShowVersionUnsupportedAlert(true); const checkVersionSupport = React.useCallback(async () => { try { @@ -40,24 +28,14 @@ if (isVersionSupported) { return; } - Alert.alert( - appOutOfDateAlertDetails.title, - appOutOfDateAlertDetails.message, - [ - { - text: 'OK', - onPress: onUsernameAlertAcknowledged, - }, - ], - { cancelable: false }, - ); + showVersionUnsupportedAlert(); } catch (error) { console.log('Error checking version:', error); } }, [ callVersionSupportedByIdentity, dispatchActionPromise, - onUsernameAlertAcknowledged, + showVersionUnsupportedAlert, ]); React.useEffect(() => { diff --git a/native/utils/hooks.js b/native/utils/hooks.js --- a/native/utils/hooks.js +++ b/native/utils/hooks.js @@ -3,6 +3,14 @@ import AsyncStorage from '@react-native-async-storage/async-storage'; import * as React from 'react'; +import { useLogOut, logOutActionTypes } from 'lib/actions/user-actions.js'; +import { isLoggedIn } from 'lib/selectors/user-selectors.js'; +import { useDispatchActionPromise } from 'lib/utils/redux-promise-utils.js'; + +import { useSelector } from '../redux/redux-utils.js'; +import { appOutOfDateAlertDetails } from '../utils/alert-messages.js'; +import Alert from '../utils/alert.js'; + function useOnFirstLaunchEffect(uniqueKey: string, effect: () => mixed) { const [started, setStarted] = React.useState(false); @@ -26,4 +34,30 @@ }, [effect, started, uniqueKey]); } -export { useOnFirstLaunchEffect }; +function useShowVersionUnsupportedAlert(shouldCallLogOut: boolean): () => void { + const loggedIn = useSelector(isLoggedIn); + const dispatchActionPromise = useDispatchActionPromise(); + const callLogOut = useLogOut(); + + const onUsernameAlertAcknowledged = React.useCallback(() => { + if (loggedIn && shouldCallLogOut) { + void dispatchActionPromise(logOutActionTypes, callLogOut()); + } + }, [callLogOut, dispatchActionPromise, loggedIn, shouldCallLogOut]); + + return React.useCallback(() => { + Alert.alert( + appOutOfDateAlertDetails.title, + appOutOfDateAlertDetails.message, + [ + { + text: 'OK', + onPress: onUsernameAlertAcknowledged, + }, + ], + { cancelable: false }, + ); + }, [onUsernameAlertAcknowledged]); +} + +export { useOnFirstLaunchEffect, useShowVersionUnsupportedAlert };