import React, { useEffect, useRef } from 'react' import { ActivityIndicator, Linking, Platform, View } from 'react-native' import { MyText, theme, tw } from 'common-ui' import type { ReactParentComponent } from '@packages/shared' import { trpc } from '../src/trpc-client' import { useRegion } from '@/src/hooks/useRegion' import { useLocationStore } from '@/src/store/locationStore' import LocationFallbackFlow from '@/src/components/location/LocationFallbackFlow' // How often the gate re-checks the device location while it is up. const LOCATION_POLL_MS = 3000 const LoadingScreen: React.FC<{ label?: string }> = ({ label }) => ( {label ? {label} : null} ) /** * Region gate + location fallback. * * The browsing region is derived from the device location (see `useRegion`), * anchored on the user's default address: when the location resolves to the * default's region (or to no serviceable region) we use the default; only a * different serviceable region makes us ask. While that decision is in flight we * show a spinner; once a region exists the app renders, otherwise the user is * asked to choose an address. */ const LocationTestWrapper: React.FC = ({ children }) => { const { regionId, clearRegion, hydrated, isResolvingRegion, locationStatus, refreshLocation, } = useRegion() const probeLocation = useLocationStore((state) => state.probe) // The region decision happens once per app launch, so a user who leaves for the // OS settings and turns the location on would come back to a page that never // notices. While this gate is up, poll for the location becoming usable and // re-run the decision when it does (which unmounts the gate if it resolves). // // `refreshLocation` is a fresh function on every render, so it is held in a ref // to keep the polling effect from restarting on each render. const refreshLocationRef = useRef(refreshLocation) useEffect(() => { refreshLocationRef.current = refreshLocation }) // Only meaningful once the first decision has settled. const gateUp = regionId == null && hydrated && !isResolvingRegion // Last probe result. The first one only seeds the baseline: we act on a change // (location just turned on / permission just granted), never on the poll itself. const wasLocationUsable = useRef(null) useEffect(() => { if (!gateUp) return let cancelled = false const check = async () => { const usable = await probeLocation() if (cancelled) return const previous = wasLocationUsable.current wasLocationUsable.current = usable if (usable && previous === false) { refreshLocationRef.current() } } void check() const timer = setInterval(() => void check(), LOCATION_POLL_MS) return () => { cancelled = true clearInterval(timer) } }, [gateUp, probeLocation]) const { data: regions } = trpc.common.listRegions.useQuery() // Tell the user *why* the location isn't usable before asking for an address. const locationNotice = (() => { switch (locationStatus) { case 'denied': return 'Location access is not granted. Allow location access for accurate delivery, or add a delivery address below.' case 'services-off': return 'Your device location is turned off. Turn it on, or add a delivery address below.' case 'unavailable': case 'error': return 'We could not get your location. Please try again, or add a delivery address below.' default: return undefined } })() // Deep-link to the OS location settings when the app can't fix it itself // (location services off, or permission denied — iOS won't re-prompt). const openLocationSettings = () => { void (async () => { try { if (Platform.OS === 'android') { await Linking.sendIntent('android.settings.LOCATION_SOURCE_SETTINGS') } else { await Linking.openSettings() } } catch { try { await Linking.openSettings() } catch { // Give up silently — the notice still explains the situation. } } })() } // Validate the saved region against the live list. A stale/unknown id must // lead to selection, never a silent checkout fallback. Membership-only: no // setRegion here, so this effect can never re-trigger itself. useEffect(() => { if (!hydrated || !regions || regionId == null) return if (!regions.some((region) => region.id === regionId)) { clearRegion() } }, [hydrated, regions, regionId, clearRegion]) // A known region short-circuits every check below. if (regionId != null) { return <>{children} } // Wait for the persisted region to load before deciding: location tracking // must not start while a stored choice may still appear. if (!hydrated) { return } // Still deriving the region from device location / the default address. if (isResolvingRegion) { return } // No region resolved -> pick an address or add one. return ( ) } export default LocationTestWrapper