Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

3.7 KB
Raw Blame
import { formatTime, splitReservationDateTime } from '../../../../utils/formatters'
import type { Day, Reservation, TranslationFn } from '../../../../types'
import { linkedDayDate } from '../../../../components/Planner/bookings/bookingFacts'

/**
 * Transport view-model — the real-data counterpart to the demo's `trsSecs`
 * (spec 03 §1.4). The sections, their order and the type colours come from the
 * desktop bookings model (groupTransports, TRANSPORT_TYPE_COLOR) so both surfaces
 * agree; what is left here is the phone cards' own reading of a booking.
 */

export interface TransitLeg {
  mode?: string
  line?: string | null
  from?: { name?: string; time?: string | null }
  to?: { name?: string; time?: string | null }
}

export interface TransportMeta {
  airline?: string
  flight_number?: string
  train_number?: string
  seat?: string
  class?: string
  platform?: string
  price?: string | number
  priceCurrency?: string
  departure_airport?: string
  arrival_airport?: string
  check_in_time?: string
  check_in_end_time?: string
  check_out_time?: string
  transit?: { legs?: TransitLeg[] }
}

/** Parse the reservation's JSON metadata blob, tolerant of string or object. */
export function parseTransportMeta(res: Reservation): TransportMeta {
  try {
    return (typeof res.metadata === 'string'
      ? JSON.parse(res.metadata || '{}')
      : res.metadata || {}) as TransportMeta
  } catch {
    return {}
  }
}

/**
 * A start time, or a range to an end time, as the card's time cell shows it. A
 * unit both ends carry is said once, after the range ("09:05 – 10:15 Uhr", not
 * "09:05 Uhr – 10:15 Uhr", "9:05 – 10:15 AM"): the doubled one made a German
 * range too long for its cell, and it was cut before its end.
 */
export function timeRange(start: string, end: string | null, locale: string, timeFormat: string): string {
  const from = formatTime(start, locale, timeFormat)
  if (!end) return from
  const to = formatTime(end, locale, timeFormat)
  // The word after the last space, where it holds no digit: "Uhr", "AM", "PM".
  const space = from.lastIndexOf(' ')
  const unit = space > 0 && !/\d/.test(from.slice(space)) ? from.slice(space) : ''
  return unit && to.endsWith(unit) ? `${from.slice(0, space)} – ${to}` : `${from} – ${to}`
}

/**
 * The date and time cells of a phone booking or transport card: the day (or day
 * range) the booking sits on with its calendar date beside it, else its own
 * date, and its time or time range. A dash stands in for whichever is missing.
 */
export function cardWhen(
  res: Reservation,
  startDay: Day | undefined,
  endDay: Day | undefined,
  t: TranslationFn,
  locale: string,
  timeFormat: string,
): { dayValue: string; dayDate: string | null; dayRange: boolean; timeValue: string } {
  const startDt = splitReservationDateTime(res.reservation_time)
  const endDt = splitReservationDateTime(res.reservation_end_time)
  const fmtDate = (date: string) =>
    new Date(`${date}T00:00:00Z`).toLocaleDateString(locale, { day: 'numeric', month: 'short', timeZone: 'UTC' })
  const dayLabel = (day: Day) => day.title || t('dayplan.dayN', { n: day.day_number })
  const timeValue = startDt.time ? timeRange(startDt.time, endDt.time, locale, timeFormat) : '—'
  const rangeEnd = startDay && endDay && endDay.id !== startDay.id ? endDay : undefined
  const dayValue = startDay
    ? `${dayLabel(startDay)}${rangeEnd ? ` – ${dayLabel(rangeEnd)}` : ''}`
    : startDt.date
      ? fmtDate(startDt.date)
      : '—'
  // The same date the desktop card prints beside the day, so a long day title
  // can never hide when the booking actually is.
  const dayDate = startDay ? linkedDayDate(startDay, endDay, { start: startDt.date, end: endDt.date }, locale, ' – ') : null
  return { dayValue, dayDate, dayRange: !!rangeEnd, timeValue }
}