mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 01:12:16 +00:00
fix(tours): match a saved ski route by its ski choice, and keep the planner under the lint limits
The saved ski route compared against an undefined skiKey, so opening a saved ski tour threw. The held seed now lives in useSavedSkiSeed and is matched by skiRouteKey(skiChoice). The ski route facts move to skiRouteFacts.ts so the summary and leg rows only export components, the rail renders SkiPlannerSummary, and openTour, useTourRoute and useAddonManager shed the branches and lines the new ski code had added.
This commit is contained in:
@@ -1,78 +1,16 @@
|
||||
import {
|
||||
SKI_DIFFICULTIES,
|
||||
skiLiftClassSchema,
|
||||
type SkiColourScheme,
|
||||
type SkiDifficulty,
|
||||
type SkiRouteOkLeg,
|
||||
type SkiRouteOptionsInput,
|
||||
type SkiRouteSegment,
|
||||
} from '@trek/shared';
|
||||
import type { SkiColourScheme, SkiRouteOkLeg, SkiRouteSegment } from '@trek/shared';
|
||||
import { AlertTriangle, CableCar, Footprints, MountainSnow, Spline, Sun } from 'lucide-react';
|
||||
import { useTranslation } from '../../../i18n';
|
||||
import { fs } from '../../shared/DialogShell';
|
||||
import type { SkiLegProblem } from '../planner/skiRouting';
|
||||
import { formatTourDuration } from '../planner/tourDurationInput';
|
||||
import type { SkiChoice } from './skiChoice';
|
||||
import { skiFixesOf } from './skiRouteFacts';
|
||||
import { skiPaintOf } from './skiRouteStyle';
|
||||
|
||||
const ACTION =
|
||||
'inline-flex h-[22px] flex-none items-center gap-1 rounded-full bg-surface-card px-2 font-semibold text-content shadow-sm ring-1 ring-inset ring-edge-faint transition-colors hover:bg-surface-hover disabled:opacity-40';
|
||||
|
||||
/** One change a failed leg's hint suggests, as a change of the ski choice. */
|
||||
interface SkiFix {
|
||||
labelKey: string;
|
||||
apply: (choice: SkiChoice) => SkiChoice;
|
||||
}
|
||||
|
||||
const withOptions = (choice: SkiChoice, patch: Partial<SkiRouteOptionsInput>): SkiChoice => ({
|
||||
...choice,
|
||||
options: { ...choice.options, ...patch },
|
||||
});
|
||||
|
||||
/** The fixes a failed leg's hint offers, each a change of the ski choice that would plan it. */
|
||||
export function skiFixesOf(problem: SkiLegProblem): SkiFix[] {
|
||||
const hint = problem.hint;
|
||||
if (!hint) return [];
|
||||
const fixes: SkiFix[] = [];
|
||||
const level = hint.raise_difficulty;
|
||||
if (level) {
|
||||
const graded = (SKI_DIFFICULTIES as readonly string[]).includes(level);
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.raiseDifficulty',
|
||||
apply: (choice) =>
|
||||
withOptions(choice, graded ? { max_difficulty: level as SkiDifficulty } : { allow_freeride: true }),
|
||||
});
|
||||
}
|
||||
const lift = skiLiftClassSchema.safeParse(hint.allow_lift);
|
||||
if (lift.success) {
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.allowLift',
|
||||
apply: (choice) =>
|
||||
withOptions(choice, { avoid_lifts: (choice.options?.avoid_lifts ?? []).filter((c) => c !== lift.data) }),
|
||||
});
|
||||
}
|
||||
if (hint.allow_lift_down) {
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.liftDown',
|
||||
apply: (choice) => withOptions(choice, { allow_lift_down: true }),
|
||||
});
|
||||
}
|
||||
const technique = hint.technique;
|
||||
if (technique) fixes.push({ labelKey: 'tours.ski.fix.technique', apply: (choice) => ({ ...choice, technique }) });
|
||||
const discipline = hint.discipline;
|
||||
if (discipline) {
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.discipline',
|
||||
apply: (choice) => ({
|
||||
...choice,
|
||||
discipline,
|
||||
technique: discipline === 'cross_country' ? choice.technique : null,
|
||||
}),
|
||||
});
|
||||
}
|
||||
return fixes;
|
||||
}
|
||||
|
||||
/**
|
||||
* A ski leg the piste router could not plan: why, the run or lift it is about,
|
||||
* and the change that would plan it, applied with one press. A snowshoer's or
|
||||
@@ -176,7 +114,7 @@ export function SkiLegSteps({ leg, scheme }: { leg: SkiRouteOkLeg; scheme: SkiCo
|
||||
{steps.map((step, at) => (
|
||||
<li
|
||||
key={`${step.from}-${at}`}
|
||||
className="inline-flex min-w-0 max-w-full items-center gap-1 rounded-full bg-surface-secondary py-0.5 ps-1.5 pe-2 text-content-secondary"
|
||||
className="inline-flex min-w-0 max-w-full items-center gap-1 rounded-full bg-surface-secondary py-0.5 pe-2 ps-1.5 text-content-secondary"
|
||||
style={fs(10.5, 'caption')}
|
||||
>
|
||||
<span
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import type { SkiFlag, SkiGeometryPoint, SkiRouteOkLeg, SkiSegmentKind, SkiWhy } from '@trek/shared';
|
||||
import { Info, MountainSnow } from 'lucide-react';
|
||||
import type { ReactNode } from 'react';
|
||||
import { useTranslation } from '../../../i18n';
|
||||
@@ -8,71 +7,7 @@ import { fs } from '../../shared/DialogShell';
|
||||
import type { SkiTourRoute } from '../planner/skiRouting';
|
||||
import { formatTourDuration } from '../planner/tourDurationInput';
|
||||
import { RailCard, RailFacts } from '../tourParts';
|
||||
|
||||
/** Flags that say a stretch rests on a guess of the map data, not on what is mapped. */
|
||||
const GUESSED: ReadonlySet<SkiFlag> = new Set([
|
||||
'inferred_link',
|
||||
'inferred_crossing',
|
||||
'dir_from_dem',
|
||||
'technique_assumed',
|
||||
'area_derived',
|
||||
'difficulty_unknown',
|
||||
]);
|
||||
|
||||
/** Stretches ridden on a lift, a funicular or rack railway, or a gondola down. */
|
||||
const RIDES: ReadonlySet<SkiSegmentKind> = new Set(['lift', 'rail', 'lift_down']);
|
||||
|
||||
type Seconds = SkiRouteOkLeg['seconds'];
|
||||
type Avoided = Extract<SkiWhy, { kind: 'avoided_difficulty' }>;
|
||||
|
||||
/** The time of every planned leg, by what it is spent on. */
|
||||
export function skiSecondsOf(route: SkiTourRoute): Seconds {
|
||||
const total: Seconds = { ski: 0, lift: 0, wait: 0, walk: 0 };
|
||||
for (const leg of route.legs) {
|
||||
if (leg?.status !== 'ok') continue;
|
||||
total.ski += leg.seconds.ski;
|
||||
total.lift += leg.seconds.lift;
|
||||
total.wait += leg.seconds.wait;
|
||||
total.walk += leg.seconds.walk;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/** The height lost along a stretch of a leg's line, counted where its points have a height. */
|
||||
function descentAlong(geometry: readonly SkiGeometryPoint[], from: number, to: number): number {
|
||||
let down = 0;
|
||||
let last: number | null = null;
|
||||
for (const [, , height] of geometry.slice(from, to + 1)) {
|
||||
if (height === null) continue;
|
||||
if (last !== null && height < last) down += last - height;
|
||||
last = height;
|
||||
}
|
||||
return down;
|
||||
}
|
||||
|
||||
/** The metres of runs, the height skied down on them, and the lift rides of every planned leg. */
|
||||
export function skiTallyOf(route: SkiTourRoute): { runsM: number; downM: number; rides: number } {
|
||||
const tally = { runsM: 0, downM: 0, rides: 0 };
|
||||
for (const leg of route.legs) {
|
||||
if (leg?.status !== 'ok') continue;
|
||||
for (const segment of leg.segments) {
|
||||
if (RIDES.has(segment.kind)) tally.rides += 1;
|
||||
if (segment.kind !== 'run') continue;
|
||||
tally.runsM += segment.length_m;
|
||||
tally.downM += descentAlong(leg.geometry, segment.from, segment.to);
|
||||
}
|
||||
}
|
||||
return tally;
|
||||
}
|
||||
|
||||
/** The harder runs the route kept clear of, and whether any stretch rests on a guess. */
|
||||
function skiWhyOf(route: SkiTourRoute): { avoided: Avoided[]; guessed: boolean } {
|
||||
const why = route.legs.flatMap((leg) => (leg?.status === 'ok' ? leg.why : []));
|
||||
return {
|
||||
avoided: why.flatMap((entry) => (entry.kind === 'avoided_difficulty' ? [entry] : [])),
|
||||
guessed: why.some((entry) => entry.kind === 'flag' && GUESSED.has(entry.flag)),
|
||||
};
|
||||
}
|
||||
import { skiSecondsOf, skiTallyOf, skiWhyOf, type SkiAvoided } from './skiRouteFacts';
|
||||
|
||||
const minutes = (seconds: number) => formatTourDuration(Math.round(seconds / 60));
|
||||
|
||||
@@ -132,7 +67,7 @@ export function SkiRouteSummary({ route }: { route: SkiTourRoute | null }) {
|
||||
const seconds = skiSecondsOf(route);
|
||||
const tally = skiTallyOf(route);
|
||||
const { avoided, guessed } = skiWhyOf(route);
|
||||
const level = (entry: Avoided) => t(`tours.ski.difficulty.${entry.level}`);
|
||||
const level = (entry: SkiAvoided) => t(`tours.ski.difficulty.${entry.level}`);
|
||||
const facts = [
|
||||
{ label: t('tours.ski.summary.ski'), value: minutes(seconds.ski) },
|
||||
{ label: t('tours.ski.summary.lift'), value: minutes(seconds.lift) },
|
||||
@@ -169,3 +104,8 @@ export function SkiRouteSummary({ route }: { route: SkiTourRoute | null }) {
|
||||
</SummaryCard>
|
||||
);
|
||||
}
|
||||
|
||||
/** SKI-02: the summary in the planner's rail, for a ski tour only. */
|
||||
export function SkiPlannerSummary({ planner }: { planner: { isSki: boolean; skiRoute: SkiTourRoute | null } }) {
|
||||
return planner.isSki ? <SkiRouteSummary route={planner.skiRoute} /> : null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import {
|
||||
SKI_DIFFICULTIES,
|
||||
skiLiftClassSchema,
|
||||
type SkiDifficulty,
|
||||
type SkiFlag,
|
||||
type SkiGeometryPoint,
|
||||
type SkiRouteOkLeg,
|
||||
type SkiRouteOptionsInput,
|
||||
type SkiSegmentKind,
|
||||
type SkiWhy,
|
||||
} from '@trek/shared';
|
||||
import type { SkiLegProblem, SkiTourRoute } from '../planner/skiRouting';
|
||||
import type { SkiChoice } from './skiChoice';
|
||||
|
||||
/** Flags that say a stretch rests on a guess of the map data, not on what is mapped. */
|
||||
const GUESSED: ReadonlySet<SkiFlag> = new Set([
|
||||
'inferred_link',
|
||||
'inferred_crossing',
|
||||
'dir_from_dem',
|
||||
'technique_assumed',
|
||||
'area_derived',
|
||||
'difficulty_unknown',
|
||||
]);
|
||||
|
||||
/** Stretches ridden on a lift, a funicular or rack railway, or a gondola down. */
|
||||
const RIDES: ReadonlySet<SkiSegmentKind> = new Set(['lift', 'rail', 'lift_down']);
|
||||
|
||||
type Seconds = SkiRouteOkLeg['seconds'];
|
||||
|
||||
/** A harder run the piste router kept the route clear of. */
|
||||
export type SkiAvoided = Extract<SkiWhy, { kind: 'avoided_difficulty' }>;
|
||||
|
||||
/** The time of every planned leg, by what it is spent on. */
|
||||
export function skiSecondsOf(route: SkiTourRoute): Seconds {
|
||||
const total: Seconds = { ski: 0, lift: 0, wait: 0, walk: 0 };
|
||||
for (const leg of route.legs) {
|
||||
if (leg?.status !== 'ok') continue;
|
||||
total.ski += leg.seconds.ski;
|
||||
total.lift += leg.seconds.lift;
|
||||
total.wait += leg.seconds.wait;
|
||||
total.walk += leg.seconds.walk;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/** The height lost along a stretch of a leg's line, counted where its points have a height. */
|
||||
function descentAlong(geometry: readonly SkiGeometryPoint[], from: number, to: number): number {
|
||||
let down = 0;
|
||||
let last: number | null = null;
|
||||
for (const [, , height] of geometry.slice(from, to + 1)) {
|
||||
if (height === null) continue;
|
||||
if (last !== null && height < last) down += last - height;
|
||||
last = height;
|
||||
}
|
||||
return down;
|
||||
}
|
||||
|
||||
/** The metres of runs, the height skied down on them, and the lift rides of every planned leg. */
|
||||
export function skiTallyOf(route: SkiTourRoute): { runsM: number; downM: number; rides: number } {
|
||||
const tally = { runsM: 0, downM: 0, rides: 0 };
|
||||
for (const leg of route.legs) {
|
||||
if (leg?.status !== 'ok') continue;
|
||||
for (const segment of leg.segments) {
|
||||
if (RIDES.has(segment.kind)) tally.rides += 1;
|
||||
if (segment.kind !== 'run') continue;
|
||||
tally.runsM += segment.length_m;
|
||||
tally.downM += descentAlong(leg.geometry, segment.from, segment.to);
|
||||
}
|
||||
}
|
||||
return tally;
|
||||
}
|
||||
|
||||
/** The harder runs the route kept clear of, and whether any stretch rests on a guess. */
|
||||
export function skiWhyOf(route: SkiTourRoute): { avoided: SkiAvoided[]; guessed: boolean } {
|
||||
const why = route.legs.flatMap((leg) => (leg?.status === 'ok' ? leg.why : []));
|
||||
return {
|
||||
avoided: why.flatMap((entry) => (entry.kind === 'avoided_difficulty' ? [entry] : [])),
|
||||
guessed: why.some((entry) => entry.kind === 'flag' && GUESSED.has(entry.flag)),
|
||||
};
|
||||
}
|
||||
|
||||
/** One change a failed leg's hint suggests, as a change of the ski choice. */
|
||||
interface SkiFix {
|
||||
labelKey: string;
|
||||
apply: (choice: SkiChoice) => SkiChoice;
|
||||
}
|
||||
|
||||
const withOptions = (choice: SkiChoice, patch: Partial<SkiRouteOptionsInput>): SkiChoice => ({
|
||||
...choice,
|
||||
options: { ...choice.options, ...patch },
|
||||
});
|
||||
|
||||
/** The fixes a failed leg's hint offers, each a change of the ski choice that would plan it. */
|
||||
export function skiFixesOf(problem: SkiLegProblem): SkiFix[] {
|
||||
const hint = problem.hint;
|
||||
if (!hint) return [];
|
||||
const fixes: SkiFix[] = [];
|
||||
const level = hint.raise_difficulty;
|
||||
if (level) {
|
||||
const graded = (SKI_DIFFICULTIES as readonly string[]).includes(level);
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.raiseDifficulty',
|
||||
apply: (choice) =>
|
||||
withOptions(choice, graded ? { max_difficulty: level as SkiDifficulty } : { allow_freeride: true }),
|
||||
});
|
||||
}
|
||||
const lift = skiLiftClassSchema.safeParse(hint.allow_lift);
|
||||
if (lift.success) {
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.allowLift',
|
||||
apply: (choice) =>
|
||||
withOptions(choice, { avoid_lifts: (choice.options?.avoid_lifts ?? []).filter((c) => c !== lift.data) }),
|
||||
});
|
||||
}
|
||||
if (hint.allow_lift_down) {
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.liftDown',
|
||||
apply: (choice) => withOptions(choice, { allow_lift_down: true }),
|
||||
});
|
||||
}
|
||||
const technique = hint.technique;
|
||||
if (technique) fixes.push({ labelKey: 'tours.ski.fix.technique', apply: (choice) => ({ ...choice, technique }) });
|
||||
const discipline = hint.discipline;
|
||||
if (discipline) {
|
||||
fixes.push({
|
||||
labelKey: 'tours.ski.fix.discipline',
|
||||
apply: (choice) => ({
|
||||
...choice,
|
||||
discipline,
|
||||
technique: discipline === 'cross_country' ? choice.technique : null,
|
||||
}),
|
||||
});
|
||||
}
|
||||
return fixes;
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
import type { SkiLiftClass, SkiRouteOkLeg, SkiRouteSegment } from '@trek/shared';
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
import type { SkiLiftClass, SkiRouteOkLeg, SkiRouteSegment } from '@trek/shared';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import type { SkiLegProblem, SkiTourRoute } from '../planner/skiRouting';
|
||||
import { SkiLegFailureRow, SkiLegSteps, skiFixesOf } from './SkiLegRows';
|
||||
import { SkiLegFailureRow, SkiLegSteps } from './SkiLegRows';
|
||||
import { skiFixesOf, skiSecondsOf, skiTallyOf } from './skiRouteFacts';
|
||||
import { SkiRouteOptions } from './SkiRouteOptions';
|
||||
import { skiPaintOf } from './skiRouteStyle';
|
||||
import { SkiRouteSummary, skiSecondsOf, skiTallyOf } from './SkiRouteSummary';
|
||||
import { SkiPlannerSummary, SkiRouteSummary } from './SkiRouteSummary';
|
||||
|
||||
// FE-TOUR-SKI-UI-001 to FE-TOUR-SKI-UI-012
|
||||
// FE-TOUR-SKI-UI-001 to FE-TOUR-SKI-UI-013
|
||||
|
||||
function segment(kind: SkiRouteSegment['kind'], extra: Partial<SkiRouteSegment> = {}): SkiRouteSegment {
|
||||
return {
|
||||
@@ -259,6 +260,13 @@ describe('SkiRouteSummary (SKI-02)', () => {
|
||||
render(<SkiRouteSummary route={route({ saved: true, legs: [], build: null })} />);
|
||||
expect(screen.getByTestId('ski-route-summary')).not.toHaveTextContent('tours.ski.summary.build');
|
||||
});
|
||||
|
||||
it('FE-TOUR-SKI-UI-013: the planner rail shows the summary for a ski tour only', () => {
|
||||
const planned = route({ legs: [okLeg()] });
|
||||
expect(render(<SkiPlannerSummary planner={{ isSki: false, skiRoute: planned }} />).container).toBeEmptyDOMElement();
|
||||
render(<SkiPlannerSummary planner={{ isSki: true, skiRoute: planned }} />);
|
||||
expect(screen.getByTestId('ski-route-summary')).toHaveTextContent('tours.ski.summary.ski');
|
||||
});
|
||||
});
|
||||
|
||||
describe('skiPaintOf (SKI-02)', () => {
|
||||
|
||||
Reference in New Issue
Block a user