-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseGeocode.js
More file actions
31 lines (28 loc) · 1.22 KB
/
Copy pathuseGeocode.js
File metadata and controls
31 lines (28 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { useQuery } from '@tanstack/react-query'
// Reject geocoding hits that resolve to a whole state, country, or other
// large administrative region. Centroids of these (e.g. "California" →
// Sequoia National Forest) silently produce wilderness searches with zero
// trial sites. Better to fall back to no-location search than to pin to a
// meaningless centroid.
const REJECTED_ADDRESSTYPES = new Set(['state', 'country', 'region', 'province'])
async function geocode(location) {
const url = `https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(location)}&format=json&limit=1&addressdetails=1`
const res = await fetch(url, {
headers: { 'User-Agent': 'IRIS-ClinicalTrialFinder/1.0' },
})
if (!res.ok) throw new Error('Geocoding request failed')
const data = await res.json()
if (!data.length) return null
const hit = data[0]
if (REJECTED_ADDRESSTYPES.has(hit.addresstype)) return null
return { lat: parseFloat(hit.lat), lng: parseFloat(hit.lon) }
}
export function useGeocode(location) {
return useQuery({
queryKey: ['geocode', location],
queryFn: () => geocode(location),
enabled: Boolean(location),
staleTime: 1000 * 60 * 60, // 1 hour — zip codes don't move
retry: false,
})
}