import { useState, useCallback } from 'react'; import * as Location from 'expo-location'; interface GeoState { loading: boolean; error: string | null; } /** * Native (iOS/Android) geolocation hook using expo-location. * Identical public API to useGeolocation.ts (web version) — * Metro resolves this file on native, the .ts file on web. */ export function useGeolocation() { const [state, setState] = useState({ loading: false, error: null }); const locate = useCallback((onSuccess: (lat: number, lng: number) => void) => { setState({ loading: true, error: null }); void (async () => { try { const { status } = await Location.requestForegroundPermissionsAsync(); if (status !== Location.PermissionStatus.GRANTED) { setState({ loading: false, error: 'Location permission denied. Please enable it in Settings.', }); return; } const loc = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High, }); setState({ loading: false, error: null }); onSuccess(loc.coords.latitude, loc.coords.longitude); } catch (err) { let message = 'Unable to get location. Try again or place the pin manually.'; if (err instanceof Error) { if (err.message.includes('timed out')) { message = 'Location request timed out. Try again.'; } } setState({ loading: false, error: message }); } })(); }, []); const clearError = useCallback(() => { setState((s) => ({ ...s, error: null })); }, []); return { ...state, locate, clearError }; }