freshyo/apps/user-ui/components/UpdateChecker.tsx
2026-11-05 23:33:30 +05:30

120 lines
4.3 KiB
TypeScript

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<ReactParentComponent> = ({ 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}
<BottomDialog
open={prompt !== null}
onClose={handleClose}
enableDismiss={isClosable}
>
<View
testID="update-dialog"
style={[tw`px-6`, { height: Math.round(height * DIALOG_HEIGHT_RATIO) }]}
>
<View style={tw`flex-1 justify-between pb-6`}>
<View>
<MyText style={tw`text-xl font-bold text-gray-900`}>
Update available
</MyText>
<MyText style={tw`text-sm text-gray-600 mt-2`}>
{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.'}
</MyText>
</View>
<View>
{isClosable ? (
<MyButton
testID="update-later"
textContent="Later"
fillColor="gray3"
textColor="black2"
fullWidth
onPress={handleClose}
/>
) : null}
<MyButton
testID="update-restart"
textContent={isUpdating ? 'Updating…' : 'Update & Restart'}
fullWidth
disabled={isUpdating}
onPress={applyUpdate}
style={isClosable ? tw`mt-2` : undefined}
/>
</View>
</View>
</View>
</BottomDialog>
</>
);
};
export default UpdateChecker;