Files
TREK/client/src/components/Planner/useLegModeActions.ts
T
Maurice c1c77d89b5 fix(day-plan): make the legs from a booking or the hotel tappable on the phone
On the phone timeline only a leg that leaves a stop opened the travel mode
menu. The leg from the morning hotel to the first stop, the leg from the last
stop back to the hotel and the leg from a booking's arrival to the next stop
stayed inert, although the desktop day plan already changes them through the
stop at their other end. A TestFlight tester could not tap the walk from their
train's arrival to the hotel.

The timeline now knows which stop owns each of these legs: the first stop's
incoming mode for the morning hotel leg and for a leg out of a booking, the
last stop's outgoing mode for the evening hotel leg. Tapping them in edit mode
opens the same menu as on the desktop, and the phone sets the incoming mode
the way the desktop does.

A leg no stop owns, such as a booking's arrival to the hotel, has no mode to
store. It follows the day default, and on both the phone and the desktop it
now offers the public transit search where a trip has dates, instead of
reacting to nothing.
2026-11-25 10:21:43 +01:00

77 lines
2.9 KiB
TypeScript

import { type LucideIcon, RotateCcw, TramFront } from 'lucide-react';
import { useCallback } from 'react';
import { assignmentRepo } from '../../repo/assignmentRepo';
import type { TripStoreState } from '../../store/tripStore';
import { type RouteModeOption, routeModeIcon, useRouteModeOptions } from './routeModes';
type Translate = (key: string, params?: Record<string, string | number>) => string;
/** One entry of the connector menu, in the shape the context menu draws. */
export interface LegModeMenuItem {
label?: string;
icon?: LucideIcon;
onClick?: () => void;
divider?: boolean;
}
/**
* The menu a leg's connector opens (#1281): every route profile, public transit
* for the leg when it can be searched (#2398), and the day default, which clears
* the leg's own mode again.
*/
export function legModeMenuItems(
options: RouteModeOption[],
pick: (mode: string | null) => void,
t: Translate,
planTransit?: () => void
): LegModeMenuItem[] {
return [
...options.map((o) => ({ label: o.label, icon: routeModeIcon(o.key), onClick: () => pick(o.key) })),
...(planTransit ? [{ label: t('transit.title'), icon: TramFront, onClick: planTransit }] : []),
{ divider: true },
{ label: t('dayplan.transportMode.useDefault'), icon: RotateCcw, onClick: () => pick(null) },
];
}
/**
* The menu of a leg no stop owns, a booking's arrival to the night's hotel: nothing
* keeps a mode for it, so it follows the day default and only public transit can be
* planned for it.
*/
export function transitOnlyMenuItems(planTransit: () => void, t: Translate): LegModeMenuItem[] {
return [{ label: t('transit.title'), icon: TramFront, onClick: planTransit }];
}
export interface LegModeDeps {
tripId: number;
toast: { error: (message: string) => void };
t: Translate;
tripActions: Pick<TripStoreState, 'refreshDays'>;
}
/**
* The per-leg travel mode behind both the desktop day plan and the phone timeline.
* Each side shows a new mode at once its own way; `persistLegMode` then saves it for
* the leg leaving a stop, or with 'incoming' for the one entering it. A refused write
* says why and reloads the days, so the shown mode goes back.
*/
export function useLegModeActions({ tripId, toast, t, tripActions }: LegModeDeps) {
const routeModeOptions = useRouteModeOptions();
const persistLegMode = useCallback(
(assignmentId: number, mode: string | null, direction?: 'incoming') => {
assignmentRepo.setLegMode(tripId, assignmentId, mode, direction).catch((err: unknown) => {
toast.error(err instanceof Error ? err.message : t('common.unknownError'));
void tripActions.refreshDays(tripId);
});
},
[tripId, toast, t, tripActions]
);
const legModeMenu = (pick: (mode: string | null) => void, planTransit?: () => void) =>
legModeMenuItems(routeModeOptions, pick, t, planTransit);
return { routeModeOptions, legModeMenu, persistLegMode };
}