refactor(planner): share the per leg travel mode between desktop and phone

The day plan's connector menu and the phone timeline's are built by
legModeMenuItems, and both save a leg's mode through useLegModeActions, which
reports a refused write and reloads the days the same way on both.
This commit is contained in:
Maurice
2026-10-08 21:07:11 +00:00
parent 20d7e62afc
commit 38c7d76c5d
5 changed files with 188 additions and 42 deletions
@@ -1,10 +1,10 @@
import { useRef, useState, type MouseEvent } from 'react'
import { ArrowRight, BedDouble, CalendarDays, CalendarRange, ChevronRight, Compass, LogIn, LogOut, MapPin, Pencil, PencilLine, Route, Ticket, TrainFront, Undo2, RotateCcw, TramFront } from 'lucide-react'
import { ArrowRight, BedDouble, CalendarDays, CalendarRange, ChevronRight, Compass, LogIn, LogOut, MapPin, Pencil, PencilLine, Route, Ticket, TrainFront, Undo2 } from 'lucide-react'
import type { LucideIcon } from 'lucide-react'
import { useContextMenu, ContextMenu } from '../../../../components/shared/ContextMenu'
import MarkdownText from '../../../../components/shared/MarkdownText'
import { fmtTransitDuration } from '../../../../components/Planner/transitDisplay'
import { routeModeIcon } from '../../../../components/Planner/routeModes'
import { legModeMenuItems } from '../../../../components/Planner/useLegModeActions'
import { formatTime } from '../../../../utils/formatters'
import { useMPlanTimeline, type MPlanTimelineController } from './useMPlanTimeline'
import { cityPillsForDay } from './planTimelineModel'
@@ -47,12 +47,12 @@ export default function MPlanTimeline({ planner, shell }: MPlanTimelineProps) {
const openLegMenu = (e: MouseEvent, assignmentId: number, seg: RouteSegment) => {
// Public transit sits under the road profiles, as on the desktop (#2398).
const transitLeg = tl.transitLegFor(seg)
legMenu.open(e, [
...tl.routeModeOptions.map(o => ({ label: o.label, icon: routeModeIcon(o.key), onClick: () => tl.setLegMode(assignmentId, o.key) })),
...(transitLeg ? [{ label: t('transit.title'), icon: TramFront, onClick: () => tl.planTransitLeg(transitLeg) }] : []),
{ divider: true },
{ label: t('dayplan.transportMode.useDefault'), icon: RotateCcw, onClick: () => tl.setLegMode(assignmentId, null) },
])
legMenu.open(e, legModeMenuItems(
tl.routeModeOptions,
mode => tl.setLegMode(assignmentId, mode),
t,
transitLeg ? () => tl.planTransitLeg(transitLeg) : undefined,
))
}
// Plugin time contributions in the day plan (dayScheduleProvider hook) —
// slotted under their anchor rows, same as the desktop sidebar.
@@ -1,8 +1,8 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useTripStore } from '../../../../store/tripStore'
import { useRouteCalculation } from '../../../../hooks/useRouteCalculation'
import { assignmentsApi, reservationsApi, weatherApi } from '../../../../api/client'
import { useRouteModeOptions } from '../../../../components/Planner/routeModes'
import { reservationsApi, weatherApi } from '../../../../api/client'
import { useLegModeActions } from '../../../../components/Planner/useLegModeActions'
import { getDayBookendHotels } from '../../../../utils/dayOrder'
import { dayWeatherAnchor } from '../../../../utils/dayWeather'
import { metadataWithLegPositions, planMergedOrder, withTransportPositions } from '../../../../utils/mergedOrder'
@@ -327,7 +327,7 @@ export function useMPlanTimeline(planner: TripPlanner) {
: null
// ── Per-segment travel mode (#1281) ──
const routeModeOptions = useRouteModeOptions()
const { routeModeOptions, persistLegMode } = useLegModeActions({ tripId, toast, t, tripActions })
// Set the mode of the leg leaving a stop — optimistic, then persisted; null clears
// the override back to the day default. Sticky against the whole-day picker.
@@ -337,11 +337,8 @@ export function useMPlanTimeline(planner: TripPlanner) {
useTripStore.setState(state => ({
assignments: { ...state.assignments, [key]: (state.assignments[key] || []).map(a => (a.id === assignmentId ? { ...a, leg_transport_mode: mode } : a)) },
}))
assignmentsApi.updateTransport(tripId, assignmentId, mode).catch((err: unknown) => {
toast.error(err instanceof Error ? err.message : t('common.unknownError'))
tripActions.refreshDays(tripId)
})
}, [day, tripId, toast, t, tripActions])
persistLegMode(assignmentId, mode)
}, [day, persistLegMode])
// ── Public transit for one leg (#2398) ──
// The entry the desktop connector menu carries: the automated search, seeded with