mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 05:56:53 +00:00
3a67a8e279
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.
34 lines
1.2 KiB
TypeScript
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
|
|
}
|