mirror of
https://github.com/zvx-echo6/navi.git
synced 2026-05-20 22:54:42 +02:00
fix: search geocode now includes viewport bias in API requests
Symptom: HAR capture showed /api/geocode requests with NO lat/lon/zoom params despite map being centered on Twin Falls. Results returned out-of-state addresses (Illinois, Iowa, Arkansas). Root cause: SearchBar subscribed to mapCenter via React hook, but the value was stale at search time due to render timing. Fix: api.js searchGeocode now reads mapCenter directly from the store via useStore.getState() at call time. This is the correct pattern for non-component code. SearchBar no longer passes mapCenter as a param.
This commit is contained in:
parent
9db8cec0f6
commit
60effce679
2 changed files with 16 additions and 6 deletions
18
src/api.js
18
src/api.js
|
|
@ -1,3 +1,5 @@
|
|||
import { useStore } from './store'
|
||||
|
||||
const GEOCODE_URL = '/api/geocode'
|
||||
const VALHALLA_URL = '/valhalla/route'
|
||||
const VALHALLA_OPTIMIZED_URL = '/valhalla/optimized_route'
|
||||
|
|
@ -10,11 +12,19 @@ const VALHALLA_HEIGHT_URL = '/valhalla/height'
|
|||
* @param {AbortSignal} signal
|
||||
* @returns {Promise<{query, results, count}>}
|
||||
*/
|
||||
export async function searchGeocode(query, limit = 6, signal, viewport = null) {
|
||||
export async function searchGeocode(query, limit = 6, signal) {
|
||||
const params = new URLSearchParams({ q: query, limit: String(limit) })
|
||||
if (viewport?.lat != null) params.set('lat', String(viewport.lat))
|
||||
if (viewport?.lon != null) params.set('lon', String(viewport.lon))
|
||||
if (viewport?.zoom != null) params.set('zoom', String(Math.round(viewport.zoom)))
|
||||
// Read current mapCenter directly from store (non-reactive, correct for non-component code)
|
||||
const mapCenter = useStore.getState().mapCenter
|
||||
if (mapCenter?.lat != null && Number.isFinite(mapCenter.lat)) {
|
||||
params.set('lat', String(mapCenter.lat))
|
||||
}
|
||||
if (mapCenter?.lon != null && Number.isFinite(mapCenter.lon)) {
|
||||
params.set('lon', String(mapCenter.lon))
|
||||
}
|
||||
if (mapCenter?.zoom != null && Number.isFinite(mapCenter.zoom)) {
|
||||
params.set('zoom', String(Math.round(mapCenter.zoom)))
|
||||
}
|
||||
const resp = await fetch(`${GEOCODE_URL}?${params}`, { signal, timeout: 5000 })
|
||||
if (!resp.ok) throw new Error(`Geocode error: ${resp.status}`)
|
||||
return resp.json()
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ const SearchBar = forwardRef(function SearchBar(_, ref) {
|
|||
const setClickMarker = useStore((s) => s.setClickMarker)
|
||||
const setEditingContact = useStore((s) => s.setEditingContact)
|
||||
const clearPendingDestination = useStore((s) => s.clearPendingDestination)
|
||||
const mapCenter = useStore((s) => s.mapCenter)
|
||||
// mapCenter now read directly in api.js
|
||||
|
||||
useEffect(() => {
|
||||
inputRef.current?.focus()
|
||||
|
|
@ -100,7 +100,7 @@ const SearchBar = forwardRef(function SearchBar(_, ref) {
|
|||
setSearchLoading(true)
|
||||
|
||||
try {
|
||||
const data = await searchGeocode(q.trim(), 6, ctrl.signal, mapCenter)
|
||||
const data = await searchGeocode(q.trim(), 6, ctrl.signal)
|
||||
const combined = [...contactResults, ...(data.results || [])]
|
||||
setResults(combined)
|
||||
setAutocompleteOpen(combined.length > 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue