mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
5aa36aaf3d
Six things the merged search and the offline cache got wrong. An index failure took the OpenStreetMap answer down with it. Only the OSM half of the Promise.all had a catch, so a rejection on the index side discarded ten results that had already arrived and sent the same query to Nominatim a second time, behind its 1.1 s process-wide throttle. The index refusing a query is ordinary traffic rather than an outage — a common single word without coordinates is turned down upstream as too expensive — so this fired in normal use. Each side now catches its own failure, and the keyless fallback reuses the answer OpenStreetMap already gave instead of asking again for a list that cannot have changed. Dedup never fired for a non-Latin name. namesOverlap splits on [^a-z0-9], so 長谷寺, Кремль and مسجد الشيخ زايد all tokenise to nothing and the function returned false — the same place came back once from each source, one row under the other, and picking either gave it a different id. That is the corpus this whole feature was measured on. Two names with no Latin words are now compared strictly, folded through NFKC so the width variants the sources disagree on still match; sharing a character is deliberately not enough, because deduping 東京タワー against 東京タワー駅前郵便局 would swallow a real result. expand=1 answered nothing for an index place while expand=0 answered in full. The expanded lookup degrades OSM ids to the plain one and had no branch for the new family, so the richer request was the poorer one, over REST and MCP alike. The bias circle could exceed Google's 50 km ceiling. A trip spread over more than that produced a radius Google rejects outright, turning a search into an error toast. Clamped on both sides — the client because it draws the box, the server because the radius arrives over the wire. The offline cache was rebuilt on every sync and never deleted. syncTrip wrote its meta row with put(), which replaced the whole record and dropped the fingerprint saying the cached area was current, so every login, reconnect and manual sync re-downloaded megabytes that had not changed. And clearTripData never touched areaPlaces, so a trip switched off for offline use kept answering offline searches and its rows stayed for the life of the install. clearCachedPlaces, which claimed to do that job, was called from nowhere; its only test asserted with .every() on an empty table and passed on nothing. Picking an offline suggestion ended in an error toast. The details lookup had no cache path, so it failed and the callers fell back to searching for "name, address" — which the cache matches on the folded name alone, so it found nothing for a place that was sitting right there. A miss stays a miss.
130 lines
5.2 KiB
TypeScript
130 lines
5.2 KiB
TypeScript
import { useMemo } from 'react'
|
|
import { useTripStore } from '../store/tripStore'
|
|
|
|
/**
|
|
* Where the user is planning right now, as a hint for the place search.
|
|
*
|
|
* Without it the search cannot tell which of a thousand identically named
|
|
* places is meant: "Hase-dera" returns the temple in Nara rather than the one
|
|
* in Kamakura, and a common single word is refused by the index as too
|
|
* expensive to answer, which drops the search back to Nominatim alone.
|
|
*
|
|
* The hint has to be NARROW, and that is the part that is easy to get wrong.
|
|
* Measured over 126 places of a real trip through Japan, searched against a
|
|
* live instance:
|
|
*
|
|
* no hint at all 54.8% found, 38.1% at rank 1
|
|
* box around the WHOLE trip 57.1% found, 17.5% at rank 1
|
|
* box around the nearest few places 70.6% found, 34.9% at rank 1
|
|
*
|
|
* A box spanning a whole round trip has its centre somewhere between the
|
|
* cities, points at nothing, and costs more rank-1 hits than it gains. It is
|
|
* worse than no hint. So the day being planned comes first, and a hint that
|
|
* ends up wider than a metropolitan area is dropped rather than sent.
|
|
*/
|
|
|
|
export interface LocationBiasBox {
|
|
low: { lat: number; lng: number }
|
|
high: { lat: number; lng: number }
|
|
}
|
|
|
|
export interface LocationBiasPoint {
|
|
lat: number
|
|
lng: number
|
|
radius?: number
|
|
}
|
|
|
|
/**
|
|
* Widest hint still worth sending, as a radius. Roughly a metropolitan area:
|
|
* far enough to cover a city and its surroundings, narrow enough that its
|
|
* centre still means something. The old value here was 500km of diagonal,
|
|
* which is how the whole-trip box slipped through.
|
|
*/
|
|
const MAX_RADIUS_KM = 60
|
|
|
|
const KM_PER_DEGREE = 111
|
|
|
|
interface Coord { lat?: number | string | null; lng?: number | string | null }
|
|
|
|
export function boxFromCoords(coords: Coord[]): LocationBiasBox | undefined {
|
|
let minLat = Infinity, maxLat = -Infinity, minLng = Infinity, maxLng = -Infinity
|
|
for (const p of coords) {
|
|
// Number(null) is 0, and 0/0 is a real coordinate in the Gulf of Guinea.
|
|
// A place without coordinates would otherwise stretch the box across half
|
|
// the planet and the hint would be dropped for being too wide.
|
|
if (p.lat == null || p.lng == null || p.lat === '' || p.lng === '') continue
|
|
const lat = Number(p.lat), lng = Number(p.lng)
|
|
if (!Number.isFinite(lat) || !Number.isFinite(lng)) continue
|
|
if (lat < minLat) minLat = lat
|
|
if (lat > maxLat) maxLat = lat
|
|
if (lng < minLng) minLng = lng
|
|
if (lng > maxLng) maxLng = lng
|
|
}
|
|
if (!Number.isFinite(minLat)) return undefined
|
|
return { low: { lat: minLat, lng: minLng }, high: { lat: maxLat, lng: maxLng } }
|
|
}
|
|
|
|
/** Half the diagonal of a box, in kilometres — how far the hint reaches. */
|
|
export function radiusKm(box: LocationBiasBox): number {
|
|
const lat = (box.low.lat + box.high.lat) / 2
|
|
const dLat = box.high.lat - box.low.lat
|
|
const dLng = box.high.lng - box.low.lng
|
|
return Math.sqrt(
|
|
(dLat * KM_PER_DEGREE) ** 2 + (dLng * KM_PER_DEGREE * Math.cos(lat * (Math.PI / 180))) ** 2,
|
|
) / 2
|
|
}
|
|
|
|
/**
|
|
* The centre of a box, with the radius that covers it.
|
|
*
|
|
* Autocomplete takes a box, search takes a point — same hint, two shapes,
|
|
* because the two upstream APIs were built years apart. The radius has a floor
|
|
* so a day whose places sit on one street still biases toward the
|
|
* neighbourhood rather than the doorstep.
|
|
*/
|
|
export function pointFromBox(box: LocationBiasBox | undefined): LocationBiasPoint | undefined {
|
|
if (!box) return undefined
|
|
return {
|
|
lat: (box.low.lat + box.high.lat) / 2,
|
|
lng: (box.low.lng + box.high.lng) / 2,
|
|
// Google caps a Text Search bias circle at 50 km and rejects a wider one
|
|
// outright, so the box for a trip spread over more than that is clamped
|
|
// rather than sent and refused. A bias is a hint about where to look; the
|
|
// hint is no worse for being drawn at the largest radius the API accepts.
|
|
radius: Math.min(50_000, Math.round(Math.max(radiusKm(box), 5) * 1000)),
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The hint for the place search, in both shapes the two endpoints want.
|
|
*
|
|
* Preference order, narrowest first:
|
|
* 1. the places of the day currently open — this is the area being planned
|
|
* 2. the trip's places, but only if they still fit inside one region
|
|
* Anything wider is dropped: no hint beats a hint pointing at open country.
|
|
*/
|
|
export function useLocationBias(): { box?: LocationBiasBox; point?: LocationBiasPoint } {
|
|
const places = useTripStore((s) => s.places)
|
|
const assignments = useTripStore((s) => s.assignments)
|
|
const selectedDayId = useTripStore((s) => s.selectedDayId)
|
|
|
|
return useMemo(() => {
|
|
const alle = places || []
|
|
|
|
// The day being planned. Places reach a day through assignments, so the
|
|
// ids come from there and the coordinates from the place pool.
|
|
let quelle: Coord[] = []
|
|
if (selectedDayId != null) {
|
|
const desTages = new Set(
|
|
(assignments?.[String(selectedDayId)] || []).map((a) => a.place_id),
|
|
)
|
|
if (desTages.size > 0) quelle = alle.filter((p) => desTages.has(Number(p.id)))
|
|
}
|
|
if (quelle.length === 0) quelle = alle
|
|
|
|
const box = boxFromCoords(quelle)
|
|
if (!box || radiusKm(box) > MAX_RADIUS_KM) return {}
|
|
return { box, point: pointFromBox(box) }
|
|
}, [places, assignments, selectedDayId])
|
|
}
|