mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-26 06:37:33 +00:00
e64ade010f
On a day a flight, train, ferry, cruise or bus leaves and lands, tonight's stay was seated by its check-in time. A hotel in Munich checked into from 15:00 stood ahead of a 15:15 flight out of Hamburg, so the drive went from the Munich hotel to Hamburg airport and reported the flight as nine hours late. A check-in is the earliest arrival, not an appointment: such a stay now waits behind the landing when it lies nearer the arrival than the departure, and its check-in only holds it when the ride lands before the room is ready. A time typed on the stop keeps its seat. With the stays at both ends of a day switched on: - a day with nothing but the next stay's check-in and a ride booked across it gets no drive from one hotel to the other, the same rule the day plan follows since #2476; - a stay is never joined by road to a terminal more than 2000 km away. The check moved to shared/roadtrip so Days and the road trip read one copy. A departure terminal's pin is dated: reaching it more than half a day late no longer moves the flight to the next day without a word, it is a late warning with the full minutes. A check-in evening also carries the stay's check-in time, as a label only. calculate_roadtrip runs the same code and its description says all of the above.
128 lines
5.9 KiB
TypeScript
128 lines
5.9 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
import type { Reservation, ReservationEndpoint } from '../types'
|
|
import { calculateRouteWithLegs } from '../components/Map/RouteCalculator'
|
|
import { MAX_DRIVE_KM } from '@trek/shared/roadtrip'
|
|
import { haversineKm } from '../utils/geo'
|
|
|
|
/**
|
|
* Real road-network geometry for road-based transport bookings (car, bus, taxi,
|
|
* bicycle), so their map lines follow actual streets instead of a straight
|
|
* as-the-crow-flies line — the same idea as the real transit paths (#1065),
|
|
* but routed on demand rather than stored on the reservation.
|
|
*
|
|
* Trains and "other transport" keep their straight line (rail/unknown modes
|
|
* aren't road-routable); flights/cruises/ferries use the great-circle arc.
|
|
* Any routing failure falls back to the straight line the overlay already draws.
|
|
*/
|
|
|
|
const ROAD_PROFILE: Record<string, 'driving' | 'cycling'> = {
|
|
car: 'driving',
|
|
bus: 'driving',
|
|
taxi: 'driving',
|
|
bicycle: 'cycling',
|
|
}
|
|
|
|
// Beyond this straight-line distance a single leg of a car/taxi/bike (or even coach)
|
|
// booking is almost always a data quirk or an inter-continental hop the road router
|
|
// can't resolve — keep the straight line and don't hammer the public OSRM demo.
|
|
// Shared with the day-route builder, which draws the same conclusion about a
|
|
// booking endpoint that landed next to a local stop (#2133).
|
|
const MAX_ROUTE_KM = MAX_DRIVE_KM
|
|
|
|
function orderedWaypoints(r: Reservation): ReservationEndpoint[] {
|
|
return (r.endpoints || [])
|
|
.filter(e => e.role === 'from' || e.role === 'to' || e.role === 'stop')
|
|
.slice()
|
|
.sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0))
|
|
}
|
|
|
|
/**
|
|
* Returns a map of reservation id → routed [lat, lng] polyline for the
|
|
* road-based bookings among `reservations`. Missing entries mean "not routed
|
|
* (yet / not routable)" — the caller should draw its straight line for those.
|
|
* Routing runs once per reservation waypoint-set and is cached across the app
|
|
* by RouteCalculator, so day switches and re-renders don't re-fetch.
|
|
*/
|
|
export function useTransportRoutes(reservations: Reservation[]): Map<number, [number, number][]> {
|
|
const [routes, setRoutes] = useState<Map<number, [number, number][]>>(new Map())
|
|
// id → waypoint signature already fetched/attempted, so an unchanged booking
|
|
// is never re-requested even as the reservations array identity churns.
|
|
const attemptedRef = useRef<Map<number, string>>(new Map())
|
|
|
|
useEffect(() => {
|
|
// Captured once: this is the same Map instance for the hook's whole
|
|
// lifetime (only ever mutated in place, never reassigned), so closing over
|
|
// it here — rather than reading `attemptedRef.current` again inside the
|
|
// cleanup below — is equivalent and keeps the cleanup lint-clean.
|
|
const attempted = attemptedRef.current
|
|
const jobs: { id: number; profile: 'driving' | 'cycling'; points: { lat: number; lng: number }[] }[] = []
|
|
for (const r of reservations) {
|
|
const profile = ROAD_PROFILE[r.type]
|
|
if (!profile) continue
|
|
const wps = orderedWaypoints(r)
|
|
if (wps.length < 2) continue
|
|
// Per leg, not over the whole booking: a road trip with stops is long by design
|
|
// and its total would trip a limit meant to catch a single impossible hop. It is
|
|
// one leg spanning an ocean that the road router cannot resolve, not the sum (#1797).
|
|
let tooFar = false
|
|
for (let i = 0; i < wps.length - 1; i++) {
|
|
if (haversineKm(wps[i], wps[i + 1]) > MAX_ROUTE_KM) { tooFar = true; break }
|
|
}
|
|
if (tooFar) continue
|
|
const key = `${profile}:${wps.map(w => `${w.lat},${w.lng}`).join('|')}`
|
|
if (attempted.get(r.id) === key) continue
|
|
attempted.set(r.id, key)
|
|
jobs.push({ id: r.id, profile, points: wps.map(w => ({ lat: w.lat, lng: w.lng })) })
|
|
}
|
|
if (!jobs.length) return
|
|
|
|
// A fresh controller per effect run (never a ref-cached singleton): React
|
|
// StrictMode's dev-only mount->cleanup->remount cycle runs this effect's
|
|
// cleanup once before the real, lasting mount — a controller reused across
|
|
// that cleanup would already be permanently aborted by the time the real
|
|
// mount's fetch starts, so its request would fail instantly every time.
|
|
const controller = new AbortController()
|
|
// Ids this run's jobs have finished for (success or a genuine, non-abort
|
|
// failure) — checked synchronously in cleanup below, so it stays empty for
|
|
// a cleanup that fires before any of them settle (StrictMode's simulated
|
|
// cleanup always does, since it runs in the same tick, before any await
|
|
// resolves) and is already populated for one that fires afterward (e.g. an
|
|
// unrelated deps change once this run's fetches are long done).
|
|
const settledIds = new Set<number>()
|
|
let cancelled = false
|
|
void (async () => {
|
|
// Sequential to stay gentle on the shared public router.
|
|
for (const job of jobs) {
|
|
try {
|
|
const result = await calculateRouteWithLegs(job.points, { signal: controller.signal, profile: job.profile })
|
|
settledIds.add(job.id)
|
|
if (cancelled) return
|
|
if (result.coordinates.length >= 2) {
|
|
setRoutes(prev => {
|
|
const next = new Map(prev)
|
|
next.set(job.id, result.coordinates)
|
|
return next
|
|
})
|
|
}
|
|
} catch {
|
|
// A genuine failure (not this run being cancelled) also counts as
|
|
// settled — leave it marked attempted, the overlay keeps the
|
|
// straight line. If it WAS the cancellation that rejected this
|
|
// fetch, cleanup already ran (synchronously, before this catch) and
|
|
// un-marked it via `settledIds` never having gained this id in time.
|
|
if (!cancelled) settledIds.add(job.id)
|
|
}
|
|
}
|
|
})()
|
|
return () => {
|
|
cancelled = true
|
|
controller.abort()
|
|
for (const job of jobs) {
|
|
if (!settledIds.has(job.id)) attempted.delete(job.id)
|
|
}
|
|
}
|
|
}, [reservations])
|
|
|
|
return routes
|
|
}
|