Files
TREK/client/src/components/Planner/transitLeg.ts
T
Maurice 9f0caf8539 feat(tours): public transit between two city walks in the day plan, from the first walk's end at the time it ends (CW-08)
"Public transit" on the connector between two city walks in the day plan,
on the desktop and on the phone, now opens the existing search from where
the first walk really ends to where the next one starts, at the
coordinates the day's route draws, named after the first walk's last stop
and the next walk's first stop. A loop ends at its first stop again. A stop
without a name takes the name of the trip place it stands for, and a walk
whose stops cannot be read on this device names that end "End of" or
"Start of" the walk. The search opens on the walks' day and departs when
the first walk ends: its start that day, the time on the day's entry
before the place's own, plus its planned total, a total entered by hand
before walking and stays. The assignment's own end time is not used.

Above the search a note says when the first walk ends, "at the earliest"
where a stay or the walking time is not known yet, and when the next walk
starts. Without a start or a total no time is made up: the time field
stays empty and Search waits until one is chosen. A walk that only ends
after midnight says so, and the time field stays empty as well. On any other day, or for
any other leg, the search opens as before, with 09:00 as its default. A
connection that arrives after the next walk starts says so in its card,
before its legs are opened and before it is taken. Nothing is saved until
"Add to day", which keeps the connection as its own journey in the plan as
before; both walks stay two walks on foot and nothing is moved.

A journey saved before a walk that now arrives after the walk starts says
so on its row in the plan, on the desktop and the phone, while the plan is
looked at. Nothing is rewritten. The desktop no longer draws a connector of
nothing between a stop and a booking at the same spot, as the phone already
did, so a journey saved at the walks' ends adds no walk on either side. A
leg leaving a loop tour no longer departs at the tour's start time.

The leg is told by the walk it leaves, so two walks of a day that end at
one spot, such as two loops from the hotel, each search from their own end
and time, and a stop between two walks keeps the leg an ordinary one. Only
the leg asked for last opens the search, and none opens once the plan is
left while the walks' stops are still being read.

Both views run on one shared hook, the walk's stops are read through one
helper the stop list uses as well, and the walk's start on a day is worked
out in one place for the times shown in the detail and here.
2026-10-20 07:09:57 +00:00

99 lines
4.1 KiB
TypeScript

import type { Accommodation, Assignment, Reservation, RouteSegment } from '../../types'
import type { WalkTransitContext } from '../Tours/walk/walkTransit'
import type { PickedPlace } from './TransitSearchPanel'
/** What the transit search is opened with for one leg of a day. */
export interface TransitLeg {
from: PickedPlace
to: PickedPlace
time: string | null
}
/** A leg as a view hands it on to open the search: its day and, between two city walks, what it says of them (CW-08). */
export interface PlannedTransitLeg extends TransitLeg {
dayId: number
walk?: WalkTransitContext
}
/** What the search is seeded with: a leg, or the ends of a journey whose route is changed. */
export interface TransitPrefill {
from?: PickedPlace | null
to?: PickedPlace | null
time?: string | null
walk?: WalkTransitContext | null
}
/**
* A connector's endpoints carry only coordinates (RouteSegment.from/to, both
* [lat, lng]); resolve them back to the NAMES the transit search shows by indexing
* every located place, hotel and booking endpoint on the trip. Name is a property
* of a coordinate, so a trip-wide index is fine; a connector only renders when both
* ends are located, so a coordinate that misses the index is rare (nameless pick).
*/
export function buildTransitNameIndex(
assignments: Record<string, Assignment[]>,
accommodations: Accommodation[],
reservations: Reservation[],
): Map<string, string> {
const m = new Map<string, string>()
const put = (lat?: number | null, lng?: number | null, name?: string | null) => {
if (lat == null || lng == null || !name) return
const k = `${lat},${lng}`
if (!m.has(k)) m.set(k, name)
}
for (const list of Object.values(assignments)) {
for (const a of list) put(a.place?.lat, a.place?.lng, a.place?.name)
}
for (const acc of accommodations) put(acc.place_lat, acc.place_lng, acc.place_name)
for (const r of reservations) {
for (const ep of (r.endpoints || [])) put(ep.lat, ep.lng, ep.name)
}
return m
}
/**
* The leg's departure time must be resolved WITHIN its day: the same located POI can
* be revisited on another day at a different time (a supported pattern), so a
* trip-wide coordinate index would return the wrong day's time. Scope to this day's
* assignments (and the reservations that touch it) so a revisit keeps its own time.
*/
function originDepartureTime(
originKey: string,
dayId: number,
assignments: Record<string, Assignment[]>,
reservations: Reservation[],
): string | null {
// A tour's place is where it starts and its time when it starts: a leg leaving a loop is no departure then.
const stops = (assignments[String(dayId)] ?? []).filter(a => a.tour_place_id == null || a.tour_place_id !== a.place?.id)
for (const a of stops) {
if (a.place?.lat != null && a.place?.lng != null && `${a.place.lat},${a.place.lng}` === originKey) return a.place.place_time ?? null
}
for (const r of reservations) {
if (r.day_id !== dayId && r.end_day_id !== dayId) continue
for (const ep of (r.endpoints || [])) {
if (ep.lat != null && ep.lng != null && `${ep.lat},${ep.lng}` === originKey) return ep.local_time ?? null
}
}
return null
}
/**
* The public transport prefill for a leg, from its RouteSegment: MOTIS is driven off
* the coordinates, the names come from the index and the departure time from the
* origin's own day. Accepts 'H:mm' or 'HH:mm[:ss]', normalised to 'HH:mm'. Shared by
* the desktop day plan and the phone timeline, so both open the search alike.
*/
export function buildTransitLeg(
seg: RouteSegment | undefined,
dayId: number,
names: Map<string, string>,
assignments: Record<string, Assignment[]>,
reservations: Reservation[],
): TransitLeg | null {
if (!seg?.from || !seg?.to) return null
const pick = (c: [number, number]): PickedPlace => ({ name: names.get(`${c[0]},${c[1]}`) || '', lat: c[0], lng: c[1] })
const rawTime = originDepartureTime(`${seg.from[0]},${seg.from[1]}`, dayId, assignments, reservations)
const m = rawTime ? /^(\d{1,2}):(\d{2})/.exec(rawTime) : null
return { from: pick(seg.from), to: pick(seg.to), time: m ? `${m[1].padStart(2, '0')}:${m[2]}` : null }
}