Files
TREK/client/src/components/Planner/locationPicks.ts
T
Maurice 3a67a8e279 fix(planner): trip places in every manual location field, never at 0,0
The trip's places went into the field's search results as they were. A place
without a pin was picked as 0,0, because Number(null) is a finite 0, and a
highlight left over from a longer search could point past the shorter list
and throw on Enter. The phone's form, the train stations and the car stops
were not offered the places at all.

The places are now prepared once per form (named, with a real coordinate,
each once) and handed to every location field of the manual tab, on the
desktop and on the phone. The field shows them while it is empty or holds
fewer than three characters, narrowed by what was typed, and derives that
list rather than writing it into the search results, so a late search answer
cannot replace it and a place already picked does not reopen it.
2026-09-23 13:41:31 +02:00

34 lines
1.2 KiB
TypeScript

import type { LocationPoint } from './LocationSelect'
/** A trip place as far as a location field can offer it. */
interface PlaceLike {
name?: string | null
lat?: number | string | null
lng?: number | string | null
address?: string | null
}
/**
* The trip's places as ready-made picks for a location field.
*
* Only a place with a name and a real coordinate is offered. A place without one would
* be picked as 0,0 (`Number(null)` is a finite 0), which puts the endpoint of a booking
* in the Gulf of Guinea. The same place added twice is offered once.
*/
export function toLocationPicks(places: readonly PlaceLike[] | null | undefined): LocationPoint[] {
const picks: LocationPoint[] = []
const seen = new Set<string>()
for (const place of places ?? []) {
const name = place.name?.trim()
if (!name || place.lat == null || place.lng == null || place.lat === '' || place.lng === '') continue
const lat = Number(place.lat)
const lng = Number(place.lng)
if (!Number.isFinite(lat) || !Number.isFinite(lng)) continue
const key = `${name}:${lat}:${lng}`
if (seen.has(key)) continue
seen.add(key)
picks.push({ name, lat, lng, address: place.address ?? null })
}
return picks
}