Files
TREK/client/src/components/Tours/walk/walkTransit.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

223 lines
9.1 KiB
TypeScript

import { clockOf, minutesOf, type TourListItem } from '@trek/shared';
import type { Assignment, Place, Reservation, TranslationFn } from '../../../types';
import type { MergedItem } from '../../../utils/dayMerge';
import { splitReservationDateTime } from '../../../utils/formatters';
import type { DayItineraryItem } from '../../Map/dayTourProjection';
import type { TransitLeg } from '../../Planner/transitLeg';
import type { TourPlannerWaypoint } from '../planner/tourPlannerWaypoints';
import { tourPlannedTimes } from '../tourPresentation';
import { walkStartOf } from './walkSchedule';
/**
* CW-08: public transit between two city walks on one day. The leg runs from
* where the first walk really ends to where the next one starts, and departs
* when the first one ends: its start that day and its planned total, a total
* entered by hand before the one worked out. Where that cannot be worked out
* no time is made up. The walks stay two walks on foot; nothing here is saved.
*/
const DAY_MINUTES = 24 * 60;
/** Closer than this, in degrees, a leg's end is the walk's end: the route copies the coordinates. */
const SAME_SPOT_DEG = 1e-9;
/** Which tour a place is, as the trip's tour list knows it. */
export type TourOf = (placeId: number) => TourListItem | undefined;
/** What the transit search says of the two walks a leg runs between. */
export interface WalkTransitContext {
/** The day both walks are on: on another day the search no longer speaks of them. */
dayId: number;
fromWalk: string;
toWalk: string;
/** `HH:MM` the first walk ends that day; null where it cannot be worked out or ends on the next day. */
ends: string | null;
/** The first walk ends after midnight, on another day than the one searched on. */
endsNextDay: boolean;
/** The end leaves out a time not known yet, so the walk ends then at the earliest. */
endsAtLeast: boolean;
/** `HH:MM` the next walk starts that day; null without a start time. */
nextStarts: string | null;
}
type TourItem = Extract<DayItineraryItem, { kind: 'tour' }>;
/** A walk in the day's route: its item there and the tour it is. */
interface DayWalk {
item: TourItem;
tour: TourListItem;
}
/** The two walks a leg runs between. */
export interface WalkPair {
first: DayWalk;
next: DayWalk;
}
/** A walk the arrival comes too late for: its name and the `HH:MM` it starts. */
export interface WalkLate {
walk: string;
time: string;
}
function sameSpot(point: { lat: number; lng: number } | null, other: { lat: number; lng: number }): boolean {
return !!point && Math.abs(point.lat - other.lat) < SAME_SPOT_DEG && Math.abs(point.lng - other.lng) < SAME_SPOT_DEG;
}
function walkOf(item: DayItineraryItem | undefined, tourOf: TourOf): DayWalk | null {
if (item?.kind !== 'tour') return null;
const tour = tourOf(item.placeId);
return tour?.tour_type === 'walk' ? { item, tour } : null;
}
/** Whether the day's route goes to an item: in the route, and a walk or a stop with a location. */
function routeGoesTo(item: DayItineraryItem): boolean {
const { assignment } = item;
if (assignment.route_excluded || assignment.accommodation_id != null) return false;
return item.kind === 'tour' || (!!assignment.place?.lat && !!assignment.place?.lng);
}
/**
* The walk a leg leaves from its end and the walk it reaches at its start, or
* null where the leg is no such leg. The leg is told by the entry it leaves,
* as the connector carries it, since two walks of a day can end at one spot;
* it reaches the next point the route goes to, so that must be the next walk.
*/
export function walkPairAt(
items: readonly DayItineraryItem[],
leg: Pick<TransitLeg, 'from' | 'to'>,
tourOf: TourOf,
originId: number | undefined
): WalkPair | null {
const index = items.findIndex((item) => item.assignment.id === originId);
const first = walkOf(items[index], tourOf);
if (!first || !sameSpot(first.item.end, leg.from)) return null;
const next = walkOf(items.slice(index + 1).find(routeGoesTo), tourOf);
return next && sameSpot(next.item.start, leg.to) ? { first, next } : null;
}
/**
* When a walk ends after a start, in minutes after midnight, or null without
* a start or a total. Past midnight it ends on the next day. A list that says
* nothing of the stops leaves the stays out, so that end is a lower bound too.
*/
export function walkEndOf(
start: number | null,
tour: TourListItem
): { minutes: number; atLeast: boolean; nextDay: boolean } | null {
const times = tourPlannedTimes(tour);
if (start === null || times.plannedTotalMinutes === null) return null;
const minutes = start + times.plannedTotalMinutes;
return {
minutes,
atLeast: times.atLeast || (!times.manuallyOverridden && times.walk === null),
nextDay: minutes >= DAY_MINUTES,
};
}
/** A stop as named: its own name, else the trip place it stands for. */
function stopName(stop: TourPlannerWaypoint | undefined, places: readonly Pick<Place, 'id' | 'name'>[]): string {
if (!stop) return '';
return stop.name || places.find((place) => place.id === stop.linkedPlaceId)?.name || '';
}
export interface WalkTransitInput {
leg: TransitLeg;
dayId: number;
pair: WalkPair;
/** Each walk's stops as read; null where they could not be read. */
stops: { first: readonly TourPlannerWaypoint[] | null; next: readonly TourPlannerWaypoint[] | null };
places: readonly Pick<Place, 'id' | 'name'>[];
t: TranslationFn;
}
/**
* The search for a leg between two walks: from the first walk's last stop to
* the next one's first, each named, else said as the end or start of its walk,
* at the coordinates the day's route draws, so a journey saved there joins it.
* It departs when the first walk ends, and never at the time the leg's own
* point carries: that is when the walk starts.
*/
export function walkTransitLeg({ leg, dayId, pair, stops, places, t }: WalkTransitInput): TransitLeg & {
walk: WalkTransitContext;
} {
const { first, next } = pair;
const firstStops = stops.first ?? [];
// A loop ends where it starts, at its first stop again.
const endStop = first.item.loop ? firstStops[0] : firstStops[firstStops.length - 1];
const end = walkEndOf(walkStartOf(first.item.assignment), first.tour);
// Past midnight no time on the search's day is made up.
const ends = end && !end.nextDay ? clockOf(end.minutes) : null;
const nextStart = walkStartOf(next.item.assignment);
return {
from: { ...leg.from, name: stopName(endStop, places) || t('tours.walk.transit.endOf', { walk: first.tour.name }) },
to: {
...leg.to,
name: stopName(stops.next?.[0], places) || t('tours.walk.transit.startOf', { walk: next.tour.name }),
},
time: ends,
walk: {
dayId,
fromWalk: first.tour.name,
toWalk: next.tour.name,
ends,
endsNextDay: end?.nextDay ?? false,
endsAtLeast: end?.atLeast ?? false,
nextStarts: nextStart === null ? null : clockOf(nextStart),
},
};
}
/**
* Whether an arrival comes after a start on a day: a later date always, the
* same date by the clock. An arrival without a date is on that day. Nothing is
* said without a date, a start or an arrival time.
*/
export function arrivesAfterStart(
arrival: { date: string | null; time: string | null },
date: string | null,
start: string | null
): boolean {
const startMinutes = minutesOf(start);
const arrivalMinutes = minutesOf(arrival.time);
if (!date || startMinutes === null || arrivalMinutes === null) return false;
const arrivalDate = arrival.date ?? date;
return arrivalDate === date ? arrivalMinutes > startMinutes : arrivalDate > date;
}
/** The next walk a connection found in the search arrives too late for, or null. */
export function lateForWalk(
walk: WalkTransitContext | null,
date: string | null,
arrival: { date: string | null; time: string | null }
): WalkLate | null {
if (!walk?.nextStarts || !arrivesAfterStart(arrival, date, walk.nextStarts)) return null;
return { walk: walk.toWalk, time: walk.nextStarts };
}
export interface JourneyLateInput {
reservation: Pick<Reservation, 'type' | 'reservation_end_time'>;
/** The day's items after the journey, in their order. */
following: readonly MergedItem[];
date: string | null;
tourOf: TourOf;
}
/**
* A saved journey that now arrives after the walk it leads to starts, said
* while the plan is looked at and never written: a walk moved or a journey
* changed since shows here, and nothing is replaced. Only a public transit
* journey, followed by a walk with a start time, notes aside.
*/
export function journeyLate({ reservation, following, date, tourOf }: JourneyLateInput): WalkLate | null {
if (reservation.type !== 'transit') return null;
const next = following.find((item) => item.type !== 'note');
if (next?.type !== 'place') return null;
const assignment: Assignment = next.data;
const tour = assignment.tour_place_id === assignment.place_id ? tourOf(assignment.place_id) : undefined;
const start = walkStartOf(assignment);
if (tour?.tour_type !== 'walk' || start === null) return null;
const time = clockOf(start);
const arrival = splitReservationDateTime(reservation.reservation_end_time);
return arrivesAfterStart(arrival, date, time) ? { walk: tour.name, time } : null;
}