feat(flight-live): a live boarding pass on the web, desktop and phone

The booking dialog and the phone's transport sheet show a flight's moment,
both airports with planned and expected times, the route as an arc with the
aircraft on it and the numbers of the flight; booking cards and day plan
rows get a one-line state with a progress bar. The trip map draws the
aircraft in the air on Leaflet and GL, and the admin settings carry the
switch. Translated into every language.
This commit is contained in:
Maurice
2026-11-13 15:29:10 +01:00
parent 154563f34d
commit 4796615439
91 changed files with 3520 additions and 25 deletions
@@ -0,0 +1,51 @@
import { escapeHtml } from '@trek/shared';
import L from 'leaflet';
import { useMemo } from 'react';
import { Marker, Tooltip } from 'react-leaflet';
import { frameLng } from '../../utils/lngFrame';
import { aircraftMarkerHtml, useLiveAircraft, type LiveAircraft } from './flightAircraft';
import './flightAircraft.css';
import { useLngFrame } from './lngFrameContext';
function aircraftIcon(a: LiveAircraft): L.DivIcon {
return L.divIcon({
className: '',
html: aircraftMarkerHtml(a, escapeHtml),
iconSize: [30, 30],
iconAnchor: [15, 15],
});
}
/**
* The trip's flights in the air, on the Leaflet map: one marker per aircraft
* at its live position, turned to its track. A click opens the booking.
*/
export default function FlightAircraftLayer({
reservations,
onOpen,
}: {
/** The bookings this map shows; only their aircraft are drawn. */
reservations: readonly { id: number }[] | undefined;
onOpen?: (reservationId: number) => void;
}) {
const aircraft = useLiveAircraft(reservations);
const frame = useLngFrame();
const icons = useMemo(() => new Map(aircraft.map((a) => [a.key, aircraftIcon(a)])), [aircraft]);
return (
<>
{aircraft.map((a) => (
<Marker
key={a.key}
position={[a.lat, frameLng(frame, a.lng)]}
icon={icons.get(a.key)}
zIndexOffset={2000}
eventHandlers={{ click: () => onOpen?.(a.reservationId) }}
>
<Tooltip direction="top" offset={[0, -16]} opacity={1} className="map-tooltip">
{a.label}
</Tooltip>
</Marker>
))}
</>
);
}
+2 -2
View File
@@ -407,7 +407,7 @@ function TrackCasingPane({ onReady }: { onReady: (ready: boolean) => void }) {
import { useMarkerThumbs } from './useMarkerThumbs'
import { markerPhotoHtml, placePhotoFull, placePhotoUrl } from './placePhoto'
import { useGeolocation } from '../../hooks/useGeolocation'
import { useGeolocation, type GeoPosition, type TrackingMode } from '../../hooks/useGeolocation'
import LocationButton from './LocationButton'
import { useIsPhone } from '../../mobile/useIsPhone'
import { useIsWide } from '../../hooks/sizeClass'
@@ -415,7 +415,6 @@ import { useIsWide } from '../../hooks/sizeClass'
// Live-location rendering inside the Leaflet map. Subscribes via the
// shared useGeolocation hook so the Leaflet and Mapbox variants behave
// identically. Heading is shown as a rotated conic SVG when available.
import type { GeoPosition, TrackingMode } from '../../hooks/useGeolocation'
import { SelectionController } from './SelectionController'
import { LngFrameProvider, useLngFrame, useTripLngFrame } from './lngFrameContext'
import { frameLine, frameLng } from '../../utils/lngFrame'
@@ -1078,6 +1077,7 @@ export const MapView = memo(function MapView({
showStats={showReservationStats}
onEndpointClick={onReservationClick}
roadRoutes={transportRoutes}
aircraftOf={reservations}
/>
{hazards?.length > 0 && <HazardLayers hazards={hazards} />}
+9 -11
View File
@@ -23,6 +23,7 @@ import { markerPhotoHtml, placePhotoFull, placePhotoUrl } from './placePhoto'
import { isStandardFamily, supportsCustom3d, wantsTerrain, addCustom3dBuildings, addTerrainAndSky } from './mapboxSetup'
import { attachLocationMarker, type LocationMarkerHandle } from './locationMarkerMapbox'
import { ReservationMapboxOverlay } from './reservationsMapbox'
import { useFlightAircraftGL } from './useFlightAircraftGL'
import { useTransportRoutes } from '../../hooks/useTransportRoutes'
import { visibleRouteReservations } from '../../utils/reservationRoutes'
import { safeHexColor } from '../../utils/safeColor'
@@ -1838,22 +1839,19 @@ export function MapViewGL({
useEffect(() => {
const map = mapRef.current
if (!map || !mapReady) return
if (!reservationOverlayRef.current) {
reservationOverlayRef.current = new ReservationMapboxOverlay(map, {
showConnections: true,
showStats: showReservationStats,
showEndpointLabels,
onEndpointClick: (id) => onReservationClickRef.current?.(id),
}, gl.Marker as any)
}
reservationOverlayRef.current.update(visibleReservations, {
const options = {
showConnections: true,
showStats: showReservationStats,
showEndpointLabels,
onEndpointClick: (id) => onReservationClickRef.current?.(id),
}, transportRoutes)
onEndpointClick: (id: number) => onReservationClickRef.current?.(id),
}
reservationOverlayRef.current ??= new ReservationMapboxOverlay(map, options, gl.Marker as any)
reservationOverlayRef.current.update(visibleReservations, options, transportRoutes)
}, [visibleReservations, transportRoutes, showReservationStats, showEndpointLabels, mapReady, glProvider])
// The trip's flights in the air, as on the Leaflet map.
useFlightAircraftGL(mapRef, mapReady, gl?.Marker, id => onReservationClickRef.current?.(id), reservations)
// Fit bounds on fitKey change — matches the Leaflet BoundsController
const paddingOpts = useMemo(() => {
if (isMobile) return { top: 40, right: 20, bottom: 40, left: 20 }
@@ -11,6 +11,7 @@ import { TRANSPORT_COLOR, TRANSPORT_META, transportHop, type TransportHop, type
import type { Reservation } from '../../types'
import { frameLine } from '../../utils/lngFrame'
import { useLngFrame } from './lngFrameContext'
import FlightAircraftLayer from './FlightAircraftLayer'
const ENDPOINT_PANE = 'reservation-endpoints'
@@ -69,9 +70,11 @@ interface Props {
// Real road-network geometry for car/bus/taxi/bicycle bookings, keyed by
// reservation id. When present it is drawn instead of the straight arc.
roadRoutes?: Map<number, [number, number][]>
// Every booking of the map, drawn or not: the live aircraft of its flights show regardless of the route switch.
aircraftOf?: readonly { id: number }[]
}
export default function ReservationOverlay({ reservations, showConnections, onEndpointClick, roadRoutes }: Props) {
export default function ReservationOverlay({ reservations, showConnections, onEndpointClick, roadRoutes, aircraftOf }: Props) {
useEndpointPane()
const map = useMap()
const [zoom, setZoom] = useState(() => map.getZoom())
@@ -117,10 +120,12 @@ export default function ReservationOverlay({ reservations, showConnections, onEn
return set
}, [visibleItems, zoom, map])
if (!showConnections) return null
const aircraft = <FlightAircraftLayer reservations={aircraftOf} onOpen={onEndpointClick} />
if (!showConnections) return aircraft
return (
<>
{aircraft}
{visibleItems.map(item => {
if (item.transitSegs.length > 0) {
return item.transitSegs.map((seg, segIdx) => (
@@ -0,0 +1,84 @@
/*
* A live aircraft on the trip map (flightAircraft.ts). Both renderers position
* the outer element themselves, so the halo animation runs on a child and
* never fights the position. The colour comes in as --trek-aircraft, the
* map's transport colour, so the aircraft matches the flight lines it rides. The marker sits on the map's own colours, not on
* the theme's surfaces, so it stays legible on light and dark basemaps alike.
*/
@keyframes trek-aircraft-halo {
from {
opacity: 0.55;
transform: scale(0.7);
}
to {
opacity: 0;
transform: scale(1.9);
}
}
.trek-aircraft {
/* The marker is the disc alone, so the map's centre anchor sits on the aircraft;
the label hangs beside it out of the flow, on the right in either direction. */
position: relative;
width: 30px;
height: 30px;
direction: ltr;
pointer-events: auto;
cursor: pointer;
}
.trek-aircraft__halo {
position: absolute;
left: 0;
top: 0;
width: 30px;
height: 30px;
border-radius: 999px;
background: var(--trek-aircraft);
animation: trek-aircraft-halo 2.4s ease-out infinite;
}
.trek-aircraft__disc {
position: relative;
display: grid;
place-items: center;
width: 30px;
height: 30px;
border-radius: 999px;
background: #fff;
color: var(--trek-aircraft);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.28);
}
.trek-aircraft__label {
position: absolute;
left: 36px;
top: 50%;
transform: translateY(-50%);
padding: 3px 8px;
border-radius: 999px;
background: rgba(17, 24, 39, 0.82);
color: #fff;
font-family: var(--font-system);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.3px;
white-space: nowrap;
}
.trek-aircraft.is-stale .trek-aircraft__halo {
animation: none;
opacity: 0;
}
.trek-aircraft.is-stale .trek-aircraft__disc {
opacity: 0.6;
}
@media (prefers-reduced-motion: reduce) {
.trek-aircraft__halo {
animation: none;
opacity: 0;
}
}
@@ -0,0 +1,79 @@
import type { FlightLiveLeg } from '@trek/shared';
import { useMemo } from 'react';
import { useShallow } from 'zustand/react/shallow';
import { useFlightLiveStore } from '../../store/flightLiveStore';
import { useTripStore } from '../../store/tripStore';
import { TRANSPORT_COLOR } from './transportHops';
/**
* The aircraft of a trip's flights that are in the air (or rolling) right now,
* drawn on the trip map by both map engines (FlightAircraftLayer for Leaflet,
* useFlightAircraftGL for Mapbox/MapLibre) from the same markup, so the two
* look alike.
*/
export interface LiveAircraft {
key: string;
reservationId: number;
lat: number;
lng: number;
/** Degrees clockwise from north. */
track: number;
label: string;
/** Position is old (no signal over an ocean): drawn faded. */
stale: boolean;
}
function liveAircraftOf(legs: FlightLiveLeg[]): LiveAircraft[] {
return legs.flatMap((leg) => {
const pos = leg.position;
if (!pos || (leg.phase !== 'airborne' && leg.phase !== 'taxiing')) return [];
return [
{
key: `${leg.reservation_id}:${leg.leg_index}`,
reservationId: leg.reservation_id,
lat: pos.lat,
lng: pos.lng,
track: pos.track_deg ?? 0,
label: leg.flight_number,
stale: leg.stale,
},
];
});
}
const NO_LEGS: FlightLiveLeg[] = [];
/**
* The open trip's aircraft in the air, limited to the bookings the map was
* handed: the same map component draws collections and tour previews, which
* must not show the last opened trip's flights. Re-renders only when one of
* them moves.
*/
export function useLiveAircraft(reservations: readonly { id: number }[] | undefined): LiveAircraft[] {
const tripId = useTripStore((s) => s.trip?.id);
// The store's own leg objects, so the selection stays equal until one changes.
const legs = useFlightLiveStore(
useShallow((s) => (tripId == null ? NO_LEGS : Object.values(s.trips[tripId]?.legs ?? {})))
);
return useMemo(() => {
const ids = new Set((reservations ?? []).map((r) => r.id));
return ids.size === 0 ? [] : liveAircraftOf(legs).filter((a) => ids.has(a.reservationId));
}, [legs, reservations]);
}
/** An airliner seen from above, nose up (north), so `rotate(track)` points it where it flies. */
const PLANE_PATH =
'M12 2c.7 0 1.2.8 1.2 1.7v5.6l7.6 4.5v2l-7.6-2.2v4.6l2.3 1.8v1.6L12 20.6l-3.5 1v-1.6l2.3-1.8v-4.6L3.2 15.8v-2l7.6-4.5V3.7C10.8 2.8 11.3 2 12 2Z';
/**
* The marker's markup: a white disc with a soft halo, the aircraft turned to
* its track in the map's transport colour, and the flight number on a pill beside it.
* Static text only goes in through `escape`, the caller's HTML escaper.
*/
export function aircraftMarkerHtml(a: LiveAircraft, escape: (s: string) => string): string {
return `<div class="trek-aircraft${a.stale ? ' is-stale' : ''}" style="--trek-aircraft:${TRANSPORT_COLOR}" role="img" aria-label="${escape(a.label)}">
<span class="trek-aircraft__halo"></span>
<span class="trek-aircraft__disc"><svg viewBox="0 0 24 24" width="18" height="18" style="transform:rotate(${Math.round(a.track)}deg)"><path d="${PLANE_PATH}" fill="currentColor"/></svg></span>
<span class="trek-aircraft__label">${escape(a.label)}</span>
</div>`;
}
@@ -0,0 +1,72 @@
import { escapeHtml } from '@trek/shared';
import { useEffect, useRef } from 'react';
import { aircraftMarkerHtml, useLiveAircraft } from './flightAircraft';
import './flightAircraft.css';
interface GlMarker {
setLngLat(lngLat: [number, number]): GlMarker;
addTo(map: unknown): GlMarker;
remove(): void;
getElement(): HTMLElement;
}
/**
* The trip's flights in the air on the Mapbox/MapLibre map: one HTML marker
* per aircraft, moved in place when its position changes and removed when it
* lands. A click opens the booking. The map is read through its ref inside the
* effects, never during render; a rebuilt map (style or provider change) drops
* the markers of the old one.
*/
export function useFlightAircraftGL(
mapRef: { current: unknown },
mapReady: boolean,
// The map library's Marker class, injected like the other GL overlays do.
Marker: (new (opts: { element: HTMLElement; anchor: string }) => GlMarker) | undefined,
onOpen: (reservationId: number) => void,
/** The bookings this map shows; only their aircraft are drawn. */
reservations: readonly { id: number }[] | undefined
): void {
const aircraft = useLiveAircraft(reservations);
const markers = useRef(new Map<string, GlMarker>());
const markersOf = useRef<unknown>(null);
const openRef = useRef(onOpen);
useEffect(() => {
openRef.current = onOpen;
});
useEffect(() => {
const map = mapRef.current;
if (!map || !mapReady || !Marker) return;
if (markersOf.current !== map) {
// The markers belonged to a map that is gone; the new one starts empty.
markers.current.clear();
markersOf.current = map;
}
const live = new Set(aircraft.map((a) => a.key));
for (const [key, marker] of markers.current) {
if (!live.has(key)) {
marker.remove();
markers.current.delete(key);
}
}
for (const a of aircraft) {
let marker = markers.current.get(a.key);
if (!marker) {
const el = document.createElement('div');
el.addEventListener('click', () => openRef.current(a.reservationId));
marker = new Marker({ element: el, anchor: 'center' }).setLngLat([a.lng, a.lat]).addTo(map);
markers.current.set(a.key, marker);
}
marker.getElement().innerHTML = aircraftMarkerHtml(a, escapeHtml);
marker.setLngLat([a.lng, a.lat]);
}
}, [aircraft, mapRef, mapReady, Marker]);
useEffect(() => {
const current = markers.current;
return () => {
for (const marker of current.values()) marker.remove();
current.clear();
};
}, []);
}