mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
feat(tours): show the known surfaces and way types along a hike (MAP-04)
The hike's "SAC grades" card becomes "Trail details": one switch, then a segmented choice between SAC, Surface and Way type, three things kept apart and drawn along the route on the planner map one at a time. Every facet reads from the same answer: the ways under the line are matched on foot once, their OpenStreetMap values fetched once through the trail-grades read MAP-03 already uses, and each stretch is placed against the tour's own line once. Switching facets asks nothing new. The server now hands on a way's surface and highway tags as OpenStreetMap wrote them, only trimmed and bounded. The planner groups them (asphalt or concrete, paving stones, compacted, gravel, natural ground, grass, rock, wood or metal, unpaved without a kind, other; path, footway, steps, farm or forest track, cycle path, minor road, road, via ferrata, other), and each row's tooltip names the values recorded, such as surface=gravel, surface=pebblestone. The router's own surface reading is never used: it is normalised and a default where nothing is recorded. What is not known stays unknown and says why, in every facet: no value recorded, a value no group takes or two at once (unclear, never the nearest group), no way clearly under the tour's line, or a way that could not be asked about. Imported tracks and hand-drawn stretches keep their geometry; a matched way only counts where it keeps within 25 m of it. A summary badge gives the known share and the most common class; the SAC facet keeps its highest step and routing limit badges. Nothing about the tour, its places or its days changes, and no routing preference is set.
This commit is contained in:
@@ -51,6 +51,8 @@ const grade = (id: number, mtb: string | null, uphill: string | null = null): To
|
||||
mtb_scale_imba: null,
|
||||
sac_scale: null,
|
||||
sac_scale_unclear: null,
|
||||
surface: null,
|
||||
highway: null,
|
||||
});
|
||||
|
||||
function show(state: Partial<RouteWaysState>, overrides: Partial<TourPlannerController> = {}) {
|
||||
|
||||
@@ -76,7 +76,16 @@ describe('useRouteWays (BIK-01 to BIK-05, RUN-02)', () => {
|
||||
it('FE-TOUR-WAYS-HOOK-003: a mountain bike tour asks the grades of its ways, and keeps what could not be asked apart', async () => {
|
||||
gradesRepo.grades.mockResolvedValue({
|
||||
ways: [
|
||||
{ id: 11, mtb_scale: '2', mtb_scale_uphill: null, mtb_scale_imba: null, sac_scale: null, sac_scale_unclear: null },
|
||||
{
|
||||
id: 11,
|
||||
mtb_scale: '2',
|
||||
mtb_scale_uphill: null,
|
||||
mtb_scale_imba: null,
|
||||
sac_scale: null,
|
||||
sac_scale_unclear: null,
|
||||
surface: null,
|
||||
highway: null,
|
||||
},
|
||||
],
|
||||
unanswered: [12],
|
||||
});
|
||||
|
||||
@@ -22,7 +22,7 @@ export interface TrailGrades {
|
||||
export interface RouteWaysState {
|
||||
status: RouteWaysStatus;
|
||||
ways: RouteWays | null;
|
||||
/** BIK-05 and MAP-03, where asked for: the trail grades of its ways. */
|
||||
/** BIK-05, MAP-03 and MAP-04, where asked for: what OpenStreetMap records for its ways. */
|
||||
grades: TrailGrades | null;
|
||||
}
|
||||
|
||||
@@ -67,9 +67,10 @@ async function askRouteWays(
|
||||
}
|
||||
|
||||
/**
|
||||
* BIK-01 to BIK-05 and MAP-03: what the tour being planned runs on, asked of
|
||||
* the router once its route has settled, and where asked for the trail grades
|
||||
* of its ways (a mountain bike tour's, a hike's SAC grades). A new route makes
|
||||
* BIK-01 to BIK-05, MAP-03 and MAP-04: what the tour being planned runs on,
|
||||
* asked of the router once its route has settled, and where asked for what
|
||||
* OpenStreetMap records for its ways (a mountain bike tour's grades; a hike's
|
||||
* SAC grades, surfaces and kinds of way). A new route makes
|
||||
* the answer on its way stale and takes the map's highlight down; nothing of
|
||||
* it changes the tour.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { fireEvent, render, screen, within } from '../../../../tests/helpers/render';
|
||||
import type { FacetRow } from './hikeFacets';
|
||||
import { HikeWaysCard } from './HikeWaysCard';
|
||||
import type { HikeWays } from './useHikeWays';
|
||||
|
||||
const hook = vi.hoisted(() => ({ ways: null as unknown, input: null as unknown }));
|
||||
vi.mock('./useHikeWays', () => ({
|
||||
useHikeWays: (input: unknown) => {
|
||||
hook.input = input;
|
||||
return hook.ways;
|
||||
},
|
||||
}));
|
||||
|
||||
// FE-HIKE-WAYSCARD-001 to FE-HIKE-WAYSCARD-011
|
||||
|
||||
const LINE: [number, number][] = [
|
||||
[47.0, 11.0],
|
||||
[47.01, 11.0],
|
||||
];
|
||||
const SAC_ROWS: FacetRow[] = [
|
||||
{ key: 't2', lengthM: 1200, share: 0.4, tags: ['mountain_hiking'] },
|
||||
{ key: 't4', lengthM: 600, share: 0.2, tags: ['alpine_hiking'] },
|
||||
{ key: 'untagged', lengthM: 700, share: 0.2, tags: [] },
|
||||
{ key: 'unclear', lengthM: 300, share: 0.1, tags: ['T3', 'hiking;mountain_hiking'] },
|
||||
{ key: 'unmatched', lengthM: 100, share: 0.05, tags: [] },
|
||||
{ key: 'unasked', lengthM: 100, share: 0.05, tags: [] },
|
||||
];
|
||||
const SURFACE_ROWS: FacetRow[] = [
|
||||
{ key: 'gravel', lengthM: 1500, share: 0.5, tags: ['gravel', 'pebblestone'] },
|
||||
{ key: 'rock', lengthM: 600, share: 0.2, tags: ['rock'] },
|
||||
{ key: 'untagged', lengthM: 600, share: 0.2, tags: [] },
|
||||
{ key: 'unclear', lengthM: 300, share: 0.1, tags: ['gravel;ground'] },
|
||||
];
|
||||
const WAY_ROWS: FacetRow[] = [
|
||||
{ key: 'path', lengthM: 2000, share: 0.7, tags: ['path'] },
|
||||
{ key: 'track', lengthM: 900, share: 0.3, tags: ['track'] },
|
||||
];
|
||||
|
||||
function ways(overrides: Partial<HikeWays> = {}): HikeWays {
|
||||
return {
|
||||
available: true,
|
||||
shown: true,
|
||||
toggle: vi.fn(),
|
||||
facet: 'sac',
|
||||
chooseFacet: vi.fn(),
|
||||
status: 'ready',
|
||||
rows: SAC_ROWS,
|
||||
knownShare: 0.6,
|
||||
mostly: SAC_ROWS[0],
|
||||
highest: 4,
|
||||
focus: null,
|
||||
focusOn: vi.fn(),
|
||||
retry: vi.fn(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function show(state: HikeWays, routingLimit: number | null = 3) {
|
||||
hook.ways = state;
|
||||
return render(<HikeWaysCard tripId={4} route={LINE} routingLimit={routingLimit} />);
|
||||
}
|
||||
|
||||
async function tooltipOf(testId: string, text: string | RegExp) {
|
||||
fireEvent.mouseEnter(screen.getByTestId(testId));
|
||||
expect(await screen.findByText(text)).toBeInTheDocument();
|
||||
fireEvent.mouseLeave(screen.getByTestId(testId));
|
||||
}
|
||||
|
||||
beforeEach(() => localStorage.clear());
|
||||
|
||||
describe('HikeWaysCard (MAP-03, MAP-04)', () => {
|
||||
it('FE-HIKE-WAYSCARD-001: without a line to describe there is no card', () => {
|
||||
show(ways({ available: false, shown: false, status: 'off', rows: [] }));
|
||||
expect(screen.queryByTestId('hike-ways-card')).toBeNull();
|
||||
expect(hook.input).toEqual({ tripId: 4, route: LINE, current: true });
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-002: switched off it only offers the switch, and the switch turns it on', () => {
|
||||
const state = ways({ shown: false, status: 'off', rows: [] });
|
||||
show(state);
|
||||
const toggle = screen.getByRole('button', { name: 'Show along the route' });
|
||||
expect(toggle).toHaveAttribute('aria-pressed', 'false');
|
||||
expect(screen.queryByRole('group', { name: 'What to show' })).toBeNull();
|
||||
expect(screen.queryByTestId('sac-row-t2')).toBeNull();
|
||||
fireEvent.click(toggle);
|
||||
expect(state.toggle).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-003: switched on it offers the SAC grade, the surface and the kind of way apart', () => {
|
||||
const state = ways();
|
||||
show(state);
|
||||
const facets = screen.getByRole('group', { name: 'What to show' });
|
||||
expect(within(facets).getByRole('button', { name: 'SAC' })).toHaveAttribute('aria-pressed', 'true');
|
||||
expect(within(facets).getByRole('button', { name: 'Surface' })).toHaveAttribute('aria-pressed', 'false');
|
||||
fireEvent.click(within(facets).getByRole('button', { name: 'Way type' }));
|
||||
expect(state.chooseFacet).toHaveBeenCalledWith('way');
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-004: lists the known steps with their share, the unknown apart, and the routing limit as a setting', () => {
|
||||
show(ways());
|
||||
expect(within(screen.getByTestId('sac-row-t2')).getByText('T2')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('sac-row-t2')).toHaveTextContent('Mountain hiking');
|
||||
expect(screen.getByTestId('sac-row-t2')).toHaveTextContent('40 %');
|
||||
expect(within(screen.getByTestId('sac-row-untagged')).getByText('?')).toBeInTheDocument();
|
||||
expect(screen.getByText('Unknown')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('sac-known')).toHaveTextContent('60 %');
|
||||
expect(screen.getByTestId('sac-highest')).toHaveTextContent('T4');
|
||||
expect(screen.queryByTestId('sac-mostly')).toBeNull();
|
||||
expect(screen.getByTestId('sac-routing-limit')).toHaveTextContent('Routing limit');
|
||||
expect(screen.getByTestId('sac-routing-limit')).toHaveTextContent('T3');
|
||||
// Only a known step above the limit says so; the unknown never does.
|
||||
expect(within(screen.getByTestId('sac-row-t4')).getByText('Above the limit')).toBeInTheDocument();
|
||||
expect(within(screen.getByTestId('sac-row-t2')).queryByText('Above the limit')).toBeNull();
|
||||
expect(within(screen.getByTestId('sac-row-untagged')).queryByText('Above the limit')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-005: an import, planned without a routing limit, shows none and nothing above it', () => {
|
||||
show(ways({ highest: null, rows: SAC_ROWS.slice(2), knownShare: 0, mostly: null }), null);
|
||||
expect(screen.queryByTestId('sac-routing-limit')).toBeNull();
|
||||
expect(screen.queryByTestId('sac-highest')).toBeNull();
|
||||
expect(screen.queryByText('Above the limit')).toBeNull();
|
||||
expect(screen.getByTestId('sac-known')).toHaveTextContent('0 %');
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-006: a row draws its class alone on the map, and shows that it does', () => {
|
||||
const state = ways({ focus: 't4' });
|
||||
show(state);
|
||||
expect(screen.getByTestId('sac-row-t4')).toHaveAttribute('aria-pressed', 'true');
|
||||
expect(screen.getByTestId('sac-row-t2')).toHaveAttribute('aria-pressed', 'false');
|
||||
fireEvent.click(screen.getByTestId('sac-row-t2'));
|
||||
expect(state.focusOn).toHaveBeenCalledWith('t2');
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-007: says when it is asking, offline, or failed with a way to ask again', () => {
|
||||
const loading = show(ways({ status: 'loading', rows: [] }));
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Reading the ways along the hike');
|
||||
loading.unmount();
|
||||
const offline = show(ways({ status: 'offline', rows: [] }));
|
||||
expect(screen.getByText('The trail details need a connection.')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: /try again/i })).toBeNull();
|
||||
offline.unmount();
|
||||
const state = ways({ status: 'failed', rows: [] });
|
||||
show(state);
|
||||
expect(screen.getByText(/The tour is unchanged/)).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole('button', { name: /try again/i }));
|
||||
expect(state.retry).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-008: an SAC row names its scale and its source, an unclear one what OpenStreetMap wrote', async () => {
|
||||
show(ways());
|
||||
await tooltipOf('sac-row-t2', 'SAC hiking scale T2, from OpenStreetMap: sac_scale=mountain_hiking');
|
||||
await tooltipOf('sac-row-unclear', /T3, hiking;mountain_hiking/);
|
||||
await tooltipOf('sac-row-untagged', /Unknown does not mean easy/);
|
||||
await tooltipOf('sac-row-unmatched', /no way lies clearly under the line/i);
|
||||
// A way that could not be asked about says so in its row; there is nothing more to name.
|
||||
expect(screen.getByTestId('sac-row-unasked')).toHaveTextContent('Could not be asked this time');
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-009: the surfaces list their groups, the most common one, and no grade or routing limit', () => {
|
||||
show(ways({ facet: 'surface', rows: SURFACE_ROWS, knownShare: 0.7, mostly: SURFACE_ROWS[0], highest: null }));
|
||||
expect(screen.getByTestId('surface-row-gravel')).toHaveTextContent('Gravel');
|
||||
expect(screen.getByTestId('surface-row-gravel')).toHaveTextContent('50 %');
|
||||
// A known group carries its colour only; the question mark is the unknown's.
|
||||
expect(within(screen.getByTestId('surface-row-gravel')).queryByText('?')).toBeNull();
|
||||
expect(within(screen.getByTestId('surface-row-untagged')).getByText('?')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('surface-row-untagged')).toHaveTextContent('No surface in OpenStreetMap');
|
||||
expect(screen.getByTestId('surface-known')).toHaveTextContent('70 %');
|
||||
expect(screen.getByTestId('surface-mostly')).toHaveTextContent('Gravel');
|
||||
expect(screen.queryByTestId('sac-routing-limit')).toBeNull();
|
||||
expect(screen.queryByTestId('sac-highest')).toBeNull();
|
||||
expect(screen.queryByText('Above the limit')).toBeNull();
|
||||
expect(screen.getByText(/router's own reading is not used/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-010: a surface row names the values OpenStreetMap recorded, the untagged one that nothing was', async () => {
|
||||
show(ways({ facet: 'surface', rows: SURFACE_ROWS, knownShare: 0.7, mostly: SURFACE_ROWS[0], highest: null }));
|
||||
await tooltipOf('surface-row-gravel', 'Recorded in OpenStreetMap as surface=gravel, surface=pebblestone');
|
||||
await tooltipOf('surface-row-unclear', /surface=gravel;ground/);
|
||||
await tooltipOf('surface-row-untagged', /A router may assume one/);
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYSCARD-011: the kinds of way read under their own names and source', async () => {
|
||||
const state = ways({ facet: 'way', rows: WAY_ROWS, knownShare: 1, mostly: WAY_ROWS[0], highest: null });
|
||||
show(state);
|
||||
expect(screen.getByTestId('way-row-track')).toHaveTextContent('Farm or forest track');
|
||||
expect(screen.getByTestId('way-mostly')).toHaveTextContent('Path');
|
||||
expect(screen.queryByText('Unknown')).toBeNull();
|
||||
await tooltipOf('way-row-path', 'Recorded in OpenStreetMap as highway=path');
|
||||
fireEvent.click(screen.getByTestId('way-row-track'));
|
||||
expect(state.focusOn).toHaveBeenCalledWith('track');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import { Footprints, Layers, MountainSnow, Signpost } from 'lucide-react';
|
||||
import { useTranslation } from '../../../i18n';
|
||||
import ToggleSwitch from '../../Settings/ToggleSwitch';
|
||||
import { Segmented, type SegmentedOption } from '../../shared/dialogParts';
|
||||
import { fs } from '../../shared/DialogShell';
|
||||
import { ReadStatusNotice } from '../ReadStatusNotice';
|
||||
import { RailCard } from '../tourParts';
|
||||
import type { HikeFacet } from './hikeFacetSpecs';
|
||||
import { HikeWaysList } from './HikeWaysList';
|
||||
import { useHikeWays } from './useHikeWays';
|
||||
|
||||
const ICON = { size: 13, strokeWidth: 2 } as const;
|
||||
|
||||
/**
|
||||
* MAP-03 and MAP-04: what OpenStreetMap records for the ways along a hike, on
|
||||
* the traveller's word. Switched on, the ways under the line are read once
|
||||
* and drawn along it on the map by the facet picked: the SAC grade, the
|
||||
* surface or the kind of way, three things kept apart, each class with its
|
||||
* share; the unknown stays unknown and says why. The routing limit, where the
|
||||
* hike was planned with one, sits apart as the setting it is. Nothing here
|
||||
* changes the tour.
|
||||
*/
|
||||
export function HikeWaysCard({
|
||||
tripId,
|
||||
route,
|
||||
routeCurrent = true,
|
||||
routingLimit,
|
||||
}: {
|
||||
tripId: number | string;
|
||||
/** The tour's own line, or null while there is none. */
|
||||
route: [number, number][] | null;
|
||||
/** False while the line is being planned anew. */
|
||||
routeCurrent?: boolean;
|
||||
/** The routing limit the hike is planned with, or null where it was not routed (an import). */
|
||||
routingLimit: number | null;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const ways = useHikeWays({ tripId, route, current: routeCurrent });
|
||||
if (!ways.available) return null;
|
||||
const facets: SegmentedOption<HikeFacet>[] = [
|
||||
{ value: 'sac', label: t('tours.hike.ways.facet.sac'), icon: <MountainSnow {...ICON} /> },
|
||||
{ value: 'surface', label: t('tours.hike.ways.facet.surface'), icon: <Layers {...ICON} /> },
|
||||
{ value: 'way', label: t('tours.hike.ways.facet.way'), icon: <Signpost {...ICON} /> },
|
||||
];
|
||||
return (
|
||||
<RailCard icon={<Footprints size={14} strokeWidth={2} />} title={t('tours.hike.ways.title')} foldId="hike-ways">
|
||||
<div data-testid="hike-ways-card" className="flex min-w-0 flex-col gap-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<span aria-hidden="true" className="min-w-0 flex-1 text-content-secondary" style={fs(12, 'body')}>
|
||||
{t('tours.hike.ways.show')}
|
||||
</span>
|
||||
<ToggleSwitch on={ways.shown} onToggle={ways.toggle} label={t('tours.hike.ways.show')} />
|
||||
</div>
|
||||
{ways.shown && (
|
||||
<>
|
||||
<Segmented<HikeFacet>
|
||||
value={ways.facet}
|
||||
options={facets}
|
||||
onChange={ways.chooseFacet}
|
||||
label={t('tours.hike.ways.facets')}
|
||||
fill
|
||||
/>
|
||||
<ReadStatusNotice
|
||||
status={ways.status}
|
||||
messages={{
|
||||
loading: t('tours.hike.ways.loading'),
|
||||
offline: t('tours.hike.ways.offline'),
|
||||
failed: t('tours.hike.ways.failed'),
|
||||
}}
|
||||
onRetry={ways.retry}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{ways.shown && ways.status === 'ready' && <HikeWaysList ways={ways} limit={routingLimit} />}
|
||||
</div>
|
||||
</RailCard>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
import { Info, SlidersHorizontal } from 'lucide-react';
|
||||
import { useTranslation } from '../../../i18n';
|
||||
import { useSettingsStore } from '../../../store/settingsStore';
|
||||
import { formatDistance } from '../../../utils/units';
|
||||
import { SoftPill } from '../../Planner/planParts';
|
||||
import { fs } from '../../shared/DialogShell';
|
||||
import { Tooltip } from '../../shared/Tooltip';
|
||||
import { bikePaintCss } from '../bike/bikePaint';
|
||||
import { FigureBadge } from '../paddle/FigureBadge';
|
||||
import { HIKE_FACETS, type HikeFacet } from './hikeFacetSpecs';
|
||||
import { facetPaint, isUnknownClass, type FacetRow } from './hikeFacets';
|
||||
import { hikePaintCss } from './hikePaint';
|
||||
import { sacStepOfClass } from './sacGrades';
|
||||
import type { HikeWays } from './useHikeWays';
|
||||
|
||||
const percent = (share: number) => `${Math.round(share * 100)} %`;
|
||||
const stepName = (step: number) => `T${step}`;
|
||||
|
||||
const sacKey = (name: string) => `tours.hike.sac.${name}`;
|
||||
const surfaceKey = (name: string) => `tours.hike.surface.${name}`;
|
||||
const wayKey = (name: string) => `tours.hike.way.${name}`;
|
||||
const sharedKey = (name: string) => `tours.hike.ways.${name}`;
|
||||
|
||||
/** A facet's own texts; a way not matched or not asked reads the same in every facet. */
|
||||
const FACET_KEY: Readonly<Record<HikeFacet, (name: string) => string>> = {
|
||||
sac: sacKey,
|
||||
surface: surfaceKey,
|
||||
way: wayKey,
|
||||
};
|
||||
|
||||
/** The OpenStreetMap key a facet's values are written under, named in the tooltips. */
|
||||
const FACET_TAG: Readonly<Record<HikeFacet, string>> = { sac: 'sac_scale', surface: 'surface', way: 'highway' };
|
||||
|
||||
function labelKey(facet: HikeFacet, key: string): string {
|
||||
return key === 'unmatched' || key === 'unasked' ? sharedKey(key) : FACET_KEY[facet](key);
|
||||
}
|
||||
|
||||
/** The colour a class is drawn in on the map, as a CSS variable: its own, the grey of the unknown otherwise. */
|
||||
function paintOf(facet: HikeFacet, key: string): string {
|
||||
const paint = facetPaint(HIKE_FACETS[facet], key);
|
||||
return paint === 'unknown' ? bikePaintCss('unknown') : hikePaintCss(paint);
|
||||
}
|
||||
|
||||
/** What OpenStreetMap wrote for a row, as `key=value`, each once. */
|
||||
const writtenTags = (facet: HikeFacet, row: FacetRow) => row.tags.map((tag) => `${FACET_TAG[facet]}=${tag}`).join(', ');
|
||||
|
||||
/**
|
||||
* A class as a small white chip: a dot in its map colour, with the step of an
|
||||
* SAC grade, or a question mark for the unknown.
|
||||
*/
|
||||
function ClassChip({ facet, row }: { facet: HikeFacet; row: FacetRow }) {
|
||||
const step = sacStepOfClass(row.key);
|
||||
const unknown = isUnknownClass(row.key);
|
||||
return (
|
||||
<span className="inline-flex h-[18px] w-[34px] flex-none items-center justify-center gap-1 rounded-full bg-surface-card font-geist font-semibold tabular-nums text-content shadow-sm ring-1 ring-inset ring-edge-faint">
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className="h-2 w-2 flex-none rounded-full"
|
||||
style={{ background: paintOf(facet, row.key) }}
|
||||
/>
|
||||
{(step !== null || unknown) && (
|
||||
<span className={unknown ? 'text-content-faint' : ''} style={fs(10.5)}>
|
||||
{step === null ? '?' : stepName(step)}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/** Where a row's class comes from, said in its tooltip: what OpenStreetMap wrote, or why it is unknown. */
|
||||
function useRowSource(facet: HikeFacet, row: FacetRow): string {
|
||||
const { t } = useTranslation();
|
||||
const step = sacStepOfClass(row.key);
|
||||
if (step !== null) return t('tours.hike.sac.stepTip', { step: stepName(step), tag: row.tags[0] ?? '' });
|
||||
if (row.key === 'untagged') return t(FACET_KEY[facet]('untaggedTip'));
|
||||
if (row.key === 'unmatched') return t('tours.hike.ways.unmatchedTip');
|
||||
if (row.key === 'unasked') return '';
|
||||
if (row.key === 'unclear' && facet === 'sac') return t('tours.hike.sac.unclearTip', { tags: row.tags.join(', ') });
|
||||
if (row.key === 'unclear') return t('tours.hike.ways.unclearTip', { tags: writtenTags(facet, row) });
|
||||
return t('tours.hike.ways.recordedTip', { tags: writtenTags(facet, row) });
|
||||
}
|
||||
|
||||
/**
|
||||
* One class along the hike: its chip, its name, how far and how much of the
|
||||
* route it covers. Pressed, the map draws its stretches alone; pressed again,
|
||||
* every class again.
|
||||
*/
|
||||
function ClassRow({ row, ways, limit }: { row: FacetRow; ways: HikeWays; limit: number | null }) {
|
||||
const { t } = useTranslation();
|
||||
const unit = useSettingsStore((state) => state.settings.distance_unit) ?? 'metric';
|
||||
const source = useRowSource(ways.facet, row);
|
||||
const step = sacStepOfClass(row.key);
|
||||
const pressed = ways.focus === row.key;
|
||||
return (
|
||||
<li>
|
||||
<Tooltip label={source} disabled={!source} placement="top">
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={pressed}
|
||||
data-testid={`${ways.facet}-row-${row.key}`}
|
||||
onClick={() => ways.focusOn(row.key)}
|
||||
className={`flex w-full min-w-0 items-center gap-2 rounded-lg px-1.5 py-1 text-start transition-colors hover:bg-surface-tertiary focus-visible:outline focus-visible:outline-2 focus-visible:outline-accent ${pressed ? 'bg-surface-tertiary' : ''}`}
|
||||
>
|
||||
<ClassChip facet={ways.facet} row={row} />
|
||||
<span
|
||||
className={`min-w-0 flex-1 truncate ${isUnknownClass(row.key) ? 'text-content-muted' : 'text-content'}`}
|
||||
style={fs(12, 'body')}
|
||||
>
|
||||
{t(labelKey(ways.facet, row.key))}
|
||||
</span>
|
||||
{step !== null && limit !== null && step > limit && (
|
||||
<SoftPill tone="warning">{t('tours.hike.sac.aboveLimit')}</SoftPill>
|
||||
)}
|
||||
<span className="flex-none font-geist tabular-nums text-content-faint" style={fs(11)}>
|
||||
{formatDistance(row.lengthM / 1000, unit)}
|
||||
</span>
|
||||
<span className="w-10 flex-none text-end font-geist tabular-nums text-content-secondary" style={fs(11)}>
|
||||
{percent(row.share)}
|
||||
</span>
|
||||
</button>
|
||||
</Tooltip>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
/** The classes as one bar along the route's length, in the map's colours, so the mix reads at a glance. */
|
||||
function ClassBar({ ways }: { ways: HikeWays }) {
|
||||
return (
|
||||
<span aria-hidden="true" className="flex h-2 w-full overflow-hidden rounded-full bg-surface-tertiary">
|
||||
{ways.rows.map((row) => (
|
||||
<span
|
||||
key={row.key}
|
||||
className="h-full"
|
||||
style={{ width: `${row.share * 100}%`, background: paintOf(ways.facet, row.key) }}
|
||||
/>
|
||||
))}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* What is known at a glance: how much of the route the facet knows, and the
|
||||
* class covering the most of it, or for the SAC grades the highest step and,
|
||||
* apart from it and marked as a setting, the routing limit the hike was
|
||||
* planned with, which says nothing about the ways themselves.
|
||||
*/
|
||||
function FacetSummary({ ways, limit }: { ways: HikeWays; limit: number | null }) {
|
||||
const { t } = useTranslation();
|
||||
const { facet } = ways;
|
||||
return (
|
||||
<div className="flex flex-wrap items-center gap-1.5">
|
||||
<FigureBadge
|
||||
label={t('tours.hike.ways.known')}
|
||||
value={percent(ways.knownShare)}
|
||||
info={t(FACET_KEY[facet]('knownTip'))}
|
||||
testId={`${facet}-known`}
|
||||
/>
|
||||
{facet !== 'sac' && ways.mostly && (
|
||||
<FigureBadge
|
||||
label={t('tours.hike.ways.mostly')}
|
||||
value={t(labelKey(facet, ways.mostly.key))}
|
||||
testId={`${facet}-mostly`}
|
||||
/>
|
||||
)}
|
||||
{ways.highest !== null && (
|
||||
<FigureBadge label={t('tours.hike.sac.highest')} value={stepName(ways.highest)} testId="sac-highest" />
|
||||
)}
|
||||
{facet === 'sac' && limit !== null && (
|
||||
<FigureBadge
|
||||
label={t('tours.hike.sac.routingLimit')}
|
||||
value={stepName(limit)}
|
||||
icon={<SlidersHorizontal size={10} strokeWidth={2.2} className="flex-none text-content-faint" />}
|
||||
info={t('tours.hike.sac.routingLimitTip')}
|
||||
testId="sac-routing-limit"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* MAP-03 and MAP-04: one facet of the hike laid out: the summary, the mix as
|
||||
* a bar, the known classes, the unknown apart under their own heading, and
|
||||
* where the values come from.
|
||||
*/
|
||||
export function HikeWaysList({ ways, limit }: { ways: HikeWays; limit: number | null }) {
|
||||
const { t } = useTranslation();
|
||||
const known = ways.rows.filter((row) => !isUnknownClass(row.key));
|
||||
const unknown = ways.rows.filter((row) => isUnknownClass(row.key));
|
||||
return (
|
||||
<>
|
||||
<FacetSummary ways={ways} limit={limit} />
|
||||
<ClassBar ways={ways} />
|
||||
{known.length > 0 && (
|
||||
<ul className="m-0 flex list-none flex-col p-0">
|
||||
{known.map((row) => (
|
||||
<ClassRow key={row.key} row={row} ways={ways} limit={limit} />
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{unknown.length > 0 && (
|
||||
<div className="flex min-w-0 flex-col gap-1">
|
||||
<span
|
||||
className="font-geist font-bold uppercase tracking-[.08em] text-content-faint"
|
||||
style={fs(9.5, 'caption')}
|
||||
>
|
||||
{t('tours.hike.ways.unknown')}
|
||||
</span>
|
||||
<ul className="m-0 flex list-none flex-col p-0">
|
||||
{unknown.map((row) => (
|
||||
<ClassRow key={row.key} row={row} ways={ways} limit={limit} />
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
<span className="inline-flex gap-1.5 text-content-faint" style={fs(11, 'body')}>
|
||||
<Info size={12} strokeWidth={2} className="mt-0.5 flex-none" />
|
||||
{t(FACET_KEY[ways.facet]('source'))}
|
||||
</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { fireEvent, render, screen, within } from '../../../../tests/helpers/render';
|
||||
import { SacGradesCard } from './SacGradesCard';
|
||||
import type { SacGradeRow, SacGrades } from './useSacGrades';
|
||||
|
||||
const hook = vi.hoisted(() => ({ grades: null as unknown, input: null as unknown }));
|
||||
vi.mock('./useSacGrades', () => ({
|
||||
useSacGrades: (input: unknown) => {
|
||||
hook.input = input;
|
||||
return hook.grades;
|
||||
},
|
||||
}));
|
||||
|
||||
// FE-HIKE-SACCARD-001 to FE-HIKE-SACCARD-007
|
||||
|
||||
const LINE: [number, number][] = [
|
||||
[47.0, 11.0],
|
||||
[47.01, 11.0],
|
||||
];
|
||||
const ROWS: SacGradeRow[] = [
|
||||
{ key: 't2', lengthM: 1200, share: 0.4, tags: ['mountain_hiking'] },
|
||||
{ key: 't4', lengthM: 600, share: 0.2, tags: ['alpine_hiking'] },
|
||||
{ key: 'unclear', lengthM: 300, share: 0.1, tags: ['T3', 'hiking;mountain_hiking'] },
|
||||
{ key: 'untagged', lengthM: 700, share: 0.2, tags: [] },
|
||||
{ key: 'unmatched', lengthM: 100, share: 0.05, tags: [] },
|
||||
{ key: 'unasked', lengthM: 100, share: 0.05, tags: [] },
|
||||
];
|
||||
|
||||
function grades(overrides: Partial<SacGrades> = {}): SacGrades {
|
||||
return {
|
||||
available: true,
|
||||
shown: true,
|
||||
toggle: vi.fn(),
|
||||
status: 'ready',
|
||||
rows: ROWS,
|
||||
knownShare: 0.6,
|
||||
highest: 4,
|
||||
focus: null,
|
||||
focusOn: vi.fn(),
|
||||
retry: vi.fn(),
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function show(state: SacGrades, routingLimit: number | null = 3) {
|
||||
hook.grades = state;
|
||||
return render(<SacGradesCard tripId={4} route={LINE} routingLimit={routingLimit} />);
|
||||
}
|
||||
|
||||
beforeEach(() => localStorage.clear());
|
||||
|
||||
describe('SacGradesCard (MAP-03)', () => {
|
||||
it('FE-HIKE-SACCARD-001: without a line to grade there is no card', () => {
|
||||
show(grades({ available: false, shown: false, status: 'off', rows: [] }));
|
||||
expect(screen.queryByTestId('sac-grades-card')).toBeNull();
|
||||
expect(hook.input).toEqual({ tripId: 4, route: LINE, current: true });
|
||||
});
|
||||
|
||||
it('FE-HIKE-SACCARD-002: switched off it only offers the switch, and the switch turns it on', () => {
|
||||
const state = grades({ shown: false, status: 'off', rows: [] });
|
||||
show(state);
|
||||
const toggle = screen.getByRole('button', { name: 'Show along the route' });
|
||||
expect(toggle).toHaveAttribute('aria-pressed', 'false');
|
||||
expect(screen.queryByTestId('sac-row-t2')).toBeNull();
|
||||
fireEvent.click(toggle);
|
||||
expect(state.toggle).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-HIKE-SACCARD-003: lists the known steps with their share, the unknown apart, and the routing limit as a setting', () => {
|
||||
show(grades());
|
||||
expect(within(screen.getByTestId('sac-row-t2')).getByText('T2')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('sac-row-t2')).toHaveTextContent('Mountain hiking');
|
||||
expect(screen.getByTestId('sac-row-t2')).toHaveTextContent('40 %');
|
||||
expect(within(screen.getByTestId('sac-row-untagged')).getByText('?')).toBeInTheDocument();
|
||||
expect(screen.getByText('Unknown')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('sac-known')).toHaveTextContent('60 %');
|
||||
expect(screen.getByTestId('sac-highest')).toHaveTextContent('T4');
|
||||
expect(screen.getByTestId('sac-routing-limit')).toHaveTextContent('Routing limit');
|
||||
expect(screen.getByTestId('sac-routing-limit')).toHaveTextContent('T3');
|
||||
// Only a known step above the limit says so; the unknown never does.
|
||||
expect(within(screen.getByTestId('sac-row-t4')).getByText('Above the limit')).toBeInTheDocument();
|
||||
expect(within(screen.getByTestId('sac-row-t2')).queryByText('Above the limit')).toBeNull();
|
||||
expect(within(screen.getByTestId('sac-row-untagged')).queryByText('Above the limit')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-SACCARD-004: an import, planned without a routing limit, shows none and nothing above it', () => {
|
||||
show(grades({ highest: null, rows: ROWS.slice(2), knownShare: 0 }), null);
|
||||
expect(screen.queryByTestId('sac-routing-limit')).toBeNull();
|
||||
expect(screen.queryByTestId('sac-highest')).toBeNull();
|
||||
expect(screen.queryByText('Above the limit')).toBeNull();
|
||||
expect(screen.getByTestId('sac-known')).toHaveTextContent('0 %');
|
||||
});
|
||||
|
||||
it('FE-HIKE-SACCARD-005: a row draws its class alone on the map, and shows that it does', () => {
|
||||
const state = grades({ focus: 't4' });
|
||||
show(state);
|
||||
expect(screen.getByTestId('sac-row-t4')).toHaveAttribute('aria-pressed', 'true');
|
||||
expect(screen.getByTestId('sac-row-t2')).toHaveAttribute('aria-pressed', 'false');
|
||||
fireEvent.click(screen.getByTestId('sac-row-t2'));
|
||||
expect(state.focusOn).toHaveBeenCalledWith('t2');
|
||||
});
|
||||
|
||||
it('FE-HIKE-SACCARD-006: says when it is asking, offline, or failed with a way to ask again', () => {
|
||||
const loading = show(grades({ status: 'loading', rows: [] }));
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Reading the SAC grades');
|
||||
loading.unmount();
|
||||
const offline = show(grades({ status: 'offline', rows: [] }));
|
||||
expect(screen.getByText('The SAC grades need a connection.')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: /try again/i })).toBeNull();
|
||||
offline.unmount();
|
||||
const state = grades({ status: 'failed', rows: [] });
|
||||
show(state);
|
||||
expect(screen.getByText(/The tour is unchanged/)).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole('button', { name: /try again/i }));
|
||||
expect(state.retry).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-HIKE-SACCARD-007: a row names its scale and its source, an unclear one what OpenStreetMap wrote', async () => {
|
||||
show(grades());
|
||||
fireEvent.mouseEnter(screen.getByTestId('sac-row-t2'));
|
||||
const source = 'SAC hiking scale T2, from OpenStreetMap: sac_scale=mountain_hiking';
|
||||
expect(await screen.findByText(source)).toBeInTheDocument();
|
||||
fireEvent.mouseLeave(screen.getByTestId('sac-row-t2'));
|
||||
fireEvent.mouseEnter(screen.getByTestId('sac-row-unclear'));
|
||||
expect(await screen.findByText(/T3, hiking;mountain_hiking/)).toBeInTheDocument();
|
||||
fireEvent.mouseLeave(screen.getByTestId('sac-row-unclear'));
|
||||
fireEvent.mouseEnter(screen.getByTestId('sac-row-untagged'));
|
||||
expect(await screen.findByText(/Unknown does not mean easy/)).toBeInTheDocument();
|
||||
fireEvent.mouseLeave(screen.getByTestId('sac-row-untagged'));
|
||||
fireEvent.mouseEnter(screen.getByTestId('sac-row-unmatched'));
|
||||
expect(await screen.findByText(/no way lies clearly under the line/i)).toBeInTheDocument();
|
||||
// A way that could not be asked about says so in its row; there is nothing more to name.
|
||||
expect(screen.getByTestId('sac-row-unasked')).toHaveTextContent('Could not be asked this time');
|
||||
});
|
||||
});
|
||||
@@ -1,218 +0,0 @@
|
||||
import { Info, MountainSnow, SlidersHorizontal } from 'lucide-react';
|
||||
import { useTranslation } from '../../../i18n';
|
||||
import { useSettingsStore } from '../../../store/settingsStore';
|
||||
import { formatDistance } from '../../../utils/units';
|
||||
import { SoftPill } from '../../Planner/planParts';
|
||||
import ToggleSwitch from '../../Settings/ToggleSwitch';
|
||||
import { fs } from '../../shared/DialogShell';
|
||||
import { Tooltip } from '../../shared/Tooltip';
|
||||
import { bikePaintCss } from '../bike/bikePaint';
|
||||
import { FigureBadge } from '../paddle/FigureBadge';
|
||||
import { ReadStatusNotice } from '../ReadStatusNotice';
|
||||
import { RailCard } from '../tourParts';
|
||||
import { hikePaintCss } from './hikePaint';
|
||||
import { isSacStep, sacStepOfClass, type SacClass } from './sacGrades';
|
||||
import { useSacGrades, type SacGradeRow, type SacGrades } from './useSacGrades';
|
||||
|
||||
const percent = (share: number) => `${Math.round(share * 100)} %`;
|
||||
const stepName = (step: number) => `T${step}`;
|
||||
|
||||
/** The colour a class is drawn in on the map, as a CSS variable: the step's own, the grey of the unknown otherwise. */
|
||||
const paintOf = (key: SacClass) => (isSacStep(key) ? hikePaintCss(key) : bikePaintCss('unknown'));
|
||||
|
||||
/** A grade as a small white chip: a dot in its map colour and the step, or a question mark for the unknown. */
|
||||
function GradeChip({ grade }: { grade: SacClass }) {
|
||||
const step = sacStepOfClass(grade);
|
||||
return (
|
||||
<span className="inline-flex h-[18px] w-[34px] flex-none items-center justify-center gap-1 rounded-full bg-surface-card font-geist font-semibold tabular-nums text-content shadow-sm ring-1 ring-inset ring-edge-faint">
|
||||
<span aria-hidden="true" className="h-2 w-2 flex-none rounded-full" style={{ background: paintOf(grade) }} />
|
||||
<span className={step === null ? 'text-content-faint' : ''} style={fs(10.5)}>
|
||||
{step === null ? '?' : stepName(step)}
|
||||
</span>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/** Where a row's grade comes from, said in its tooltip: the scale and the tag, or why it is unknown. */
|
||||
function useRowSource(row: SacGradeRow): string {
|
||||
const { t } = useTranslation();
|
||||
const step = sacStepOfClass(row.key);
|
||||
if (step !== null) return t('tours.hike.sac.stepTip', { step: stepName(step), tag: row.tags[0] ?? '' });
|
||||
if (row.key === 'untagged') return t('tours.hike.sac.untaggedTip');
|
||||
if (row.key === 'unclear') return t('tours.hike.sac.unclearTip', { tags: row.tags.join(', ') });
|
||||
if (row.key === 'unmatched') return t('tours.hike.sac.unmatchedTip');
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* One class of the hike: its chip, its name, how far and how much of the
|
||||
* route it covers. Pressed, the map draws its stretches alone; pressed again,
|
||||
* every grade again.
|
||||
*/
|
||||
function GradeRow({ row, grades, limit }: { row: SacGradeRow; grades: SacGrades; limit: number | null }) {
|
||||
const { t } = useTranslation();
|
||||
const unit = useSettingsStore((state) => state.settings.distance_unit) ?? 'metric';
|
||||
const source = useRowSource(row);
|
||||
const step = sacStepOfClass(row.key);
|
||||
const pressed = grades.focus === row.key;
|
||||
return (
|
||||
<li>
|
||||
<Tooltip label={source} disabled={!source} placement="top">
|
||||
<button
|
||||
type="button"
|
||||
aria-pressed={pressed}
|
||||
data-testid={`sac-row-${row.key}`}
|
||||
onClick={() => grades.focusOn(row.key)}
|
||||
className={`flex w-full min-w-0 items-center gap-2 rounded-lg px-1.5 py-1 text-start transition-colors hover:bg-surface-tertiary focus-visible:outline focus-visible:outline-2 focus-visible:outline-accent ${pressed ? 'bg-surface-tertiary' : ''}`}
|
||||
>
|
||||
<GradeChip grade={row.key} />
|
||||
<span
|
||||
className={`min-w-0 flex-1 truncate ${step === null ? 'text-content-muted' : 'text-content'}`}
|
||||
style={fs(12, 'body')}
|
||||
>
|
||||
{t(`tours.hike.sac.${row.key}`)}
|
||||
</span>
|
||||
{step !== null && limit !== null && step > limit && (
|
||||
<SoftPill tone="warning">{t('tours.hike.sac.aboveLimit')}</SoftPill>
|
||||
)}
|
||||
<span className="flex-none font-geist tabular-nums text-content-faint" style={fs(11)}>
|
||||
{formatDistance(row.lengthM / 1000, unit)}
|
||||
</span>
|
||||
<span className="w-10 flex-none text-end font-geist tabular-nums text-content-secondary" style={fs(11)}>
|
||||
{percent(row.share)}
|
||||
</span>
|
||||
</button>
|
||||
</Tooltip>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
/** The classes as one bar along the route's length, in the map's colours, so the mix reads at a glance. */
|
||||
function GradeBar({ rows }: { rows: SacGradeRow[] }) {
|
||||
return (
|
||||
<span aria-hidden="true" className="flex h-2 w-full overflow-hidden rounded-full bg-surface-tertiary">
|
||||
{rows.map((row) => (
|
||||
<span key={row.key} className="h-full" style={{ width: `${row.share * 100}%`, background: paintOf(row.key) }} />
|
||||
))}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* What is known at a glance: how much of the route has a grade, the highest
|
||||
* one, and apart from them, marked as a setting, the routing limit the hike
|
||||
* was planned with, which says nothing about the ways themselves.
|
||||
*/
|
||||
function GradeSummary({ grades, limit }: { grades: SacGrades; limit: number | null }) {
|
||||
const { t } = useTranslation();
|
||||
return (
|
||||
<div className="flex flex-wrap items-center gap-1.5">
|
||||
<FigureBadge
|
||||
label={t('tours.hike.sac.known')}
|
||||
value={percent(grades.knownShare)}
|
||||
info={t('tours.hike.sac.knownTip')}
|
||||
testId="sac-known"
|
||||
/>
|
||||
{grades.highest !== null && (
|
||||
<FigureBadge label={t('tours.hike.sac.highest')} value={stepName(grades.highest)} testId="sac-highest" />
|
||||
)}
|
||||
{limit !== null && (
|
||||
<FigureBadge
|
||||
label={t('tours.hike.sac.routingLimit')}
|
||||
value={stepName(limit)}
|
||||
icon={<SlidersHorizontal size={10} strokeWidth={2.2} className="flex-none text-content-faint" />}
|
||||
info={t('tours.hike.sac.routingLimitTip')}
|
||||
testId="sac-routing-limit"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function GradeList({ grades, limit }: { grades: SacGrades; limit: number | null }) {
|
||||
const { t } = useTranslation();
|
||||
const known = grades.rows.filter((row) => isSacStep(row.key));
|
||||
const unknown = grades.rows.filter((row) => !isSacStep(row.key));
|
||||
return (
|
||||
<>
|
||||
<GradeSummary grades={grades} limit={limit} />
|
||||
<GradeBar rows={grades.rows} />
|
||||
{known.length > 0 && (
|
||||
<ul className="m-0 flex list-none flex-col p-0">
|
||||
{known.map((row) => (
|
||||
<GradeRow key={row.key} row={row} grades={grades} limit={limit} />
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
{unknown.length > 0 && (
|
||||
<div className="flex min-w-0 flex-col gap-1">
|
||||
<span
|
||||
className="font-geist font-bold uppercase tracking-[.08em] text-content-faint"
|
||||
style={fs(9.5, 'caption')}
|
||||
>
|
||||
{t('tours.hike.sac.unknown')}
|
||||
</span>
|
||||
<ul className="m-0 flex list-none flex-col p-0">
|
||||
{unknown.map((row) => (
|
||||
<GradeRow key={row.key} row={row} grades={grades} limit={limit} />
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
<span className="inline-flex gap-1.5 text-content-faint" style={fs(11, 'body')}>
|
||||
<Info size={12} strokeWidth={2} className="mt-0.5 flex-none" />
|
||||
{t('tours.hike.sac.source')}
|
||||
</span>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* MAP-03: the SAC grades along a hike, on the traveller's word. Switched on,
|
||||
* the grades OpenStreetMap gives the ways under the line are read and drawn
|
||||
* along it on the map, each class with its share; the unknown stays unknown
|
||||
* and says why. The routing limit, where the hike was planned with one, sits
|
||||
* apart as the setting it is. Nothing here changes the tour.
|
||||
*/
|
||||
export function SacGradesCard({
|
||||
tripId,
|
||||
route,
|
||||
routeCurrent = true,
|
||||
routingLimit,
|
||||
}: {
|
||||
tripId: number | string;
|
||||
/** The tour's own line, or null while there is none. */
|
||||
route: [number, number][] | null;
|
||||
/** False while the line is being planned anew. */
|
||||
routeCurrent?: boolean;
|
||||
/** The routing limit the hike is planned with, or null where it was not routed (an import). */
|
||||
routingLimit: number | null;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const grades = useSacGrades({ tripId, route, current: routeCurrent });
|
||||
if (!grades.available) return null;
|
||||
return (
|
||||
<RailCard icon={<MountainSnow size={14} strokeWidth={2} />} title={t('tours.hike.sac.title')} foldId="hike-sac">
|
||||
<div data-testid="sac-grades-card" className="flex min-w-0 flex-col gap-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<span aria-hidden="true" className="min-w-0 flex-1 text-content-secondary" style={fs(12, 'body')}>
|
||||
{t('tours.hike.sac.show')}
|
||||
</span>
|
||||
<ToggleSwitch on={grades.shown} onToggle={grades.toggle} label={t('tours.hike.sac.show')} />
|
||||
</div>
|
||||
{grades.shown && (
|
||||
<ReadStatusNotice
|
||||
status={grades.status}
|
||||
messages={{
|
||||
loading: t('tours.hike.sac.loading'),
|
||||
offline: t('tours.hike.sac.offline'),
|
||||
failed: t('tours.hike.sac.failed'),
|
||||
}}
|
||||
onRetry={grades.retry}
|
||||
/>
|
||||
)}
|
||||
{grades.shown && grades.status === 'ready' && <GradeList grades={grades} limit={routingLimit} />}
|
||||
</div>
|
||||
</RailCard>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { HikeFacetSpec } from './hikeFacets';
|
||||
import { SAC_FACET } from './sacGrades';
|
||||
import { SURFACE_FACET, WAY_FACET } from './wayTagFacets';
|
||||
|
||||
/** What a hike's card reads along the line, one at a time: the SAC grade, the surface or the kind of way. */
|
||||
export type HikeFacet = 'sac' | 'surface' | 'way';
|
||||
|
||||
export const HIKE_FACETS: Readonly<Record<HikeFacet, HikeFacetSpec<string>>> = {
|
||||
sac: SAC_FACET,
|
||||
surface: SURFACE_FACET,
|
||||
way: WAY_FACET,
|
||||
};
|
||||
@@ -0,0 +1,131 @@
|
||||
import type { TourTrailGrade } from '@trek/shared';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { RouteWayEdge, RouteWays } from '../bike/routeWays';
|
||||
import { facetClassifier, facetOverlay, facetPaint, facetRows, isUnknownClass, knownShareOf } from './hikeFacets';
|
||||
import { SAC_FACET } from './sacGrades';
|
||||
import { SURFACE_FACET } from './wayTagFacets';
|
||||
|
||||
// FE-HIKE-FACET-001 to FE-HIKE-FACET-008
|
||||
|
||||
const LINE: [number, number][] = Array.from({ length: 8 }, (_, index): [number, number] => [47 + index / 1000, 11]);
|
||||
|
||||
const edge = (from: number, wayId: number | null, lengthM = 100): RouteWayEdge => ({
|
||||
from,
|
||||
to: from + 1,
|
||||
lengthM,
|
||||
surface: 'paved',
|
||||
way: 'path',
|
||||
wayId,
|
||||
});
|
||||
|
||||
const grade = (id: number, values: Partial<TourTrailGrade>): TourTrailGrade => ({
|
||||
id,
|
||||
mtb_scale: null,
|
||||
mtb_scale_uphill: null,
|
||||
mtb_scale_imba: null,
|
||||
sac_scale: null,
|
||||
sac_scale_unclear: null,
|
||||
surface: null,
|
||||
highway: null,
|
||||
...values,
|
||||
});
|
||||
|
||||
// One stretch of each kind: T3 on gravel (twice as long), T1 on pebbles, unclear, untagged, unasked, no way,
|
||||
// and a T6 way on rock that strays from the tour's own line.
|
||||
const WAYS: RouteWays = {
|
||||
line: LINE,
|
||||
edges: [edge(0, 1, 200), edge(1, 2), edge(2, 3), edge(3, 4), edge(4, 5), edge(5, null), edge(6, 7)],
|
||||
};
|
||||
const GRADES = {
|
||||
byWay: new Map([
|
||||
[1, grade(1, { sac_scale: 'demanding_mountain_hiking', surface: 'gravel' })],
|
||||
[2, grade(2, { sac_scale: 'hiking', surface: 'pebblestone' })],
|
||||
[3, grade(3, { sac_scale_unclear: 'T3;T4', surface: 'gravel;ground' })],
|
||||
[7, grade(7, { sac_scale: 'difficult_alpine_hiking', surface: 'rock' })],
|
||||
]),
|
||||
unasked: new Set([5]),
|
||||
};
|
||||
const STRAYS = new Set([WAYS.edges[6]]);
|
||||
|
||||
describe('hikeFacets (MAP-03, MAP-04)', () => {
|
||||
it('FE-HIKE-FACET-001: reads each stretch to its class, and every unknown to its own reason, in every facet', () => {
|
||||
expect(WAYS.edges.map(facetClassifier(SAC_FACET, GRADES, STRAYS))).toEqual([
|
||||
't3',
|
||||
't1',
|
||||
'unclear',
|
||||
'untagged',
|
||||
'unasked',
|
||||
'unmatched',
|
||||
'unmatched',
|
||||
]);
|
||||
expect(WAYS.edges.map(facetClassifier(SURFACE_FACET, GRADES, STRAYS))).toEqual([
|
||||
'gravel',
|
||||
'gravel',
|
||||
'unclear',
|
||||
'untagged',
|
||||
'unasked',
|
||||
'unmatched',
|
||||
'unmatched',
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-002: the rows come in the facet order, the known first, each with what OpenStreetMap wrote', () => {
|
||||
const rows = facetRows(SAC_FACET, WAYS, GRADES, facetClassifier(SAC_FACET, GRADES, STRAYS));
|
||||
expect(rows.map((row) => [row.key, row.tags])).toEqual([
|
||||
['t1', ['hiking']],
|
||||
['t3', ['demanding_mountain_hiking']],
|
||||
['untagged', []],
|
||||
['unclear', ['T3;T4']],
|
||||
['unmatched', []],
|
||||
['unasked', []],
|
||||
]);
|
||||
expect(rows.find((row) => row.key === 't3')).toMatchObject({ lengthM: 200, share: 0.25 });
|
||||
// The stray T6 way is not the tour's: its grade is neither counted nor named.
|
||||
expect(rows.find((row) => row.key === 'unmatched')).toMatchObject({ lengthM: 200, tags: [] });
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-003: a group names every value it took, each once', () => {
|
||||
const rows = facetRows(SURFACE_FACET, WAYS, GRADES, facetClassifier(SURFACE_FACET, GRADES, STRAYS));
|
||||
expect(rows[0]).toMatchObject({ key: 'gravel', lengthM: 300, tags: ['gravel', 'pebblestone'] });
|
||||
expect(rows.find((row) => row.key === 'unclear')?.tags).toEqual(['gravel;ground']);
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-004: the known share counts the known classes only', () => {
|
||||
const rows = facetRows(SAC_FACET, WAYS, GRADES, facetClassifier(SAC_FACET, GRADES, STRAYS));
|
||||
expect(knownShareOf(rows)).toBeCloseTo(3 / 8);
|
||||
expect(knownShareOf([])).toBe(0);
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-005: tells the unknown classes from the known', () => {
|
||||
expect(['untagged', 'unclear', 'unmatched', 'unasked'].every(isUnknownClass)).toBe(true);
|
||||
expect(isUnknownClass('t1')).toBe(false);
|
||||
expect(isUnknownClass('gravel')).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-006: paints a known class in its facet colour and every unknown in the grey of the unknown', () => {
|
||||
expect(facetPaint(SAC_FACET, 't2')).toBe('t2');
|
||||
expect(facetPaint(SURFACE_FACET, 'gravel')).toBe('surface:gravel');
|
||||
expect(facetPaint(SURFACE_FACET, 'unasked')).toBe('unknown');
|
||||
expect(facetPaint(SAC_FACET, 'untagged')).toBe('unknown');
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-007: draws every class along the line in its paint, neighbours of a class joined', () => {
|
||||
const classOf = facetClassifier(SAC_FACET, GRADES, STRAYS);
|
||||
const overlay = facetOverlay(SAC_FACET, WAYS, facetRows(SAC_FACET, WAYS, GRADES, classOf), classOf);
|
||||
expect(overlay.paints).toEqual(['t1', 't3', 'unknown', 'unknown', 'unknown', 'unknown']);
|
||||
expect(overlay.lines).toHaveLength(6);
|
||||
// The no-way stretch and the stray one sit side by side and are one stretch of their class.
|
||||
expect(overlay.lines[4]).toEqual(LINE.slice(5, 8));
|
||||
// On the surface facet the two gravel ways side by side are one stretch.
|
||||
const surfaceOf = facetClassifier(SURFACE_FACET, GRADES, STRAYS);
|
||||
const surfaces = facetOverlay(SURFACE_FACET, WAYS, facetRows(SURFACE_FACET, WAYS, GRADES, surfaceOf), surfaceOf);
|
||||
expect(surfaces.lines[0]).toEqual(LINE.slice(0, 3));
|
||||
expect(surfaces.paints[0]).toBe('surface:gravel');
|
||||
});
|
||||
|
||||
it('FE-HIKE-FACET-008: draws one class alone when it is picked', () => {
|
||||
const classOf = facetClassifier(SAC_FACET, GRADES, STRAYS);
|
||||
const overlay = facetOverlay(SAC_FACET, WAYS, facetRows(SAC_FACET, WAYS, GRADES, classOf), classOf, 't3');
|
||||
expect(overlay).toEqual({ lines: [LINE.slice(0, 2)], paints: ['t3'] });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
import type { TourTrailGrade } from '@trek/shared';
|
||||
import { sharesBy, stretchesOf, type ClassShare, type RouteWayEdge, type RouteWays } from '../bike/routeWays';
|
||||
import type { TrailGrades } from '../bike/useRouteWays';
|
||||
import type { HikePaintKey } from './hikePaint';
|
||||
|
||||
/**
|
||||
* MAP-03 and MAP-04: what a hike's card reads along the line, one facet at a
|
||||
* time (the SAC grade, the surface, the kind of way), from the values
|
||||
* OpenStreetMap records for the ways under it. Each facet sorts a way's value
|
||||
* into its own classes; what is not known stays unknown, for the same four
|
||||
* reasons in every facet: the way carries no such value (`untagged`), one
|
||||
* the facet cannot place (`unclear`), no way lies clearly under the tour's
|
||||
* line (`unmatched`), or OpenStreetMap could not be asked this time
|
||||
* (`unasked`). The router's own reading of a way is never one of the values:
|
||||
* it may be normalised or a default. Pure, so the classes, their shares and
|
||||
* their stretches are tested apart from the requests.
|
||||
*/
|
||||
|
||||
export type UnknownClass = 'untagged' | 'unclear' | 'unmatched' | 'unasked';
|
||||
|
||||
const UNKNOWN_CLASSES: readonly UnknownClass[] = ['untagged', 'unclear', 'unmatched', 'unasked'];
|
||||
|
||||
export function isUnknownClass(key: string): key is UnknownClass {
|
||||
return (UNKNOWN_CLASSES as readonly string[]).includes(key);
|
||||
}
|
||||
|
||||
/** What a facet reads of one way: the class its value falls in, and the value as OpenStreetMap wrote it. */
|
||||
interface FacetReading<K extends string> {
|
||||
key: K | 'untagged' | 'unclear';
|
||||
written: string | null;
|
||||
}
|
||||
|
||||
export interface HikeFacetSpec<K extends string> {
|
||||
/** The known classes, in the order the card lists them. */
|
||||
classes: readonly K[];
|
||||
read(grade: TourTrailGrade | undefined): FacetReading<K>;
|
||||
/** The paint a known class is drawn in; the unknown is always the grey of the unknown. */
|
||||
paint(key: K): HikePaintKey;
|
||||
}
|
||||
|
||||
type FacetPaint = HikePaintKey | 'unknown';
|
||||
|
||||
export interface FacetRow<K extends string = string> extends ClassShare<K | UnknownClass> {
|
||||
/** What OpenStreetMap wrote for this class's ways, each value once; none for a way not matched or not asked. */
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
type Grades = Pick<TrailGrades, 'byWay' | 'unasked'>;
|
||||
|
||||
/** How each stretch of the matched line reads in one facet, given the edges that stray from the tour's own line. */
|
||||
export function facetClassifier<K extends string>(
|
||||
spec: HikeFacetSpec<K>,
|
||||
grades: Grades,
|
||||
strays: ReadonlySet<RouteWayEdge>
|
||||
): (edge: RouteWayEdge) => K | UnknownClass {
|
||||
return (edge) => {
|
||||
if (edge.wayId === null || strays.has(edge)) return 'unmatched';
|
||||
if (grades.unasked.has(edge.wayId)) return 'unasked';
|
||||
return spec.read(grades.byWay.get(edge.wayId)).key;
|
||||
};
|
||||
}
|
||||
|
||||
/** The paint of a class in its facet. */
|
||||
export function facetPaint<K extends string>(spec: HikeFacetSpec<K>, key: K | UnknownClass): FacetPaint {
|
||||
return isUnknownClass(key) ? 'unknown' : spec.paint(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* The share of each class along the line in the card's order, the known
|
||||
* classes first, each with the values OpenStreetMap wrote for it; classes the
|
||||
* line has none of are left out.
|
||||
*/
|
||||
export function facetRows<K extends string>(
|
||||
spec: HikeFacetSpec<K>,
|
||||
ways: RouteWays,
|
||||
grades: Grades,
|
||||
classOf: (edge: RouteWayEdge) => K | UnknownClass
|
||||
): FacetRow<K>[] {
|
||||
const order: readonly string[] = [...spec.classes, ...UNKNOWN_CLASSES];
|
||||
const tags = new Map<string, Set<string>>();
|
||||
for (const edge of ways.edges) {
|
||||
const key = classOf(edge);
|
||||
if (edge.wayId === null || key === 'unmatched' || key === 'unasked') continue;
|
||||
const written = spec.read(grades.byWay.get(edge.wayId)).written;
|
||||
if (written) tags.set(key, (tags.get(key) ?? new Set()).add(written));
|
||||
}
|
||||
return sharesBy(ways, classOf)
|
||||
.sort((a, b) => order.indexOf(a.key) - order.indexOf(b.key))
|
||||
.map((row) => ({ ...row, tags: [...(tags.get(row.key) ?? [])] }));
|
||||
}
|
||||
|
||||
/** Of the whole line, 0 to 1, the share whose class is known. */
|
||||
export function knownShareOf(rows: readonly FacetRow[]): number {
|
||||
return rows.filter((row) => !isUnknownClass(row.key)).reduce((sum, row) => sum + row.share, 0);
|
||||
}
|
||||
|
||||
interface FacetOverlay {
|
||||
lines: [number, number][][];
|
||||
paints: FacetPaint[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Every class's stretches with their paints, for drawing the facet along the
|
||||
* whole line at once; `only` draws one class alone.
|
||||
*/
|
||||
export function facetOverlay<K extends string>(
|
||||
spec: HikeFacetSpec<K>,
|
||||
ways: RouteWays,
|
||||
rows: readonly FacetRow<K>[],
|
||||
classOf: (edge: RouteWayEdge) => K | UnknownClass,
|
||||
only: string | null = null
|
||||
): FacetOverlay {
|
||||
const lines: [number, number][][] = [];
|
||||
const paints: FacetPaint[] = [];
|
||||
for (const row of rows) {
|
||||
if (only && row.key !== only) continue;
|
||||
for (const line of stretchesOf(ways, (edge) => classOf(edge) === row.key)) {
|
||||
lines.push(line);
|
||||
paints.push(facetPaint(spec, row.key));
|
||||
}
|
||||
}
|
||||
return { lines, paints };
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { useMapPaint } from '../../Map/mapPaint';
|
||||
|
||||
/**
|
||||
* MAP-03: the colours of the SAC grades along a hike (styles/hike.css), by
|
||||
* name. The card takes the CSS variable, which follows the theme on its own;
|
||||
* the map reads them resolved (`useHikePaint`). A way without a known grade
|
||||
* MAP-03 and MAP-04: the colours of what a hike's card draws along the line
|
||||
* (styles/hike.css), by name: the SAC grades, the surfaces and the kinds of
|
||||
* way. The card takes the CSS variable, which follows the theme on its own;
|
||||
* the map reads them resolved (`useHikePaint`). A way whose class is unknown
|
||||
* is drawn in the bike paints' grey of the unknown, the one grey the route
|
||||
* paints use for what the data does not say.
|
||||
* paints use for what the data does not say. The surface and way keys carry
|
||||
* their facet's prefix, so they never meet the bike paints' own names.
|
||||
*/
|
||||
const HIKE_PAINT_TOKENS = {
|
||||
t1: '--hike-sac-t1',
|
||||
@@ -14,6 +16,25 @@ const HIKE_PAINT_TOKENS = {
|
||||
t4: '--hike-sac-t4',
|
||||
t5: '--hike-sac-t5',
|
||||
t6: '--hike-sac-t6',
|
||||
'surface:paved': '--hike-surface-paved',
|
||||
'surface:sett': '--hike-surface-sett',
|
||||
'surface:compacted': '--hike-surface-compacted',
|
||||
'surface:gravel': '--hike-surface-gravel',
|
||||
'surface:ground': '--hike-surface-ground',
|
||||
'surface:grass': '--hike-surface-grass',
|
||||
'surface:rock': '--hike-surface-rock',
|
||||
'surface:wood': '--hike-surface-wood',
|
||||
'surface:unpaved': '--hike-surface-unpaved',
|
||||
'surface:other': '--hike-surface-other',
|
||||
'way:path': '--hike-way-path',
|
||||
'way:footway': '--hike-way-footway',
|
||||
'way:steps': '--hike-way-steps',
|
||||
'way:track': '--hike-way-track',
|
||||
'way:cycleway': '--hike-way-cycleway',
|
||||
'way:quietRoad': '--hike-way-quiet-road',
|
||||
'way:road': '--hike-way-road',
|
||||
'way:viaFerrata': '--hike-way-via-ferrata',
|
||||
'way:other': '--hike-way-other',
|
||||
} as const;
|
||||
|
||||
export type HikePaintKey = keyof typeof HIKE_PAINT_TOKENS;
|
||||
|
||||
@@ -1,115 +1,51 @@
|
||||
import type { TourTrailGrade } from '@trek/shared';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import type { RouteWayEdge, RouteWays } from '../bike/routeWays';
|
||||
import {
|
||||
highestStep,
|
||||
isSacStep,
|
||||
sacClassifier,
|
||||
sacOverlay,
|
||||
sacPaintKey,
|
||||
sacShares,
|
||||
sacStepOfClass,
|
||||
sacTagOf,
|
||||
unclearTags,
|
||||
} from './sacGrades';
|
||||
import { highestStep, SAC_FACET, sacStepOfClass } from './sacGrades';
|
||||
|
||||
// FE-HIKE-SAC-001 to FE-HIKE-SAC-007
|
||||
// FE-HIKE-SAC-001 to FE-HIKE-SAC-004
|
||||
|
||||
const LINE: [number, number][] = Array.from({ length: 8 }, (_, index): [number, number] => [47 + index / 1000, 11]);
|
||||
|
||||
const edge = (from: number, wayId: number | null, lengthM = 100): RouteWayEdge => ({
|
||||
from,
|
||||
to: from + 1,
|
||||
lengthM,
|
||||
surface: 'unknown',
|
||||
way: 'path',
|
||||
wayId,
|
||||
});
|
||||
|
||||
const grade = (id: number, sac: Partial<Pick<TourTrailGrade, 'sac_scale' | 'sac_scale_unclear'>>): TourTrailGrade => ({
|
||||
id,
|
||||
const grade = (values: Partial<TourTrailGrade>): TourTrailGrade => ({
|
||||
id: 1,
|
||||
mtb_scale: null,
|
||||
mtb_scale_uphill: null,
|
||||
mtb_scale_imba: null,
|
||||
sac_scale: null,
|
||||
sac_scale_unclear: null,
|
||||
...sac,
|
||||
surface: null,
|
||||
highway: null,
|
||||
...values,
|
||||
});
|
||||
|
||||
// One stretch of each kind: T3 (twice as long), T1, unclear, untagged, unasked, no way, a T6 way that strays.
|
||||
const WAYS: RouteWays = {
|
||||
line: LINE,
|
||||
edges: [
|
||||
edge(0, 1, 200),
|
||||
edge(1, 2),
|
||||
edge(2, 3),
|
||||
edge(3, 4),
|
||||
edge(4, 5),
|
||||
edge(5, null),
|
||||
edge(6, 7),
|
||||
],
|
||||
};
|
||||
const GRADES = {
|
||||
byWay: new Map([
|
||||
[1, grade(1, { sac_scale: 'demanding_mountain_hiking' })],
|
||||
[2, grade(2, { sac_scale: 'hiking' })],
|
||||
[3, grade(3, { sac_scale_unclear: 'T3;T4' })],
|
||||
[7, grade(7, { sac_scale: 'difficult_alpine_hiking' })],
|
||||
]),
|
||||
unasked: new Set([5]),
|
||||
};
|
||||
const STRAYS = new Set([WAYS.edges[6]]);
|
||||
|
||||
describe('sacGrades (MAP-03)', () => {
|
||||
it('FE-HIKE-SAC-001: reads each stretch to its step, and every unknown to its own reason, never to T1', () => {
|
||||
const classOf = sacClassifier(GRADES, STRAYS);
|
||||
expect(WAYS.edges.map(classOf)).toEqual(['t3', 't1', 'unclear', 'untagged', 'unasked', 'unmatched', 'unmatched']);
|
||||
it('FE-HIKE-SAC-001: reads a way to its step as OpenStreetMap wrote it, an unclear grade as unclear, none as untagged', () => {
|
||||
expect(SAC_FACET.read(grade({ sac_scale: 'demanding_mountain_hiking' }))).toEqual({
|
||||
key: 't3',
|
||||
written: 'demanding_mountain_hiking',
|
||||
});
|
||||
expect(SAC_FACET.read(grade({ sac_scale: 'hiking' })).key).toBe('t1');
|
||||
expect(SAC_FACET.read(grade({ sac_scale_unclear: 'T3;T4' }))).toEqual({ key: 'unclear', written: 'T3;T4' });
|
||||
// A surface or a kind of way says nothing about the grade.
|
||||
expect(SAC_FACET.read(grade({ surface: 'rock', highway: 'path' }))).toEqual({ key: 'untagged', written: null });
|
||||
expect(SAC_FACET.read(undefined)).toEqual({ key: 'untagged', written: null });
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-002: the shares come in the scale order, the steps first, then the unknown', () => {
|
||||
const rows = sacShares(WAYS, sacClassifier(GRADES, STRAYS));
|
||||
expect(rows.map((row) => row.key)).toEqual(['t1', 't3', 'untagged', 'unclear', 'unmatched', 'unasked']);
|
||||
expect(rows.find((row) => row.key === 't3')).toMatchObject({ lengthM: 200, share: 0.25 });
|
||||
expect(rows.find((row) => row.key === 'unmatched')?.lengthM).toBe(200);
|
||||
it('FE-HIKE-SAC-002: names a step by its class and paints it in its own colour', () => {
|
||||
expect(SAC_FACET.classes).toEqual(['t1', 't2', 't3', 't4', 't5', 't6']);
|
||||
expect(SAC_FACET.paint('t2')).toBe('t2');
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-003: names a step by its class, its value in OpenStreetMap and its paint', () => {
|
||||
expect(isSacStep('t4')).toBe(true);
|
||||
expect(isSacStep('unclear')).toBe(false);
|
||||
expect(isSacStep('untagged')).toBe(false);
|
||||
it('FE-HIKE-SAC-003: a class stands for its step, the unknown and other facets for none', () => {
|
||||
expect(sacStepOfClass('t6')).toBe(6);
|
||||
expect(sacStepOfClass('t1')).toBe(1);
|
||||
expect(sacStepOfClass('untagged')).toBeNull();
|
||||
expect(sacTagOf('t1')).toBe('hiking');
|
||||
expect(sacTagOf('t6')).toBe('difficult_alpine_hiking');
|
||||
expect(sacPaintKey('t2')).toBe('t2');
|
||||
expect(sacPaintKey('unasked')).toBe('unknown');
|
||||
expect(sacStepOfClass('unclear')).toBeNull();
|
||||
expect(sacStepOfClass('gravel')).toBeNull();
|
||||
expect(sacStepOfClass('t7')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-004: hands on an unclear grade as OpenStreetMap wrote it, each value once', () => {
|
||||
const twice: RouteWays = { line: LINE, edges: [edge(0, 3), edge(1, 3), edge(2, 1)] };
|
||||
expect(unclearTags(twice, GRADES, () => true)).toEqual(['T3;T4']);
|
||||
expect(unclearTags(twice, GRADES, () => false)).toEqual([]);
|
||||
expect(unclearTags({ line: LINE, edges: [edge(0, null)] }, GRADES, () => true)).toEqual([]);
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-005: the highest step known, or none where no step is', () => {
|
||||
const rows = sacShares(WAYS, sacClassifier(GRADES, STRAYS));
|
||||
expect(highestStep(rows)).toBe(3);
|
||||
expect(highestStep(rows.filter((row) => !isSacStep(row.key)))).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-006: draws every class along the line in its paint, neighbours of a class joined', () => {
|
||||
const classOf = sacClassifier(GRADES, STRAYS);
|
||||
const overlay = sacOverlay(WAYS, sacShares(WAYS, classOf), classOf);
|
||||
expect(overlay.paints).toEqual(['t1', 't3', 'unknown', 'unknown', 'unknown', 'unknown']);
|
||||
expect(overlay.lines).toHaveLength(6);
|
||||
// The no-way stretch and the stray one sit side by side and are one stretch of their class.
|
||||
expect(overlay.lines[4]).toEqual(LINE.slice(5, 8));
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-007: draws one class alone when it is picked', () => {
|
||||
const classOf = sacClassifier(GRADES, STRAYS);
|
||||
const overlay = sacOverlay(WAYS, sacShares(WAYS, classOf), classOf, 't3');
|
||||
expect(overlay).toEqual({ lines: [LINE.slice(0, 2)], paints: ['t3'] });
|
||||
it('FE-HIKE-SAC-004: the highest step known, or none where no step is', () => {
|
||||
expect(highestStep([{ key: 't1' }, { key: 't3' }, { key: 'untagged' }])).toBe(3);
|
||||
expect(highestStep([{ key: 'unclear' }, { key: 'unmatched' }])).toBeNull();
|
||||
expect(highestStep([])).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,122 +1,36 @@
|
||||
import { SAC_SCALE_VALUES, sacStepOf, type SacScale, type TourTrailGrade } from '@trek/shared';
|
||||
import { sharesBy, stretchesOf, type ClassShare, type RouteWayEdge, type RouteWays } from '../bike/routeWays';
|
||||
import type { TrailGrades } from '../bike/useRouteWays';
|
||||
import type { HikePaintKey } from './hikePaint';
|
||||
import { sacStepOf } from '@trek/shared';
|
||||
import type { HikeFacetSpec } from './hikeFacets';
|
||||
|
||||
/**
|
||||
* MAP-03: the SAC grades along a hike, read stretch by stretch from the
|
||||
* `sac_scale` OpenStreetMap gives the ways under it. Pure, so the classes,
|
||||
* their shares and their stretches are tested apart from the requests.
|
||||
*
|
||||
* A step (`t1` to `t6`) is only ever what OpenStreetMap wrote for that way.
|
||||
* Everything else is unknown, and the reason stays apart: the way has no
|
||||
* grade (`untagged`), a grade that names no single step (`unclear`), no way
|
||||
* clearly under the tour's line (`unmatched`), or OpenStreetMap could not be
|
||||
* asked this time (`unasked`). None of them is ever read as T1.
|
||||
* `sac_scale` OpenStreetMap gives the ways under it. A step (`t1` to `t6`)
|
||||
* is only ever what OpenStreetMap wrote for that way; a grade that names no
|
||||
* single step is unclear, and none of the unknown is ever read as T1.
|
||||
*/
|
||||
|
||||
type SacStepClass = 't1' | 't2' | 't3' | 't4' | 't5' | 't6';
|
||||
type SacUnknownClass = 'untagged' | 'unclear' | 'unmatched' | 'unasked';
|
||||
export type SacClass = SacStepClass | SacUnknownClass;
|
||||
|
||||
/** The order the card lists the classes in: the steps from easy to hard, then the unknown. */
|
||||
const SAC_CLASSES: readonly SacClass[] = [
|
||||
't1',
|
||||
't2',
|
||||
't3',
|
||||
't4',
|
||||
't5',
|
||||
't6',
|
||||
'untagged',
|
||||
'unclear',
|
||||
'unmatched',
|
||||
'unasked',
|
||||
];
|
||||
|
||||
export function isSacStep(key: SacClass): key is SacStepClass {
|
||||
function isSacStep(key: string): key is SacStepClass {
|
||||
return /^t[1-6]$/.test(key);
|
||||
}
|
||||
|
||||
/** The step a class stands for, 1 to 6; null for the unknown. */
|
||||
export function sacStepOfClass(key: SacClass): number | null {
|
||||
export function sacStepOfClass(key: string): number | null {
|
||||
return isSacStep(key) ? Number(key.slice(1)) : null;
|
||||
}
|
||||
|
||||
/** The `sac_scale` value OpenStreetMap writes for a step's class. */
|
||||
export function sacTagOf(key: SacStepClass): SacScale {
|
||||
return SAC_SCALE_VALUES[Number(key.slice(1)) - 1];
|
||||
}
|
||||
|
||||
/** The paint of a class: the step's own, and the grey of the unknown for every kind of unknown. */
|
||||
export function sacPaintKey(key: SacClass): HikePaintKey | 'unknown' {
|
||||
return isSacStep(key) ? key : 'unknown';
|
||||
}
|
||||
|
||||
function stepClassOf(grade: TourTrailGrade | undefined): SacClass {
|
||||
if (grade?.sac_scale) return `t${sacStepOf(grade.sac_scale)}`;
|
||||
return grade?.sac_scale_unclear ? 'unclear' : 'untagged';
|
||||
}
|
||||
|
||||
/** How each stretch of the matched line reads, given the grades and the edges that stray from the tour's own line. */
|
||||
export function sacClassifier(
|
||||
grades: Pick<TrailGrades, 'byWay' | 'unasked'>,
|
||||
strays: ReadonlySet<RouteWayEdge>
|
||||
): (edge: RouteWayEdge) => SacClass {
|
||||
return (edge) => {
|
||||
if (edge.wayId === null || strays.has(edge)) return 'unmatched';
|
||||
if (grades.unasked.has(edge.wayId)) return 'unasked';
|
||||
return stepClassOf(grades.byWay.get(edge.wayId));
|
||||
};
|
||||
}
|
||||
|
||||
/** The share of each class along the line, in the card's order; classes the line has none of are left out. */
|
||||
export function sacShares(ways: RouteWays, classOf: (edge: RouteWayEdge) => SacClass): ClassShare<SacClass>[] {
|
||||
return sharesBy(ways, classOf).sort((a, b) => SAC_CLASSES.indexOf(a.key) - SAC_CLASSES.indexOf(b.key));
|
||||
}
|
||||
|
||||
/** The grade's tags as OpenStreetMap wrote them where they name no single step, each once. */
|
||||
export function unclearTags(
|
||||
ways: RouteWays,
|
||||
grades: Pick<TrailGrades, 'byWay'>,
|
||||
inClass: (edge: RouteWayEdge) => boolean
|
||||
): string[] {
|
||||
const tags = new Set<string>();
|
||||
for (const edge of ways.edges) {
|
||||
const written = edge.wayId === null ? null : grades.byWay.get(edge.wayId)?.sac_scale_unclear;
|
||||
if (written && inClass(edge)) tags.add(written);
|
||||
}
|
||||
return [...tags];
|
||||
}
|
||||
|
||||
/** The highest step known anywhere along the line, or null where none is. */
|
||||
export function highestStep(rows: readonly ClassShare<SacClass>[]): number | null {
|
||||
/** The highest step known among the classes along the line, or null where none is. */
|
||||
export function highestStep(rows: readonly { key: string }[]): number | null {
|
||||
const steps = rows.map((row) => sacStepOfClass(row.key)).filter((step): step is number => step !== null);
|
||||
return steps.length ? Math.max(...steps) : null;
|
||||
}
|
||||
|
||||
interface SacOverlay {
|
||||
lines: [number, number][][];
|
||||
paints: (HikePaintKey | 'unknown')[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Every class's stretches with their paints, for drawing the grades along the
|
||||
* whole line at once; `only` draws one class alone.
|
||||
*/
|
||||
export function sacOverlay(
|
||||
ways: RouteWays,
|
||||
rows: readonly ClassShare<SacClass>[],
|
||||
classOf: (edge: RouteWayEdge) => SacClass,
|
||||
only: SacClass | null = null
|
||||
): SacOverlay {
|
||||
const lines: [number, number][][] = [];
|
||||
const paints: (HikePaintKey | 'unknown')[] = [];
|
||||
for (const row of rows) {
|
||||
if (only && row.key !== only) continue;
|
||||
for (const line of stretchesOf(ways, (edge) => classOf(edge) === row.key)) {
|
||||
lines.push(line);
|
||||
paints.push(sacPaintKey(row.key));
|
||||
}
|
||||
}
|
||||
return { lines, paints };
|
||||
}
|
||||
export const SAC_FACET: HikeFacetSpec<SacStepClass> = {
|
||||
classes: ['t1', 't2', 't3', 't4', 't5', 't6'],
|
||||
read: (grade) => {
|
||||
if (grade?.sac_scale) return { key: `t${sacStepOf(grade.sac_scale)}`, written: grade.sac_scale };
|
||||
if (grade?.sac_scale_unclear) return { key: 'unclear', written: grade.sac_scale_unclear };
|
||||
return { key: 'untagged', written: null };
|
||||
},
|
||||
paint: (key) => key,
|
||||
};
|
||||
|
||||
+65
-21
@@ -4,7 +4,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { useTourRouteHighlightStore } from '../../../store/tourRouteHighlightStore';
|
||||
import type { RouteWayEdge, RouteWays } from '../bike/routeWays';
|
||||
import type { RouteWaysState } from '../bike/useRouteWays';
|
||||
import { useSacGrades } from './useSacGrades';
|
||||
import { useHikeWays } from './useHikeWays';
|
||||
|
||||
const hook = vi.hoisted(() => ({
|
||||
state: null as unknown,
|
||||
@@ -18,7 +18,7 @@ vi.mock('../bike/useRouteWays', () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
// FE-HIKE-SAC-HOOK-001 to FE-HIKE-SAC-HOOK-008
|
||||
// FE-HIKE-WAYS-HOOK-001 to FE-HIKE-WAYS-HOOK-011
|
||||
|
||||
const ROUTE: [number, number][] = [
|
||||
[47.0, 11.0],
|
||||
@@ -30,19 +30,22 @@ const edge = (from: number, wayId: number): RouteWayEdge => ({
|
||||
from,
|
||||
to: from + 1,
|
||||
lengthM: 100,
|
||||
surface: 'unknown',
|
||||
// The router's own reading, which no facet may take for a recorded value.
|
||||
surface: 'paved',
|
||||
way: 'path',
|
||||
wayId,
|
||||
});
|
||||
const WAYS: RouteWays = { line: ROUTE, edges: [edge(0, 1), edge(1, 2), edge(2, 3)] };
|
||||
const grade = (id: number, sac: Partial<TourTrailGrade>): TourTrailGrade => ({
|
||||
const grade = (id: number, values: Partial<TourTrailGrade>): TourTrailGrade => ({
|
||||
id,
|
||||
mtb_scale: null,
|
||||
mtb_scale_uphill: null,
|
||||
mtb_scale_imba: null,
|
||||
sac_scale: null,
|
||||
sac_scale_unclear: null,
|
||||
...sac,
|
||||
surface: null,
|
||||
highway: null,
|
||||
...values,
|
||||
});
|
||||
const READY: RouteWaysState = {
|
||||
status: 'ready',
|
||||
@@ -50,8 +53,8 @@ const READY: RouteWaysState = {
|
||||
grades: {
|
||||
status: 'ready',
|
||||
byWay: new Map([
|
||||
[1, grade(1, { sac_scale: 'mountain_hiking' })],
|
||||
[2, grade(2, { sac_scale: 'alpine_hiking' })],
|
||||
[1, grade(1, { sac_scale: 'mountain_hiking', surface: 'gravel', highway: 'track' })],
|
||||
[2, grade(2, { sac_scale: 'alpine_hiking', surface: 'rock', highway: 'path' })],
|
||||
]),
|
||||
unasked: new Set(),
|
||||
},
|
||||
@@ -59,7 +62,7 @@ const READY: RouteWaysState = {
|
||||
|
||||
function shownHook(state: RouteWaysState, route: [number, number][] | null = ROUTE) {
|
||||
hook.state = state;
|
||||
const view = renderHook((props: { route: [number, number][] | null }) => useSacGrades({ tripId: 5, ...props }), {
|
||||
const view = renderHook((props: { route: [number, number][] | null }) => useHikeWays({ tripId: 5, ...props }), {
|
||||
initialProps: { route },
|
||||
});
|
||||
act(() => view.result.current.toggle());
|
||||
@@ -73,19 +76,19 @@ beforeEach(() => {
|
||||
useTourRouteHighlightStore.getState().clear();
|
||||
});
|
||||
|
||||
describe('useSacGrades (MAP-03)', () => {
|
||||
it('FE-HIKE-SAC-HOOK-001: without a line of two points there is nothing to grade and nothing is asked', () => {
|
||||
describe('useHikeWays (MAP-03, MAP-04)', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-001: without a line of two points there is nothing to read and nothing is asked', () => {
|
||||
for (const route of [null, ROUTE.slice(0, 1)]) {
|
||||
const { result } = renderHook(() => useSacGrades({ tripId: 5, route }));
|
||||
const { result } = renderHook(() => useHikeWays({ tripId: 5, route }));
|
||||
expect(result.current.available).toBe(false);
|
||||
expect(result.current.status).toBe('off');
|
||||
expect(hook.input).toMatchObject({ active: false });
|
||||
}
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-002: off until switched on, then asks for the ways on foot with their grades', () => {
|
||||
const { result } = renderHook(() => useSacGrades({ tripId: 5, route: ROUTE }));
|
||||
expect(result.current).toMatchObject({ available: true, shown: false, status: 'off', rows: [] });
|
||||
it('FE-HIKE-WAYS-HOOK-002: off until switched on, then asks for the ways on foot with what OpenStreetMap records', () => {
|
||||
const { result } = renderHook(() => useHikeWays({ tripId: 5, route: ROUTE }));
|
||||
expect(result.current).toMatchObject({ available: true, shown: false, status: 'off', facet: 'sac', rows: [] });
|
||||
expect(hook.input).toMatchObject({ tripId: 5, active: false, route: ROUTE, matching: 'foot', withGrades: true });
|
||||
hook.state = { status: 'loading', ways: null, grades: null };
|
||||
act(() => result.current.toggle());
|
||||
@@ -94,7 +97,7 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
expect(hook.input).toMatchObject({ active: true });
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-003: says when the ways or the grades are on their way, offline or failed', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-003: says when the ways or their values are on their way, offline or failed', () => {
|
||||
const cases: [RouteWaysState, string][] = [
|
||||
[{ status: 'offline', ways: null, grades: null }, 'offline'],
|
||||
[{ status: 'failed', ways: null, grades: null }, 'failed'],
|
||||
@@ -111,7 +114,7 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
expect(useTourRouteHighlightStore.getState().highlight).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-004: lists the known steps and the unknown, and draws them all along the line', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-004: lists the known SAC steps and the unknown, and draws them all along the line', () => {
|
||||
const { result } = shownHook(READY);
|
||||
expect(result.current.status).toBe('ready');
|
||||
expect(result.current.rows.map((row) => [row.key, row.tags])).toEqual([
|
||||
@@ -128,7 +131,7 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-005: a picked class is drawn alone, picked again every class is drawn again', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-005: a picked class is drawn alone, picked again every class is drawn again', () => {
|
||||
const { result } = shownHook(READY);
|
||||
act(() => result.current.focusOn('t4'));
|
||||
expect(result.current.focus).toBe('t4');
|
||||
@@ -142,7 +145,7 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
expect(useTourRouteHighlightStore.getState().highlight?.id).toBe('sac');
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-006: a hike nobody could be asked about fails once with a retry rather than one unknown row', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-006: a hike nobody could be asked about fails once with a retry rather than one unknown row', () => {
|
||||
const { result } = shownHook({
|
||||
...READY,
|
||||
grades: { status: 'ready', byWay: new Map(), unasked: new Set([1, 2, 3]) },
|
||||
@@ -154,7 +157,7 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
expect(hook.retry).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-007: switched off or left, the map is as it was; a way off the line is never graded', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-007: switched off or left, the map is as it was; a way off the line is never read', () => {
|
||||
const { result, unmount } = shownHook(READY);
|
||||
act(() => result.current.focusOn('t2'));
|
||||
act(() => result.current.toggle());
|
||||
@@ -171,11 +174,14 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
const off = shownHook(READY, elsewhere);
|
||||
expect(off.result.current.rows.map((row) => row.key)).toEqual(['unmatched']);
|
||||
expect(off.result.current.highest).toBeNull();
|
||||
act(() => off.result.current.chooseFacet('surface'));
|
||||
expect(off.result.current.rows.map((row) => row.key)).toEqual(['unmatched']);
|
||||
expect(off.result.current.mostly).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-SAC-HOOK-008: while the line is planned anew the card waits for it and grades nothing of the old one', () => {
|
||||
it('FE-HIKE-WAYS-HOOK-008: while the line is planned anew the card waits for it and reads nothing of the old one', () => {
|
||||
hook.state = READY;
|
||||
const { result } = renderHook(() => useSacGrades({ tripId: 5, route: ROUTE, current: false }));
|
||||
const { result } = renderHook(() => useHikeWays({ tripId: 5, route: ROUTE, current: false }));
|
||||
act(() => result.current.toggle());
|
||||
expect(result.current.available).toBe(true);
|
||||
expect(result.current.status).toBe('loading');
|
||||
@@ -183,4 +189,42 @@ describe('useSacGrades (MAP-03)', () => {
|
||||
expect(hook.input).toMatchObject({ active: true, route: null });
|
||||
expect(useTourRouteHighlightStore.getState().highlight).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYS-HOOK-009: the surfaces are the recorded ones, never the router reading, and draw in their own paints', () => {
|
||||
const { result } = shownHook(READY);
|
||||
act(() => result.current.chooseFacet('surface'));
|
||||
expect(result.current.facet).toBe('surface');
|
||||
expect(result.current.rows.map((row) => [row.key, row.tags])).toEqual([
|
||||
['gravel', ['gravel']],
|
||||
['rock', ['rock']],
|
||||
['untagged', []],
|
||||
]);
|
||||
expect(result.current.highest).toBeNull();
|
||||
expect(result.current.mostly?.key).toBe('gravel');
|
||||
expect(useTourRouteHighlightStore.getState().highlight).toMatchObject({
|
||||
id: 'surface',
|
||||
paints: ['surface:gravel', 'surface:rock', 'unknown'],
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYS-HOOK-010: the kinds of way sit apart from the surfaces and the grades; a new facet lets the picked class go', () => {
|
||||
const { result } = shownHook(READY);
|
||||
act(() => result.current.focusOn('t2'));
|
||||
act(() => result.current.chooseFacet('way'));
|
||||
expect(result.current.focus).toBeNull();
|
||||
expect(result.current.rows.map((row) => row.key)).toEqual(['path', 'track', 'untagged']);
|
||||
expect(useTourRouteHighlightStore.getState().highlight?.id).toBe('way');
|
||||
act(() => result.current.focusOn('track'));
|
||||
expect(useTourRouteHighlightStore.getState().highlight).toMatchObject({ id: 'way-track', paints: ['way:track'] });
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYS-HOOK-011: every facet is read from the one answer, asked once on foot', () => {
|
||||
const { result } = shownHook(READY);
|
||||
const asked = hook.input;
|
||||
act(() => result.current.chooseFacet('surface'));
|
||||
act(() => result.current.chooseFacet('way'));
|
||||
// The same question on every render: switching facets asks nothing new.
|
||||
expect(hook.input).toEqual(asked);
|
||||
expect(result.current.status).toBe('ready');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useTourRouteHighlightStore } from '../../../store/tourRouteHighlightStore';
|
||||
import type { RouteWayEdge } from '../bike/routeWays';
|
||||
import { useRouteWays, type RouteWaysState } from '../bike/useRouteWays';
|
||||
import { HIKE_FACETS, type HikeFacet } from './hikeFacetSpecs';
|
||||
import { facetClassifier, facetOverlay, facetRows, isUnknownClass, knownShareOf, type FacetRow } from './hikeFacets';
|
||||
import { straysFrom } from './routeMatch';
|
||||
import { highestStep } from './sacGrades';
|
||||
|
||||
type HikeWaysStatus = 'off' | 'loading' | 'offline' | 'failed' | 'ready';
|
||||
|
||||
export interface HikeWays {
|
||||
/** Whether the card has a line to describe at all. */
|
||||
available: boolean;
|
||||
/** Whether the card is switched on: only then is anything asked for and drawn on the map. */
|
||||
shown: boolean;
|
||||
toggle: () => void;
|
||||
facet: HikeFacet;
|
||||
chooseFacet: (facet: HikeFacet) => void;
|
||||
status: HikeWaysStatus;
|
||||
/** The facet's classes along the line, the known first. */
|
||||
rows: FacetRow[];
|
||||
/** Of the whole line, 0 to 1, the share whose class is known. */
|
||||
knownShare: number;
|
||||
/** The known class covering the most of the line, or null. */
|
||||
mostly: FacetRow | null;
|
||||
/** On the SAC facet, the highest step known along the line, or null. */
|
||||
highest: number | null;
|
||||
/** The class drawn alone on the map, or null while every class is. */
|
||||
focus: string | null;
|
||||
focusOn: (key: string) => void;
|
||||
retry: () => void;
|
||||
}
|
||||
|
||||
const NO_STRAYS: ReadonlySet<RouteWayEdge> = new Set();
|
||||
|
||||
function statusOf(state: RouteWaysState, nothingAnswered: boolean): HikeWaysStatus {
|
||||
if (state.status === 'idle') return 'off';
|
||||
if (state.status !== 'ready') return state.status;
|
||||
const grades = state.grades?.status ?? 'failed';
|
||||
if (grades === 'ready') return nothingAnswered ? 'failed' : 'ready';
|
||||
return grades;
|
||||
}
|
||||
|
||||
function mostlyOf(rows: readonly FacetRow[]): FacetRow | null {
|
||||
return rows.reduce<FacetRow | null>(
|
||||
(best, row) => (!isUnknownClass(row.key) && (!best || row.lengthM > best.lengthM) ? row : best),
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* MAP-03 and MAP-04: what OpenStreetMap records for the ways along a hike,
|
||||
* switched on by the traveller. One read serves every facet: the ways under
|
||||
* the line are asked of the router once, matched on foot, and their values of
|
||||
* OpenStreetMap once, and each stretch is placed against the tour's own line
|
||||
* once; the facet picked (SAC grade, surface, kind of way) only decides how
|
||||
* the stretches are sorted and drawn along the line on the planner map,
|
||||
* every class at once or the one picked alone. Nothing here changes the
|
||||
* tour, its places or its days; switched off, the map is as it was.
|
||||
*/
|
||||
export function useHikeWays(input: {
|
||||
tripId: number | string;
|
||||
/** The tour's own line, or null while there is none. */
|
||||
route: [number, number][] | null;
|
||||
/** False while the line is being planned anew: the card stays, and waits for the new one. */
|
||||
current?: boolean;
|
||||
}): HikeWays {
|
||||
const { tripId, route, current = true } = input;
|
||||
const available = !!route && route.length >= 2;
|
||||
const [shown, setShown] = useState(false);
|
||||
const [facet, setFacet] = useState<HikeFacet>('sac');
|
||||
const [picked, setPicked] = useState<string | null>(null);
|
||||
const on = available && shown;
|
||||
const line = current ? route : null;
|
||||
const state = useRouteWays({ tripId, active: on, route: line, matching: 'foot', withGrades: true });
|
||||
const { ways, grades } = state;
|
||||
const spec = HIKE_FACETS[facet];
|
||||
const strays = useMemo(() => (ways && line ? straysFrom(ways, line) : NO_STRAYS), [ways, line]);
|
||||
const classOf = useMemo(() => (grades ? facetClassifier(spec, grades, strays) : null), [spec, grades, strays]);
|
||||
const rows = useMemo(
|
||||
() => (ways && grades?.status === 'ready' && classOf ? facetRows(spec, ways, grades, classOf) : []),
|
||||
[spec, ways, grades, classOf]
|
||||
);
|
||||
// Not a single way could be asked about: said once with a way to ask again, not as a row covering the whole hike.
|
||||
const nothingAnswered = rows.length > 0 && rows.every((row) => row.key === 'unasked');
|
||||
const status = !on ? 'off' : current ? statusOf(state, nothingAnswered) : 'loading';
|
||||
const focus = rows.some((row) => row.key === picked) ? picked : null;
|
||||
|
||||
useEffect(() => {
|
||||
if (status !== 'ready' || !ways || !classOf) return;
|
||||
const overlay = facetOverlay(spec, ways, rows, classOf, focus);
|
||||
const highlight = useTourRouteHighlightStore.getState();
|
||||
highlight.show({ id: focus ? `${facet}-${focus}` : facet, ...overlay, paint: 'unknown' });
|
||||
return () => highlight.clear();
|
||||
}, [status, spec, facet, ways, rows, classOf, focus]);
|
||||
|
||||
const toggle = useCallback(() => {
|
||||
setShown((value) => !value);
|
||||
setPicked(null);
|
||||
}, []);
|
||||
const chooseFacet = useCallback((next: HikeFacet) => {
|
||||
setFacet(next);
|
||||
setPicked(null);
|
||||
}, []);
|
||||
const focusOn = useCallback((key: string) => setPicked((now) => (now === key ? null : key)), []);
|
||||
|
||||
const shownRows = status === 'ready' ? rows : [];
|
||||
return {
|
||||
available,
|
||||
shown: on,
|
||||
toggle,
|
||||
facet,
|
||||
chooseFacet,
|
||||
status,
|
||||
rows: shownRows,
|
||||
knownShare: knownShareOf(shownRows),
|
||||
mostly: mostlyOf(shownRows),
|
||||
highest: facet === 'sac' ? highestStep(shownRows) : null,
|
||||
focus,
|
||||
focusOn,
|
||||
retry: state.retry,
|
||||
};
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useTourRouteHighlightStore } from '../../../store/tourRouteHighlightStore';
|
||||
import type { ClassShare, RouteWayEdge, RouteWays } from '../bike/routeWays';
|
||||
import { useRouteWays, type RouteWaysState } from '../bike/useRouteWays';
|
||||
import { straysFrom } from './routeMatch';
|
||||
import {
|
||||
highestStep,
|
||||
isSacStep,
|
||||
sacClassifier,
|
||||
sacOverlay,
|
||||
sacShares,
|
||||
sacTagOf,
|
||||
unclearTags,
|
||||
type SacClass,
|
||||
} from './sacGrades';
|
||||
|
||||
type SacGradesStatus = 'off' | 'loading' | 'offline' | 'failed' | 'ready';
|
||||
|
||||
export interface SacGradeRow extends ClassShare<SacClass> {
|
||||
/** What OpenStreetMap wrote in `sac_scale` for this class's ways: the step's value, or the unclear ones. */
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
export interface SacGrades {
|
||||
/** Whether the card has a line to describe at all. */
|
||||
available: boolean;
|
||||
/** Whether the grades are switched on: only then are they asked for and drawn on the map. */
|
||||
shown: boolean;
|
||||
toggle: () => void;
|
||||
status: SacGradesStatus;
|
||||
rows: SacGradeRow[];
|
||||
/** Of the whole line, 0 to 1, the share whose step is known. */
|
||||
knownShare: number;
|
||||
/** The highest step known along the line, or null. */
|
||||
highest: number | null;
|
||||
/** The class drawn alone on the map, or null while every class is. */
|
||||
focus: SacClass | null;
|
||||
focusOn: (key: SacClass) => void;
|
||||
retry: () => void;
|
||||
}
|
||||
|
||||
const NO_STRAYS: ReadonlySet<RouteWayEdge> = new Set();
|
||||
|
||||
function statusOf(state: RouteWaysState, nothingAnswered: boolean): SacGradesStatus {
|
||||
if (state.status === 'idle') return 'off';
|
||||
if (state.status !== 'ready') return state.status;
|
||||
const grades = state.grades?.status ?? 'failed';
|
||||
if (grades === 'ready') return nothingAnswered ? 'failed' : 'ready';
|
||||
return grades;
|
||||
}
|
||||
|
||||
function rowsOf(
|
||||
ways: RouteWays,
|
||||
grades: NonNullable<RouteWaysState['grades']>,
|
||||
classOf: (edge: RouteWayEdge) => SacClass
|
||||
): SacGradeRow[] {
|
||||
return sacShares(ways, classOf).map((row) => ({
|
||||
...row,
|
||||
tags: isSacStep(row.key) ? [sacTagOf(row.key)] : unclearTags(ways, grades, (edge) => classOf(edge) === row.key),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* MAP-03: the SAC grades along a hike, switched on by the traveller. Only
|
||||
* then are the ways under the line asked of the router and their grades of
|
||||
* OpenStreetMap, and the grades are drawn along the line on the planner map:
|
||||
* every class at once, or the one picked alone. Nothing here changes the
|
||||
* tour, its places or its days; switched off, the map is as it was.
|
||||
*/
|
||||
export function useSacGrades(input: {
|
||||
tripId: number | string;
|
||||
/** The tour's own line, or null while there is none. */
|
||||
route: [number, number][] | null;
|
||||
/** False while the line is being planned anew: the card stays, and waits for the new one. */
|
||||
current?: boolean;
|
||||
}): SacGrades {
|
||||
const { tripId, route, current = true } = input;
|
||||
const available = !!route && route.length >= 2;
|
||||
const [shown, setShown] = useState(false);
|
||||
const [picked, setPicked] = useState<SacClass | null>(null);
|
||||
const on = available && shown;
|
||||
const line = current ? route : null;
|
||||
const state = useRouteWays({ tripId, active: on, route: line, matching: 'foot', withGrades: true });
|
||||
const { ways, grades } = state;
|
||||
const strays = useMemo(() => (ways && line ? straysFrom(ways, line) : NO_STRAYS), [ways, line]);
|
||||
const classOf = useMemo(() => (grades ? sacClassifier(grades, strays) : null), [grades, strays]);
|
||||
const rows = useMemo(
|
||||
() => (ways && grades?.status === 'ready' && classOf ? rowsOf(ways, grades, classOf) : []),
|
||||
[ways, grades, classOf]
|
||||
);
|
||||
// Not a single way could be asked about: said once with a way to ask again, not as a row covering the whole hike.
|
||||
const nothingAnswered = rows.length > 0 && rows.every((row) => row.key === 'unasked');
|
||||
const status = !on ? 'off' : current ? statusOf(state, nothingAnswered) : 'loading';
|
||||
const focus = rows.some((row) => row.key === picked) ? picked : null;
|
||||
|
||||
useEffect(() => {
|
||||
if (status !== 'ready' || !ways || !classOf) return;
|
||||
const overlay = sacOverlay(ways, rows, classOf, focus);
|
||||
const highlight = useTourRouteHighlightStore.getState();
|
||||
const id = focus ? `sac-${focus}` : 'sac';
|
||||
highlight.show({ id, lines: overlay.lines, paint: 'unknown', paints: overlay.paints });
|
||||
return () => highlight.clear();
|
||||
}, [status, ways, rows, classOf, focus]);
|
||||
|
||||
const toggle = useCallback(() => {
|
||||
setShown((value) => !value);
|
||||
setPicked(null);
|
||||
}, []);
|
||||
const focusOn = useCallback((key: SacClass) => setPicked((current) => (current === key ? null : key)), []);
|
||||
|
||||
const shownRows = status === 'ready' ? rows : [];
|
||||
return {
|
||||
available,
|
||||
shown: on,
|
||||
toggle,
|
||||
status,
|
||||
rows: shownRows,
|
||||
knownShare: shownRows.filter((row) => isSacStep(row.key)).reduce((sum, row) => sum + row.share, 0),
|
||||
highest: highestStep(shownRows),
|
||||
focus,
|
||||
focusOn,
|
||||
retry: state.retry,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import type { TourTrailGrade } from '@trek/shared';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { SURFACE_FACET, WAY_FACET } from './wayTagFacets';
|
||||
|
||||
// FE-HIKE-WAYTAG-001 to FE-HIKE-WAYTAG-006
|
||||
|
||||
const grade = (values: Partial<TourTrailGrade>): TourTrailGrade => ({
|
||||
id: 1,
|
||||
mtb_scale: null,
|
||||
mtb_scale_uphill: null,
|
||||
mtb_scale_imba: null,
|
||||
sac_scale: null,
|
||||
sac_scale_unclear: null,
|
||||
surface: null,
|
||||
highway: null,
|
||||
...values,
|
||||
});
|
||||
|
||||
const surfaceOf = (surface: string) => SURFACE_FACET.read(grade({ surface })).key;
|
||||
const wayOf = (highway: string) => WAY_FACET.read(grade({ highway })).key;
|
||||
|
||||
describe('wayTagFacets (MAP-04)', () => {
|
||||
it('FE-HIKE-WAYTAG-001: sorts the recorded surfaces into their groups', () => {
|
||||
expect(['asphalt', 'concrete:plates', 'paved'].map(surfaceOf)).toEqual(['paved', 'paved', 'paved']);
|
||||
expect(['sett', 'cobblestone', 'paving_stones'].map(surfaceOf)).toEqual(['sett', 'sett', 'sett']);
|
||||
expect(['compacted', 'fine_gravel'].map(surfaceOf)).toEqual(['compacted', 'compacted']);
|
||||
expect(['gravel', 'pebblestone'].map(surfaceOf)).toEqual(['gravel', 'gravel']);
|
||||
expect(['ground', 'dirt', 'earth', 'mud', 'sand'].map(surfaceOf)).toEqual(Array(5).fill('ground'));
|
||||
expect(['grass', 'rock', 'stepping_stones', 'wood', 'metal_grid', 'unpaved', 'snow'].map(surfaceOf)).toEqual([
|
||||
'grass',
|
||||
'rock',
|
||||
'rock',
|
||||
'wood',
|
||||
'wood',
|
||||
'unpaved',
|
||||
'other',
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYTAG-002: sorts the recorded kinds of way into their groups', () => {
|
||||
expect(['path', 'bridleway', 'footway', 'pedestrian', 'steps', 'track'].map(wayOf)).toEqual([
|
||||
'path',
|
||||
'path',
|
||||
'footway',
|
||||
'footway',
|
||||
'steps',
|
||||
'track',
|
||||
]);
|
||||
expect(['cycleway', 'residential', 'service', 'secondary', 'road', 'via_ferrata', 'corridor'].map(wayOf)).toEqual([
|
||||
'cycleway',
|
||||
'quietRoad',
|
||||
'quietRoad',
|
||||
'road',
|
||||
'road',
|
||||
'viaFerrata',
|
||||
'other',
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYTAG-003: a value no group takes, or two values at once, is unclear, never the nearest group', () => {
|
||||
expect(surfaceOf('gravel;ground')).toBe('unclear');
|
||||
expect(surfaceOf('gravvel')).toBe('unclear');
|
||||
expect(wayOf('path;track')).toBe('unclear');
|
||||
expect(wayOf('motorway')).toBe('unclear');
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYTAG-004: looks a value up as OpenStreetMap writes it, and hands it on as it was written', () => {
|
||||
expect(SURFACE_FACET.read(grade({ surface: 'Asphalt' }))).toEqual({ key: 'paved', written: 'Asphalt' });
|
||||
expect(WAY_FACET.read(grade({ highway: ' Track ' }))).toEqual({ key: 'track', written: ' Track ' });
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYTAG-005: a way without the value is untagged, whatever else it records', () => {
|
||||
expect(SURFACE_FACET.read(grade({ highway: 'path', sac_scale: 'hiking' }))).toEqual({
|
||||
key: 'untagged',
|
||||
written: null,
|
||||
});
|
||||
expect(WAY_FACET.read(grade({ surface: 'gravel' })).key).toBe('untagged');
|
||||
expect(WAY_FACET.read(undefined).key).toBe('untagged');
|
||||
});
|
||||
|
||||
it('FE-HIKE-WAYTAG-006: lists the groups in order and paints each under its facet name', () => {
|
||||
expect(SURFACE_FACET.classes[0]).toBe('paved');
|
||||
expect(SURFACE_FACET.classes).toContain('unpaved');
|
||||
expect(WAY_FACET.classes).toEqual([
|
||||
'path',
|
||||
'footway',
|
||||
'steps',
|
||||
'track',
|
||||
'cycleway',
|
||||
'quietRoad',
|
||||
'road',
|
||||
'viaFerrata',
|
||||
'other',
|
||||
]);
|
||||
expect(SURFACE_FACET.paint('ground')).toBe('surface:ground');
|
||||
expect(WAY_FACET.paint('quietRoad')).toBe('way:quietRoad');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import type { TourTrailGrade } from '@trek/shared';
|
||||
import type { HikeFacetSpec } from './hikeFacets';
|
||||
import type { HikePaintKey } from './hikePaint';
|
||||
|
||||
/**
|
||||
* MAP-04: the surfaces and the kinds of way along a hike, read from the
|
||||
* `surface` and `highway` values OpenStreetMap records for the ways under
|
||||
* it, never from the router's reading of them (normalised, and a default
|
||||
* where nothing is recorded). Each group lists the OpenStreetMap values it
|
||||
* takes, and only those: a value outside every group, or two joined by `;`,
|
||||
* is unclear rather than placed in the nearest group. The examples a group
|
||||
* is named after promise nothing about coverage.
|
||||
*/
|
||||
|
||||
type HikeSurfaceClass =
|
||||
| 'paved'
|
||||
| 'sett'
|
||||
| 'compacted'
|
||||
| 'gravel'
|
||||
| 'ground'
|
||||
| 'grass'
|
||||
| 'rock'
|
||||
| 'wood'
|
||||
| 'unpaved'
|
||||
| 'other';
|
||||
|
||||
type HikeWayClass = 'path' | 'footway' | 'steps' | 'track' | 'cycleway' | 'quietRoad' | 'road' | 'viaFerrata' | 'other';
|
||||
|
||||
/** The `surface` values of each group, smooth and sealed first, then loose, natural and built over. */
|
||||
const SURFACE_GROUPS: Readonly<Record<HikeSurfaceClass, readonly string[]>> = {
|
||||
paved: ['asphalt', 'concrete', 'concrete:plates', 'concrete:lanes', 'chipseal', 'paved'],
|
||||
sett: ['paving_stones', 'paving_stones:lanes', 'sett', 'cobblestone', 'unhewn_cobblestone', 'bricks', 'grass_paver'],
|
||||
compacted: ['compacted', 'fine_gravel'],
|
||||
gravel: ['gravel', 'pebblestone', 'shells'],
|
||||
ground: ['ground', 'dirt', 'earth', 'soil', 'mud', 'sand', 'woodchips'],
|
||||
grass: ['grass'],
|
||||
rock: ['rock', 'bare_rock', 'stone', 'stepping_stones'],
|
||||
wood: ['wood', 'metal', 'metal_grid'],
|
||||
// OpenStreetMap only says the way is not paved, not what it is instead.
|
||||
unpaved: ['unpaved'],
|
||||
other: ['artificial_turf', 'tartan', 'rubber', 'clay', 'acrylic', 'carpet', 'plastic', 'snow', 'ice', 'salt'],
|
||||
};
|
||||
|
||||
/** The `highway` values of each group, the ways made for walking first, then those shared with traffic. */
|
||||
const WAY_GROUPS: Readonly<Record<HikeWayClass, readonly string[]>> = {
|
||||
path: ['path', 'bridleway'],
|
||||
footway: ['footway', 'pedestrian'],
|
||||
steps: ['steps'],
|
||||
track: ['track'],
|
||||
cycleway: ['cycleway'],
|
||||
quietRoad: ['residential', 'living_street', 'service', 'unclassified'],
|
||||
road: [
|
||||
'tertiary',
|
||||
'tertiary_link',
|
||||
'secondary',
|
||||
'secondary_link',
|
||||
'primary',
|
||||
'primary_link',
|
||||
'trunk',
|
||||
'trunk_link',
|
||||
'road',
|
||||
],
|
||||
viaFerrata: ['via_ferrata'],
|
||||
other: ['corridor', 'platform', 'busway', 'construction', 'elevator'],
|
||||
};
|
||||
|
||||
/**
|
||||
* A facet that sorts one written value by its groups. The value is looked up
|
||||
* as OpenStreetMap writes its values, in lower case and trimmed; what the
|
||||
* card shows is the value as it was written.
|
||||
*/
|
||||
function groupedFacet<K extends string>(
|
||||
field: 'surface' | 'highway',
|
||||
groups: Readonly<Record<K, readonly string[]>>,
|
||||
paint: (key: K) => HikePaintKey
|
||||
): HikeFacetSpec<K> {
|
||||
const classes = Object.keys(groups) as K[];
|
||||
const groupOf = new Map<string, K>(classes.flatMap((key) => groups[key].map((value) => [value, key] as const)));
|
||||
return {
|
||||
classes,
|
||||
read: (grade: TourTrailGrade | undefined) => {
|
||||
const written = grade?.[field] ?? null;
|
||||
if (!written) return { key: 'untagged', written: null };
|
||||
return { key: groupOf.get(written.trim().toLowerCase()) ?? 'unclear', written };
|
||||
},
|
||||
paint,
|
||||
};
|
||||
}
|
||||
|
||||
export const SURFACE_FACET = groupedFacet<HikeSurfaceClass>('surface', SURFACE_GROUPS, (key) => `surface:${key}`);
|
||||
|
||||
export const WAY_FACET = groupedFacet<HikeWayClass>('highway', WAY_GROUPS, (key) => `way:${key}`);
|
||||
@@ -23,10 +23,10 @@ vi.mock('../paddle/PaddleCheckCard', () => ({
|
||||
),
|
||||
}))
|
||||
|
||||
// MAP-03: the SAC card is its own suite's; here it only has to sit where the rails put it, with what they hand it.
|
||||
vi.mock('../hike/SacGradesCard', () => ({
|
||||
SacGradesCard: ({ route, routeCurrent, routingLimit }: { route: [number, number][] | null; routeCurrent?: boolean; routingLimit: number | null }) => (
|
||||
<div data-testid="sac-grades-card" data-route={route ? route.length : 'none'} data-current={String(routeCurrent ?? true)}
|
||||
// MAP-03 and MAP-04: the hike's ways card is its own suite's; here it only has to sit where the rails put it, with what they hand it.
|
||||
vi.mock('../hike/HikeWaysCard', () => ({
|
||||
HikeWaysCard: ({ route, routeCurrent, routingLimit }: { route: [number, number][] | null; routeCurrent?: boolean; routingLimit: number | null }) => (
|
||||
<div data-testid="hike-ways-card" data-route={route ? route.length : 'none'} data-current={String(routeCurrent ?? true)}
|
||||
data-limit={String(routingLimit)} />
|
||||
),
|
||||
}))
|
||||
@@ -1024,16 +1024,16 @@ describe('a run in the planner rails (RUN-01)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('SAC grades in the planner rails (MAP-03)', () => {
|
||||
describe('the hike ways card in the planner rails (MAP-03, MAP-04)', () => {
|
||||
it('MAP03-RAIL-001: a hike being edited carries the card with its line, whether it is planned now, and its routing limit', () => {
|
||||
const { unmount } = render(<TourPlannerRail planner={planner({ mode: { type: 'new-draft' }, routeCurrent: true, maxHikingDifficulty: 3 })} />)
|
||||
const card = screen.getByTestId('sac-grades-card')
|
||||
const card = screen.getByTestId('hike-ways-card')
|
||||
expect(card).toHaveAttribute('data-route', '2')
|
||||
expect(card).toHaveAttribute('data-current', 'true')
|
||||
expect(card).toHaveAttribute('data-limit', '3')
|
||||
unmount()
|
||||
render(<TourPlannerRail planner={planner({ mode: { type: 'new-draft' }, routeCurrent: false })} />)
|
||||
expect(screen.getByTestId('sac-grades-card')).toHaveAttribute('data-current', 'false')
|
||||
expect(screen.getByTestId('hike-ways-card')).toHaveAttribute('data-current', 'false')
|
||||
})
|
||||
|
||||
it('MAP03-RAIL-002: a hike import carries it without a routing limit; a bike tour and a bike import do not', () => {
|
||||
@@ -1043,13 +1043,13 @@ describe('SAC grades in the planner rails (MAP-03)', () => {
|
||||
readOnlyGpxAnalysis: analyzeRouteGeometry(geometry),
|
||||
})
|
||||
const hike = render(<TourPlannerRail planner={imported('hike')} />)
|
||||
expect(screen.getByTestId('sac-grades-card')).toHaveAttribute('data-route', '3')
|
||||
expect(screen.getByTestId('sac-grades-card')).toHaveAttribute('data-limit', 'null')
|
||||
expect(screen.getByTestId('hike-ways-card')).toHaveAttribute('data-route', '3')
|
||||
expect(screen.getByTestId('hike-ways-card')).toHaveAttribute('data-limit', 'null')
|
||||
hike.unmount()
|
||||
const bikeImport = render(<TourPlannerRail planner={imported('bike')} />)
|
||||
expect(screen.queryByTestId('sac-grades-card')).toBeNull()
|
||||
expect(screen.queryByTestId('hike-ways-card')).toBeNull()
|
||||
bikeImport.unmount()
|
||||
render(<TourPlannerRail planner={planner({ mode: { type: 'new-draft' }, tourType: 'bike', isBike: true, routeCurrent: false })} />)
|
||||
expect(screen.queryByTestId('sac-grades-card')).toBeNull()
|
||||
expect(screen.queryByTestId('hike-ways-card')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -53,7 +53,7 @@ import { BikeWaysCard } from '../bike/BikeWaysCard'
|
||||
import { RunWaysCard } from '../run/RunWaysCard'
|
||||
import { HorseRoutesCard } from '../horse/HorseRoutesCard'
|
||||
import { RecordedRideCard } from '../horse/RecordedRideFacts'
|
||||
import { SacGradesCard } from '../hike/SacGradesCard'
|
||||
import { HikeWaysCard } from '../hike/HikeWaysCard'
|
||||
import { walkTimeFields } from '../walk/walkTimeFields'
|
||||
|
||||
const STATUS_KEY: Partial<Record<TourPlannerStatus, string>> = {
|
||||
@@ -351,7 +351,7 @@ function GpxViewRail({
|
||||
)}
|
||||
</RailCard>
|
||||
{tour.tour_type === 'hike' && (
|
||||
<SacGradesCard tripId={planner.tripId} route={analysis?.routeCoordinates ?? null} routingLimit={null} />
|
||||
<HikeWaysCard tripId={planner.tripId} route={analysis?.routeCoordinates ?? null} routingLimit={null} />
|
||||
)}
|
||||
</div>
|
||||
{tour.tour_type === 'kayak' && (
|
||||
@@ -475,7 +475,7 @@ export function TourPlannerRail({ planner,
|
||||
<HorseRoutesCard planner={planner} />
|
||||
|
||||
{planner.tourType === 'hike' && (
|
||||
<SacGradesCard tripId={planner.tripId} route={planner.route} routeCurrent={planner.routeCurrent}
|
||||
<HikeWaysCard tripId={planner.tripId} route={planner.route} routeCurrent={planner.routeCurrent}
|
||||
routingLimit={planner.maxHikingDifficulty} />
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user