From 64445a5204091d98680f5660b4ad78ebf6f6674f Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 20 Oct 2026 13:20:12 +0200 Subject: [PATCH] feat(tours): compare other ways for one section of a hike (RTE-02) A routed section of a hike between two neighbouring points can now be compared with the other ways the router knows between the same two points, under the same limit and preferences. "Compare ways for the next leg" in a waypoint's menu opens a card over the planner map: the way taken now and up to three others, each drawn on the map in its own colour, with length, climb, descent and walking time by the signposts' rule. Figures without heights stay unknown, and the differences to the current way are shown only where both are known. When the router knows no other way, or does not answer, the card says so. Showing, hovering and choosing change nothing. Only "Take this way" does: the router is first asked whether a few shaping points hold the chosen way (the check the road trip's picker already uses), and only then are they set into that one leg as a single step of the history, with a "Way taken" note and its undo at the foot of the map. Other legs, fixed lines, days and points stay as they were, the totals follow from the replanned route, and the shaping points can be dragged like any other. A section that changed while the router was asked, or a way it does not keep to, is not written, and the card says why. The ways are drawn in 3D too. --- .../src/components/Map/valhallaRoute.test.ts | 17 + client/src/components/Map/valhallaRoute.ts | 5 +- .../Tours/planner/LegVariantsCard.test.tsx | 173 ++++++++ .../Tours/planner/LegVariantsCard.tsx | 402 ++++++++++++++++++ .../Tours/planner/TourWaypointList.test.tsx | 43 +- .../Tours/planner/TourWaypointList.tsx | 24 +- .../components/Tours/planner/hikingCosting.ts | 45 ++ .../Tours/planner/hikingPreferences.test.tsx | 7 +- .../Tours/planner/legVariantPaint.test.ts | 76 ++++ .../Tours/planner/legVariantPaint.ts | 65 +++ .../Tours/planner/legVariantRouting.test.ts | 141 ++++++ .../Tours/planner/legVariantRouting.ts | 58 +++ .../Tours/planner/legVariants.test.ts | 175 ++++++++ .../components/Tours/planner/legVariants.ts | 183 ++++++++ .../Tours/planner/routeLegAt.test.ts | 23 +- .../components/Tours/planner/routeLegAt.ts | 44 +- .../planner/tourPlannerWaypoints.test.ts | 36 +- .../Tours/planner/tourPlannerWaypoints.ts | 25 ++ .../components/Tours/planner/tourRouting.ts | 37 +- .../Tours/planner/useAppliedWayHint.test.ts | 70 +++ .../Tours/planner/useAppliedWayHint.ts | 58 +++ .../Tours/planner/useLegVariants.test.tsx | 263 ++++++++++++ .../Tours/planner/useLegVariants.ts | 219 ++++++++++ .../Tours/planner/useRouteShaping.ts | 37 +- .../useTourPlanner.routeTools.test.tsx | 31 ++ .../Tours/planner/useTourPlanner.ts | 12 +- .../Tours/terrain/TourTerrainMap.tsx | 3 + .../Tours/terrain/terrainLayers.test.ts | 17 + .../components/Tours/terrain/terrainLayers.ts | 41 +- .../Tours/terrain/useTourTerrain.ts | 9 +- .../tripPlanner/TourPlannerMapStage.test.tsx | 55 ++- .../pages/tripPlanner/TourPlannerMapStage.tsx | 44 +- .../pages/tripPlanner/useTripPlannerPage.ts | 8 +- client/src/styles/hike.css | 17 + client/tests/helpers/tourPlannerStub.ts | 17 + shared/src/i18n/ar/tours.ts | 17 + shared/src/i18n/az/tours.ts | 17 + shared/src/i18n/br/tours.ts | 18 + shared/src/i18n/ca/tours.ts | 18 + shared/src/i18n/cs/tours.ts | 17 + shared/src/i18n/de/tours.ts | 18 + shared/src/i18n/en/tours.ts | 17 + shared/src/i18n/es/tours.ts | 18 + shared/src/i18n/et/tours.ts | 17 + shared/src/i18n/fr/tours.ts | 18 + shared/src/i18n/gr/tours.ts | 18 + shared/src/i18n/hu/tours.ts | 18 + shared/src/i18n/id/tours.ts | 18 + shared/src/i18n/it/tours.ts | 18 + shared/src/i18n/ja/tours.ts | 17 + shared/src/i18n/ko/tours.ts | 17 + shared/src/i18n/nl/tours.ts | 17 + shared/src/i18n/pl/tours.ts | 17 + shared/src/i18n/ru/tours.ts | 17 + shared/src/i18n/sk/tours.ts | 17 + shared/src/i18n/sv/tours.ts | 17 + shared/src/i18n/th/tours.ts | 17 + shared/src/i18n/tours-locales.spec.ts | 2 +- shared/src/i18n/tr/tours.ts | 17 + shared/src/i18n/uk/tours.ts | 17 + shared/src/i18n/vi/tours.ts | 18 + shared/src/i18n/zh-TW/tours.ts | 17 + shared/src/i18n/zh/tours.ts | 17 + 63 files changed, 2840 insertions(+), 111 deletions(-) create mode 100644 client/src/components/Tours/planner/LegVariantsCard.test.tsx create mode 100644 client/src/components/Tours/planner/LegVariantsCard.tsx create mode 100644 client/src/components/Tours/planner/hikingCosting.ts create mode 100644 client/src/components/Tours/planner/legVariantPaint.test.ts create mode 100644 client/src/components/Tours/planner/legVariantPaint.ts create mode 100644 client/src/components/Tours/planner/legVariantRouting.test.ts create mode 100644 client/src/components/Tours/planner/legVariantRouting.ts create mode 100644 client/src/components/Tours/planner/legVariants.test.ts create mode 100644 client/src/components/Tours/planner/legVariants.ts create mode 100644 client/src/components/Tours/planner/useAppliedWayHint.test.ts create mode 100644 client/src/components/Tours/planner/useAppliedWayHint.ts create mode 100644 client/src/components/Tours/planner/useLegVariants.test.tsx create mode 100644 client/src/components/Tours/planner/useLegVariants.ts diff --git a/client/src/components/Map/valhallaRoute.test.ts b/client/src/components/Map/valhallaRoute.test.ts index 2182a8eec..a6acc53e1 100644 --- a/client/src/components/Map/valhallaRoute.test.ts +++ b/client/src/components/Map/valhallaRoute.test.ts @@ -519,6 +519,23 @@ describe('valhallaAlternates', () => { expect(bodies[1]).not.toHaveProperty('alternates') }) + it('VALHALLA-ALT-005: a section of a hike is offered ways on foot under its own limit and preferences (RTE-02)', async () => { + const bodies: Record[] = [] + server.use(http.post(FOSSGIS_VALHALLA, async ({ request }) => { + bodies.push(await request.json() as Record) + return HttpResponse.json(withAlternates([answer({ time: 12_000 })])) + })) + + const ways = await valhallaAlternates(HAMBURG, BERLIN, 'walking', [], 3, undefined, { max_hiking_difficulty: 4, use_hills: 0.2 }) + + expect(ways).toHaveLength(2) + expect(bodies[0]).toMatchObject({ + alternates: 3, + costing: 'pedestrian', + costing_options: { pedestrian: { max_hiking_difficulty: 4, use_hills: 0.2 } }, + }) + }) + it('VALHALLA-ALT-004: a refusal is asked once more, then is no answer; an instance with its own OSRM asks nothing', async () => { let calls = 0 server.use(http.post(FOSSGIS_VALHALLA, () => { calls++; return new HttpResponse(null, { status: 429 }) })) diff --git a/client/src/components/Map/valhallaRoute.ts b/client/src/components/Map/valhallaRoute.ts index 30cf31b91..d114047bf 100644 --- a/client/src/components/Map/valhallaRoute.ts +++ b/client/src/components/Map/valhallaRoute.ts @@ -460,6 +460,8 @@ async function requestRun( * quickest road beside a day Valhalla had routed was offered as the faster way, and * taking it changed nothing. Valhalla answers `alternates` between exactly two locations, * which is all one leg is, and weighs every one of them the way it weighed the day. + * `costingOptions` carries the rest of how that day was weighed, such as a hike's limit + * and preferences (RTE-02), so a section of a tour is offered ways under its own rules. */ export async function valhallaAlternates( from: Waypoint, @@ -468,10 +470,11 @@ export async function valhallaAlternates( avoid: readonly AvoidClass[], count: number, signal?: AbortSignal, + costingOptions: Readonly = {}, ): Promise { const base = valhallaBase() if (!base || notValhalla.has(base)) return null - const body = { ...routeBody([from, to], profile, avoid), ...(count > 0 ? { alternates: count } : {}) } + const body = { ...routeBody([from, to], profile, avoid, costingOptions), ...(count > 0 ? { alternates: count } : {}) } return askWithRetry(base, async () => { const data = await postRoute(base, body, signal) return data === null ? null : alternatesFrom(data) diff --git a/client/src/components/Tours/planner/LegVariantsCard.test.tsx b/client/src/components/Tours/planner/LegVariantsCard.test.tsx new file mode 100644 index 000000000..c056884de --- /dev/null +++ b/client/src/components/Tours/planner/LegVariantsCard.test.tsx @@ -0,0 +1,173 @@ +// FE-TOUR-LEG-VARIANTS-CARD-001 to FE-TOUR-LEG-VARIANTS-CARD-007: the card that compares the ways +// of a hike's section over the planner map (RTE-02). +import userEvent from '@testing-library/user-event'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { fireEvent, render, screen, within } from '../../../../tests/helpers/render'; +import { seedStore } from '../../../../tests/helpers/store'; +import { idleLegVariants } from '../../../../tests/helpers/tourPlannerStub'; +import { useSettingsStore } from '../../../store/settingsStore'; +import LegVariantsOverlay, { LegVariantsCard } from './LegVariantsCard'; +import type { LegVariant } from './legVariants'; +import type { LegComparison } from './useLegVariants'; +import type { TourPlannerController } from './useTourPlanner'; + +const POINTS = [ + { id: 'a', lat: 47, lng: 11, role: 'start' as const }, + { id: 's', lat: 47.001, lng: 11.01, role: 'via' as const, shaping: true }, + { id: 'b', lat: 47, lng: 11.02, role: 'end' as const }, +]; + +const way = (key: LegVariant['key'], overrides: Partial = {}): LegVariant => ({ + key, + current: key === 'current', + direct: false, + coordinates: [ + [47, 11], + [47, 11.02], + ], + distanceM: 1500, + gainM: 120, + lossM: 40, + minutes: 35, + heights: 'known', + ...overrides, +}); + +function comparison(overrides: Partial = {}): LegComparison { + return { + leg: { + fromId: 'a', + toId: 's', + from: { lat: 47, lng: 11 }, + to: { lat: 47.001, lng: 11.01 }, + costing: { max_hiking_difficulty: 2 }, + key: 'a>s', + }, + phase: 'choose', + variants: [way('current'), way('a', { distanceM: 1900, gainM: 180, minutes: 47 })], + selected: null, + proving: false, + notice: null, + ...overrides, + }; +} + +type CardPlanner = Pick; + +function planner(open: LegComparison | null, overrides: Partial = {}): CardPlanner { + return { + waypoints: POINTS, + legVariants: { ...idleLegVariants(), comparison: open }, + canEdit: true, + isSaving: false, + ...overrides, + } as unknown as CardPlanner; +} + +function renderCard(open: LegComparison, overrides: Partial = {}) { + const controller = planner(open, overrides); + render(); + return controller.legVariants; +} + +beforeEach(() => { + seedStore(useSettingsStore, { settings: { distance_unit: 'metric' } }); +}); + +describe('LegVariantsCard', () => { + it('FE-TOUR-LEG-VARIANTS-CARD-001: names the section by its ends and lists each way with its figures', () => { + renderCard(comparison()); + const card = screen.getByTestId('leg-variants-card'); + expect(within(card).getByRole('heading', { name: 'Other ways for this section' })).toBeInTheDocument(); + expect(within(card).getByLabelText('Section from Waypoint 1 to Shaping point')).toBeInTheDocument(); + const [current, other] = within(card).getAllByRole('radio'); + expect(current).toHaveAccessibleName('Current way'); + expect(other).toHaveAccessibleName('Way A'); + expect(within(current).getByText('1.5 km')).toBeInTheDocument(); + expect(within(current).getByText('120 m')).toBeInTheDocument(); + expect(within(current).getByText('40 m')).toBeInTheDocument(); + // How the other way differs: longer, more climb, more time; the way taken now has no difference to show. + const delta = within(other).getByTestId('leg-variant-delta'); + expect(delta).toHaveTextContent('+400 m'); + expect(delta).toHaveTextContent('+60 m'); + expect(delta).toHaveTextContent('+12 min'); + expect(within(current).queryByTestId('leg-variant-delta')).not.toBeInTheDocument(); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-002: heights still asked for turn, heights not to be had stay unknown, never a guess', () => { + renderCard( + comparison({ + variants: [ + way('current', { gainM: null, lossM: null, minutes: null, heights: 'unknown' }), + way('a', { gainM: null, lossM: null, minutes: null, heights: 'pending', distanceM: 1400 }), + ], + }) + ); + const [current, other] = screen.getAllByRole('radio'); + expect(within(current).getAllByText('Unknown')).toHaveLength(3); + expect(within(other).getAllByLabelText('Heights are being looked up')).toHaveLength(3); + // Only the length is known on both, so only it is compared. + expect(within(other).getByTestId('leg-variant-delta')).toHaveTextContent('−100 m'); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-003: choosing marks a way; taking it is a separate, explicit step', async () => { + const actions = renderCard(comparison()); + const apply = screen.getByTestId('leg-variants-apply'); + expect(apply).toBeDisabled(); + await userEvent.click(screen.getByRole('radio', { name: 'Way A' })); + expect(actions.select).toHaveBeenCalledWith('a'); + expect(actions.apply).not.toHaveBeenCalled(); + fireEvent.mouseEnter(screen.getByRole('radio', { name: 'Way A' })); + expect(actions.hover).toHaveBeenLastCalledWith('a'); + fireEvent.mouseLeave(screen.getByRole('radio', { name: 'Way A' })); + expect(actions.hover).toHaveBeenLastCalledWith(null); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-004: a chosen other way can be taken; the way taken now and a locked tour cannot', async () => { + const actions = renderCard(comparison({ selected: 'a' })); + expect(screen.getByRole('radio', { name: 'Way A' })).toHaveAttribute('aria-checked', 'true'); + await userEvent.click(screen.getByRole('button', { name: 'Take this way' })); + expect(actions.apply).toHaveBeenCalledTimes(1); + + renderCard(comparison({ selected: 'current' })); + expect(screen.getAllByTestId('leg-variants-apply').at(-1)).toBeDisabled(); + renderCard(comparison({ selected: 'a' }), { isSaving: true }); + expect(screen.getAllByTestId('leg-variants-apply').at(-1)).toBeDisabled(); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-005: while a choice is checked the list stands still and the button says what happens', async () => { + const actions = renderCard(comparison({ selected: 'a', proving: true })); + expect(screen.getByTestId('leg-variants-apply')).toHaveTextContent('Checking the way'); + expect(screen.getByTestId('leg-variants-apply')).toBeDisabled(); + await userEvent.click(screen.getByRole('radio', { name: 'Current way' })); + expect(actions.select).not.toHaveBeenCalled(); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-006: says what it is doing and why nothing changed', () => { + renderCard(comparison({ phase: 'loading', variants: [way('current')] })); + expect(screen.getByRole('status')).toHaveTextContent('Looking for other ways'); + renderCard(comparison({ phase: 'none', variants: [way('current')] })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The router knows no other way'); + renderCard(comparison({ phase: 'failed', variants: [way('current')] })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The router did not answer'); + renderCard(comparison({ notice: 'notHeld' })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The router does not keep to this way'); + renderCard(comparison({ notice: 'stale' })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('The tour changed in the meantime'); + renderCard(comparison({ notice: 'failed' })); + expect(screen.getAllByRole('status').at(-1)).toHaveTextContent('This way could not be checked'); + }); + + it('FE-TOUR-LEG-VARIANTS-CARD-007: the overlay shows only while a section is compared; cancel, the cross and Escape close it', async () => { + const { rerender } = render(); + expect(screen.queryByTestId('leg-variants-overlay')).not.toBeInTheDocument(); + + const open = planner(comparison()); + rerender(); + expect(screen.getByTestId('leg-variants-overlay').style.bottom).toBe('66px'); + await userEvent.click(screen.getByRole('button', { name: 'Cancel' })); + await userEvent.click(screen.getByRole('button', { name: 'Close' })); + fireEvent.keyDown(document, { key: 'Escape' }); + expect(open.legVariants.close).toHaveBeenCalledTimes(3); + }); +}); diff --git a/client/src/components/Tours/planner/LegVariantsCard.tsx b/client/src/components/Tours/planner/LegVariantsCard.tsx new file mode 100644 index 000000000..c688ea449 --- /dev/null +++ b/client/src/components/Tours/planner/LegVariantsCard.tsx @@ -0,0 +1,402 @@ +import { + AlertTriangle, + ArrowRight, + Check, + Clock, + Loader2, + MoveHorizontal, + Route, + Shuffle, + TrendingDown, + TrendingUp, + X, +} from 'lucide-react'; +import { useEffect, type ReactNode } from 'react'; +import { useTranslation } from '../../../i18n'; +import { useSettingsStore } from '../../../store/settingsStore'; +import type { DistanceUnit } from '../../../types'; +import { formatDistance, formatElevation } from '../../../utils/units'; +import { MAP_CARD_GLASS } from '../../Map/mapControlShadow'; +import { SoftPill, WhiteBadge } from '../../Planner/planParts'; +import { fs } from '../../shared/DialogShell'; +import { Tooltip } from '../../shared/Tooltip'; +import { TourNotice } from '../tourParts'; +import { variantPaintCss } from './legVariantPaint'; +import { variantDelta, type LegVariant } from './legVariants'; +import { formatTourDuration } from './tourDurationInput'; +import { stopNumbersOf } from './tourPlannerWaypoints'; +import type { LegComparison } from './useLegVariants'; +import type { TourPlannerController } from './useTourPlanner'; + +type Translate = ReturnType['t']; + +/** A signed difference: a plus for more, a minus sign for fewer, nothing for none worth printing. */ +function signed(value: number, format: (absolute: number) => string): string | null { + const text = format(Math.abs(value)); + if (Math.abs(value) < 1e-9 || /^0\s/.test(text)) return null; + return `${value > 0 ? '+' : '−'}${text}`; +} + +/** An end of the section by name: the stop by its number, else the shaping point it is. */ +function endName(stop: number | undefined, t: Translate): string { + return stop === undefined ? t('tours.planner.shapingPoint') : t('tours.planner.waypointLabel', { n: stop }); +} + +/** + * One end of the section, as the waypoint list numbers it: a stop by its + * number, a shaping point as the small dot it is on the map. + */ +function LegEnd({ stop }: { stop: number | undefined }) { + if (stop === undefined) { + return ( +