import { useId, type ReactNode } from 'react' import { CalendarDays, Clock, ExternalLink, FileText, Pencil } from 'lucide-react' import Markdown from 'react-markdown' import remarkGfm from 'remark-gfm' import remarkBreaks from 'remark-breaks' import { markdownLinkComponents } from '../shared/markdownLink' import { BlurredCode } from '../shared/BookingCode' import { DialogButton, DialogFooter, DialogSection, DialogShell, FooterSpacer, PILL } from '../shared/DialogShell' import { useTripStore } from '../../store/tripStore' import { formatTime, splitReservationDateTime } from '../../utils/formatters' import { parseReservationMetadata, seatCells } from '../../utils/flightLegs' import { BookingDialogHeader } from './bookings/BookingDialogShell' import { BOX, Eyebrow, Field, fs, toneColor, toneOf } from './bookings/bookingParts' import { filesFor } from '../../utils/reservationFiles' import { bookingDayLabel, segmentCodeLabel } from './transportDetailModel' import { TransitLegs, transitSummary } from './bookings/transitParts' import { bookingDates } from './bookings/bookingsModel' import { flowText } from '../../utils/flowText' import type { Day, Reservation } from '../../types' type Translate = (key: string, params?: Record) => string interface DayPlanSidebarTransportDetailModalProps { transportDetail: Reservation | null setTransportDetail: (v: Reservation | null) => void onNavigateToFiles?: () => void /** Opens the edit form for this reservation (shown as a footer action). */ onEdit?: (res: Reservation) => void t: Translate locale: string timeFormat: string } /** One labelled value of the grid; a booking code is drawn through the blur preference. */ interface DetailCell { label: string value: string code?: boolean } /** * The booking a rental pill, a transit row or a map endpoint opens from the * plan: the head band tinted by its status like the booking dialogs, the * booking's facts one labelled field each, a transit journey leg by leg, the * notes and the files, and the edit in the bar at the foot. */ export function DayPlanSidebarTransportDetailModal({ transportDetail, ...rest }: DayPlanSidebarTransportDetailModalProps) { if (!transportDetail) return null return } function TransportDetailDialog({ res, setTransportDetail, onNavigateToFiles, onEdit, t, locale, timeFormat }: Omit & { res: Reservation }) { const titleId = useId() const allFiles = useTripStore(s => s.files) const days = useTripStore(s => s.days) const close = () => setTransportDetail(null) const meta = parseReservationMetadata(res) const transit = transitSummary(meta) const files = filesFor(res, allFiles || []) const when = whenOf(res, days, locale, timeFormat) const cells = detailCells(res, meta, t) const confirmed = res.status === 'confirmed' const stats: { value: string; label: string }[] = [] if (transit && transit.legs.length > 0) { if (transit.duration) stats.push({ value: durationText(transit.duration, t), label: t('transit.durationLabel') }) stats.push({ value: transit.transfers > 0 ? String(transit.transfers) : t('transit.direct'), label: t('transit.transfersLabel') }) // Under a minute of walking is not worth a tile. if (transit.walk > 59) stats.push({ value: t('transit.min', { count: Math.round(transit.walk / 60) }), label: t('transit.walkLabel') }) } const header = ( {confirmed ? t('planner.resConfirmed') : t('planner.resPending')} {(when.day || when.time) && ( {when.day ? : } {when.day} {when.time && {when.time}} )} )} /> ) const footer = ( {t('common.close')} {onEdit && ( onEdit(res)} icon={}>{t('common.edit')} )} ) return ( {cells.length > 0 && (
{cells.map((c, i) => ( {c.code ? {c.value} : c.value} ))}
)} {res.location && {res.location}} {stats.length > 0 && (
{stats.map(s => (
{s.value}
{s.label}
))}
)} {/* Public-transit itinerary (#1065): the legs the transit search stored. */} {transit && transit.legs.length > 0 && ( )} {res.notes && (
{res.notes}
)} {files.length > 0 && (
{files.map(f => ( ))}
)}
) } /** The facts of a flight or a train, the booking code and the code of each segment. */ function detailCells(res: Reservation, meta: Record, t: Translate): DetailCell[] { const cells: DetailCell[] = [] const add = (label: string, value: unknown, code = false) => { if (value) cells.push({ label, value: String(value), code }) } if (res.type === 'flight') { add(t('reservations.meta.airline'), meta.airline) add(t('reservations.meta.flightNumber'), meta.flight_number) add(t('reservations.meta.from'), meta.departure_airport) add(t('reservations.meta.to'), meta.arrival_airport) } else if (res.type === 'train') { add(t('reservations.meta.trainNumber'), meta.train_number) add(t('reservations.meta.platform'), meta.platform) } // Each leg's seat under its route on a booking with a stop, not just the first leg's. if (res.type === 'flight' || res.type === 'train') { for (const c of seatCells(res, t('reservations.meta.seat'))) add(c.label, c.value) } add(t('reservations.confirmationCode'), res.confirmation_number, true) // A stopover booking can carry its own reference per segment (#1943); the // flat fields above only ever describe the first leg. Drawn as codes, so the // blur preference covers them like the booking's own. const legs = Array.isArray(meta.legs) ? meta.legs as { from?: string; to?: string; confirmation_number?: string }[] : [] if (legs.length > 1) { for (const leg of legs) { if (!leg?.confirmation_number) continue add(segmentCodeLabel(leg, t), leg.confirmation_number, true) } } return cells } /** * The booking's calendar day (both, when it arrives on a later one) and its time or * time range, each empty when the booking has none. A booking holding only times * takes the date of the day it is linked to (#3122). */ function whenOf(res: Reservation, days: Day[], locale: string, timeFormat: string): { day: string; time: string } { const { time } = splitReservationDateTime(res.reservation_time) const { time: endTime } = splitReservationDateTime(res.reservation_end_time) const dates = bookingDates(res, days || []) // An arrival date alone would read as the departure, so it only joins a start date. const span = dates.start ? [dates.start, dates.end !== dates.start ? dates.end : null].filter((d): d is string => !!d) : [] const day = span.length > 0 ? flowText(span.map(d => bookingDayLabel(d, locale))) : '' const start = time ? formatTime(time, locale, timeFormat) : '' const end = endTime ? formatTime(endTime, locale, timeFormat) : '' return { day, time: start && end ? `${start} → ${end}` : start } } /** A journey's length in the reader's language: minutes up to an hour, then hours and minutes. */ function durationText(seconds: number, t: Translate): string { const mins = Math.round(seconds / 60) if (mins < 60) return t('transit.min', { count: mins }) const hours = Math.floor(mins / 60) const minutes = mins % 60 return minutes > 0 ? t('dawarich.duration.hoursMinutes', { hours, minutes }) : t('dawarich.duration.hours', { hours }) } /** A labelled value too long for a grid cell, such as an address. */ function DetailField({ label, children }: { label: string; children: ReactNode }) { return (
{label}
{children}
) }