fix(tours): a city walk saved offline shows its stays and total at once, and the planner plans the total its overview shows (CW-02)

A city walk created or changed while offline now carries its stops, the
minutes its stays are entered with and how many have none yet, worked out
from its points the way the server will answer them. The day row, the tour
detail and the summary show the walking time with the stays as soon as the
walk is saved, with "at least" where a stay is missing, and a stay changed
while the write waits shows its new total rather than the one of the last
answer. The server and the client count the stops in one shared place.

The planner plans a walk whose walking time is not known yet with at least
the stays entered, as its overview says, so the planned field and the check
against the longest plan read the same total.

In the tour detail a longer time such as an "at least" total wraps on a
phone instead of being cut, and the boxes of a row keep one height. A total
entered by hand falls back to its short name where its column is too narrow.
This commit is contained in:
Maurice
2026-10-20 01:53:24 +00:00
parent 188953158a
commit c836ee3988
14 changed files with 196 additions and 44 deletions
@@ -1,6 +1,6 @@
import { useState } from 'react'
import { fireEvent, render, screen, waitFor } from '../../../tests/helpers/render'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { TourListItem } from '@trek/shared'
import { buildPlace } from '../../../tests/helpers/factories'
import { paddleBench } from '../../../tests/helpers/paddleCheck'
@@ -61,6 +61,11 @@ beforeEach(() => {
seedStore(useSettingsStore, { settings: { distance_unit: 'metric' } })
})
// A repository answer one test stubs never reaches the tests after it.
afterEach(() => {
vi.restoreAllMocks()
})
describe('TourDetailDialog', () => {
it('keeps desktop detail non-modal and returns focus to its Tour row on Escape', async () => {
const opener = document.createElement('button')
@@ -159,6 +164,11 @@ describe('TourDetailDialog', () => {
view.rerender(<TourDetailDialog {...defaultProps} tour={{ ...walk, station_stay_minutes: 90, station_stays_unknown: 1 }} />)
expect(screen.getByLabelText('Walking: 45 min, Stays: at least 1 h 30 min, Planned: at least 2 h 15 min'))
.toHaveTextContent('≥ 2 h 15 min')
// On a phone's three narrow columns the longer values wrap rather than being cut.
const stays = screen.getByText('Stays').closest('dt')?.nextElementSibling
expect(stays).toHaveTextContent('at least 1 h 30 min')
expect(stays).toHaveClass('break-words')
expect(stays).not.toHaveClass('truncate')
view.rerender(<TourDetailDialog {...defaultProps}
tour={{ ...walk, station_stay_minutes: 90, station_stays_unknown: 1, planned_duration_minutes: 240 }} />)
@@ -13,6 +13,7 @@ import {
} from '@trek/shared';
import type { RouteGeometryAnalysis } from '../../../utils/routeGeometry';
import { hikingTimeMinutes } from '../hikingTime';
import { walkTimesOf, type WalkTimes } from './stationModel';
/** The editable fields of a saved tour as the planner opens them, each with its empty value. */
export function openedTourFields(tour: TourListItem) {
@@ -52,8 +53,11 @@ export interface TourPlannerFactsInput {
/** A paddle tour's estimate on the water; null for a hike. */
paddleEstimate: { waterMinutes: number | null; estimateMinutes: number | null } | null;
routeAnalysis: RouteGeometryAnalysis | null;
/** CW-02: a city walk's stays, summed as far as they are known; null for any other type, which takes its breaks. */
stationStayMinutes?: number | null;
/**
* CW-02: a city walk's stays as far as known, and of how many stations; null
* for any other type, which takes its breaks.
*/
stays?: Pick<WalkTimes, 'stayMinutes' | 'unknownStays' | 'stations'> | null;
/** The router's own time, seconds: a bike tour's riding time; stands in for a hike until the heights are in. */
durationSeconds: number | null;
}
@@ -78,6 +82,16 @@ function movingMinutes(input: TourPlannerFactsInput, routerMinutes: number | nul
: routerMinutes;
}
/**
* CW-02: a city walk's times, the total its overview shows. Its stops are its
* stays and it has no breaks on top, so nothing counts twice; the stays entered
* count even while the walking time is not known. Null for any other type.
*/
function walkTimesFor(input: TourPlannerFactsInput, walkingMinutes: number | null): WalkTimes | null {
if (input.tourType !== 'walk') return null;
return walkTimesOf({ stayMinutes: 0, unknownStays: 0, stations: 0, ...input.stays, walkingMinutes });
}
/** What the type adds to a save: a hike its routing limit, a bike tour its profile, a paddle tour its boat, water and speed. */
function typeFieldsOf(input: TourPlannerFactsInput) {
const isBike = input.tourType === 'bike';
@@ -118,9 +132,9 @@ export function tourPlannerFacts(input: TourPlannerFactsInput) {
const { durationSeconds, breakAdditionalMinutes, plannedDurationMinutes } = input;
const routerMinutes = durationSeconds === null ? null : Math.round(durationSeconds / 60);
const walkingDurationMinutes = movingMinutes(input, routerMinutes);
// A city walk's stops are its stays; it has no breaks on top, so nothing counts twice.
const onTop = input.tourType === 'walk' ? (input.stationStayMinutes ?? 0) : (breakAdditionalMinutes ?? 0);
const automaticPlannedTotalMinutes = walkingDurationMinutes === null ? null : walkingDurationMinutes + onTop;
const walk = walkTimesFor(input, walkingDurationMinutes);
const withBreaks = walkingDurationMinutes === null ? null : walkingDurationMinutes + (breakAdditionalMinutes ?? 0);
const automaticPlannedTotalMinutes = walk ? walk.totalMinutes : withBreaks;
const { boatType, waterContext } = input.paddle;
return {
isPaddle,
@@ -130,6 +144,8 @@ export function tourPlannerFacts(input: TourPlannerFactsInput) {
breakAdditionalInvalid: input.tourType !== 'walk' && invalidMinutes(breakAdditionalMinutes),
paddleIncomplete: isPaddle && (boatType === null || waterContext === null),
walkingDurationMinutes,
/** CW-02: a city walk's walking time, stays and total; null for any other type. */
stationStays: walk,
automaticPlannedTotalMinutes,
plannedTotalMinutes: plannedDurationMinutes ?? automaticPlannedTotalMinutes,
plannedTotalInvalid:
@@ -96,7 +96,7 @@ describe('tourPlannerFacts', () => {
routeAnalysis: analysis,
durationSeconds: 3_000,
breakAdditionalMinutes: 40,
stationStayMinutes: 75,
stays: { stayMinutes: 75, unknownStays: 0, stations: 2 },
hikingPreferences: { avoid_steps: true },
});
const facts = tourPlannerFacts(walk);
@@ -111,10 +111,34 @@ describe('tourPlannerFacts', () => {
bike_profile: null,
boat_type: null,
});
expect(facts.stationStays).toMatchObject({
walkingMinutes: 50,
stayMinutes: 75,
totalMinutes: 125,
partial: false,
});
// Stays nobody entered count for nothing; a total typed by hand still wins.
expect(tourPlannerFacts({ ...walk, stationStayMinutes: null }).automaticPlannedTotalMinutes).toBe(50);
expect(tourPlannerFacts({ ...walk, stays: null }).automaticPlannedTotalMinutes).toBe(50);
expect(tourPlannerFacts({ ...walk, plannedDurationMinutes: 180 }).plannedTotalMinutes).toBe(180);
expect(tourPlannerFacts({ ...walk, durationSeconds: null }).automaticPlannedTotalMinutes).toBeNull();
expect(tourPlannerFacts({ ...walk, stays: null, durationSeconds: null }).automaticPlannedTotalMinutes).toBeNull();
});
it('CW-02: a walk whose walking time is not known plans at least the stays entered, as its overview says', () => {
const walk = hike({
tourType: 'walk',
durationSeconds: null,
stays: { stayMinutes: 120, unknownStays: 1, stations: 3 },
});
const facts = tourPlannerFacts(walk);
expect(facts.walkingDurationMinutes).toBeNull();
expect(facts.plannedTotalMinutes).toBe(120);
expect(facts.stationStays).toMatchObject({ walkingMinutes: null, totalMinutes: 120, partial: true });
expect(facts.plannedTotalInvalid).toBe(false);
// Stays past the longest plan a tour may have hold the save up as the walking time would.
expect(
tourPlannerFacts({ ...walk, stays: { stayMinutes: 1_500, unknownStays: 0, stations: 2 } }).plannedTotalInvalid
).toBe(true);
expect(tourPlannerFacts(hike({ durationSeconds: 600 })).stationStays).toBeNull();
});
});
@@ -5,7 +5,7 @@ import { tourRepo } from '../../../repo/tourRepo'
import { randomId } from '../../../utils/randomId'
import { analyzeRouteGeometry, type RouteProfileFocus } from '../../../utils/routeGeometry'
import { closeLoop, reverseRoute, toggleFreeLeg, withPointAppended } from './tourRouteActions'
import { walkTimes, walkTimesOf } from './stationModel'
import { walkTimes } from './stationModel'
import { hasCompleteElevation, useTourRoute } from './useTourRoute'
import { normalizePlannerWaypoints, plannerWaypointsOf, ridablePoints, routeInputsKey, sameWaypointSnapshot, savedWaypointsOf, unlinkedPoint, withSavedKeys, type TourPlannerWaypoint } from './tourPlannerWaypoints'
import { openedTourFields, tourPlannerFacts } from './tourPlannerFacts'
@@ -499,13 +499,12 @@ export function useTourPlanner({ tripId, active = true, onSaved, canEdit: editPe
const facts = useMemo(() => tourPlannerFacts({
tourType, maxHikingDifficulty, hikingPreferences, bikeProfile, website, plannedDurationMinutes, breakAdditionalMinutes,
paddle: { boatType, waterContext, speedKmh, portageSpeedKmh, lakeStyle },
paddleSpeedInvalid, paddleEstimate, routeAnalysis, durationSeconds, stationStayMinutes: stays?.stayMinutes ?? null,
paddleSpeedInvalid, paddleEstimate, routeAnalysis, durationSeconds, stays,
}), [bikeProfile, boatType, breakAdditionalMinutes, durationSeconds, hikingPreferences, lakeStyle, maxHikingDifficulty, paddleEstimate, paddleSpeedInvalid,
plannedDurationMinutes, portageSpeedKmh, routeAnalysis, speedKmh, stays, tourType, waterContext, website])
// CW-02: a city walk's times as the overview says them, from the same derivation as its planned total.
const { websiteInvalid, plannedDurationInvalid, breakAdditionalInvalid, paddleIncomplete, walkingDurationMinutes,
plannedTotalMinutes, plannedTotalInvalid, savedDurationSeconds, savedBreakAdditionalMinutes, typeFields } = facts
// CW-02: and with the walking time, the walk's times as the overview says them.
const stationStays = useMemo(() => stays && walkTimesOf({ ...stays, walkingMinutes: walkingDurationMinutes }), [stays, walkingDurationMinutes])
plannedTotalMinutes, plannedTotalInvalid, savedDurationSeconds, savedBreakAdditionalMinutes, typeFields, stationStays } = facts
const returnToNeutral = useCallback(() => {
if (saveInFlightRef.current) return
+5 -3
View File
@@ -301,7 +301,9 @@ type FieldColumns = keyof typeof FIELD_COLUMNS
/**
* Labelled values in the booking-card look: a fixed number of columns where the
* caller wants one row per group, otherwise as many as fit.
* caller wants one row per group, otherwise as many as fit. A value too long for
* its column, such as a city walk's "at least" total on a phone, wraps rather
* than being cut, and the boxes of a row keep one height.
*/
export function TourFactFields({ fields, columns }: { fields: readonly TourMetricField[]; columns?: FieldColumns }) {
return (
@@ -309,7 +311,7 @@ export function TourFactFields({ fields, columns }: { fields: readonly TourMetri
className={`m-0 grid gap-2 ${columns ? FIELD_COLUMNS[columns] : 'grid-cols-[repeat(auto-fit,minmax(104px,1fr))]'}`}
>
{fields.map((field) => (
<div key={field.label} className="min-w-0">
<div key={field.label} className="flex min-w-0 flex-col">
<dt className="mb-[3px] flex min-w-0 items-center gap-0.5">
<Eyebrow className="relative min-w-0 truncate">
<FitLabel full={field.label} short={field.shortLabel} />
@@ -321,7 +323,7 @@ export function TourFactFields({ fields, columns }: { fields: readonly TourMetri
)}
</dt>
<dd
className={`${BOX} m-0 truncate px-[10px] py-[7px] text-center font-semibold tabular-nums text-content`}
className={`${BOX} m-0 flex flex-1 flex-col justify-center break-words px-[10px] py-[7px] text-center font-semibold tabular-nums text-content`}
style={fs(12.5, 'body')}
>
{field.value}
@@ -75,6 +75,15 @@ describe('walkTimeFields', () => {
'Computed',
'Not calculated',
]);
// A column too narrow for the full label names the manual total by what sets it apart.
const manual = walkTimeFields({
walking,
walkingMinutes: 45,
stays: { stayMinutes: 0, unknownStays: 0, stations: 2 },
manualMinutes: 240,
t,
})[2];
expect(manual).toMatchObject({ label: 'Planned (Manual override)', shortLabel: 'Manual override' });
});
it('keeps the walking label and its explanation as the planner hands them in', () => {
@@ -57,6 +57,8 @@ export function walkTimeFields({
...parts,
{
label: `${planned} (${t('tours.planner.plannedTotalManual')})`,
// A narrow column names it by what sets it apart from the computed total beside it.
shortLabel: t('tours.planner.plannedTotalManual'),
value: formatPlannedTourDuration(manualMinutes),
},
{ label: t('tours.walk.totalComputed'), value: computed },