fix(mobile): offer public transit in the day plan's leg menu, as on the desktop

The connector menu on the phone listed only the road profiles, so the
transit search could not be opened for a single leg there. It now carries
the Public transit entry whenever the trip has dates, seeded with the leg's
two ends and the departure of the stop it leaves, and the transport sheet
passes that departure on to the search.

The prefill is built by one module the desktop day plan now uses as well.

Closes #2398
This commit is contained in:
Maurice
2026-09-19 22:39:05 +02:00
parent 3ad40e056d
commit 00de3c3a1d
9 changed files with 283 additions and 58 deletions
@@ -9,6 +9,7 @@ import { avatarSrc } from '../../utils/avatarSrc'
import { safeHttpUrl } from '../../utils/safeUrl'
import { ChevronDown, ChevronRight, ChevronUp, Compass, Navigation, RotateCcw, ExternalLink, Clock, Pencil, GripVertical, Ticket, Plus, FileText, Trash2, Car, Lock, Hotel, Footprints, Route as RouteIcon, Bookmark, StickyNote, TramFront, Zap } from 'lucide-react'
import { type PickedPlace } from './TransitSearchPanel'
import { buildTransitLeg, buildTransitNameIndex } from './transitLeg'
import { assignmentsApi, reservationsApi, daysApi } from '../../api/client'
import { calculateRouteWithLegs, optimizeRoute, generateGoogleMapsUrl, generateCoMapsUrl, type NamedWaypoint } from '../Map/RouteCalculator'
import GoogleMapsIcon from '../shared/GoogleMapsIcon'
@@ -1434,61 +1435,18 @@ const DayPlanSidebar = React.memo(function DayPlanSidebar(props: DayPlanSidebarP
})
}
// A connector's endpoints carry only coordinates (RouteSegment.from/to, both
// [lat, lng]); resolve them back to the NAMES the transit search shows by indexing
// every located place, hotel and booking endpoint on the trip. Name is a property
// of a coordinate, so a trip-wide index is fine; a connector only renders when both
// ends are located, so a coordinate that misses the index is rare (nameless pick).
const transitNameIndex = useMemo(() => {
const m = new Map<string, string>()
const put = (lat?: number | null, lng?: number | null, name?: string | null) => {
if (lat == null || lng == null || !name) return
const k = `${lat},${lng}`
if (!m.has(k)) m.set(k, name)
}
for (const list of Object.values(assignments)) {
for (const a of list) put(a.place?.lat, a.place?.lng, a.place?.name)
}
for (const acc of accommodations) put(acc.place_lat, acc.place_lng, acc.place_name)
for (const r of reservations) {
for (const ep of (r.endpoints || [])) put(ep.lat, ep.lng, ep.name)
}
return m
}, [assignments, accommodations, reservations])
// The leg's departure time must be resolved WITHIN its day: the same located POI can
// be revisited on another day at a different time (a supported pattern), so a
// trip-wide coordinate index would return the wrong day's time. Scope to this day's
// assignments (and the reservations that touch it) so a revisit keeps its own time.
const originDepartureTime = (originKey: string, dayId: number): string | null => {
for (const a of assignments[String(dayId)] ?? []) {
if (a.place?.lat != null && a.place?.lng != null && `${a.place.lat},${a.place.lng}` === originKey) return a.place.place_time ?? null
}
for (const r of reservations) {
if (r.day_id !== dayId && r.end_day_id !== dayId) continue
for (const ep of (r.endpoints || [])) {
if (ep.lat != null && ep.lng != null && `${ep.lat},${ep.lng}` === originKey) return ep.local_time ?? null
}
}
return null
}
// Build the "public transport" prefill for a leg from its RouteSegment: MOTIS is
// driven off the coordinates, the names come from the index and the departure time
// from the origin's own day. Accepts 'H:mm' or 'HH:mm[:ss]', normalised to 'HH:mm'.
const buildTransitLeg = (seg: RouteSegment | undefined, dayId: number): { from: PickedPlace; to: PickedPlace; time: string | null } | null => {
if (!seg?.from || !seg?.to) return null
const pick = (c: [number, number]): PickedPlace => ({ name: transitNameIndex.get(`${c[0]},${c[1]}`) || '', lat: c[0], lng: c[1] })
const rawTime = originDepartureTime(`${seg.from[0]},${seg.from[1]}`, dayId)
const m = rawTime ? /^(\d{1,2}):(\d{2})/.exec(rawTime) : null
return { from: pick(seg.from), to: pick(seg.to), time: m ? `${m[1].padStart(2, '0')}:${m[2]}` : null }
}
// Coordinates back to names for the transit search, over every located place,
// hotel and booking endpoint on the trip (see transitLeg.ts).
const transitNameIndex = useMemo(
() => buildTransitNameIndex(assignments, accommodations, reservations),
[assignments, accommodations, reservations],
)
// The extra connector-menu entry (#1281 follow-up): search public transit for this
// leg instead of drawing a road route. Only when a handler is wired (day has dates).
const transitLegMenuItem = (dayId: number, seg?: RouteSegment) => {
if (!onPlanTransitLeg) return []
const leg = buildTransitLeg(seg, dayId)
const leg = buildTransitLeg(seg, dayId, transitNameIndex, assignments, reservations)
if (!leg) return []
return [{ label: t('transit.title'), icon: TramFront, onClick: () => onPlanTransitLeg({ dayId, from: leg.from, to: leg.to, time: leg.time }) }]
}
@@ -0,0 +1,72 @@
import { describe, expect, it } from 'vitest'
import { buildTransitLeg, buildTransitNameIndex } from './transitLeg'
import type { Accommodation, Assignment, Reservation, RouteSegment } from '../../types'
// FE-PLANNER-TRANSITLEG-001 to FE-PLANNER-TRANSITLEG-006
const visit = (id: number, dayId: number, name: string, lat: number, lng: number, placeTime?: string) =>
({ id, day_id: dayId, place: { id, name, lat, lng, place_time: placeTime ?? null } }) as unknown as Assignment
const seg = (from: [number, number], to: [number, number]) => ({ from, to }) as unknown as RouteSegment
describe('buildTransitNameIndex', () => {
it('FE-PLANNER-TRANSITLEG-001: names every located place, hotel and booking endpoint by its coordinate', () => {
const names = buildTransitNameIndex(
{ '1': [visit(11, 1, 'Louvre', 48.86, 2.34)] },
[{ place_lat: 48.85, place_lng: 2.35, place_name: 'Hotel Lutetia' } as unknown as Accommodation],
[{ endpoints: [{ lat: 48.88, lng: 2.36, name: 'Gare du Nord' }] } as unknown as Reservation],
)
expect(names.get('48.86,2.34')).toBe('Louvre')
expect(names.get('48.85,2.35')).toBe('Hotel Lutetia')
expect(names.get('48.88,2.36')).toBe('Gare du Nord')
})
it('FE-PLANNER-TRANSITLEG-002: keeps the first name for a coordinate and skips what has no position or name', () => {
const names = buildTransitNameIndex(
{ '1': [visit(11, 1, 'First', 1, 2), visit(12, 1, 'Second', 1, 2)] },
[{ place_lat: null, place_lng: null, place_name: 'Nowhere' } as unknown as Accommodation],
[{ endpoints: [{ lat: 3, lng: 4, name: '' }] } as unknown as Reservation, {} as Reservation],
)
expect(names.get('1,2')).toBe('First')
expect(names.size).toBe(1)
})
})
describe('buildTransitLeg', () => {
const assignments = {
'1': [visit(11, 1, 'Louvre', 48.86, 2.34, '9:05'), visit(12, 1, 'Orsay', 48.87, 2.33)],
'2': [visit(21, 2, 'Louvre', 48.86, 2.34, '15:30:00')],
}
const names = buildTransitNameIndex(assignments, [], [])
it('FE-PLANNER-TRANSITLEG-003: seeds both ends and the origin\'s time on that day, as HH:mm', () => {
expect(buildTransitLeg(seg([48.86, 2.34], [48.87, 2.33]), 1, names, assignments, [])).toEqual({
from: { name: 'Louvre', lat: 48.86, lng: 2.34 },
to: { name: 'Orsay', lat: 48.87, lng: 2.33 },
time: '09:05',
})
// The same place on another day carries that day's time.
expect(buildTransitLeg(seg([48.86, 2.34], [48.87, 2.33]), 2, names, assignments, [])?.time).toBe('15:30')
})
it('FE-PLANNER-TRANSITLEG-004: takes the time from a booking endpoint that touches the day', () => {
const reservations = [
{ day_id: 9, end_day_id: null, endpoints: [{ lat: 1, lng: 1, local_time: '07:00' }] },
{ day_id: 3, end_day_id: 1, endpoints: [{ lat: 1, lng: 1, local_time: '18:45' }] },
] as unknown as Reservation[]
expect(buildTransitLeg(seg([1, 1], [2, 2]), 1, new Map(), {}, reservations)).toEqual({
from: { name: '', lat: 1, lng: 1 },
to: { name: '', lat: 2, lng: 2 },
time: '18:45',
})
})
it('FE-PLANNER-TRANSITLEG-005: leaves the time empty when the origin has none', () => {
expect(buildTransitLeg(seg([48.87, 2.33], [48.86, 2.34]), 1, names, assignments, [])?.time).toBeNull()
})
it('FE-PLANNER-TRANSITLEG-006: answers nothing for a leg without both ends', () => {
expect(buildTransitLeg(undefined, 1, names, assignments, [])).toBeNull()
expect(buildTransitLeg({ from: [1, 1] } as unknown as RouteSegment, 1, names, assignments, [])).toBeNull()
})
})
@@ -0,0 +1,81 @@
import type { Accommodation, Assignment, Reservation, RouteSegment } from '../../types'
import type { PickedPlace } from './TransitSearchPanel'
/** What the transit search is opened with for one leg of a day. */
export interface TransitLeg {
from: PickedPlace
to: PickedPlace
time: string | null
}
/**
* A connector's endpoints carry only coordinates (RouteSegment.from/to, both
* [lat, lng]); resolve them back to the NAMES the transit search shows by indexing
* every located place, hotel and booking endpoint on the trip. Name is a property
* of a coordinate, so a trip-wide index is fine; a connector only renders when both
* ends are located, so a coordinate that misses the index is rare (nameless pick).
*/
export function buildTransitNameIndex(
assignments: Record<string, Assignment[]>,
accommodations: Accommodation[],
reservations: Reservation[],
): Map<string, string> {
const m = new Map<string, string>()
const put = (lat?: number | null, lng?: number | null, name?: string | null) => {
if (lat == null || lng == null || !name) return
const k = `${lat},${lng}`
if (!m.has(k)) m.set(k, name)
}
for (const list of Object.values(assignments)) {
for (const a of list) put(a.place?.lat, a.place?.lng, a.place?.name)
}
for (const acc of accommodations) put(acc.place_lat, acc.place_lng, acc.place_name)
for (const r of reservations) {
for (const ep of (r.endpoints || [])) put(ep.lat, ep.lng, ep.name)
}
return m
}
/**
* The leg's departure time must be resolved WITHIN its day: the same located POI can
* be revisited on another day at a different time (a supported pattern), so a
* trip-wide coordinate index would return the wrong day's time. Scope to this day's
* assignments (and the reservations that touch it) so a revisit keeps its own time.
*/
function originDepartureTime(
originKey: string,
dayId: number,
assignments: Record<string, Assignment[]>,
reservations: Reservation[],
): string | null {
for (const a of assignments[String(dayId)] ?? []) {
if (a.place?.lat != null && a.place?.lng != null && `${a.place.lat},${a.place.lng}` === originKey) return a.place.place_time ?? null
}
for (const r of reservations) {
if (r.day_id !== dayId && r.end_day_id !== dayId) continue
for (const ep of (r.endpoints || [])) {
if (ep.lat != null && ep.lng != null && `${ep.lat},${ep.lng}` === originKey) return ep.local_time ?? null
}
}
return null
}
/**
* The public transport prefill for a leg, from its RouteSegment: MOTIS is driven off
* the coordinates, the names come from the index and the departure time from the
* origin's own day. Accepts 'H:mm' or 'HH:mm[:ss]', normalised to 'HH:mm'. Shared by
* the desktop day plan and the phone timeline, so both open the search alike.
*/
export function buildTransitLeg(
seg: RouteSegment | undefined,
dayId: number,
names: Map<string, string>,
assignments: Record<string, Assignment[]>,
reservations: Reservation[],
): TransitLeg | null {
if (!seg?.from || !seg?.to) return null
const pick = (c: [number, number]): PickedPlace => ({ name: names.get(`${c[0]},${c[1]}`) || '', lat: c[0], lng: c[1] })
const rawTime = originDepartureTime(`${seg.from[0]},${seg.from[1]}`, dayId, assignments, reservations)
const m = rawTime ? /^(\d{1,2}):(\d{2})/.exec(rawTime) : null
return { from: pick(seg.from), to: pick(seg.to), time: m ? `${m[1].padStart(2, '0')}:${m[2]}` : null }
}