import { useCallback, useState } from 'react'; import type { LatLng } from '@/lib/crewHelpers'; interface GeolocationState { coords: LatLng | null; loading: boolean; error: string | null; } /** * Thin wrapper around the browser Geolocation API for one-shot "where am I" * lookups (used by the Crews "Near me" toggle). Never auto-requests — the user * must call {@link request} so we don't prompt on page load. */ export function useGeolocation() { const [state, setState] = useState({ coords: null, loading: false, error: null, }); const request = useCallback(() => { if (!('geolocation' in navigator)) { setState((s) => ({ ...s, error: 'Geolocation is not supported on this device.' })); return; } setState((s) => ({ ...s, loading: true, error: null })); navigator.geolocation.getCurrentPosition( (pos) => { setState({ coords: { lat: pos.coords.latitude, lng: pos.coords.longitude }, loading: false, error: null, }); }, (err) => { setState({ coords: null, loading: false, error: err.code === err.PERMISSION_DENIED ? 'Location permission denied.' : 'Could not get your location.', }); }, { enableHighAccuracy: false, timeout: 10000, maximumAge: 5 * 60 * 1000 }, ); }, []); const clear = useCallback(() => { setState({ coords: null, loading: false, error: null }); }, []); return { ...state, request, clear }; }