mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
fork

Configure Feed

Select the types of activity you want to include in your feed.

at uiwork 78 lines 2.7 kB view raw
1import * as Updates from 'expo-updates' 2import {useCallback, useEffect} from 'react' 3import {AppState} from 'react-native' 4import {logger} from '#/logger' 5import {useModalControls} from '#/state/modals' 6import {t} from '@lingui/macro' 7 8export function useOTAUpdate() { 9 const {openModal} = useModalControls() 10 11 // HELPER FUNCTIONS 12 const showUpdatePopup = useCallback(() => { 13 openModal({ 14 name: 'confirm', 15 title: t`Update Available`, 16 message: t`A new version of the app is available. Please update to continue using the app.`, 17 onPressConfirm: async () => { 18 Updates.reloadAsync().catch(err => { 19 throw err 20 }) 21 }, 22 }) 23 }, [openModal]) 24 const checkForUpdate = useCallback(async () => { 25 logger.debug('useOTAUpdate: Checking for update...') 26 try { 27 // Check if new OTA update is available 28 const update = await Updates.checkForUpdateAsync() 29 // If updates aren't available stop the function execution 30 if (!update.isAvailable) { 31 return 32 } 33 // Otherwise fetch the update in the background, so even if the user rejects switching to latest version it will be done automatically on next relaunch. 34 await Updates.fetchUpdateAsync() 35 // show a popup modal 36 showUpdatePopup() 37 } catch (e) { 38 logger.error('useOTAUpdate: Error while checking for update', { 39 error: e, 40 }) 41 } 42 }, [showUpdatePopup]) 43 const updateEventListener = useCallback( 44 (event: Updates.UpdateEvent) => { 45 logger.debug('useOTAUpdate: Listening for update...') 46 if (event.type === Updates.UpdateEventType.ERROR) { 47 logger.error('useOTAUpdate: Error while listening for update', { 48 message: event.message, 49 }) 50 } else if (event.type === Updates.UpdateEventType.NO_UPDATE_AVAILABLE) { 51 // Handle no update available 52 // do nothing 53 } else if (event.type === Updates.UpdateEventType.UPDATE_AVAILABLE) { 54 // Handle update available 55 // open modal, ask for user confirmation, and reload the app 56 showUpdatePopup() 57 } 58 }, 59 [showUpdatePopup], 60 ) 61 62 useEffect(() => { 63 // ADD EVENT LISTENERS 64 const updateEventSubscription = Updates.addListener(updateEventListener) 65 const appStateSubscription = AppState.addEventListener('change', state => { 66 if (state === 'active' && !__DEV__) { 67 checkForUpdate() 68 } 69 }) 70 71 // REMOVE EVENT LISTENERS (CLEANUP) 72 return () => { 73 updateEventSubscription.remove() 74 appStateSubscription.remove() 75 } 76 }, []) // eslint-disable-line react-hooks/exhaustive-deps 77 // disable exhaustive deps because we don't want to run this effect again 78}