mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-21 06:26:26 +00:00
4bac447ac8
Closes #2019 The route toggle belongs to one day; the reservation list it filtered is the whole trip. So turning on a day's routing let every transit reservation in the trip draw its geometry at once, including the ones belonging to other days — on a day that has no transport of its own, which is what the reporter saw. Only automated transports rode it, and that is the distinction they drew: a manually entered train or flight is type 'train'/'flight' and reaches the map only through its own per-booking toggle. Only the transit search stores type 'transit'. Gate the transit branch on the day, and gate only that one — the per-booking toggle stays trip-wide, because switching a booking's route on is a deliberate act about that booking. The predicate is the day-order range, not the departure day: an overnight journey carries end_day_id and has to stay on both of its days, the same rule the sidebar timeline already applies. Day order rather than day id, since ids stop being monotonic once days are reordered. The day context is optional throughout, so a map without one — collections — keeps behaving exactly as it did. Not a 4.0.0 regression: it reproduces on 3.4.1 too.
55 lines
2.7 KiB
TypeScript
55 lines
2.7 KiB
TypeScript
import { isDayInAccommodationRange } from './dayOrder'
|
|
import type { Day, Reservation } from '../types'
|
|
|
|
/**
|
|
* A reservation is routable on the map once it has at least two ordered
|
|
* endpoints (from/to/stop).
|
|
*
|
|
* Total in its argument, because it is handed straight to Array.filter over a
|
|
* list this module does not own. It used to guard the property but not the
|
|
* element, so one undefined entry took the whole trip planner down (#1979).
|
|
*/
|
|
export function isRoutableReservation(r: Pick<Reservation, 'endpoints'> | null | undefined): boolean {
|
|
return (r?.endpoints || []).length >= 2
|
|
}
|
|
|
|
export interface RouteVisibilityOptions {
|
|
/** Reservation ids resolved as currently visible for this trip (per-item toggle, bulk toggle, or the account-wide default — see connectionsVisibility.ts). */
|
|
visibleConnectionIds: number[]
|
|
/** The separate manual day-route-calculator toggle — 'transit' reservations ride it (#1065). */
|
|
showTransitRoutes: boolean
|
|
/** The day that toggle belongs to. Optional: a map without a day context (collections) keeps drawing every transit, as before. */
|
|
selectedDayId?: number | null
|
|
/** The trip's days, needed to order them — day ids are not monotonic once days have been reordered. */
|
|
days?: Day[]
|
|
}
|
|
|
|
/**
|
|
* Does this transit journey actually run on the selected day? `showTransitRoutes` is one
|
|
* day's toggle but the reservation list is trip-wide, so without this every automated
|
|
* transport in the trip drew its geometry as soon as any day's route was switched on (#2019).
|
|
*
|
|
* The span, not the departure day: an overnight journey carries end_day_id and has to stay
|
|
* on both of its days — same rule getTransportForDay applies to the sidebar timeline.
|
|
* A journey bound to no day keeps drawing; hiding something that legitimately belongs
|
|
* to today is the worse failure.
|
|
*/
|
|
function transitRunsOnDay(r: Reservation, selectedDayId: number | null | undefined, days: Day[]): boolean {
|
|
if (selectedDayId == null) return true
|
|
const startDayId = r.day_id ?? r.end_day_id
|
|
if (startDayId == null) return true
|
|
const day = days.find(d => d.id === selectedDayId)
|
|
if (!day) return true
|
|
return isDayInAccommodationRange(day, startDayId, r.end_day_id ?? startDayId, days)
|
|
}
|
|
|
|
/** Which reservations should draw a route on the map, combining the two independent toggles above. */
|
|
export function visibleRouteReservations(reservations: Reservation[], options: RouteVisibilityOptions): Reservation[] {
|
|
const { visibleConnectionIds, showTransitRoutes, selectedDayId, days } = options
|
|
const set = new Set(visibleConnectionIds || [])
|
|
return reservations.filter(r =>
|
|
(r.type === 'transit' && showTransitRoutes && transitRunsOnDay(r, selectedDayId, days || [])) ||
|
|
set.has(r.id)
|
|
)
|
|
}
|