import React, { useCallback, useEffect, useRef, useState } from 'react'; import * as Updates from 'expo-updates'; import { useWindowDimensions, View } from 'react-native'; import { BottomDialog, MyButton, MyText, tw } from 'common-ui'; import type { ReactParentComponent } from '@packages/shared'; import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks'; import constants from '@/src/constants'; /** Share of the window height the prompt occupies. */ const DIALOG_HEIGHT_RATIO = 0.4; /** * Version gate for the user app. * * Instead of expo-updates' own check, we compare the number shipped in this * build (`constants.INTERNAL_APP_VERSION`) with the backend's * `internalAppVersion` constant. When the backend is ahead, the update is * fetched and the app reloads. Both the gate and its close behaviour come from * the backend's `updatePrompt` object (`isEnabled` / `isClosable`), so the * prompt can be switched off — or made dismissable — without shipping a build. * * Rendered as the app's BottomDialog (not a native Alert) so it looks like the * rest of the dialogs and can show an in-progress state while updating. */ const UpdateChecker: React.FC = ({ children }) => { const { data, isPlaceholderData } = useGetEssentialConsts(); const { height } = useWindowDimensions(); const [prompt, setPrompt] = useState<{ isClosable: boolean } | null>(null); const [isUpdating, setIsUpdating] = useState(false); const prompted = useRef(false); useEffect(() => { if (prompted.current) return; // The hook rehydrates the last persisted payload as placeholder data before // the network call resolves. That snapshot can predate an `isEnabled: false` // flip, so never decide from it. if (isPlaceholderData) return; const remoteVersion = data?.internalAppVersion; if (typeof remoteVersion !== 'number') return; const updatePrompt = data?.updatePrompt; if (updatePrompt?.isEnabled !== true) return; if (constants.INTERNAL_APP_VERSION >= remoteVersion) return; prompted.current = true; setPrompt({ isClosable: updatePrompt.isClosable === true }); }, [data, isPlaceholderData]); const isClosable = prompt?.isClosable === true; // Backdrop tap / Android back. A non-closable prompt is the hard gate, so it // ignores both (`BottomDialog` also drops its dismiss handlers then). const handleClose = useCallback(() => { setPrompt((current) => (current?.isClosable ? null : current)); }, []); const applyUpdate = useCallback(async () => { setIsUpdating(true); try { await Updates.fetchUpdateAsync(); await Updates.reloadAsync(); } catch { // If the update cannot be applied, leave the app as-is. } finally { setIsUpdating(false); } }, []); return ( <> {children} Update available {isClosable ? 'A new version of the app is available. Update now, or continue and restart later.' : 'A new version of the app is available. Please restart to continue.'} {isClosable ? ( ) : null} ); }; export default UpdateChecker;