164 lines
5.8 KiB
TypeScript
164 lines
5.8 KiB
TypeScript
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 }) => (
|
|
<View testID="location-loading" style={tw`flex-1 justify-center items-center bg-white`}>
|
|
<ActivityIndicator size="large" color={theme.colors.brand500} />
|
|
{label ? <MyText style={tw`text-gray-500 mt-4`}>{label}</MyText> : null}
|
|
</View>
|
|
)
|
|
|
|
/**
|
|
* 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<ReactParentComponent> = ({ 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<boolean | null>(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 <LoadingScreen />
|
|
}
|
|
|
|
// Still deriving the region from device location / the default address.
|
|
if (isResolvingRegion) {
|
|
return <LoadingScreen label="Finding your region..." />
|
|
}
|
|
|
|
// No region resolved -> pick an address or add one.
|
|
return (
|
|
<View style={tw`flex-1 bg-white`}>
|
|
<LocationFallbackFlow
|
|
notice={locationNotice}
|
|
// Startup gate: no region yet, so nothing is chosen — don't mark a card as
|
|
// selected, or it reads as "already done" instead of "tap to continue".
|
|
highlightActiveAddress={false}
|
|
onOpenSettings={
|
|
locationStatus === 'services-off' || locationStatus === 'denied'
|
|
? openLocationSettings
|
|
: undefined
|
|
}
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
export default LocationTestWrapper
|