freshyo/apps/user-ui/components/UpdateChecker.tsx
2026-10-01 13:22:34 +05:30

54 lines
1.7 KiB
TypeScript

import React, { useEffect, useRef } from 'react';
import * as Updates from 'expo-updates';
import { Alert } from 'react-native';
import type { ReactParentComponent } from '@packages/shared';
import { useGetEssentialConsts } from '@/src/hooks/prominent-api-hooks';
import constants from '@/src/constants';
/**
* 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 (blocking dialog, no "later").
*/
const UpdateChecker: React.FC<ReactParentComponent> = ({ children }) => {
const { data } = useGetEssentialConsts();
const prompted = useRef(false);
useEffect(() => {
// Never nag in development; the dev build talks to a dev backend.
if (__DEV__) return;
if (prompted.current) return;
const remoteVersion = data?.internalAppVersion;
if (typeof remoteVersion !== 'number') return;
if (constants.INTERNAL_APP_VERSION >= remoteVersion) return;
prompted.current = true;
Alert.alert(
'Update available',
'A new version of the app is available. Please restart to continue.',
[
{
text: 'Update & Restart',
onPress: async () => {
try {
await Updates.fetchUpdateAsync();
await Updates.reloadAsync();
} catch {
// If the update cannot be applied, leave the app as-is.
}
},
},
],
{ cancelable: false }
);
}, [data]);
return <>{children}</>;
};
export default UpdateChecker;