Files
TREK/client/src/components/Roadtrip/stayReading.ts
T
Maurice 154c88b48a feat(roadtrip): leave a stop at its End and show the stay that makes
A visit's End could be edited and was shown in road trip mode, but the
schedule never read it: the drive always left a stop after its stay, so
a traveller who knew when they had to leave but not how long they would
stand there had no way to say so.

The End is now the time the drive leaves. RoadtripStop.leaveAt is
filled from the visit's End (or its place's) on the client by
useRoadtripRoutes and on the server by roadtrip-plan.service, so
calculate_roadtrip and get_roadtrip_context agree with the planner, and
computeSchedule takes it as the departure it already knew how to hold.
With Start and End both set the stay is the time between them. With
daily travel times on, the stay is the time until then, and an End
before the window start starts the day early the way a Start does.
Before the first fixed time the chain works backwards through an End as
it does through a pinned Start.

An End is read against the drive: the last passing of that clock time
counts as missed when the drive went past it on the road or earlier
the same day, anything else is the next one, so a drive across
midnight neither waits a whole day nor pretends to be on time. A drive
that gets there after the End leaves on arrival and files a
missedLeave finding, shown beside the late badge in the rail and on the
phone.

The rail's STAY badge, the inspector's Stay tile, the phone stop sheet
and the phone chain show the stay the End makes, with an "until 14:00"
hint. It is read off the schedule and never written to
duration_minutes: the stay belongs to the place, the End to one visit,
and a stored copy would go stale as soon as the arrival moved. For such
a stop the stay dialog, desktop and phone, shows that stay read only
and offers to remove the End, through the visit's time route with an
offline path (assignmentRepo.setTimes, setAssignmentTimes). While the
road trip is on, the End field in both place forms says the drive
leaves at that time.

The dead checkoutAt in the plan service is gone. New strings in all 23
locales.
2026-09-19 19:24:52 +02:00

132 lines
5.3 KiB
TypeScript

import { formatClock, parseClock, type ScheduleEntry } from './roadtripModel'
import type { RoadtripDay, RoadtripStop } from '@trek/shared/roadtrip'
import type { StayDraft } from './RoadtripStayModal'
import type { RoadtripStayControl } from './VisitControls'
/**
* How long a stop is stood at, the way a stay badge shows it.
*
* For most stops that is the stay the place carries. A stop with a time set to leave it
* is stood at from whenever the drive gets there until that time, so its stay is what
* the schedule made of it and `until` says when it ends. The number is read here and
* never written back to the place: the stay belongs to the place, the end time to one
* visit of it, and a stored copy would be wrong the moment the arrival moved.
*/
export interface StayReading {
minutes: number | null
/** The time the stop is left at as it was set, or null for a stop that takes its stay. */
until: string | null
/**
* The daily travel hours close before `until` comes. The stop is where that day ends and
* the drive goes on the next morning, so it is left neither at that time nor at the one
* the row shows, and no length measured against either would be true.
*/
dayEndsFirst?: boolean
}
export function readStay(
stop: Pick<RoadtripStop, 'dwellMinutes' | 'leaveAt'>,
entry: { arrival?: string | null; departure?: string | null } | null | undefined,
): StayReading {
const leave = parseClock(stop.leaveAt)
if (leave === null) return { minutes: stop.dwellMinutes, until: null }
const until = formatClock(leave)
const arrival = parseClock(entry?.arrival)
const departure = parseClock(entry?.departure)
if (arrival === null || departure === null) return { minutes: null, until }
// A schedule leaves such a stop at the time, or on arrival when it got there too late
// for it. Any other departure is the window shutting first.
if (departure !== leave && departure !== arrival) return { minutes: null, until, dayEndsFirst: true }
return {
// Round the clock once: reached before midnight and left the morning after, the
// departure reads as the smaller of the two.
minutes: (departure - arrival + 1440) % 1440,
until,
}
}
/** How late the drive gets to a stop for the time it is set to be left at, or null. */
export function missedLeaveOf(day: Pick<RoadtripDay, 'schedule'>, index: number): number | null {
return day.schedule.warnings.find(w => w.index === index && w.code === 'missedLeave')?.minutes ?? null
}
/**
* The length a stay badge prints, or null for none.
*
* No stay on an ordinary stop is nothing to show. On a stop left at a set time a zero is
* an answer: it is left the moment the drive gets there.
*/
export function shownStay(stay: StayReading): number | null {
return stay.until ? stay.minutes : stay.minutes || null
}
/** One visit on the routed days, with the entry of the card that draws it. */
export interface LocatedStop {
day: RoadtripDay
index: number
stop: RoadtripStop
entry: ScheduleEntry | undefined
}
/**
* Finds a visit across the routed days.
*
* A stop reached after a night drive is drawn on the card of the day it is reached on,
* not the one it is stored on, so `preferDayId` is only where the search starts.
*/
export function locateStop(
days: readonly RoadtripDay[],
assignmentId: number,
preferDayId?: number | null,
): LocatedStop | null {
const preferred = days.find(d => d.dayId === preferDayId)
for (const day of preferred ? [preferred, ...days.filter(d => d !== preferred)] : days) {
const index = day.stops.findIndex(s => s.assignmentId === assignmentId && !s.automaticNight)
if (index >= 0) return { day, index, stop: day.stops[index], entry: day.schedule.entries[index] }
}
return null
}
/**
* What the stay dialog opens on for one stop of a card.
*
* `missedBy` is the card's own finding, carried rather than worked out again from the two
* clocks: which day a leave time falls on is the schedule's call, and a second reading of
* it here is a second answer.
*/
export function stayDraftOf(stop: RoadtripStop, entry: ScheduleEntry | undefined, missedBy: number | null = null): StayDraft {
return {
placeId: stop.placeId,
name: stop.name,
minutes: stop.dwellMinutes,
arrival: entry?.arrival ?? null,
departure: entry?.departure ?? null,
leaveAt: stop.leaveAt ?? null,
missedBy,
assignmentId: stop.assignmentId,
dayId: stop.ownerDayId,
}
}
/**
* The place inspector's Stay tile.
*
* With the visit on the drive it reads the same as the rail does. A place selected
* without one it can be pinned to (planned twice, say) falls back to the stay it carries.
*/
export function inspectorStay(
days: readonly RoadtripDay[],
stop: RoadtripStop | undefined,
place: { id: number; name: string; duration_minutes?: number | null },
onEdit?: (draft: StayDraft) => void,
): RoadtripStayControl {
const located = stop ? locateStop(days, stop.assignmentId, stop.ownerDayId) : null
const draft: StayDraft = located
? stayDraftOf(located.stop, located.entry, missedLeaveOf(located.day, located.index))
: { placeId: place.id, name: place.name, minutes: place.duration_minutes ?? null, arrival: null }
const reading = located
? readStay(located.stop, located.entry)
: { minutes: stop ? stop.dwellMinutes : place.duration_minutes ?? null, until: null }
return { ...reading, onEdit: onEdit ? () => onEdit(draft) : undefined }
}