Files
TREK/client/src/components/Planner/bookings/bookingFacts.ts
T
Maurice b7951baf23 fix(ci): green the dev gates after the native and popup rounds
- RTL: the measured pills and centred controls use logical forms or a marked line
- i18n: the two native widget numbers are positions, not counts (NOT_PLURAL)
- Types: styleOverride on the lazy GL map, the location and shared-file payloads,
  the capabilities fallback, the transport sheet's stay metadata
- ESLint: latest-value refs move to layout effects, usePresence keeps the last
  open state in state, the booking import opens clean with a shared document
  kept in the list (the reset effect used to clear it again)
- Layers: the phone import sheet lives under mobile/ and reads through tripRepo;
  the offline host check moves to utils/tileUrl
- Sizes: plugin layer geometry, hardNavigate and the exit animations get
  modules of their own
- Tests follow the status radios, the stay chip overview, the dock + button
  for the first list and the field token; store registry lists the new stores
2026-11-06 18:24:25 +01:00

161 lines
8.1 KiB
TypeScript

import type { Day, Reservation, ReservationEndpoint } from '../../../types'
import { formatPriceText, formatTime, splitReservationDateTime } from '../../../utils/formatters'
import { getFlightLegs, getTrainLegs, usesStationRoute } from '../../../utils/flightLegs'
import { safeExternalHref } from '../../../utils/safeUrl'
import { daySpan, parseMeta, type AssignmentLookupEntry } from './bookingsModel'
export interface FactsContext {
t: (key: string, params?: Record<string, string | number>) => string
locale: string
timeFormat: string
days: Day[]
assignmentLookup: Record<number, AssignmentLookupEntry>
tripCurrency?: string | null
/** Whether an expense is linked; then the mirrored meta price is not shown a second time. */
hasLinkedCost: boolean
}
export interface BookingFacts {
/** The day or day range, e.g. "Day 2 → Day 7", with the calendar date beside it. */
day: { label: string; date: string | null; range: boolean } | null
/** Start and end time, or null without either. */
time: string | null
startTime: string | null
endTime: string | null
startDate: string | null
endDate: string | null
cells: { label: string; value: string }[]
endpoints: ReservationEndpoint[]
legCodes: { route: string; code: string }[]
place: string | null
accommodation: string | null
linked: string | null
url: { href: string | null; text: string } | null
isHotel: boolean
}
const fmtDate = (date: string, locale: string, weekday = true) =>
new Date(`${date}T00:00:00Z`).toLocaleDateString(locale, { ...(weekday ? { weekday: 'short' } : {}), day: 'numeric', month: 'short', timeZone: 'UTC' })
export function formatDay(date: string, locale: string, weekday = true): string {
return fmtDate(date, locale, weekday)
}
/**
* The calendar date beside a linked day or day range, with a weekday when it is
* a single date. A day without a date of its own (a trip planned without dates)
* borrows the booking's own start or end date, so a date is never missing when
* the booking knows one.
*/
export function linkedDayDate(
start: Day,
end: Day | undefined,
booking: { start: string | null; end: string | null },
locale: string,
separator = ' → ',
): string | null {
const first = start.date || booking.start
const last = end && end.id !== start.id ? end.date || booking.end : null
const dates = [first, last !== first ? last : null].filter(Boolean) as string[]
return dates.length ? dates.map(d => fmtDate(d, locale, dates.length === 1)).join(separator) : null
}
/** Everything a card, a row and the detail pane print about one booking, formatted once. */
export function bookingFacts(r: Reservation, c: FactsContext): BookingFacts {
const { t, locale, timeFormat } = c
const meta = parseMeta(r)
const isHotel = r.type === 'hotel'
const span = daySpan(r, c.days)
const startDt = splitReservationDateTime(r.reservation_time)
const endDt = splitReservationDateTime(r.reservation_end_time)
const dayName = (d: Day) => d.title || t('dayplan.dayN', { n: d.day_number })
let day: BookingFacts['day'] = null
if (span.start) {
const range = !!span.end && span.end.id !== span.start.id
const label = range ? `${dayName(span.start)} → ${dayName(span.end!)}` : dayName(span.start)
day = { label, date: linkedDayDate(span.start, span.end, { start: startDt.date, end: endDt.date }, locale), range }
} else if (startDt.date) {
const range = !!endDt.date && endDt.date !== startDt.date
const label = range ? `${fmtDate(startDt.date, locale)} → ${fmtDate(endDt.date!, locale)}` : fmtDate(startDt.date, locale)
day = { label, date: null, range }
}
const eps = (r.endpoints || []).slice().sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0))
const from = eps.find(e => e.role === 'from')
const to = eps.find(e => e.role === 'to')
const startTime = startDt.time || from?.local_time || null
const endTime = endDt.time || to?.local_time || null
// A stay backed by an accommodation says its times as check-in and check-out;
// the time stamped on the booking itself is not what the traveller goes by.
const stayBacked = isHotel && !!(r.accommodation_start_day_id || r.accommodation_end_day_id)
const time = !stayBacked && (startTime || endTime)
? `${startTime ? formatTime(startTime, locale, timeFormat) : ''}${endTime ? ` → ${formatTime(endTime, locale, timeFormat)}` : ''}`.trim()
: null
const hasEndpoints = !!from && !!to
const cells: BookingFacts['cells'] = []
if (meta.airline) cells.push({ label: t('reservations.meta.airline'), value: meta.airline })
if (meta.flight_number) cells.push({ label: t('reservations.meta.flightNumber'), value: meta.flight_number })
if (!hasEndpoints && meta.departure_airport) cells.push({ label: t('reservations.meta.from'), value: meta.departure_airport })
if (!hasEndpoints && meta.arrival_airport) cells.push({ label: t('reservations.meta.to'), value: meta.arrival_airport })
if (meta.train_number) cells.push({ label: t('reservations.meta.trainNumber'), value: meta.train_number })
if (meta.platform) cells.push({ label: t('reservations.meta.platform'), value: meta.platform })
if (meta.seat) cells.push({ label: t('reservations.meta.seat'), value: meta.class ? `${meta.seat}, ${meta.class}` : meta.seat })
if (!c.hasLinkedCost && meta.price != null && meta.price !== '') {
cells.push({ label: t('reservations.price'), value: formatPriceText(meta.price, meta.priceCurrency, c.tripCurrency, locale) })
}
if (meta.check_in_time) {
cells.push({ label: t('reservations.meta.checkIn'), value: formatTime(meta.check_in_time, locale, timeFormat) + (meta.check_in_end_time ? ` → ${formatTime(meta.check_in_end_time, locale, timeFormat)}` : '') })
}
if (meta.check_out_time) cells.push({ label: t('reservations.meta.checkOut'), value: formatTime(meta.check_out_time, locale, timeFormat) })
// Per-segment codes (#1943), only on a real stopover booking.
const legs = r.type === 'flight' ? getFlightLegs(r) : usesStationRoute(r.type) ? getTrainLegs(r) : []
const legCodes = legs.length > 1
? legs.filter(l => l.confirmation_number).map(l => ({ route: [l.from, l.to].filter(Boolean).join(' → '), code: l.confirmation_number as string }))
: []
const linkedEntry = r.assignment_id ? c.assignmentLookup[r.assignment_id] : null
const linked = linkedEntry
? [linkedEntry.dayTitle || t('dayplan.dayN', { n: linkedEntry.dayNumber }), linkedEntry.placeName, linkedEntry.startTime ? `${linkedEntry.startTime}${linkedEntry.endTime ? ` → ${linkedEntry.endTime}` : ''}` : null].filter(Boolean).join(', ')
: null
return {
day, time, startTime, endTime,
startDate: startDt.date || from?.local_date || span.start?.date || null,
endDate: endDt.date || to?.local_date || span.end?.date || null,
cells, endpoints: eps.length >= 2 ? eps : [], legCodes,
place: r.location || r.place_name || null,
accommodation: r.accommodation_name || null,
linked,
url: r.url ? { href: safeExternalHref(r.url), text: r.url } : null,
isHotel,
}
}
function nightsBetween(a: string | null, b: string | null): number {
if (!a || !b) return 0
const n = Math.round((Date.parse(`${b}T00:00:00Z`) - Date.parse(`${a}T00:00:00Z`)) / 864e5)
return n > 0 ? n : 0
}
/**
* A stay's three tiles: check-in and check-out with their dates and times, and
* the nights between. The desktop booking dialog and the phone's booking sheet
* show the same ones.
*/
export function stayStats(
facts: BookingFacts,
meta: { check_in_time?: string | null; check_out_time?: string | null },
t: FactsContext['t'],
locale: string,
): { value: string; label: string }[] {
const stats: { value: string; label: string }[] = []
if (facts.startDate) stats.push({ value: formatDay(facts.startDate, locale, false), label: t('reservations.meta.checkIn') + (meta.check_in_time ? ` ${meta.check_in_time}` : '') })
if (facts.endDate) stats.push({ value: formatDay(facts.endDate, locale, false), label: t('reservations.meta.checkOut') + (meta.check_out_time ? ` ${meta.check_out_time}` : '') })
const nights = nightsBetween(facts.startDate, facts.endDate)
if (nights) stats.push({ value: String(nights), label: t('reservations.nights', { count: nights }) })
return stats
}