diff --git a/native/components/version-supported.react.js b/native/components/version-supported.react.js index 322c410ca..1541cd0ea 100644 --- a/native/components/version-supported.react.js +++ b/native/components/version-supported.react.js @@ -1,74 +1,52 @@ // @flow 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 { const versionSupportedPromise = callVersionSupportedByIdentity(); void dispatchActionPromise( versionSupportedByIdentityActionTypes, versionSupportedPromise, ); const { supported: isVersionSupported } = await versionSupportedPromise; 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(() => { if (hasRun.current) { return; } hasRun.current = true; void checkVersionSupport(); }, [checkVersionSupport]); return null; } export default VersionSupportedChecker; diff --git a/native/utils/hooks.js b/native/utils/hooks.js index 2cbf034f0..6060d32f7 100644 --- a/native/utils/hooks.js +++ b/native/utils/hooks.js @@ -1,29 +1,63 @@ // @flow 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); React.useEffect(() => { void (async () => { if (started) { return; } setStarted(true); const hasBeenExecuted = await AsyncStorage.getItem(uniqueKey); if (hasBeenExecuted) { return; } try { await effect(); } finally { await AsyncStorage.setItem(uniqueKey, 'true'); } })(); }, [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 };