feat(ferratas): keep loaded via ferratas in a tile cache on server and device, group segments, own look and count badge

This commit is contained in:
Maurice
2026-10-14 16:21:30 +02:00
parent a9882a6415
commit 160bc89e78
126 changed files with 5003 additions and 1153 deletions
+1 -2
View File
@@ -1,5 +1,4 @@
{ {
"pages/tripPlanner/PlanMapStage.tsx": 4, "pages/tripPlanner/PlanMapStage.tsx": 4,
"mobile/components/trek3d/MascotClip.tsx": 1, "mobile/components/trek3d/MascotClip.tsx": 1
"components/Ferratas/FerrataDetailOverlay.tsx": 1
} }
@@ -1,71 +1,98 @@
// FE-FERRATA-CARD-001 to FE-FERRATA-CARD-005: the card for one picked via ferrata. // FE-FERRATA-CARD-001 to FE-FERRATA-CARD-007: the card for one picked via ferrata.
import { describe, expect, it, vi } from 'vitest'; import { describe, expect, it, vi } from 'vitest';
import type { Ferrata } from '@trek/shared'; import type { Ferrata } from '@trek/shared';
import { fireEvent, render, screen } from '../../../tests/helpers/render'; import { fireEvent, render, screen } from '../../../tests/helpers/render';
import FerrataDetailCard from './FerrataDetailCard'; import FerrataDetailCard from './FerrataDetailCard';
import { type FerrataGroup, groupFerratas } from './ferrataGroups';
const FERRATA: Ferrata = { /** About 222 m of latitude per segment. */
id: 'way/77', const segment = (id: number, from: number, overrides: Partial<Ferrata> = {}): Ferrata => ({
id: `way/${id}`,
name: 'Via ferrata Pisciadù', name: 'Via ferrata Pisciadù',
grade: { raw: '5', level: 5, plus: '+' }, grade: { raw: '5', level: 5, plus: '+' },
routeName: 'Sella group', routeName: 'Sella group',
geometry: [ geometry: [
[46.53, 11.81], [from, 11.81],
[46.54, 11.82], [from + 0.002, 11.81],
], ],
osmUrl: 'https://www.openstreetmap.org/way/77', osmUrl: `https://www.openstreetmap.org/way/${id}`,
}; ...overrides,
});
const LOWER = segment(77, 46.53, { grade: { raw: '2', level: 2, plus: null } });
const UPPER = segment(78, 46.532);
function groupOf(...segments: Ferrata[]): FerrataGroup {
const [group] = groupFerratas(segments);
if (!group) throw new Error('no group');
return group;
}
describe('FerrataDetailCard', () => { describe('FerrataDetailCard', () => {
it('FE-FERRATA-CARD-001: shows the name, the route, the grade badge and the attribution', () => { it('FE-FERRATA-CARD-001: shows the name, the route, the grade range and length as badges, and the attribution', () => {
render(<FerrataDetailCard ferrata={FERRATA} />); render(<FerrataDetailCard group={groupOf(LOWER, UPPER)} segment={UPPER} />);
expect(screen.getByRole('heading', { name: 'Via ferrata Pisciadù' })).toBeInTheDocument(); expect(screen.getByRole('heading', { name: 'Via ferrata Pisciadù' })).toBeInTheDocument();
expect(screen.getByText('Sella group')).toBeInTheDocument(); expect(screen.getByText('Sella group')).toBeInTheDocument();
expect(screen.getByText('K5+')).toHaveClass('text-danger'); // A badge, not a line colour: the grade is neutral, never the tour's green to red.
expect(screen.getByText('K2 to K5+')).toHaveClass('text-content-secondary');
expect(screen.getByText('445 m')).toBeInTheDocument();
expect(screen.getByText('Data © OpenStreetMap contributors')).toBeInTheDocument(); expect(screen.getByText('Data © OpenStreetMap contributors')).toBeInTheDocument();
}); });
it('FE-FERRATA-CARD-002: links to the way on openstreetmap.org in a new tab', () => { it('FE-FERRATA-CARD-002: links to the way that was picked on openstreetmap.org, in a new tab', () => {
render(<FerrataDetailCard ferrata={FERRATA} />); render(<FerrataDetailCard group={groupOf(LOWER, UPPER)} segment={UPPER} />);
const link = screen.getByRole('link', { name: /View in OpenStreetMap/ }); const link = screen.getByRole('link', { name: /View in OpenStreetMap/ });
expect(link).toHaveAttribute('href', 'https://www.openstreetmap.org/way/77'); expect(link).toHaveAttribute('href', 'https://www.openstreetmap.org/way/78');
expect(link).toHaveAttribute('target', '_blank'); expect(link).toHaveAttribute('target', '_blank');
expect(link).toHaveAttribute('rel', 'noopener noreferrer'); expect(link).toHaveAttribute('rel', 'noopener noreferrer');
}); });
it('FE-FERRATA-CARD-003: an unnamed way takes its route name as title and does not repeat it', () => { it('FE-FERRATA-CARD-003: an unnamed ferrata takes its route name as title and does not repeat it', () => {
render(<FerrataDetailCard ferrata={{ ...FERRATA, name: null }} />); const unnamed = segment(77, 46.53, { name: null });
render(<FerrataDetailCard group={groupOf(unnamed)} segment={unnamed} />);
expect(screen.getByRole('heading', { name: 'Sella group' })).toBeInTheDocument(); expect(screen.getByRole('heading', { name: 'Sella group' })).toBeInTheDocument();
expect(screen.getAllByText('Sella group')).toHaveLength(1); expect(screen.getAllByText('Sella group')).toHaveLength(1);
}); });
it('FE-FERRATA-CARD-004: a letter scale keeps its raw grade, no grade at all reads unknown, both neutral', () => { it('FE-FERRATA-CARD-004: one grade reads once; a letter scale keeps its raw grade, none at all reads unknown', () => {
const { rerender } = render( const { rerender } = render(<FerrataDetailCard group={groupOf(UPPER)} segment={UPPER} />);
<FerrataDetailCard ferrata={{ ...FERRATA, grade: { raw: 'C/D', level: null, plus: null } }} /> expect(screen.getByText('K5+')).toBeInTheDocument();
);
const letter = segment(80, 46.53, { grade: { raw: 'C/D', level: null, plus: null } });
rerender(<FerrataDetailCard group={groupOf(letter)} segment={letter} />);
expect(screen.getByText('C/D')).toHaveClass('text-content-secondary'); expect(screen.getByText('C/D')).toHaveClass('text-content-secondary');
rerender( const bare = segment(81, 46.53, { name: null, routeName: null, grade: { raw: null, level: null, plus: null } });
<FerrataDetailCard rerender(<FerrataDetailCard group={groupOf(bare)} segment={bare} />);
ferrata={{ ...FERRATA, name: null, routeName: null, grade: { raw: null, level: null, plus: null } }}
/>
);
expect(screen.getByText('Grade unknown')).toBeInTheDocument(); expect(screen.getByText('Grade unknown')).toBeInTheDocument();
expect(screen.getByRole('heading', { name: 'Unnamed via ferrata' })).toBeInTheDocument(); expect(screen.getByRole('heading', { name: 'Unnamed via ferrata' })).toBeInTheDocument();
}); });
it('FE-FERRATA-CARD-005: offers a close button only when it can be closed', () => { it('FE-FERRATA-CARD-005: offers a close button only when it can be closed', () => {
const onClose = vi.fn(); const onClose = vi.fn();
const { rerender } = render(<FerrataDetailCard ferrata={FERRATA} />); const group = groupOf(UPPER);
const { rerender } = render(<FerrataDetailCard group={group} segment={UPPER} />);
expect(screen.queryByRole('button', { name: 'Close' })).not.toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'Close' })).not.toBeInTheDocument();
rerender(<FerrataDetailCard ferrata={FERRATA} onClose={onClose} />); rerender(<FerrataDetailCard group={group} segment={UPPER} onClose={onClose} />);
fireEvent.click(screen.getByRole('button', { name: 'Close' })); fireEvent.click(screen.getByRole('button', { name: 'Close' }));
expect(onClose).toHaveBeenCalledTimes(1); expect(onClose).toHaveBeenCalledTimes(1);
}); });
it('FE-FERRATA-CARD-006: separates nothing with a middle dot', () => {
const { container } = render(<FerrataDetailCard group={groupOf(LOWER, UPPER)} segment={LOWER} />);
expect(container.textContent).not.toContain('·');
});
it('FE-FERRATA-CARD-007: a long name wraps onto a second line instead of being cut to one', () => {
const long = 'Herrmann von Barth Klettersteig über den Wetterstein';
render(<FerrataDetailCard group={groupOf(segment(79, 46.6, { name: long }))} segment={UPPER} />);
const heading = screen.getByRole('heading', { name: long });
expect(heading).toHaveClass('line-clamp-2');
expect(heading).not.toHaveClass('truncate');
});
}); });
@@ -1,42 +1,51 @@
import { ExternalLink, Mountain, X } from 'lucide-react'; import { ExternalLink, MoveHorizontal, Route, X } from 'lucide-react';
import type { Ferrata } from '@trek/shared'; import type { Ferrata } from '@trek/shared';
import { useTranslation } from '../../i18n'; import { useTranslation } from '../../i18n';
import { type PillTone, SoftPill } from '../Planner/planParts'; import { useSettingsStore } from '../../store/settingsStore';
import { type FerrataTone, ferrataGradeLabel, ferrataGradeTone, ferrataOsmUrl } from './ferrataModel'; import { formatDistance } from '../../utils/units';
import { SoftPill } from '../Planner/planParts';
/** The badge tone per difficulty band; the same bands colour the line on the map. */ import { type FerrataGroup, ferrataLabel } from './ferrataGroups';
const PILL_TONE: Record<FerrataTone, PillTone> = { import { ferrataOsmUrl } from './ferrataModel';
easy: 'success',
medium: 'warning',
hard: 'danger',
unknown: 'neutral',
};
/** /**
* One via ferrata, picked on the map: its name, the grade as a badge, the * One via ferrata, picked on the map: its name, the route it belongs to, its
* route it belongs to and the way back to the source. OpenStreetMap's data is * grade range and total length as badges, and the way back to the source.
* ODbL, which asks for the attribution next to anything shown from it, so the * `segment` is the OSM way that was clicked, which the source link opens.
* card carries it rather than relying on the map's own corner. * OpenStreetMap's data is ODbL, which asks for the attribution next to
* anything shown from it, so the card carries it rather than relying on the
* map's own corner.
*/ */
export default function FerrataDetailCard({ ferrata, onClose }: { ferrata: Ferrata; onClose?: () => void }) { export default function FerrataDetailCard({
group,
segment,
onClose,
}: {
group: FerrataGroup;
segment: Ferrata;
onClose?: () => void;
}) {
const { t } = useTranslation(); const { t } = useTranslation();
const name = ferrata.name ?? ferrata.routeName ?? t('ferratas.unnamed'); const unit = useSettingsStore((s) => s.settings.distance_unit);
const label = ferrataLabel(group, t);
const name = label.name;
// The route line only adds something when it is not already the title. // The route line only adds something when it is not already the title.
const route = ferrata.routeName && ferrata.routeName !== name ? ferrata.routeName : null; const route = group.routeName && group.routeName !== name ? group.routeName : null;
// A letter scale ("C/D") the contract keeps as `raw` still says more than "unknown". // Without any grade the badge says so, rather than leaving a gap.
const grade = ferrataGradeLabel(ferrata.grade) ?? ferrata.grade.raw ?? t('ferratas.unknownGrade'); const grade = label.grade ?? t('ferratas.unknownGrade');
return ( return (
<article <article
className="w-72 max-w-full rounded-xl border border-edge bg-surface-card p-3 text-content shadow-lg" className="w-max min-w-[min(18rem,calc(100vw_-_2rem))] max-w-[22.5rem] rounded-xl border border-edge bg-surface-card p-3 text-content shadow-lg"
data-testid="ferrata-detail-card" data-testid="ferrata-detail-card"
> >
<div className="flex items-start gap-2"> <div className="flex items-start gap-2">
<Mountain size={16} className="mt-0.5 flex-none text-content-muted" aria-hidden="true" /> <Route size={16} className="mt-0.5 flex-none text-content-muted" aria-hidden="true" />
<div className="min-w-0 flex-1"> <div className="min-w-0 flex-1">
<h3 className="truncate text-subtitle font-semibold">{name}</h3> <h3 className="line-clamp-2 break-words text-subtitle font-semibold" title={name}>
{name}
</h3>
{route && <p className="truncate text-caption text-content-muted">{route}</p>} {route && <p className="truncate text-caption text-content-muted">{route}</p>}
</div> </div>
{onClose && ( {onClose && (
@@ -50,11 +59,16 @@ export default function FerrataDetailCard({ ferrata, onClose }: { ferrata: Ferra
</button> </button>
)} )}
</div> </div>
<div className="mt-2 flex items-center gap-2"> <div className="mt-2 flex flex-wrap items-center gap-2">
<SoftPill tone={PILL_TONE[ferrataGradeTone(ferrata.grade)]}>{grade}</SoftPill> <SoftPill>{grade}</SoftPill>
{group.lengthM > 0 && (
<SoftPill icon={<MoveHorizontal size={11} aria-hidden="true" />}>
{formatDistance(group.lengthM / 1000, unit ?? 'metric')}
</SoftPill>
)}
</div> </div>
<a <a
href={ferrataOsmUrl(ferrata)} href={ferrataOsmUrl(segment)}
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
className="mt-3 inline-flex items-center gap-1 text-body font-medium text-accent-on hover:underline" className="mt-3 inline-flex items-center gap-1 text-body font-medium text-accent-on hover:underline"
@@ -5,6 +5,8 @@ import type { Ferrata } from '@trek/shared';
import { fireEvent, render, screen } from '../../../tests/helpers/render'; import { fireEvent, render, screen } from '../../../tests/helpers/render';
import FerrataDetailOverlay from './FerrataDetailOverlay'; import FerrataDetailOverlay from './FerrataDetailOverlay';
import { groupFerratas } from './ferrataGroups';
import type { FerrataSelection } from './useFerrataMapLayer';
const FERRATA: Ferrata = { const FERRATA: Ferrata = {
id: 'way/42', id: 'way/42',
@@ -18,38 +20,54 @@ const FERRATA: Ferrata = {
osmUrl: 'https://www.openstreetmap.org/way/42', osmUrl: 'https://www.openstreetmap.org/way/42',
}; };
const [GROUP] = groupFerratas([FERRATA]);
if (!GROUP) throw new Error('no group');
const SELECTION: FerrataSelection = { group: GROUP, segment: FERRATA };
describe('FerrataDetailOverlay', () => { describe('FerrataDetailOverlay', () => {
it('FE-FERRATA-OVERLAY-001: shows nothing while no ferrata is picked', () => { it('FE-FERRATA-OVERLAY-001: shows nothing while no ferrata is picked', () => {
render(<FerrataDetailOverlay ferrata={null} onClose={vi.fn()} insetRight={400} bottom={66} />); render(<FerrataDetailOverlay selection={null} onClose={vi.fn()} insetEnd={400} bottom={66} />);
expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull(); expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull();
}); });
it('FE-FERRATA-OVERLAY-002: anchors the card bottom right, clear of the right panel and the control row', () => { it('FE-FERRATA-OVERLAY-002: anchors the card at the bottom of the inline end, clear of the panel and the control row', () => {
render(<FerrataDetailOverlay ferrata={FERRATA} onClose={vi.fn()} insetRight={400} bottom={66} />); render(<FerrataDetailOverlay selection={SELECTION} onClose={vi.fn()} insetEnd={400} bottom={66} />);
const overlay = screen.getByTestId('ferrata-detail-overlay'); const overlay = screen.getByTestId('ferrata-detail-overlay');
expect(overlay.style.right).toBe('400px'); expect(overlay.style.insetInlineEnd).toBe('400px');
// Logical only: no physical side, so a right-to-left page mirrors it.
expect(overlay.style.right).toBe('');
expect(overlay.style.left).toBe('');
expect(overlay.style.bottom).toBe('66px'); expect(overlay.style.bottom).toBe('66px');
expect(overlay).toContainElement(screen.getByTestId('ferrata-detail-card')); expect(overlay).toContainElement(screen.getByTestId('ferrata-detail-card'));
}); });
it('FE-FERRATA-OVERLAY-003: closes on a press outside it, not on one inside', () => { it('FE-FERRATA-OVERLAY-003: closes on a press outside it, not on one inside, and reports the outside press', () => {
const onClose = vi.fn(); const onClose = vi.fn();
const onOutsidePress = vi.fn();
render( render(
<div> <div>
<button type="button">map</button> <button type="button">map</button>
<FerrataDetailOverlay ferrata={FERRATA} onClose={onClose} insetRight={400} bottom={66} /> <FerrataDetailOverlay
selection={SELECTION}
onClose={onClose}
onOutsidePress={onOutsidePress}
insetEnd={400}
bottom={66}
/>
</div> </div>
); );
fireEvent.pointerDown(screen.getByText('Via delle Trincee')); fireEvent.pointerDown(screen.getByText('Via delle Trincee'));
expect(onClose).not.toHaveBeenCalled(); expect(onClose).not.toHaveBeenCalled();
expect(onOutsidePress).not.toHaveBeenCalled();
fireEvent.pointerDown(screen.getByRole('button', { name: 'map' })); fireEvent.pointerDown(screen.getByRole('button', { name: 'map' }));
expect(onClose).toHaveBeenCalledTimes(1); expect(onClose).toHaveBeenCalledTimes(1);
expect(onOutsidePress).toHaveBeenCalledTimes(1);
}); });
it('FE-FERRATA-OVERLAY-004: closes on Escape and on its own close button', () => { it('FE-FERRATA-OVERLAY-004: closes on Escape and on its own close button', () => {
const onClose = vi.fn(); const onClose = vi.fn();
render(<FerrataDetailOverlay ferrata={FERRATA} onClose={onClose} insetRight={400} bottom={66} />); render(<FerrataDetailOverlay selection={SELECTION} onClose={onClose} insetEnd={400} bottom={66} />);
fireEvent.keyDown(document, { key: 'Escape' }); fireEvent.keyDown(document, { key: 'Escape' });
expect(onClose).toHaveBeenCalledTimes(1); expect(onClose).toHaveBeenCalledTimes(1);
@@ -1,37 +1,43 @@
import { useEffect, useRef } from 'react'; import { useEffect, useRef } from 'react';
import type { Ferrata } from '@trek/shared';
import FerrataDetailCard from './FerrataDetailCard'; import FerrataDetailCard from './FerrataDetailCard';
import type { FerrataSelection } from './useFerrataMapLayer';
/** /**
* The picked ferrata's card over the map, anchored bottom right above the row * The picked ferrata's card over the map, anchored at the bottom of the inline
* of map controls. `insetRight` is what the right panel covers plus the gap the * end, above the row of map controls. `insetEnd` is what the panel on that
* layer pill keeps to the left one, so the card mirrors the pill and never * side covers plus the gap the layer pill keeps to the panel on the other, so
* slides under the panel; `bottom` clears the control row (the layer pill on * the card mirrors the pill and never slides under the panel; `bottom` clears
* the flat map, the dock and the grade legend in 3D). * the control row (the layer pill on the flat map, the dock and the grade
* legend in 3D).
* *
* It closes on a press anywhere outside it and on Escape. A press on another * It closes on a press anywhere outside it and on Escape. A press on another
* ferrata closes this card first and the click then opens that one's. * ferrata closes this card first and the click then opens that one's. The
* press outside is handed to `onOutsidePress` as well, so the map under it can
* leave the click that follows alone (see `useOutsidePressGuard`).
*/ */
export default function FerrataDetailOverlay({ export default function FerrataDetailOverlay({
ferrata, selection,
onClose, onClose,
insetRight, onOutsidePress,
insetEnd,
bottom, bottom,
}: { }: {
ferrata: Ferrata | null; selection: FerrataSelection | null;
onClose: () => void; onClose: () => void;
insetRight: number; onOutsidePress?: () => void;
insetEnd: number;
bottom: number; bottom: number;
}) { }) {
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const open = !!selection;
useEffect(() => { useEffect(() => {
if (!ferrata) return; if (!open) return;
const onDown = (event: PointerEvent) => { const onDown = (event: PointerEvent) => {
const card = ref.current; const card = ref.current;
if (card && event.target instanceof Node && card.contains(event.target)) return; if (card && event.target instanceof Node && card.contains(event.target)) return;
onOutsidePress?.();
onClose(); onClose();
}; };
const onKey = (event: KeyboardEvent) => { const onKey = (event: KeyboardEvent) => {
@@ -43,21 +49,17 @@ export default function FerrataDetailOverlay({
document.removeEventListener('pointerdown', onDown, true); document.removeEventListener('pointerdown', onDown, true);
document.removeEventListener('keydown', onKey); document.removeEventListener('keydown', onKey);
}; };
}, [ferrata, onClose]); }, [open, onClose, onOutsidePress]);
if (!ferrata) return null; if (!selection) return null;
return ( return (
<div <div
ref={ref} ref={ref}
className="absolute z-[20]" className="absolute z-[20] flex justify-end"
style={{ style={{ insetInlineEnd: insetEnd, bottom, maxWidth: `calc(100% - ${insetEnd + 32}px)` }}
right: insetRight, // rtl-lint-disable: measured from the right panel, which stays on the physical right
bottom,
maxWidth: `calc(100% - ${insetRight + 32}px)`,
}}
data-testid="ferrata-detail-overlay" data-testid="ferrata-detail-overlay"
> >
<FerrataDetailCard ferrata={ferrata} onClose={onClose} /> <FerrataDetailCard group={selection.group} segment={selection.segment} onClose={onClose} />
</div> </div>
); );
} }
@@ -1,23 +1,24 @@
import type { Map } from 'mapbox-gl'; import type { Map } from 'mapbox-gl';
import type { Ferrata } from '@trek/shared'; import type { FerrataGroup } from './ferrataGroups';
import { type FerrataSelectHandler, useFerrataLayerGL } from './useFerrataLayerGL'; import { type FerrataSelectHandler, useFerrataLayerGL } from './useFerrataLayerGL';
/** /**
* The via ferrata layer as a mountable piece for a GL map view: it renders * The via ferrata layer as a mountable piece for a GL map view: it renders
* nothing itself, the lines live on `map`. See useFerrataLayerGL for the * nothing itself, the lines live on `map`. See useFerrataLayerGL for the
* layering and the click handling. * layering, the highlight and the click handling.
*/ */
export default function FerrataLayerGL({ export default function FerrataLayerGL({
map, map,
ferratas, groups,
onSelect, onSelect,
selectedId = null,
}: { }: {
map: Map | null; map: Map | null;
ferratas: readonly Ferrata[]; groups: readonly FerrataGroup[];
onSelect: FerrataSelectHandler; onSelect: FerrataSelectHandler;
selectedId?: string | null;
}) { }) {
useFerrataLayerGL(map, ferratas, onSelect); useFerrataLayerGL(map, groups, onSelect, selectedId);
return null; return null;
} }
@@ -1,22 +1,34 @@
/** /**
* FE-FERRATA-LEAFLET-001 to FE-FERRATA-LEAFLET-007: the via ferrata layer on * FE-FERRATA-LEAFLET-001 to FE-FERRATA-LEAFLET-011: the via ferrata layer on
* the Leaflet renderer. * the Leaflet renderer.
* *
* Leaflet itself is not under test: the mock reflects each polyline's props * Leaflet itself is not under test: the mock reflects each polyline's and
* back as data attributes, so the assertions read which lines are drawn, in * marker's props back as data attributes, so the assertions read which lines
* which order and with what options. Casing and line stay non-interactive so * are drawn, in which order and with what options. Casing and line stay
* they cannot swallow clicks meant for places; only the invisible hit line on * non-interactive so they cannot swallow clicks meant for places; only the
* top carries the tooltip and the click. * invisible hit line on top carries the tooltip, the hover and the click.
*/ */
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import type { ReactNode } from 'react'; import type { ReactNode } from 'react';
import { describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Ferrata } from '@trek/shared'; import type { Ferrata } from '@trek/shared';
import FerrataLayerLeaflet from './FerrataLayerLeaflet'; import FerrataLayerLeaflet from './FerrataLayerLeaflet';
import { groupFerratas } from './ferrataGroups';
vi.mock('../../i18n', () => ({ useTranslation: () => ({ t: (key: string) => key }) })); vi.mock('../../i18n', () => ({
useTranslation: () => ({
t: (key: string, params?: Record<string, string | number>) =>
params ? `${key}(${Object.values(params).join(',')})` : key,
}),
}));
const zoom = { value: 12 };
const zoomEnd: { handler: (() => void) | null } = { handler: null };
type Handlers = Record<string, ((event: { latlng: { lat: number; lng: number } }) => void) | undefined>;
const at = { latlng: { lat: 46.505, lng: 11.705 } };
vi.mock('react-leaflet', () => ({ vi.mock('react-leaflet', () => ({
Polyline: ({ Polyline: ({
@@ -28,10 +40,10 @@ vi.mock('react-leaflet', () => ({
children, children,
}: { }: {
positions: unknown; positions: unknown;
pathOptions: { color: string; weight: number; opacity: number; dashArray?: string }; pathOptions: { color: string; weight: number; opacity: number; dashArray?: string; className?: string };
interactive?: boolean; interactive?: boolean;
bubblingMouseEvents?: boolean; bubblingMouseEvents?: boolean;
eventHandlers?: { click?: (event: { latlng: { lat: number; lng: number } }) => void }; eventHandlers?: Handlers;
children?: ReactNode; children?: ReactNode;
}) => ( }) => (
<div <div
@@ -41,103 +53,215 @@ vi.mock('react-leaflet', () => ({
data-weight={pathOptions.weight} data-weight={pathOptions.weight}
data-opacity={pathOptions.opacity} data-opacity={pathOptions.opacity}
data-dash={pathOptions.dashArray ?? ''} data-dash={pathOptions.dashArray ?? ''}
data-class={pathOptions.className ?? ''}
data-interactive={interactive === false ? 'false' : 'true'} data-interactive={interactive === false ? 'false' : 'true'}
data-bubbles={bubblingMouseEvents === false ? 'false' : 'true'} data-bubbles={bubblingMouseEvents === false ? 'false' : 'true'}
onClick={() => eventHandlers?.click?.({ latlng: { lat: 46.505, lng: 11.705 } })} onClick={() => eventHandlers?.click?.(at)}
onMouseOver={() => eventHandlers?.mouseover?.(at)}
onMouseOut={() => eventHandlers?.mouseout?.(at)}
> >
{children} {children}
</div> </div>
), ),
Tooltip: ({ children }: { children: ReactNode }) => <span data-testid="tooltip">{children}</span>, Marker: ({ position, eventHandlers }: { position: [number, number]; eventHandlers?: Handlers }) => (
<button
type="button"
data-testid="ferrata-icon"
data-position={JSON.stringify(position)}
onClick={() => eventHandlers?.click?.(at)}
/>
),
Tooltip: ({ children, className }: { children: ReactNode; className?: string }) => (
<span data-testid="tooltip" data-class={className ?? ''}>
{children}
</span>
),
// A pixel per 0.00001 degree: enough to tell icons apart that sit a few metres from each other.
useMap: () => ({
getZoom: () => zoom.value,
project: ([lat, lng]: [number, number]) => ({ x: lng * 100_000, y: -lat * 100_000 }),
}),
useMapEvents: (handlers: { zoomend?: () => void }) => {
zoomEnd.handler = handlers.zoomend ?? null;
return null;
},
})); }));
const ferrata = (id: string, name: string | null, level: number | null, routeName: string | null = null): Ferrata => ({ const ferrata = (
id, id: string,
name, name: string | null,
grade: { raw: level === null ? null : String(level), level, plus: level === 4 ? '+' : null }, level: number | null,
routeName, options: { routeName?: string | null; lat?: number; plus?: '+' | null } = {}
geometry: [ ): Ferrata => {
[46.5, 11.7], const lat = options.lat ?? 46.5;
[46.51, 11.71], return {
], id,
osmUrl: `https://www.openstreetmap.org/${id}`, name,
grade: { raw: level === null ? null : String(level), level, plus: options.plus ?? null },
routeName: options.routeName ?? null,
geometry: [
[lat, 11.7],
[lat + 0.01, 11.71],
],
osmUrl: `https://www.openstreetmap.org/${id}`,
};
};
const lines = () => screen.getAllByTestId('polyline');
beforeEach(() => {
zoom.value = 12;
}); });
describe('FerrataLayerLeaflet', () => { describe('FerrataLayerLeaflet', () => {
it('FE-FERRATA-LEAFLET-001: draws nothing for no ferratas', () => { it('FE-FERRATA-LEAFLET-001: draws nothing for no ferratas', () => {
const { container } = render(<FerrataLayerLeaflet ferratas={[]} onSelect={vi.fn()} />); const { container } = render(<FerrataLayerLeaflet groups={[]} onSelect={vi.fn()} />);
expect(container).toBeEmptyDOMElement(); expect(container).toBeEmptyDOMElement();
}); });
it('FE-FERRATA-LEAFLET-002: casing, dashed line, then the invisible hit line, in [lat, lng]', () => { it('FE-FERRATA-LEAFLET-002: a light casing, the dark dashed line, then the invisible hit line, in [lat, lng]', () => {
render(<FerrataLayerLeaflet ferratas={[ferrata('way/1', 'Pisciadù', 3)]} onSelect={vi.fn()} />); const root = document.documentElement;
const lines = screen.getAllByTestId('polyline'); root.style.setProperty('--text-primary', 'black');
root.style.setProperty('--bg-primary', 'white');
render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 3)])} onSelect={vi.fn()} />);
expect(lines.map((line) => line.dataset.weight)).toEqual(['6', '3', '16']); expect(lines().map((line) => line.dataset.weight)).toEqual(['6', '2.5', '16']);
expect(lines.map((line) => line.dataset.interactive)).toEqual(['false', 'false', 'true']); expect(lines().map((line) => line.dataset.color)).toEqual(['white', 'black', 'white']);
expect(lines[1]?.dataset.dash).toBe('6 5'); expect(lines().map((line) => line.dataset.interactive)).toEqual(['false', 'false', 'true']);
expect(lines[2]?.dataset.opacity).toBe('0'); expect(lines()[1]?.dataset.dash).toBe('6 5');
expect(JSON.parse(lines[0]?.dataset.positions ?? '[]')).toEqual([ expect(lines()[2]?.dataset.opacity).toBe('0');
expect(JSON.parse(lines()[0]?.dataset.positions ?? '[]')).toEqual([
[46.5, 11.7], [46.5, 11.7],
[46.51, 11.71], [46.51, 11.71],
]); ]);
root.style.removeProperty('--text-primary');
root.style.removeProperty('--bg-primary');
}); });
it('FE-FERRATA-LEAFLET-003: the tooltip names the ferrata and its grade, or falls back to the route and unnamed', () => { it('FE-FERRATA-LEAFLET-003: the tooltip names the ferrata and its grade range, or falls back to the route and unnamed', () => {
render( const groups = groupFerratas([
<FerrataLayerLeaflet ferrata('way/1', 'Pisciadù', 2),
ferratas={[ ferrata('way/2', 'Pisciadù', 4, { lat: 46.51, plus: '+' }),
ferrata('way/1', 'Pisciadù', 4), ferrata('way/3', null, 3, { routeName: 'Sentiero attrezzato', lat: 47 }),
ferrata('way/2', null, null, 'Sentiero attrezzato'), ferrata('way/4', null, null, { lat: 48 }),
ferrata('way/3', null, null), ]);
]} render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} />);
onSelect={vi.fn()}
/>
);
expect(screen.getAllByTestId('tooltip').map((tip) => tip.textContent)).toEqual([ expect(screen.getAllByTestId('tooltip').map((tip) => tip.textContent)).toEqual([
'PisciadùK4+', 'Pisciadùferratas.gradeRange(K2,K4+)',
'Sentiero attrezzato', 'Pisciadùferratas.gradeRange(K2,K4+)',
'Sentiero attrezzatoK3',
'ferratas.unnamed', 'ferratas.unnamed',
]); ]);
}); });
it('FE-FERRATA-LEAFLET-004: a click on the hit line hands the ferrata and the point to onSelect', () => { it('FE-FERRATA-LEAFLET-004: a click on the hit line hands the ferrata, the way and the point to onSelect', () => {
const onSelect = vi.fn(); const onSelect = vi.fn();
const target = ferrata('way/9', 'Tridentina', 2); const target = ferrata('way/9', 'Tridentina', 2);
render(<FerrataLayerLeaflet ferratas={[target]} onSelect={onSelect} />); const groups = groupFerratas([target]);
render(<FerrataLayerLeaflet groups={groups} onSelect={onSelect} />);
const [, , hit] = screen.getAllByTestId('polyline'); const hit = lines()[2];
if (!hit) throw new Error('no hit line'); if (!hit) throw new Error('no hit line');
fireEvent.click(hit); fireEvent.click(hit);
expect(onSelect).toHaveBeenCalledWith(target, { lng: 11.705, lat: 46.505 }); expect(onSelect).toHaveBeenCalledWith(groups[0], target, { lng: 11.705, lat: 46.505 });
}); });
it('FE-FERRATA-LEAFLET-006: the hit line keeps its click from reaching the map, so no control point lands under it', () => { it('FE-FERRATA-LEAFLET-005: the hit line keeps its click from reaching the map, so no control point lands under it', () => {
render(<FerrataLayerLeaflet ferratas={[ferrata('way/1', 'Pisciadù', 3)]} onSelect={vi.fn()} />); render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 3)])} onSelect={vi.fn()} />);
const [, , hit] = screen.getAllByTestId('polyline'); expect(lines()[2]?.dataset.bubbles).toBe('false');
expect(hit?.dataset.bubbles).toBe('false');
}); });
it('FE-FERRATA-LEAFLET-007: the grade sits beside the name as its own label, without a separator', () => { it('FE-FERRATA-LEAFLET-006: the grade sits beside the name as its own label, without a separator', () => {
render(<FerrataLayerLeaflet ferratas={[ferrata('way/1', 'Pisciadù', 4)]} onSelect={vi.fn()} />); render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 4)])} onSelect={vi.fn()} />);
const tip = screen.getByTestId('tooltip'); const tip = screen.getByTestId('tooltip');
expect(tip.textContent).not.toContain('·'); expect(tip.textContent).not.toContain('·');
expect(tip.querySelector('span')?.textContent).toBe('K4+'); expect(tip.querySelector('span')?.textContent).toBe('K4');
}); });
it('FE-FERRATA-LEAFLET-005: a theme change recolours the lines without a refetch', async () => { it('FE-FERRATA-LEAFLET-007: hovering a segment lights up the whole ferrata in the accent, and so does the open card', () => {
const root = document.documentElement; const root = document.documentElement;
root.style.setProperty('--warning', 'orange'); root.style.setProperty('--accent', 'navy');
const list = [ferrata('way/1', 'Pisciadù', 3)]; const groups = groupFerratas([
render(<FerrataLayerLeaflet ferratas={list} onSelect={vi.fn()} />); ferrata('way/1', 'Via A', 2),
expect(screen.getAllByTestId('polyline')[1]?.dataset.color).toBe('orange'); ferrata('way/2', 'Via A', 3, { lat: 46.51 }),
ferrata('way/3', 'Via B', 3, { lat: 47 }),
]);
const { rerender } = render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} />);
// Casings 0 to 2, lines 3 to 5, hit lines 6 to 8.
const lineColours = () =>
lines()
.slice(3, 6)
.map((line) => line.dataset.color);
expect(lineColours()).not.toContain('navy');
const hit = lines()[7];
if (!hit) throw new Error('no hit line');
fireEvent.mouseOver(hit);
expect(lineColours().map((colour) => colour === 'navy')).toEqual([true, true, false]);
fireEvent.mouseOut(hit);
expect(lineColours()).not.toContain('navy');
rerender(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} selectedId={groups[1]?.id ?? null} />);
expect(lineColours().map((colour) => colour === 'navy')).toEqual([false, false, true]);
root.style.removeProperty('--accent');
});
it('FE-FERRATA-LEAFLET-008: shows a route icon halfway along each ferrata only once zoomed in, and it opens the card', () => {
const onSelect = vi.fn();
const groups = groupFerratas([ferrata('way/1', 'Via A', 2), ferrata('way/2', 'Via B', 3, { lat: 47 })]);
render(<FerrataLayerLeaflet groups={groups} onSelect={onSelect} />);
expect(screen.queryAllByTestId('ferrata-icon')).toHaveLength(0);
zoom.value = 14;
act(() => zoomEnd.handler?.());
const icons = screen.getAllByTestId('ferrata-icon');
expect(icons).toHaveLength(2);
const [lat, lng] = JSON.parse(icons[0]?.dataset.position ?? '[]') as [number, number];
expect(lat).toBeCloseTo(46.505, 4);
expect(lng).toBeCloseTo(11.705, 4);
fireEvent.click(icons[1] as HTMLElement);
expect(onSelect).toHaveBeenCalledWith(groups[1], groups[1]?.segments[0], { lng: 11.705, lat: 46.505 });
});
it('FE-FERRATA-LEAFLET-009: a theme change recolours the lines without a refetch', async () => {
const root = document.documentElement;
root.style.setProperty('--text-primary', 'black');
render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 3)])} onSelect={vi.fn()} />);
expect(lines()[1]?.dataset.color).toBe('black');
await act(async () => { await act(async () => {
root.style.setProperty('--warning', 'gold'); root.style.setProperty('--text-primary', 'ivory');
await Promise.resolve(); await Promise.resolve();
}); });
await waitFor(() => expect(screen.getAllByTestId('polyline')[1]?.dataset.color).toBe('gold')); await waitFor(() => expect(lines()[1]?.dataset.color).toBe('ivory'));
root.style.removeProperty('--warning'); root.style.removeProperty('--text-primary');
});
it('FE-FERRATA-LEAFLET-010: a lit ferrata is drawn solid and wider on a wider casing, the hit line and tooltip carry their classes', () => {
const groups = groupFerratas([ferrata('way/1', 'Via A', 2)]);
render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} selectedId={groups[0]?.id ?? null} />);
expect(lines().map((line) => line.dataset.weight)).toEqual(['9', '4', '16']);
expect(lines()[1]?.dataset.dash).toBe('');
expect(lines()[2]?.dataset.class).toBe('trek-ferrata-hit');
expect(screen.getByTestId('tooltip').dataset.class).toBe('map-tooltip');
});
it('FE-FERRATA-LEAFLET-011: of two icons that would touch only the longer ferrata keeps its own', () => {
const short = ferrata('way/1', 'Via A', 2);
const long: Ferrata = {
...ferrata('way/2', 'Via B', 3, { lat: 46.5001 }),
geometry: [
[46.4951, 11.695],
[46.5151, 11.715],
],
};
const groups = groupFerratas([short, long]);
zoom.value = 14;
render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} />);
const icons = screen.getAllByTestId('ferrata-icon');
expect(icons).toHaveLength(1);
const [lat] = JSON.parse(icons[0]?.dataset.position ?? '[]') as [number, number];
expect(lat).toBeCloseTo(46.5051, 4);
}); });
}); });
@@ -1,79 +1,179 @@
import { useMemo } from 'react'; import L from 'leaflet';
import { Polyline, Tooltip } from 'react-leaflet'; import { useMemo, useState } from 'react';
import { Marker, Polyline, Tooltip, useMap, useMapEvents } from 'react-leaflet';
import type { Ferrata } from '@trek/shared';
import { useTranslation } from '../../i18n'; import { useTranslation } from '../../i18n';
import { ferrataGradeLabel, ferrataGradeTone, ferrataPositions } from './ferrataModel'; import { type FerrataGroup, ferrataLabel } from './ferrataGroups';
import { FERRATA_ICON_MIN_ZOOM, FERRATA_ICON_SIZE, ferrataIconSvg, placeFerrataIcons } from './ferrataIcon';
import { ferrataPositions } from './ferrataModel';
import type { FerrataSelectHandler } from './useFerrataLayerGL'; import type { FerrataSelectHandler } from './useFerrataLayerGL';
import type { FerrataMapLayer } from './useFerrataMapLayer'; import type { FerrataMapLayer } from './useFerrataMapLayer';
import { useFerrataPaint } from './useFerrataPaint'; import { useFerrataPaint } from './useFerrataPaint';
/** /**
* The via ferrata layer on the Leaflet renderer: per ferrata a casing and the * The route icon for Leaflet: the shared SVG in a divIcon, painted straight
* dashed line coloured by difficulty band, the same construction as the GL * from the tokens, so a theme change restyles it without rebuilding anything.
* layer, with a sticky tooltip naming the route and its grade. A click hands * Built once; it carries no user content.
* the ferrata to `onSelect`, like a click on the GL layer does.
*/ */
export default function FerrataLayerLeaflet({ /** The hit line's class, which index.css keeps free of the focus ring a clicked path draws. */
ferratas, const FERRATA_HIT_CLASS = 'trek-ferrata-hit';
onSelect,
}: {
ferratas: readonly Ferrata[];
onSelect: FerrataSelectHandler;
}) {
const { t } = useTranslation();
const paint = useFerrataPaint();
// Rebuilt per set of ferratas and per theme: pathOptions only reach Leaflet when their reference changes.
const lines = useMemo(
() =>
ferratas.map((ferrata) => ({
ferrata,
positions: ferrataPositions(ferrata),
casing: { color: paint.casing, weight: 6, opacity: 0.75, lineCap: 'round', lineJoin: 'round' } as const,
line: {
color: paint.tone[ferrataGradeTone(ferrata.grade)],
weight: 3,
opacity: 0.95,
dashArray: '6 5',
lineCap: 'round',
lineJoin: 'round',
} as const,
hit: { color: paint.casing, weight: 16, opacity: 0 } as const,
})),
[ferratas, paint]
);
if (lines.length === 0) return null; const ICON = L.divIcon({
html: ferrataIconSvg({ line: 'var(--text-primary)', casing: 'var(--bg-primary)' }),
className: 'trek-ferrata-icon',
iconSize: [FERRATA_ICON_SIZE, FERRATA_ICON_SIZE],
iconAnchor: [FERRATA_ICON_SIZE / 2, FERRATA_ICON_SIZE / 2],
});
/**
* The ferratas whose icon shows: none below FERRATA_ICON_MIN_ZOOM, and above
* it only those whose icon touches no other one at this zoom, the longer
* ferrata first. Leaflet has no symbol placement of its own, so this is what
* GL's `icon-allow-overlap: false` does there. Positions are projected at the
* zoom, which a pan does not change, so only a zoom or new ferratas place again.
*/
function useIconGroups(groups: readonly FerrataGroup[]): readonly FerrataGroup[] {
const map = useMap();
const [zoom, setZoom] = useState(() => map.getZoom());
useMapEvents({ zoomend: () => setZoom(map.getZoom()) });
return useMemo(() => {
if (zoom < FERRATA_ICON_MIN_ZOOM) return [];
const longestFirst = [...groups].sort((a, b) => b.lengthM - a.lengthM);
const shown = placeFerrataIcons(
longestFirst.map((group) => {
const { x, y } = map.project(group.midpoint, zoom);
return { id: group.id, x, y };
})
);
return groups.filter((group) => shown.has(group.id));
}, [map, groups, zoom]);
}
/** The tooltip's words: the ferrata's name and its grade. */
function FerrataTooltipText({ group }: { group: FerrataGroup }) {
const { t } = useTranslation();
const { name, grade } = ferrataLabel(group, t);
return ( return (
<> <>
{lines.map(({ ferrata, positions, casing }) => ( {name}
<Polyline key={`${ferrata.id}-casing`} positions={positions} pathOptions={casing} interactive={false} /> {grade && <span className="ms-1.5 font-semibold">{grade}</span>}
</>
);
}
/**
* The via ferrata layer on the Leaflet renderer, in the same construction as
* the GL layer: per OSM way a light casing and the dark dashed line, the whole
* ferrata lit (solid, wider, on a wider casing, in the highlight colour) while
* the pointer is on it or its card is open, a sticky tooltip in the map's
* tooltip look naming it with its grade, and from FERRATA_ICON_MIN_ZOOM a
* small route icon halfway along it where it does not touch another one. A
* click hands the ferrata and the way to `onSelect`, like a click on the GL
* layer does.
*/
export default function FerrataLayerLeaflet({
groups,
onSelect,
selectedId = null,
}: {
groups: readonly FerrataGroup[];
onSelect: FerrataSelectHandler;
selectedId?: string | null;
}) {
const paint = useFerrataPaint();
const iconGroups = useIconGroups(groups);
const [hovered, setHovered] = useState<string | null>(null);
// Rebuilt per set of ferratas and per theme: pathOptions only reach Leaflet when their reference changes.
const lines = useMemo(() => {
const casing = { color: paint.casing, weight: 6, opacity: 0.9, lineCap: 'round', lineJoin: 'round' } as const;
const litCasing = { ...casing, weight: 9, opacity: 1 } as const;
const line = {
color: paint.line,
weight: 2.5,
opacity: 1,
dashArray: '6 5',
lineCap: 'round',
lineJoin: 'round',
} as const;
// Solid: an empty dash list clears the dashes Leaflet would otherwise keep from the line's options.
const highlight = {
color: paint.highlight,
weight: 4,
opacity: 1,
dashArray: '',
lineCap: 'round',
lineJoin: 'round',
} as const;
// The class lets the stylesheet drop the focus ring a clicked SVG path keeps; the card has its own focus.
const hit = { color: paint.casing, weight: 16, opacity: 0, className: FERRATA_HIT_CLASS } as const;
return groups.flatMap((group) =>
group.segments.map((segment) => ({
group,
segment,
positions: ferrataPositions(segment),
casing,
litCasing,
line,
highlight,
hit,
}))
);
}, [groups, paint]);
if (lines.length === 0) return null;
const lit = (group: FerrataGroup) => group.id === hovered || group.id === selectedId;
return (
<>
{lines.map(({ group, segment, positions, casing, litCasing }) => (
<Polyline
key={`${segment.id}-casing`}
positions={positions}
pathOptions={lit(group) ? litCasing : casing}
interactive={false}
/>
))} ))}
{lines.map(({ ferrata, positions, line }) => ( {lines.map(({ group, segment, positions, line, highlight }) => (
<Polyline key={`${ferrata.id}-line`} positions={positions} pathOptions={line} interactive={false} /> <Polyline
key={`${segment.id}-line`}
positions={positions}
pathOptions={lit(group) ? highlight : line}
interactive={false}
/>
))} ))}
{/* A wide, invisible line on top carries the tooltip and the click, so a thin {/* A wide, invisible line on top carries the tooltip, the hover and the click, so a
dashed route is still easy to hit with a finger. The click stops here: it thin dashed route is still easy to hit with a finger. The click stops here: it
opens the ferrata's card and must not also drop a control point under it. */} opens the ferrata's card and must not also drop a control point under it. */}
{lines.map(({ ferrata, positions, hit }) => { {lines.map(({ group, segment, positions, hit }) => (
const name = ferrata.name ?? ferrata.routeName ?? t('ferratas.unnamed'); <Polyline
const grade = ferrataGradeLabel(ferrata.grade); key={`${segment.id}-hit`}
return ( positions={positions}
<Polyline pathOptions={hit}
key={`${ferrata.id}-hit`} bubblingMouseEvents={false}
positions={positions} eventHandlers={{
pathOptions={hit} click: (event) => onSelect(group, segment, { lng: event.latlng.lng, lat: event.latlng.lat }),
bubblingMouseEvents={false} mouseover: () => setHovered(group.id),
eventHandlers={{ click: (event) => onSelect(ferrata, { lng: event.latlng.lng, lat: event.latlng.lat }) }} mouseout: () => setHovered((current) => (current === group.id ? null : current)),
> }}
<Tooltip sticky> >
{name} <Tooltip sticky direction="top" offset={[0, -10]} opacity={1} className="map-tooltip">
{grade && <span className="ms-1.5 font-semibold">{grade}</span>} <FerrataTooltipText group={group} />
</Tooltip> </Tooltip>
</Polyline> </Polyline>
); ))}
})} {iconGroups.map((group) => (
<Marker
key={`${group.id}-icon`}
position={group.midpoint}
icon={ICON}
keyboard={false}
eventHandlers={{
click: (event) => {
const segment = group.segments[0];
if (segment) onSelect(group, segment, { lng: event.latlng.lng, lat: event.latlng.lat });
},
}}
/>
))}
</> </>
); );
} }
@@ -81,5 +181,5 @@ export default function FerrataLayerLeaflet({
/** The layer as the Leaflet map view mounts it from a `FerrataMapLayer`: drawn only while it is switched on. */ /** The layer as the Leaflet map view mounts it from a `FerrataMapLayer`: drawn only while it is switched on. */
export function FerrataMapLayerLeaflet({ layer }: { layer: FerrataMapLayer | undefined }) { export function FerrataMapLayerLeaflet({ layer }: { layer: FerrataMapLayer | undefined }) {
if (!layer?.active) return null; if (!layer?.active) return null;
return <FerrataLayerLeaflet ferratas={layer.ferratas} onSelect={layer.onSelect} />; return <FerrataLayerLeaflet groups={layer.groups} onSelect={layer.onSelect} selectedId={layer.selectedId} />;
} }
@@ -1,39 +0,0 @@
// FE-FERRATA-PILL-001 to FE-FERRATA-PILL-004: what the via ferrata layer says about itself.
import { describe, expect, it } from 'vitest';
import { render, screen } from '../../../tests/helpers/render';
import FerrataStatusPill from './FerrataStatusPill';
describe('FerrataStatusPill', () => {
it('FE-FERRATA-PILL-001: says nothing while the layer is off', () => {
const { container } = render(<FerrataStatusPill status="idle" count={0} />);
expect(container).toBeEmptyDOMElement();
});
it('FE-FERRATA-PILL-002: counts what it found, in the plural form the count takes', () => {
const { rerender } = render(<FerrataStatusPill status="ready" count={12} />);
expect(screen.getByRole('status')).toHaveTextContent('12 via ferrata routes');
rerender(<FerrataStatusPill status="ready" count={1} />);
expect(screen.getByRole('status')).toHaveTextContent('1 via ferrata route');
expect(screen.getByRole('status')).toHaveAttribute('aria-label', 'Via ferratas: 1 via ferrata route');
});
it('FE-FERRATA-PILL-003: shows loading and the two reasons a request was not made', () => {
const { rerender } = render(<FerrataStatusPill status="loading" count={0} />);
expect(screen.getByRole('status')).toHaveTextContent('Loading via ferratas…');
rerender(<FerrataStatusPill status="tooLarge" count={0} />);
expect(screen.getByRole('status')).toHaveTextContent('Zoom in to load via ferratas');
expect(screen.getByRole('status')).toHaveClass('text-content-secondary');
rerender(<FerrataStatusPill status="offline" count={0} />);
expect(screen.getByRole('status')).toHaveTextContent('Via ferratas need a connection');
});
it('FE-FERRATA-PILL-004: an unreachable Overpass reads as a problem', () => {
render(<FerrataStatusPill status="error" count={0} />);
expect(screen.getByRole('status')).toHaveTextContent('Overpass unreachable');
expect(screen.getByRole('status')).toHaveClass('text-danger');
});
});
@@ -1,65 +0,0 @@
import { Loader2, Mountain, TriangleAlert } from 'lucide-react';
import { useTranslation } from '../../i18n';
import { MAP_CONTROL_SHADOW } from '../Map/mapControlShadow';
import type { FerrataStatus } from './useTripFerratas';
/** The map controls' frosted glass, so the pill reads as part of the layer pill it sits beside. */
const GLASS = {
background: 'var(--sidebar-bg)',
backdropFilter: 'blur(20px) saturate(180%)',
WebkitBackdropFilter: 'blur(20px) saturate(180%)',
boxShadow: MAP_CONTROL_SHADOW,
};
/**
* What the via ferrata layer is doing, as one small pill over the map, in the
* row of the layer pill and at its height:
* loading, how many it found, or why there are none (Overpass unreachable, the
* area too large to ask for, the device offline). Nothing while the layer is
* off. Polite live region, so a screen reader hears the count arrive without
* being interrupted.
*/
export default function FerrataStatusPill({ status, count }: { status: FerrataStatus; count: number }) {
const { t } = useTranslation();
if (status === 'idle') return null;
const problem = status === 'error' || status === 'offline';
const label = labelOf(status, count, t);
const Icon = status === 'loading' ? Loader2 : problem ? TriangleAlert : Mountain;
return (
<div
role="status"
aria-live="polite"
aria-label={`${t('ferratas.layer')}: ${label}`}
data-testid="ferrata-status-pill"
className={`pointer-events-auto inline-flex h-[42px] flex-none items-center gap-1.5 whitespace-nowrap rounded-full px-3.5 text-caption font-semibold ${
problem ? 'text-danger' : 'text-content-secondary'
}`}
style={GLASS}
>
<Icon size={13} aria-hidden="true" className={status === 'loading' ? 'motion-safe:animate-spin' : undefined} />
<span>{label}</span>
</div>
);
}
function labelOf(
status: Exclude<FerrataStatus, 'idle'>,
count: number,
t: (key: string, params?: Record<string, string | number>) => string
) {
switch (status) {
case 'loading':
return t('ferratas.loading');
case 'error':
return t('ferratas.error');
case 'offline':
return t('ferratas.offline');
case 'tooLarge':
return t('ferratas.zoomIn');
case 'ready':
return t('ferratas.count', { count });
}
}
@@ -0,0 +1,149 @@
// FE-FERRATA-GROUP-001 to FE-FERRATA-GROUP-009: OSM's short segments read back as the ferratas a climber means.
import { describe, expect, it } from 'vitest';
import type { Ferrata } from '@trek/shared';
import {
FERRATA_NAME_REACH_M,
ferrataBboxesMeet,
ferrataGradeRange,
ferrataGroupOf,
ferrataGroupsInView,
groupFerratas,
} from './ferrataGroups';
/** About 111 m of latitude. */
const STEP = 0.001;
function segment(
id: number,
from: [number, number],
options: {
name?: string | null;
routeName?: string | null;
level?: number | null;
plus?: '+' | '-' | null;
raw?: string;
} = {}
): Ferrata {
const level = options.level === undefined ? 3 : options.level;
return {
id: `way/${id}`,
name: options.name === undefined ? 'Via X' : options.name,
routeName: options.routeName ?? null,
grade: { raw: options.raw ?? (level === null ? null : String(level)), level, plus: options.plus ?? null },
geometry: [from, [from[0] + STEP, from[1]]],
osmUrl: `https://www.openstreetmap.org/way/${id}`,
};
}
describe('groupFerratas', () => {
it('FE-FERRATA-GROUP-001: joins touching segments of one name into one ferrata, named after them', () => {
const groups = groupFerratas([
segment(2, [46.5, 11.7], { level: 4 }),
segment(1, [46.5 + STEP, 11.7], { level: 2 }),
segment(3, [46.5 + 2 * STEP, 11.7], { level: 3 }),
]);
expect(groups).toHaveLength(1);
const [group] = groups;
expect(group?.id).toBe('group/way/1');
expect(group?.name).toBe('Via X');
expect(group?.segments.map((s) => s.id)).toEqual(['way/2', 'way/1', 'way/3']);
expect(group?.lengthM).toBeGreaterThan(330);
expect(group?.lengthM).toBeLessThan(340);
});
it('FE-FERRATA-GROUP-002: chains segments within the reach, and keeps a homonym far away apart', () => {
const gap = (FERRATA_NAME_REACH_M - 50) / 111_195;
const groups = groupFerratas([
segment(1, [46.5, 11.7]),
segment(2, [46.5 + STEP + gap, 11.7]),
// The same name ten kilometres north is another ferrata.
segment(3, [46.6, 11.7]),
]);
expect(groups.map((g) => g.segments.map((s) => s.id))).toEqual([['way/1', 'way/2'], ['way/3']]);
});
it('FE-FERRATA-GROUP-003: reads names without case or outer spaces, and keeps every unnamed segment alone', () => {
const groups = groupFerratas([
segment(1, [46.5, 11.7], { name: 'Via X' }),
segment(2, [46.5 + STEP, 11.7], { name: ' via x ' }),
segment(3, [46.5, 11.71], { name: null }),
segment(4, [46.5 + STEP, 11.71], { name: null }),
]);
expect(groups.map((g) => g.segments.length)).toEqual([2, 1, 1]);
expect(groups[1]?.name).toBeNull();
});
it('FE-FERRATA-GROUP-004: groups by the route relation first, over a wider reach and whatever the ways are named', () => {
const groups = groupFerratas([
segment(10, [46.5, 11.7], { routeName: 'Brigata Tridentina', name: 'Pitch 1' }),
// 1.5 km on, the same route.
segment(11, [46.5135, 11.7], { routeName: 'Brigata Tridentina', name: null }),
// A way named like the route but outside the relation is its own ferrata.
segment(12, [46.5 + STEP, 11.7], { name: 'Brigata Tridentina' }),
]);
expect(groups).toHaveLength(2);
expect(groups[0]?.segments.map((s) => s.id)).toEqual(['way/10', 'way/11']);
expect(groups[0]?.routeName).toBe('Brigata Tridentina');
expect(groups[0]?.name).toBe('Pitch 1');
});
it('FE-FERRATA-GROUP-005: spans the grades from the easiest to the hardest segment, minus before plus', () => {
const [group] = groupFerratas([
segment(1, [46.5, 11.7], { level: 4, plus: '+' }),
segment(2, [46.5 + STEP, 11.7], { level: 2, plus: '-' }),
segment(3, [46.5 + 2 * STEP, 11.7], { level: 2 }),
segment(4, [46.5 + 3 * STEP, 11.7], { level: null }),
]);
expect(group && ferrataGradeRange(group)).toEqual({ from: 'K2-', to: 'K4+' });
expect(group?.rawGrade).toBeNull();
});
it('FE-FERRATA-GROUP-006: without a numeric grade keeps the tag as written, and has no range', () => {
const [group] = groupFerratas([segment(1, [46.5, 11.7], { level: null, raw: 'C/D' })]);
expect(group && ferrataGradeRange(group)).toBeNull();
expect(group?.rawGrade).toBe('C/D');
const [single] = groupFerratas([segment(2, [46.5, 11.7], { level: 3 })]);
expect(single && ferrataGradeRange(single)).toEqual({ from: 'K3', to: 'K3' });
});
it('FE-FERRATA-GROUP-007: puts the label halfway along the longest segment and spans every segment in its box', () => {
const long: Ferrata = {
...segment(1, [46.5, 11.7]),
geometry: [
[46.5, 11.7],
[46.5 + 4 * STEP, 11.7],
],
};
const [group] = groupFerratas([segment(2, [46.5 + 4 * STEP, 11.7]), long]);
expect(group?.midpoint[0]).toBeCloseTo(46.502, 5);
expect(group?.midpoint[1]).toBeCloseTo(11.7, 5);
expect(group?.bbox.south).toBe(46.5);
expect(group?.bbox.north).toBeCloseTo(46.505, 9);
expect(group?.bbox.west).toBe(11.7);
expect(group?.bbox.east).toBe(11.7);
});
it('FE-FERRATA-GROUP-008: finds the ferrata a way belongs to, and counts the ferratas reaching into a view', () => {
const groups = groupFerratas([
segment(1, [46.5, 11.7]),
segment(2, [46.5 + STEP, 11.7]),
segment(3, [46.6, 11.8], { name: 'Other' }),
]);
expect(ferrataGroupOf(groups, 'way/2')?.id).toBe('group/way/1');
expect(ferrataGroupOf(groups, 'way/99')).toBeNull();
expect(ferrataGroupOf(groups, null)).toBeNull();
expect(ferrataGroupsInView(groups, { south: 46.49, west: 11.69, north: 46.51, east: 11.71 })).toBe(1);
expect(ferrataGroupsInView(groups, { south: 46.4, west: 11.6, north: 46.7, east: 11.9 })).toBe(2);
expect(ferrataGroupsInView(groups, null)).toBe(0);
});
it('FE-FERRATA-GROUP-009: boxes meet on a shared edge, not across a gap; a line under two points is dropped', () => {
const box = { south: 0, west: 0, north: 1, east: 1 };
expect(ferrataBboxesMeet(box, { south: 1, west: 1, north: 2, east: 2 })).toBe(true);
expect(ferrataBboxesMeet(box, { south: 1.1, west: 0, north: 2, east: 1 })).toBe(false);
const point = { ...segment(1, [46.5, 11.7]), geometry: [[46.5, 11.7]] } as Ferrata;
expect(groupFerratas([point])).toEqual([]);
});
});
@@ -0,0 +1,270 @@
import type { Ferrata, FerrataBbox, FerrataGrade } from '@trek/shared';
import { ferrataGradeLabel, ferrataPositions } from './ferrataModel';
/**
* OpenStreetMap maps one via ferrata as many short ways: the approach, each
* pitch, a bridge, the exit. This turns those segments back into the routes a
* climber means, so the layer counts, highlights and describes one ferrata
* rather than a dozen pieces of it. Pure and React-free.
*
* Segments belong together when they share their route relation's name, or,
* with no relation, their own name, and lie close to each other: a chain of
* segments joins as long as each one comes within the reach of the next.
* Unnamed segments stay on their own; nothing says two of them are one route.
*/
/** How close two segments of the same name have to come to read as one ferrata. */
export const FERRATA_NAME_REACH_M = 300;
/**
* The same for a route relation's name. A relation is one route by definition,
* so the reach is wider; it only keeps two distant routes that happen to share
* a name apart.
*/
const FERRATA_ROUTE_REACH_M = 2000;
export interface FerrataGroup {
/** Stable for as long as the group keeps its lowest way: `group/<way id>`. */
id: string;
name: string | null;
routeName: string | null;
/** The OSM ways it is made of, in the order they arrived. */
segments: readonly Ferrata[];
/** The easiest and the hardest numeric grade among the segments, or null when none has one. */
grades: { min: FerrataGrade; max: FerrataGrade } | null;
/** A grade as tagged when no segment has a numeric one (a letter scale such as "C/D"). */
rawGrade: string | null;
/** The summed length of every segment, in metres. */
lengthM: number;
bbox: FerrataBbox;
/** Where its label goes: halfway along its longest segment, as [lat, lng]. */
midpoint: [number, number];
}
const EARTH_RADIUS_M = 6_371_000;
const RAD = Math.PI / 180;
function distanceM([lat1, lng1]: [number, number], [lat2, lng2]: [number, number]): number {
const dLat = (lat2 - lat1) * RAD;
const dLng = (lng2 - lng1) * RAD;
const a = Math.sin(dLat / 2) ** 2 + Math.cos(lat1 * RAD) * Math.cos(lat2 * RAD) * Math.sin(dLng / 2) ** 2;
return 2 * EARTH_RADIUS_M * Math.asin(Math.min(1, Math.sqrt(a)));
}
function lengthOf(points: readonly [number, number][]): number {
let total = 0;
for (let i = 1; i < points.length; i += 1) total += distanceM(points[i - 1], points[i]);
return total;
}
/**
* The box around the points, the ones that are not finite numbers skipped, or
* null when none is left.
*/
export function ferrataPointsBbox(points: Iterable<readonly [number, number]>): FerrataBbox | null {
let south = Infinity;
let west = Infinity;
let north = -Infinity;
let east = -Infinity;
for (const [lat, lng] of points) {
if (!Number.isFinite(lat) || !Number.isFinite(lng)) continue;
south = Math.min(south, lat);
north = Math.max(north, lat);
west = Math.min(west, lng);
east = Math.max(east, lng);
}
return south <= north && west <= east ? { south, west, north, east } : null;
}
/** A segment's box; the schema guarantees it at least two placed points. */
function bboxOf(points: readonly [number, number][]): FerrataBbox {
return ferrataPointsBbox(points) ?? { south: 0, west: 0, north: 0, east: 0 };
}
/** Whether two boxes share any point, edges included. */
export function ferrataBboxesMeet(a: FerrataBbox, b: FerrataBbox): boolean {
return a.south <= b.north && b.south <= a.north && a.west <= b.east && b.west <= a.east;
}
/** The point halfway along a line, by length. */
function halfway(points: readonly [number, number][]): [number, number] {
const half = lengthOf(points) / 2;
let walked = 0;
for (let i = 1; i < points.length; i += 1) {
const step = distanceM(points[i - 1], points[i]);
if (walked + step >= half && step > 0) {
const t = (half - walked) / step;
const [lat1, lng1] = points[i - 1];
const [lat2, lng2] = points[i];
return [lat1 + (lat2 - lat1) * t, lng1 + (lng2 - lng1) * t];
}
walked += step;
}
return points[0] ?? [0, 0];
}
interface Segment {
ferrata: Ferrata;
points: [number, number][];
bbox: FerrataBbox;
lengthM: number;
}
/** Whether two boxes come within `reach` metres of each other, a cheap test before any point is compared. */
function boxesWithin(a: FerrataBbox, b: FerrataBbox, reach: number): boolean {
const latPad = reach / (EARTH_RADIUS_M * RAD);
const lngPad = latPad / Math.max(0.01, Math.cos(((a.south + a.north) / 2) * RAD));
return (
a.south - latPad <= b.north && b.south - latPad <= a.north && a.west - lngPad <= b.east && b.west - lngPad <= a.east
);
}
/** Whether an end of either segment comes within `reach` of a point of the other: they touch or nearly do. */
function segmentsMeet(a: Segment, b: Segment, reach: number): boolean {
if (!boxesWithin(a.bbox, b.bbox, reach)) return false;
const near = (ends: Segment, line: Segment) => {
const tips = [ends.points[0], ends.points[ends.points.length - 1]];
return tips.some((tip) => tip !== undefined && line.points.some((point) => distanceM(tip, point) <= reach));
};
return near(a, b) || near(b, a);
}
/** What segments group by, or null for one that stays alone. Case and outer spaces do not tell two names apart. */
function groupKey(ferrata: Ferrata): { key: string; reach: number } | null {
const route = ferrata.routeName?.trim().toLocaleLowerCase();
if (route) return { key: `route:${route}`, reach: FERRATA_ROUTE_REACH_M };
const name = ferrata.name?.trim().toLocaleLowerCase();
if (name) return { key: `name:${name}`, reach: FERRATA_NAME_REACH_M };
return null;
}
/** Segments of one key joined into chains: any two that meet end up in one set. */
function chains(segments: Segment[], reach: number): Segment[][] {
const parent = segments.map((_, index) => index);
const find = (i: number): number => {
let root = i;
while (parent[root] !== root) root = parent[root];
parent[i] = root;
return root;
};
for (let i = 0; i < segments.length; i += 1) {
for (let j = i + 1; j < segments.length; j += 1) {
if (find(i) !== find(j) && segmentsMeet(segments[i], segments[j], reach)) parent[find(j)] = find(i);
}
}
const sets = new Map<number, Segment[]>();
segments.forEach((segment, index) => {
const root = find(index);
const set = sets.get(root);
if (set) set.push(segment);
else sets.set(root, [segment]);
});
return [...sets.values()];
}
/** -, none, + in that order within a level. */
const gradeRank = (grade: FerrataGrade) =>
(grade.level ?? 0) * 3 + (grade.plus === '-' ? 0 : grade.plus === '+' ? 2 : 1);
function wayNumber(id: string): number {
const n = Number(id.slice(id.indexOf('/') + 1));
return Number.isFinite(n) ? n : Number.MAX_SAFE_INTEGER;
}
function toGroup(members: Segment[]): FerrataGroup {
const segments = members.map((member) => member.ferrata);
const graded = segments.map((segment) => segment.grade).filter((grade) => grade.level !== null);
const sorted = [...graded].sort((a, b) => gradeRank(a) - gradeRank(b));
const longest = members.reduce((best, member) => (member.lengthM > best.lengthM ? member : best), members[0]);
// The name of the longest named segment speaks for the group.
const named = [...members].sort((a, b) => b.lengthM - a.lengthM).find((member) => member.ferrata.name);
const lowest = [...segments].sort((a, b) => wayNumber(a.id) - wayNumber(b.id))[0];
return {
id: `group/${lowest.id}`,
name: named?.ferrata.name ?? null,
routeName: segments.find((segment) => segment.routeName)?.routeName ?? null,
segments,
grades: sorted.length ? { min: sorted[0], max: sorted[sorted.length - 1] } : null,
rawGrade: sorted.length ? null : (segments.find((segment) => segment.grade.raw)?.grade.raw ?? null),
lengthM: members.reduce((sum, member) => sum + member.lengthM, 0),
bbox: members.slice(1).reduce(
(box, member) => ({
south: Math.min(box.south, member.bbox.south),
west: Math.min(box.west, member.bbox.west),
north: Math.max(box.north, member.bbox.north),
east: Math.max(box.east, member.bbox.east),
}),
members[0].bbox
),
midpoint: halfway(longest.points),
};
}
/** The segments as ferratas, in the order their first segment arrived. */
export function groupFerratas(ferratas: readonly Ferrata[]): FerrataGroup[] {
const keyed = new Map<string, { reach: number; segments: Segment[] }>();
const ordered: Array<Segment[] | string> = [];
for (const ferrata of ferratas) {
const points = ferrataPositions(ferrata);
if (points.length < 2) continue;
const segment: Segment = { ferrata, points, bbox: bboxOf(points), lengthM: lengthOf(points) };
const key = groupKey(ferrata);
if (!key) {
ordered.push([segment]);
continue;
}
const bucket = keyed.get(key.key);
if (bucket) bucket.segments.push(segment);
else {
keyed.set(key.key, { reach: key.reach, segments: [segment] });
ordered.push(key.key);
}
}
return ordered.flatMap((entry) => {
if (typeof entry !== 'string') return [toGroup(entry)];
const bucket = keyed.get(entry);
return bucket ? chains(bucket.segments, bucket.reach).map(toGroup) : [];
});
}
/** The group a way belongs to, for a click or a selection that names one segment. */
export function ferrataGroupOf(groups: readonly FerrataGroup[], segmentId: string | null): FerrataGroup | null {
if (!segmentId) return null;
return groups.find((group) => group.segments.some((segment) => segment.id === segmentId)) ?? null;
}
/** How many ferratas reach into the area on screen. */
export function ferrataGroupsInView(groups: readonly FerrataGroup[], view: FerrataBbox | null): number {
if (!view) return 0;
return groups.reduce((count, group) => (ferrataBboxesMeet(group.bbox, view) ? count + 1 : count), 0);
}
/**
* The group's grade on the K scale: the easiest and the hardest segment
* (`K2`, `K4`), the same label twice when they agree, null when no segment has
* a numeric grade.
*/
export function ferrataGradeRange(group: Pick<FerrataGroup, 'grades'>): { from: string; to: string } | null {
if (!group.grades) return null;
const from = ferrataGradeLabel(group.grades.min);
const to = ferrataGradeLabel(group.grades.max);
return from && to ? { from, to } : null;
}
type Translate = (key: string, params?: Record<string, string | number>) => string;
/**
* What a ferrata is called on the map and on its card: its own name, else its
* route's, else "unnamed"; and its grade, `K3` or `K2 to K4`, or without a
* numeric grade the tag as written (a letter scale such as "C/D" still says
* something), or null when there is none at all.
*/
export function ferrataLabel(
group: Pick<FerrataGroup, 'name' | 'routeName' | 'grades' | 'rawGrade'>,
t: Translate
): { name: string; grade: string | null } {
const name = group.name ?? group.routeName ?? t('ferratas.unnamed');
const range = ferrataGradeRange(group);
if (!range) return { name, grade: group.rawGrade };
return { name, grade: range.from === range.to ? range.from : t('ferratas.gradeRange', range) };
}
@@ -0,0 +1,70 @@
/**
* The name and grade of the via ferrata under the pointer on a GL map, the
* twin of the Leaflet layer's sticky tooltip: the same words, the look of the
* shared Tooltip (card surface, faint edge, caption tier), floating just above
* the pointer.
*
* Built from the DOM rather than React, because it follows every mouse move:
* a React state per move would render the whole map view each time. The words
* go in as text, never as markup. It lives in the map's own container, so it
* scrolls, hides and goes away with the map.
*/
/** How far above the pointer the label floats, in CSS pixels. */
const POINTER_GAP = 12;
const LABEL_CLASS =
'pointer-events-none absolute z-20 whitespace-nowrap rounded-lg border border-edge-faint bg-surface-card px-2.5 py-1 text-caption font-medium text-content shadow-lg';
/** Just the call the label needs, so the Mapbox and the MapLibre map both fit, and a stand-in without it gets none. */
interface LabelHost {
getContainer?(): HTMLElement;
}
interface FerrataHoverLabel {
show(text: { name: string; grade: string | null }, point: { x: number; y: number }): void;
hide(): void;
remove(): void;
}
const NO_LABEL: FerrataHoverLabel = { show: () => undefined, hide: () => undefined, remove: () => undefined };
export function ferrataHoverLabel(map: LabelHost): FerrataHoverLabel {
if (typeof map.getContainer !== 'function' || typeof document === 'undefined') return NO_LABEL;
const container = map.getContainer();
let element: HTMLDivElement | null = null;
const ensure = () => {
if (element) return element;
element = document.createElement('div');
element.className = LABEL_CLASS;
element.setAttribute('role', 'tooltip');
element.dataset.testid = 'ferrata-hover-label';
// The pointer's pixel on the map, not a side of the text: placed from the corner either way.
element.style.inset = '0px auto auto 0px';
container.appendChild(element);
return element;
};
return {
show({ name, grade }, { x, y }) {
const label = ensure();
label.replaceChildren(document.createTextNode(name));
if (grade) {
const badge = document.createElement('span');
badge.className = 'ms-1.5 font-semibold';
badge.textContent = grade;
label.appendChild(badge);
}
label.style.transform = `translate(${x}px, ${y - POINTER_GAP}px) translate(-50%, -100%)`;
label.hidden = false;
},
hide() {
if (element) element.hidden = true;
},
remove() {
element?.remove();
element = null;
},
};
}
@@ -0,0 +1,66 @@
// FE-FERRATA-ICON-001 to FE-FERRATA-ICON-004: the route icon halfway along a via ferrata.
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
FERRATA_ICON_GAP,
FERRATA_ICON_PIXEL_RATIO,
FERRATA_ICON_SIZE,
ferrataIconSvg,
loadFerrataIcon,
placeFerrataIcons,
} from './ferrataIcon';
afterEach(() => {
vi.unstubAllGlobals();
});
describe('ferrataIcon', () => {
it('FE-FERRATA-ICON-001: draws a ladder in the line colour on a disc of the casing colour', () => {
const svg = ferrataIconSvg({ line: 'var(--text-primary)', casing: 'rgb(255, 255, 255)' });
expect(svg).toContain('fill="rgb(255, 255, 255)"');
expect(svg).toContain('stroke="var(--text-primary)"');
expect(svg).toContain(`width="${FERRATA_ICON_SIZE}"`);
});
it('FE-FERRATA-ICON-002: lets nothing but a colour into the markup', () => {
const svg = ferrataIconSvg({ line: '"/><script>alert(1)</script>', casing: 'white' });
expect(svg).not.toContain('<script>');
expect(svg).toContain('stroke="currentColor"');
});
it('FE-FERRATA-ICON-003: hands GL a sharp image of the same SVG, and rejects when it cannot be decoded', async () => {
const made: Array<{ src: string; width?: number }> = [];
class FakeImage {
onload: (() => void) | null = null;
onerror: (() => void) | null = null;
constructor(public width?: number) {
made.push(this as unknown as { src: string; width?: number });
}
set src(value: string) {
(this as unknown as { _src: string })._src = value;
queueMicrotask(() => (value.includes('fail') ? this.onerror?.() : this.onload?.()));
}
get src() {
return (this as unknown as { _src: string })._src;
}
}
vi.stubGlobal('Image', FakeImage);
const image = await loadFerrataIcon({ line: 'black', casing: 'white' });
expect(image).toBe(made[0]);
expect(made[0]?.width).toBe(FERRATA_ICON_SIZE * FERRATA_ICON_PIXEL_RATIO);
expect(decodeURIComponent(made[0]?.src ?? '')).toContain('stroke="black"');
await expect(loadFerrataIcon({ line: 'fail', casing: 'white' })).rejects.toThrow('could not be drawn');
});
it('FE-FERRATA-ICON-004: leaves out an icon that would touch one already placed, first come first placed', () => {
const placed = placeFerrataIcons([
{ id: 'a', x: 100, y: 100 },
{ id: 'b', x: 100 + FERRATA_ICON_GAP - 1, y: 100 },
{ id: 'c', x: 100, y: 100 + FERRATA_ICON_GAP },
{ id: 'd', x: 400, y: 400 },
]);
expect([...placed]).toEqual(['a', 'c', 'd']);
expect(FERRATA_ICON_GAP).toBeGreaterThan(FERRATA_ICON_SIZE);
});
});
@@ -0,0 +1,79 @@
import type { FerrataPaint } from './ferrataPaint';
/**
* The small route icon a via ferrata carries at its midpoint once the map is
* zoomed in: a ladder in the line colour on a disc of the casing colour, so it
* reads as part of the line it sits on.
*
* One SVG for both renderers: Leaflet puts it in a divIcon, where it can take
* `var(--token)` straight from the page; GL needs an image with the colours
* resolved, which `loadFerrataIcon` makes from the resolved paint.
*/
/** The zoom from which the icons show; below it they would crowd a valley full of ferratas. */
export const FERRATA_ICON_MIN_ZOOM = 13;
/** The icon's size on screen, in CSS pixels. */
export const FERRATA_ICON_SIZE = 22;
/** The name the GL renderer registers the image under. */
export const FERRATA_ICON_IMAGE = 'trek-ferrata-icon';
/**
* A colour as it may go into the markup: a resolved token (a hex, `rgb()` or
* named value) or a `var(--token)`. Anything else, which a token never
* resolves to, falls back to `currentColor` rather than reaching the SVG.
*/
function safeColour(value: string): string {
return /^[#\w\s(),.%-]+$/.test(value) ? value : 'currentColor';
}
export function ferrataIconSvg(paint: Pick<FerrataPaint, 'line' | 'casing'>, size = FERRATA_ICON_SIZE): string {
const line = safeColour(paint.line);
const casing = safeColour(paint.casing);
return [
`<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 22 22" aria-hidden="true">`,
`<circle cx="11" cy="11" r="9.75" fill="${casing}" stroke="${line}" stroke-width="1.5"/>`,
`<path d="M8 6v10M14 6v10M8 8.5h6M8 11h6M8 13.5h6" fill="none" stroke="${line}" stroke-width="1.6" stroke-linecap="round"/>`,
'</svg>',
].join('');
}
/** How sharp the GL image is drawn; the map scales it back to FERRATA_ICON_SIZE. */
export const FERRATA_ICON_PIXEL_RATIO = 2;
/**
* The icon as an image for a GL map, in the colours the tokens resolve to now.
* Rejects when the browser cannot decode it, and the lines then simply go
* without their icons.
*/
export function loadFerrataIcon(paint: Pick<FerrataPaint, 'line' | 'casing'>): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const image = new Image(FERRATA_ICON_SIZE * FERRATA_ICON_PIXEL_RATIO, FERRATA_ICON_SIZE * FERRATA_ICON_PIXEL_RATIO);
image.onload = () => resolve(image);
image.onerror = () => reject(new Error('The via ferrata icon could not be drawn'));
const svg = ferrataIconSvg(paint, FERRATA_ICON_SIZE * FERRATA_ICON_PIXEL_RATIO);
image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
});
}
/** The room an icon keeps to the next one, in CSS pixels, so two discs never touch. */
export const FERRATA_ICON_GAP = FERRATA_ICON_SIZE + 4;
/**
* Which icons fit on screen without touching one another, for a renderer that
* does not place symbols itself (Leaflet; GL keeps `icon-allow-overlap` off for
* the same effect). `points` are the icons' screen positions in pixels, in the
* order they should win: an icon closer than FERRATA_ICON_GAP to one already
* placed is left out.
*/
export function placeFerrataIcons(points: ReadonlyArray<{ id: string; x: number; y: number }>): Set<string> {
const placed: Array<{ x: number; y: number }> = [];
const ids = new Set<string>();
for (const point of points) {
if (placed.some((other) => Math.hypot(other.x - point.x, other.y - point.y) < FERRATA_ICON_GAP)) continue;
placed.push(point);
ids.add(point.id);
}
return ids;
}
@@ -1,17 +1,17 @@
// FE-FERRATA-MODEL-001 to FE-FERRATA-MODEL-008: the pure half of the via ferrata layer. // FE-FERRATA-MODEL-001 to FE-FERRATA-MODEL-007: the pure half of the via ferrata layer.
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { FERRATA_BBOX_MAX_SPAN_DEG, type Ferrata } from '@trek/shared'; import { FERRATA_BBOX_MAX_SPAN_DEG, type Ferrata } from '@trek/shared';
import { groupFerratas } from './ferrataGroups';
import { import {
ferrataBboxFits, ferrataBboxFits,
ferrataBboxParam, ferrataBboxParam,
ferrataFeatureCollection, ferrataFeatureCollection,
ferrataGradeLabel, ferrataGradeLabel,
ferrataGradeTone, ferrataLabelCollection,
ferrataOsmUrl, ferrataOsmUrl,
ferrataPositions, ferrataPositions,
roundFerrataBbox,
} from './ferrataModel'; } from './ferrataModel';
const grade = (level: number | null, plus: '+' | '-' | null = null, raw: string | null = null) => ({ const grade = (level: number | null, plus: '+' | '-' | null = null, raw: string | null = null) => ({
@@ -45,56 +45,28 @@ describe('ferrataModel', () => {
expect(ferrataGradeLabel(grade(null, null, 'C/D'))).toBeNull(); expect(ferrataGradeLabel(grade(null, null, 'C/D'))).toBeNull();
}); });
it('FE-FERRATA-MODEL-003: bands levels 0 to 2 easy, 3 and 4 medium, 5 and 6 hard, none unknown', () => { it('FE-FERRATA-MODEL-003: takes an area only up to the server limit and only a real box', () => {
expect([0, 1, 2, 3, 4, 5, 6].map((level) => ferrataGradeTone(grade(level)))).toEqual([
'easy',
'easy',
'easy',
'medium',
'medium',
'hard',
'hard',
]);
expect(ferrataGradeTone(grade(null, null, 'C'))).toBe('unknown');
});
it('FE-FERRATA-MODEL-004: rounds the map area outwards to three decimals, inside the world', () => {
expect(roundFerrataBbox({ south: 46.12345, west: 11.98761, north: 46.20001, east: 12.1 })).toEqual({
south: 46.123,
west: 11.987,
north: 46.201,
east: 12.1,
});
expect(roundFerrataBbox({ south: -90.5, west: -181, north: 91, east: 180.4 })).toEqual({
south: -90,
west: -180,
north: 90,
east: 180,
});
});
it('FE-FERRATA-MODEL-005: takes an area only up to the server limit and only a real box', () => {
const max = FERRATA_BBOX_MAX_SPAN_DEG; const max = FERRATA_BBOX_MAX_SPAN_DEG;
expect(ferrataBboxFits({ south: 46, west: 11, north: 46 + max, east: 11 + max })).toBe(true); expect(ferrataBboxFits({ south: 46, west: 11, north: 46 + max, east: 11 + max })).toBe(true);
expect(ferrataBboxFits({ south: 46, west: 11, north: 46 + max + 0.01, east: 11.2 })).toBe(false); expect(ferrataBboxFits({ south: 46, west: 11, north: 46 + max + 0.01, east: 11.2 })).toBe(false);
expect(ferrataBboxFits({ south: 46, west: 11, north: 46.2, east: 11 + max + 0.01 })).toBe(false); expect(ferrataBboxFits({ south: 46, west: 11, north: 46.2, east: 11 + max + 0.01 })).toBe(false);
expect(ferrataBboxFits({ south: 46.2, west: 11, north: 46.2, east: 11.2 })).toBe(false); expect(ferrataBboxFits({ south: 46.2, west: 11, north: 46.2, east: 11.2 })).toBe(false);
// The server's own rule: a rounded 0.500 span that reads 0.5000000000000071 still goes out. // The server's own rule: a 0.500 span that reads 0.5000000000000071 still goes out.
expect(ferrataBboxFits({ south: 63.501, west: 11, north: 64.001, east: 11.2 })).toBe(true); expect(ferrataBboxFits({ south: 63.501, west: 11, north: 64.001, east: 11.2 })).toBe(true);
}); });
it('FE-FERRATA-MODEL-006: writes the bbox parameter in south,west,north,east order', () => { it('FE-FERRATA-MODEL-004: writes the bbox parameter in south,west,north,east order', () => {
expect(ferrataBboxParam({ south: 46.1, west: 11.25, north: 46.3, east: 11.5 })).toBe('46.100,11.250,46.300,11.500'); expect(ferrataBboxParam({ south: 46.1, west: 11.25, north: 46.3, east: 11.5 })).toBe('46.100,11.250,46.300,11.500');
}); });
it('FE-FERRATA-MODEL-007: builds GeoJSON with [lng, lat] coordinates and only the id and tone', () => { it('FE-FERRATA-MODEL-005: builds one GeoJSON line per way, in [lng, lat], carrying only the way and its group', () => {
const collection = ferrataFeatureCollection([FERRATA]); const collection = ferrataFeatureCollection(groupFerratas([FERRATA]));
expect(collection).toEqual({ expect(collection).toEqual({
type: 'FeatureCollection', type: 'FeatureCollection',
features: [ features: [
{ {
type: 'Feature', type: 'Feature',
properties: { id: 'way/123', tone: 'medium' }, properties: { id: 'way/123', group: 'group/way/123' },
geometry: { geometry: {
type: 'LineString', type: 'LineString',
coordinates: [ coordinates: [
@@ -109,7 +81,16 @@ describe('ferrataModel', () => {
expect(ferrataFeatureCollection([]).features).toEqual([]); expect(ferrataFeatureCollection([]).features).toEqual([]);
}); });
it('FE-FERRATA-MODEL-008: links only ever to openstreetmap.org, whatever the answer sent as osmUrl', () => { it('FE-FERRATA-MODEL-006: puts one label point per ferrata halfway along it, in [lng, lat]', () => {
const [feature] = ferrataLabelCollection(groupFerratas([FERRATA])).features;
expect(feature?.properties).toEqual({ group: 'group/way/123' });
expect(feature?.geometry.type).toBe('Point');
const [lng, lat] = feature?.geometry.coordinates ?? [0, 0];
expect(lng).toBeCloseTo(11.815, 4);
expect(lat).toBeCloseTo(46.535, 4);
});
it('FE-FERRATA-MODEL-007: links only ever to openstreetmap.org, whatever the answer sent as osmUrl', () => {
expect(ferrataOsmUrl(FERRATA)).toBe('https://www.openstreetmap.org/way/123'); expect(ferrataOsmUrl(FERRATA)).toBe('https://www.openstreetmap.org/way/123');
expect(ferrataOsmUrl({ id: 'javascript:alert(1)' })).toBe('https://www.openstreetmap.org/'); expect(ferrataOsmUrl({ id: 'javascript:alert(1)' })).toBe('https://www.openstreetmap.org/');
}); });
+45 -53
View File
@@ -1,14 +1,12 @@
import { type Ferrata, type FerrataBbox, type FerrataGrade, ferrataBboxSpanOk } from '@trek/shared'; import { type Ferrata, type FerrataBbox, type FerrataGrade, ferrataBboxSpanOk } from '@trek/shared';
/** import type { FerrataGroup } from './ferrataGroups';
* The pure half of the via ferrata layer: grade wording, the difficulty band a
* badge and a line are coloured by, the map area a request asks for, and the
* GeoJSON the GL renderer draws. React-free, so both renderers and the detail
* card read the same answers.
*/
/** The difficulty band a grade falls into; `unknown` when OSM gave no 0 to 6 level. */ /**
export type FerrataTone = 'easy' | 'medium' | 'hard' | 'unknown'; * The pure half of the via ferrata layer: grade wording, the area checks a
* request makes, and the GeoJSON the GL renderer draws. React-free, so both
* renderers and the detail card read the same answers.
*/
/** /**
* The grade as the K scale reads it: `K3`, `K4+`, `K2-`. Null when the way has * The grade as the K scale reads it: `K3`, `K4+`, `K2-`. Null when the way has
@@ -20,15 +18,6 @@ export function ferrataGradeLabel(grade: FerrataGrade): string | null {
return `K${grade.level}${grade.plus ?? ''}`; return `K${grade.level}${grade.plus ?? ''}`;
} }
/** Levels 0 to 2 are easy, 3 and 4 medium, 5 and 6 hard. */
export function ferrataGradeTone(grade: FerrataGrade): FerrataTone {
const level = grade.level;
if (level === null || level === undefined) return 'unknown';
if (level <= 2) return 'easy';
if (level <= 4) return 'medium';
return 'hard';
}
/** /**
* The line as `[lat, lng]` pairs Leaflet takes. Zod's tuple reads as "maybe two * The line as `[lat, lng]` pairs Leaflet takes. Zod's tuple reads as "maybe two
* numbers, maybe more" where the client compiles without strict mode, so the * numbers, maybe more" where the client compiles without strict mode, so the
@@ -52,27 +41,10 @@ export function ferrataOsmUrl(ferrata: Pick<Ferrata, 'id'>): string {
return /^way\/\d+$/.test(ferrata.id) ? `${OSM_ORIGIN}${ferrata.id}` : OSM_ORIGIN; return /^way\/\d+$/.test(ferrata.id) ? `${OSM_ORIGIN}${ferrata.id}` : OSM_ORIGIN;
} }
/** Three decimals is about 100 m: small pans land on the same area and do not refetch. */
const STEP = 1000;
const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
/**
* The map area rounded outwards to the request grid, so the box asked for
* always covers the box on screen.
*/
export function roundFerrataBbox(bbox: FerrataBbox): FerrataBbox {
return {
south: clamp(Math.floor(bbox.south * STEP) / STEP, -90, 90),
west: clamp(Math.floor(bbox.west * STEP) / STEP, -180, 180),
north: clamp(Math.ceil(bbox.north * STEP) / STEP, -90, 90),
east: clamp(Math.ceil(bbox.east * STEP) / STEP, -180, 180),
};
}
/** /**
* Whether the server takes this area: a real box no wider than * Whether the server takes this area: a real box no wider than
* FERRATA_BBOX_MAX_SPAN_DEG per side. A wider one is refused with a 400, so * FERRATA_BBOX_MAX_SPAN_DEG per side. Past it the layer asks the user to zoom
* the client asks the user to zoom in instead of sending it. * in instead of loading every tile of the view.
*/ */
export function ferrataBboxFits(bbox: FerrataBbox): boolean { export function ferrataBboxFits(bbox: FerrataBbox): boolean {
// The span check is the server's own, so a box the client sends is never refused as too large. // The span check is the server's own, so a box the client sends is never refused as too large.
@@ -84,34 +56,54 @@ export function ferrataBboxParam(bbox: FerrataBbox): string {
return [bbox.south, bbox.west, bbox.north, bbox.east].map((edge) => edge.toFixed(3)).join(','); return [bbox.south, bbox.west, bbox.north, bbox.east].map((edge) => edge.toFixed(3)).join(',');
} }
interface FerrataFeature { interface FerrataLineFeature {
type: 'Feature'; type: 'Feature';
properties: { id: string; tone: FerrataTone }; properties: { id: string; group: string };
geometry: { type: 'LineString'; coordinates: Array<[number, number]> }; geometry: { type: 'LineString'; coordinates: Array<[number, number]> };
} }
interface FerrataFeatureCollection { interface FerrataLabelFeature {
type: 'FeatureCollection'; type: 'Feature';
features: FerrataFeature[]; properties: { group: string };
geometry: { type: 'Point'; coordinates: [number, number] };
} }
interface FeatureCollection<F> {
type: 'FeatureCollection';
features: F[];
}
const lngLat = ([lat, lng]: [number, number]): [number, number] => [lng, lat];
/** /**
* The ferratas as a GeoJSON FeatureCollection for MapLibre or Mapbox GL. The * The segments as a GeoJSON FeatureCollection for MapLibre or Mapbox GL, one
* contract carries [lat, lng] like the rest of TREK; GeoJSON wants [lng, lat], * line per OSM way. The contract carries [lat, lng] like the rest of TREK;
* and this is the one place the layer flips it. Only the id and the tone ride * GeoJSON wants [lng, lat], and this is the one place the layer flips it. Only
* along: a click looks the ferrata up by id, so names never pass through the * the way and its group ride along: a click looks the ferrata up by id, so
* map's own feature state. * names never pass through the map's own feature state, and a highlight
* filters on the group.
*/ */
export function ferrataFeatureCollection(ferratas: readonly Ferrata[]): FerrataFeatureCollection { export function ferrataFeatureCollection(groups: readonly FerrataGroup[]): FeatureCollection<FerrataLineFeature> {
return { return {
type: 'FeatureCollection', type: 'FeatureCollection',
features: ferratas.map((ferrata) => ({ features: groups.flatMap((group) =>
group.segments.map((segment) => ({
type: 'Feature' as const,
properties: { id: segment.id, group: group.id },
geometry: { type: 'LineString' as const, coordinates: ferrataPositions(segment).map(lngLat) },
}))
),
};
}
/** One point per ferrata, halfway along it, where the GL renderer puts the route icon. */
export function ferrataLabelCollection(groups: readonly FerrataGroup[]): FeatureCollection<FerrataLabelFeature> {
return {
type: 'FeatureCollection',
features: groups.map((group) => ({
type: 'Feature' as const, type: 'Feature' as const,
properties: { id: ferrata.id, tone: ferrataGradeTone(ferrata.grade) }, properties: { group: group.id },
geometry: { geometry: { type: 'Point' as const, coordinates: lngLat(group.midpoint) },
type: 'LineString' as const,
coordinates: ferrataPositions(ferrata).map(([lat, lng]): [number, number] => [lng, lat]),
},
})), })),
}; };
} }
@@ -1,33 +1,52 @@
// FE-FERRATA-PAINT-001 to FE-FERRATA-PAINT-002: the line colours come from the status tokens. // FE-FERRATA-PAINT-001 to FE-FERRATA-PAINT-004: the line colours come from the content, surface and accent tokens.
import { afterEach, describe, expect, it } from 'vitest'; import { afterEach, describe, expect, it } from 'vitest';
import { ferrataPaint } from './ferrataPaint'; import { ferrataPaint, isGreyColour, sameFerrataPaint } from './ferrataPaint';
const root = document.documentElement; const root = document.documentElement;
afterEach(() => { afterEach(() => {
for (const name of ['--success', '--warning', '--danger', '--text-muted', '--bg-primary']) for (const name of ['--text-primary', '--bg-primary', '--accent', '--info', '--success', '--warning', '--danger'])
root.style.removeProperty(name); root.style.removeProperty(name);
}); });
describe('ferrataPaint', () => { describe('ferrataPaint', () => {
it('FE-FERRATA-PAINT-001: resolves each difficulty band and the casing from the tokens on the root', () => { it('FE-FERRATA-PAINT-001: draws the line in the content colour on a surface casing, the highlight in the accent', () => {
root.style.setProperty('--text-primary', 'black');
root.style.setProperty('--bg-primary', 'white');
root.style.setProperty('--accent', 'navy');
expect(ferrataPaint()).toEqual({ line: 'black', casing: 'white', highlight: 'navy' });
});
it('FE-FERRATA-PAINT-002: never takes the grade colours of the tour line', () => {
root.style.setProperty('--success', 'green'); root.style.setProperty('--success', 'green');
root.style.setProperty('--warning', 'orange'); root.style.setProperty('--warning', 'orange');
root.style.setProperty('--danger', 'crimson'); root.style.setProperty('--danger', 'crimson');
root.style.setProperty('--text-muted', 'gray'); const paint = Object.values(ferrataPaint());
root.style.setProperty('--bg-primary', 'white'); expect(paint).not.toContain('green');
expect(paint).not.toContain('orange');
expect(ferrataPaint()).toEqual({ expect(paint).not.toContain('crimson');
tone: { easy: 'green', medium: 'orange', hard: 'crimson', unknown: 'gray' },
casing: 'white',
});
}); });
it('FE-FERRATA-PAINT-002: falls back to currentColor where a token is not defined', () => { it('FE-FERRATA-PAINT-003: falls back to currentColor where a token is not defined, and compares by colour', () => {
expect(ferrataPaint()).toEqual({ const paint = ferrataPaint();
tone: { easy: 'currentColor', medium: 'currentColor', hard: 'currentColor', unknown: 'currentColor' }, expect(paint).toEqual({ line: 'currentColor', casing: 'currentColor', highlight: 'currentColor' });
casing: 'currentColor', expect(sameFerrataPaint(paint, { ...paint })).toBe(true);
}); expect(sameFerrataPaint(paint, { ...paint, highlight: 'red' })).toBe(false);
});
it('FE-FERRATA-PAINT-004: a grey accent, the neutral default, hands the highlight to the info colour', () => {
root.style.setProperty('--text-primary', '#111827');
root.style.setProperty('--accent', '#111827');
root.style.setProperty('--info', '#2563eb');
expect(ferrataPaint().highlight).toBe('#2563eb');
root.style.setProperty('--accent', '#4f46e5');
expect(ferrataPaint().highlight).toBe('#4f46e5');
expect(isGreyColour('#e4e4e7')).toBe(true);
expect(isGreyColour('#fff')).toBe(true);
expect(isGreyColour('rgb(228, 228, 231)')).toBe(true);
expect(isGreyColour('#d97706')).toBe(false);
expect(isGreyColour('navy')).toBe(false);
}); });
}); });
+57 -23
View File
@@ -1,28 +1,61 @@
import type { FerrataTone } from './ferrataModel';
/** /**
* The colours the via ferrata lines are drawn in, per difficulty band. * The colours the via ferrata lines are drawn in.
*
* Ferratas have a look of their own, apart from the green, yellow and red the
* tour line takes by gradient: a dark dashed line in the content colour on a
* light casing in the surface colour, and the accent for the ferrata under the
* pointer or open in the card. The grade is a badge on the card, never a line
* colour.
*
* The default accent is neutral, close to the content colour itself, so a lit
* ferrata in it would only lose its dashes. A grey accent therefore hands the
* highlight to the info colour, and the lit line is drawn solid and wider, on
* a wider casing, so it stands out by its shape as well as its colour.
* *
* Map paint cannot take a `var(--token)`: Leaflet writes the colour into an * Map paint cannot take a `var(--token)`: Leaflet writes the colour into an
* SVG `stroke` attribute and GL paint wants a resolved colour string. So the * SVG `stroke` attribute and GL paint wants a resolved colour string. So the
* status tokens are read off the document when the layer draws (the precedent * tokens are read off the document when the layer draws (the precedent is the
* is the hazard overlay in useHazardLayerGL), which keeps the lines on the * hazard overlay in useHazardLayerGL), which keeps the lines on the user's
* user's scheme without a colour literal of their own. Easy, medium and hard * scheme without a colour literal of their own.
* follow the same success, warning and danger tones the grade badge uses, so
* the line and the card agree.
*/ */
interface FerrataPaint { export interface FerrataPaint {
tone: Record<FerrataTone, string>; /** The dashed line itself. */
line: string;
/** The halo under the line, so it stays readable over a satellite or hillshade tile. */ /** The halo under the line, so it stays readable over a satellite or hillshade tile. */
casing: string; casing: string;
/** The ferrata under the pointer or open in the card. */
highlight: string;
} }
const TONE_TOKENS: Record<FerrataTone, string> = { const TOKENS = {
easy: '--success', line: '--text-primary',
medium: '--warning', casing: '--bg-primary',
hard: '--danger', highlight: '--accent',
unknown: '--text-muted', /** The highlight when the accent is a grey, which would not stand out from the line. */
}; highlightFallback: '--info',
} as const;
/** How far apart the channels of a colour may lie and it still reads as a grey. */
const GREY_SPREAD = 24;
/** The red, green and blue of a hex or `rgb()` colour, or null for anything else (a name, `currentColor`). */
function channels(colour: string): [number, number, number] | null {
const hex = /^#([\da-f]{3}|[\da-f]{6})$/i.exec(colour);
if (hex?.[1]) {
const digits = hex[1].length === 3 ? [...hex[1]].map((d) => d + d) : (hex[1].match(/../g) ?? []);
const [r = 0, g = 0, b = 0] = digits.map((pair) => parseInt(pair, 16));
return [r, g, b];
}
const rgb = /^rgba?\(\s*(\d+)[\s,]+(\d+)[\s,]+(\d+)/i.exec(colour);
if (rgb) return [Number(rgb[1]), Number(rgb[2]), Number(rgb[3])];
return null;
}
/** Whether a resolved colour is a grey (the neutral default accent, light or dark). */
export function isGreyColour(colour: string): boolean {
const rgb = channels(colour);
return !!rgb && Math.max(...rgb) - Math.min(...rgb) <= GREY_SPREAD;
}
/** What a token resolves to on the root, or `currentColor` where nothing defines it (a test DOM). */ /** What a token resolves to on the root, or `currentColor` where nothing defines it (a test DOM). */
function token(name: string, style: CSSStyleDeclaration | null): string { function token(name: string, style: CSSStyleDeclaration | null): string {
@@ -32,13 +65,14 @@ function token(name: string, style: CSSStyleDeclaration | null): string {
export function ferrataPaint(): FerrataPaint { export function ferrataPaint(): FerrataPaint {
const style = typeof document === 'undefined' ? null : getComputedStyle(document.documentElement); const style = typeof document === 'undefined' ? null : getComputedStyle(document.documentElement);
const accent = token(TOKENS.highlight, style);
return { return {
tone: { line: token(TOKENS.line, style),
easy: token(TONE_TOKENS.easy, style), casing: token(TOKENS.casing, style),
medium: token(TONE_TOKENS.medium, style), highlight: isGreyColour(accent) ? token(TOKENS.highlightFallback, style) : accent,
hard: token(TONE_TOKENS.hard, style),
unknown: token(TONE_TOKENS.unknown, style),
},
casing: token('--bg-primary', style),
}; };
} }
export function sameFerrataPaint(a: FerrataPaint, b: FerrataPaint): boolean {
return a.line === b.line && a.casing === b.casing && a.highlight === b.highlight;
}
@@ -0,0 +1,295 @@
/**
* FE-FERRATA-TILES-001 to FE-FERRATA-TILES-012: the per-trip tile store behind
* the via ferrata layer.
*
* Pinned: nothing goes out before the device cache was read, a tile is asked
* for once and never dropped, the view goes before any prefetch and runs
* beside one on its way rather than behind it, answers are
* kept per tile, the view's failures are reported and retried, and a stop
* calls off what is on its way but keeps what is loaded.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Ferrata, FerrataBbox, FerratasResponse } from '@trek/shared';
import type { FerrataTileRow } from '../../repo/ferrataRepo';
import { FerrataTileStore, ferrataRowsOf } from './ferrataTileStore';
const ferrata = (id: number, lat: number, lng: number, lat2 = lat + 0.01, lng2 = lng): Ferrata => ({
id: `way/${id}`,
name: `Ferrata ${id}`,
grade: { raw: '3', level: 3, plus: null },
routeName: null,
geometry: [
[lat, lng],
[lat2, lng2],
],
osmUrl: `https://www.openstreetmap.org/way/${id}`,
});
const reply = (ferratas: Ferrata[], truncated = false): FerratasResponse => ({
ferratas,
truncated,
source: 'openstreetmap',
attribution: '© OpenStreetMap contributors',
});
interface Call {
bbox: FerrataBbox;
signal: AbortSignal;
resolve: (value: FerratasResponse) => void;
reject: (error: unknown) => void;
}
function harness(options: { persist?: boolean } = {}) {
const calls: Call[] = [];
const persisted: FerrataTileRow[][] = [];
const fetch = vi.fn(
(bbox: FerrataBbox, signal: AbortSignal) =>
new Promise<FerratasResponse>((resolve, reject) => calls.push({ bbox, signal, resolve, reject }))
);
const persist = vi.fn((rows: FerrataTileRow[]) => {
persisted.push(rows);
return Promise.resolve();
});
const store = new FerrataTileStore({
fetch,
persist: options.persist === false ? undefined : persist,
now: () => 1_000,
retryDelaysMs: [100, 200],
});
return { store, calls, fetch, persist, persisted };
}
/** Lets a settled fetch's handlers run. */
const flush = async () => {
await Promise.resolve();
await Promise.resolve();
};
const A = { row: 930, col: 236 }; // 46.50 to 46.55, 11.80 to 11.85
const B = { row: 930, col: 237 };
const FAR = { row: 940, col: 236 };
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
describe('FerrataTileStore', () => {
it('FE-FERRATA-TILES-001: asks for nothing before the cache was read, and nothing the cache already had', () => {
const { store, fetch } = harness();
expect(store.beginHydration()).toBe(true);
expect(store.beginHydration()).toBe(false);
store.want([A, B], 'visible');
expect(fetch).not.toHaveBeenCalled();
expect(store.getSnapshot().waiting.size).toBe(2);
const cached = ferrata(1, 46.52, 11.82);
store.hydrate([
{ tile: '930:236', fetchedAt: 1, ferratas: [cached] },
{ tile: '930:237', fetchedAt: 1, ferratas: [] },
]);
expect(fetch).not.toHaveBeenCalled();
const snapshot = store.getSnapshot();
expect(snapshot.hydrated).toBe(true);
expect([...snapshot.loaded].sort()).toEqual(['930:236', '930:237']);
expect(snapshot.waiting.size).toBe(0);
expect(snapshot.ferratas).toEqual([cached]);
});
it('FE-FERRATA-TILES-002: asks for the missing tiles in one box, keeps them, and never asks for them again', async () => {
const { store, calls, fetch } = harness();
store.hydrate([]);
store.want([A, B], 'visible');
expect(fetch).toHaveBeenCalledTimes(1);
expect(calls[0]?.bbox).toEqual({ south: 46.5, west: 11.8, north: 46.55, east: 11.9 });
const found = ferrata(1, 46.52, 11.82);
calls[0]?.resolve(reply([found]));
await flush();
expect(store.getSnapshot().ferratas).toEqual([found]);
expect(store.getSnapshot().loaded.has('930:237')).toBe(true);
store.want([A, B], 'visible');
store.want([A], 'background');
expect(fetch).toHaveBeenCalledTimes(1);
});
it('FE-FERRATA-TILES-003: keeps every ferrata it ever loaded, a later answer adding to them by id', async () => {
const { store, calls } = harness();
store.hydrate([]);
store.want([A], 'visible');
calls[0]?.resolve(reply([ferrata(1, 46.52, 11.82)]));
await flush();
store.want([FAR], 'visible');
// The same way again, as a tile across its end returns it, and one more.
calls[1]?.resolve(reply([ferrata(1, 46.52, 11.82), ferrata(2, 47.01, 11.82)]));
await flush();
expect(store.getSnapshot().ferratas.map((f) => f.id)).toEqual(['way/1', 'way/2']);
});
it('FE-FERRATA-TILES-004: the view goes out beside a prefetch on its way, and prefetches go one at a time', async () => {
const { store, calls } = harness();
store.hydrate([]);
store.want([A], 'background');
store.want([FAR], 'background');
expect(calls).toHaveLength(1);
// The view does not wait for the prefetch already on its way.
store.want([B], 'visible');
expect(calls).toHaveLength(2);
expect(calls[1]?.bbox).toEqual({ south: 46.5, west: 11.85, north: 46.55, east: 11.9 });
calls[1]?.resolve(reply([]));
await flush();
expect(calls).toHaveLength(2);
calls[0]?.resolve(reply([]));
await flush();
expect(calls[2]?.bbox.south).toBe(47);
});
it('FE-FERRATA-TILES-012: runs two of the view requests at once and queues the rest behind them', async () => {
const { store, calls } = harness();
store.hydrate([]);
// Three blocks of the grid apart: three requests.
store.want([A, { row: 930, col: 240 }, { row: 930, col: 244 }], 'visible');
expect(calls).toHaveLength(2);
calls[0]?.resolve(reply([]));
await flush();
expect(calls).toHaveLength(3);
expect(calls[2]?.bbox.west).toBe(12.2);
});
it('FE-FERRATA-TILES-005: a tile a queued prefetch holds moves up with it when the view wants it', async () => {
const { store, calls } = harness();
store.hydrate([]);
store.want([A], 'background');
store.want([FAR], 'background');
store.want([B], 'background');
store.want([B], 'visible');
expect(store.getSnapshot().waiting.has('930:237')).toBe(true);
// Moved up, it takes the view's lane at once instead of waiting behind the other prefetches.
expect(calls).toHaveLength(2);
expect(calls[1]?.bbox.west).toBe(11.85);
});
it('FE-FERRATA-TILES-006: keeps each answer per tile, an empty tile included', async () => {
const { store, calls, persisted } = harness();
store.hydrate([]);
store.want([A, B], 'visible');
const inA = ferrata(1, 46.52, 11.82);
calls[0]?.resolve(reply([inA]));
await flush();
expect(persisted).toEqual([
[
{ tile: '930:236', fetchedAt: 1_000, ferratas: [inA] },
{ tile: '930:237', fetchedAt: 1_000, ferratas: [] },
],
]);
});
it('FE-FERRATA-TILES-007: reports the view failing and asks again after each delay; a 400 reads as zoom in', async () => {
const { store, calls } = harness();
store.hydrate([]);
store.want([A], 'visible');
calls[0]?.reject(new Error('502'));
await flush();
expect(store.getSnapshot().failure).toBe('unavailable');
expect(store.getSnapshot().waiting.size).toBe(0);
await vi.advanceTimersByTimeAsync(100);
expect(calls).toHaveLength(2);
calls[1]?.resolve(reply([]));
await flush();
expect(store.getSnapshot().failure).toBeNull();
store.want([FAR], 'visible');
calls[2]?.reject({ response: { status: 400 } });
await flush();
expect(store.getSnapshot().failure).toBe('refused');
await vi.advanceTimersByTimeAsync(1_000);
expect(calls).toHaveLength(3);
});
it('FE-FERRATA-TILES-008: a failed prefetch says nothing and is asked for again on the next want', async () => {
const { store, calls } = harness();
store.hydrate([]);
store.want([A], 'background');
calls[0]?.reject(new Error('502'));
await flush();
expect(store.getSnapshot().failure).toBeNull();
store.want([A], 'background');
expect(calls).toHaveLength(2);
});
it('FE-FERRATA-TILES-009: a stop aborts what is on its way and drops the queue, and keeps what is loaded', async () => {
const { store, calls } = harness();
store.hydrate([{ tile: '940:236', fetchedAt: 1, ferratas: [ferrata(9, 47.01, 11.82)] }]);
store.want([A], 'visible');
store.want([B], 'background');
expect(calls).toHaveLength(2);
store.stop();
expect(calls.map((call) => call.signal.aborted)).toEqual([true, true]);
calls[0]?.resolve(reply([ferrata(1, 46.52, 11.82)]));
await flush();
expect(calls).toHaveLength(2);
expect(store.getSnapshot().ferratas.map((f) => f.id)).toEqual(['way/9']);
expect(store.getSnapshot().waiting.size).toBe(0);
// Wanted again, the tiles go out again.
store.want([A], 'visible');
expect(calls).toHaveLength(3);
});
it('FE-FERRATA-TILES-010: tells its listeners only of a change, and keeps the list while only the tiles change', async () => {
const { store, calls } = harness({ persist: false });
const listener = vi.fn();
const unsubscribe = store.subscribe(listener);
store.hydrate([{ tile: '940:236', fetchedAt: 1, ferratas: [ferrata(9, 47.01, 11.82)] }]);
const first = store.getSnapshot();
expect(listener).toHaveBeenCalledTimes(1);
store.want([FAR], 'visible');
expect(listener).toHaveBeenCalledTimes(1);
expect(store.getSnapshot()).toBe(first);
store.want([A], 'visible');
calls[0]?.resolve(reply([]));
await flush();
expect(store.getSnapshot()).not.toBe(first);
expect(store.getSnapshot().ferratas).toBe(first.ferratas);
unsubscribe();
});
it('FE-FERRATA-TILES-011: a prefetch on its way that holds a tile of the view reports its failure as the view', async () => {
const { store, calls } = harness();
store.hydrate([]);
store.want([A], 'background');
store.want([A], 'visible');
expect(calls).toHaveLength(1);
expect(store.getSnapshot().waiting.has('930:236')).toBe(true);
calls[0]?.reject(new Error('502'));
await flush();
expect(store.getSnapshot().failure).toBe('unavailable');
expect(store.getSnapshot().waiting.size).toBe(0);
});
});
describe('ferrataRowsOf', () => {
it('gives a ferrata to every tile of the box its own line reaches into', () => {
// From 46.52 to 46.57: across the row line at 46.55.
const across = ferrata(1, 46.52, 11.82, 46.57, 11.82);
const rows = ferrataRowsOf({ minRow: 930, maxRow: 931, minCol: 236, maxCol: 237 }, [across], 5);
expect(rows.map((row) => [row.tile, row.ferratas.length])).toEqual([
['930:236', 1],
['930:237', 0],
['931:236', 1],
['931:237', 0],
]);
expect(rows.every((row) => row.fetchedAt === 5)).toBe(true);
});
});
@@ -0,0 +1,340 @@
import {
type Ferrata,
type FerrataBbox,
type FerratasResponse,
type FerrataTile,
type FerrataTileRange,
ferrataTileKey,
ferrataTileRange,
ferrataTileRequests,
ferrataTilesOf,
} from '@trek/shared';
import type { FerrataTileRow } from '../../repo/ferrataRepo';
import { ferrataPointsBbox } from './ferrataGroups';
import { ferrataPositions } from './ferrataModel';
/**
* The via ferrata layer's tiles for one trip, outside React.
*
* Every ferrata loaded for the trip stays, in a map by OSM id, whatever the
* map does afterwards: a pan or a zoom only asks for the tiles of the shared
* 0.05 degree grid that are not loaded yet, never for ones it has, and never
* drops any. Tiles wanted for the view on screen go first; the ring around it
* and the trip's area load behind them. The two have lanes of their own: the
* view's requests run beside a prefetch on its way rather than behind it,
* VISIBLE_AT_ONCE of them at a time, while prefetches go one at a time, so
* the view is never kept waiting by a prefetch. Answers go to `persist` per tile, and a
* trip's cached tiles come back through `hydrate`, which holds every request
* until the cache has been read, so a cached tile is never asked for again.
*
* Built for `useSyncExternalStore`: `subscribe` and `getSnapshot`, and the
* snapshot is a new object only when something in it changed.
*/
type FerrataTilePriority = 'visible' | 'background';
/**
* Why the last request for the view failed: `refused` (a 400, the server would
* not take the box, read as "zoom in") or `unavailable` (anything else: the
* Overpass mirrors, worth another try).
*/
type FerrataTileFailure = 'refused' | 'unavailable';
export interface FerrataTileSnapshot {
/** Every tile loaded for the trip, cached or fetched. */
loaded: ReadonlySet<string>;
/** Every ferrata those tiles hold, each once. */
ferratas: readonly Ferrata[];
/** Tiles wanted for the view that are queued or on their way. */
waiting: ReadonlySet<string>;
failure: FerrataTileFailure | null;
/** True when an answer for the view was cut at the server's cap. */
truncated: boolean;
/** Whether the cached tiles have been read. */
hydrated: boolean;
}
interface FerrataTileStoreDeps {
fetch: (bbox: FerrataBbox, signal: AbortSignal) => Promise<FerratasResponse>;
persist?: (rows: FerrataTileRow[]) => Promise<void>;
now?: () => number;
/** How long to wait before asking again for the view after the mirrors failed it. */
retryDelaysMs?: readonly number[];
}
interface Job {
bbox: FerrataBbox;
range: FerrataTileRange;
keys: string[];
priority: FerrataTilePriority;
}
const FERRATA_RETRY_DELAYS_MS = [5_000, 15_000, 45_000] as const;
/** Requests for the view on screen that may be on their way at once. */
const VISIBLE_AT_ONCE = 2;
/** Prefetches on their way at once: one, beside the view's own. */
const BACKGROUND_AT_ONCE = 1;
/** A job on its way, and the lane it took: the lane is what it counts against, even once it is moved up. */
interface Flight {
job: Job;
lane: FerrataTilePriority;
controller: AbortController;
}
function failureOf(error: unknown): FerrataTileFailure {
const status = (error as { response?: { status?: unknown } } | null)?.response?.status;
return status === 400 ? 'refused' : 'unavailable';
}
/**
* The ferratas of an answer per tile of the box it was for: each one goes to
* every tile its own box reaches into, and every tile gets a row, an empty one
* included, because "no ferrata here" is an answer worth keeping too.
*/
export function ferrataRowsOf(range: FerrataTileRange, ferratas: readonly Ferrata[], at: number): FerrataTileRow[] {
const rows = new Map(ferrataTilesOf(range).map((tile) => [ferrataTileKey(tile), [] as Ferrata[]]));
for (const ferrata of ferratas) {
const box = ferrataPointsBbox(ferrataPositions(ferrata));
if (!box) continue;
const own = ferrataTileRange(box);
const minRow = Math.max(own.minRow, range.minRow);
const maxRow = Math.min(own.maxRow, range.maxRow);
const minCol = Math.max(own.minCol, range.minCol);
const maxCol = Math.min(own.maxCol, range.maxCol);
if (minRow > maxRow || minCol > maxCol) continue;
for (const tile of ferrataTilesOf({ minRow, maxRow, minCol, maxCol }))
rows.get(ferrataTileKey(tile))?.push(ferrata);
}
return [...rows].map(([tile, list]) => ({ tile, fetchedAt: at, ferratas: list }));
}
export class FerrataTileStore {
private readonly loaded = new Set<string>();
private readonly byId = new Map<string, Ferrata>();
/** Tiles in a queued or in-flight job, so no tile is asked for twice at once. */
private readonly pending = new Set<string>();
private readonly waiting = new Set<string>();
private queue: Job[] = [];
private readonly inFlight = new Set<Flight>();
private failure: FerrataTileFailure | null = null;
private truncated = false;
private hydrated = false;
private hydrating = false;
private retryTimer: ReturnType<typeof setTimeout> | null = null;
private retryAttempt = 0;
private readonly listeners = new Set<() => void>();
private snapshot: FerrataTileSnapshot;
/** Writes to the device cache that failed; the tiles still draw, they only load again next time. */
persistFailures = 0;
constructor(private readonly deps: FerrataTileStoreDeps) {
this.snapshot = this.build();
}
subscribe = (listener: () => void): (() => void) => {
this.listeners.add(listener);
return () => {
this.listeners.delete(listener);
};
};
getSnapshot = (): FerrataTileSnapshot => this.snapshot;
/** Starts reading the cache; requests wait until `hydrate` hands its rows over. */
beginHydration(): boolean {
if (this.hydrated || this.hydrating) return false;
this.hydrating = true;
return true;
}
/** The cached tiles. Queued jobs whose tiles all came back from the cache are dropped. */
hydrate(rows: readonly FerrataTileRow[]): void {
for (const row of rows) {
this.loaded.add(row.tile);
this.waiting.delete(row.tile);
for (const ferrata of row.ferratas) this.byId.set(ferrata.id, ferrata);
}
this.hydrating = false;
this.hydrated = true;
this.queue = this.queue.filter((job) => {
if (!job.keys.every((key) => this.loaded.has(key))) return true;
for (const key of job.keys) this.pending.delete(key);
return false;
});
this.emit();
this.pump();
}
/**
* Asks for the tiles not loaded or on their way yet. `visible` ones jump the
* queue, and a background job already holding one of them is moved up with
* it; the error of an earlier view is cleared, its retry called off.
*/
want(tiles: readonly FerrataTile[], priority: FerrataTilePriority): void {
const keys = tiles.map(ferrataTileKey);
if (priority === 'visible') {
this.clearRetry();
// The ones on their way too, so their failure is reported and retried like the view's own.
const jobs = [...[...this.inFlight].map((flight) => flight.job), ...this.queue];
for (const job of jobs) {
if (job.priority === 'background' && job.keys.some((key) => keys.includes(key))) job.priority = 'visible';
}
for (const key of keys) if (!this.loaded.has(key)) this.waiting.add(key);
}
const missing = tiles.filter((tile, index) => !this.loaded.has(keys[index]) && !this.pending.has(keys[index]));
for (const request of ferrataTileRequests(missing)) {
const jobKeys = ferrataTilesOf(request.range).map(ferrataTileKey);
for (const key of jobKeys) this.pending.add(key);
this.queue.push({ ...request, keys: jobKeys, priority });
}
this.emit();
this.pump();
}
/** Calls off everything queued, in flight or due for a retry; what is loaded stays. */
stop(): void {
this.clearRetry();
for (const flight of this.inFlight) flight.controller.abort();
this.inFlight.clear();
this.queue = [];
this.pending.clear();
this.waiting.clear();
this.emit();
}
private clearRetry(): void {
if (this.retryTimer !== null) clearTimeout(this.retryTimer);
this.retryTimer = null;
}
/** How many jobs of a lane are on their way. */
private flying(lane: FerrataTilePriority): number {
let count = 0;
for (const flight of this.inFlight) if (flight.lane === lane) count += 1;
return count;
}
/** The next queued job of a priority, taken off the queue, or null for none. */
private take(priority: FerrataTilePriority): Job | null {
const index = this.queue.findIndex((job) => job.priority === priority);
if (index < 0) return null;
const [job] = this.queue.splice(index, 1);
return job ?? null;
}
/** Starts what the lanes have room for: the view's jobs in theirs, the prefetches in theirs. */
private pump(): void {
if (!this.hydrated) return;
for (const [lane, room] of [
['visible', VISIBLE_AT_ONCE],
['background', BACKGROUND_AT_ONCE],
] as const) {
while (this.flying(lane) < room) {
const job = this.take(lane);
if (!job) break;
this.launch(job, lane);
}
}
}
private launch(job: Job, lane: FerrataTilePriority): void {
const controller = new AbortController();
const flight: Flight = { job, lane, controller };
this.inFlight.add(flight);
void this.deps.fetch(job.bbox, controller.signal).then(
(reply) => {
if (controller.signal.aborted) return;
this.inFlight.delete(flight);
this.settle(job, reply);
},
(error: unknown) => {
if (controller.signal.aborted) return;
this.inFlight.delete(flight);
this.fail(job, failureOf(error));
}
);
}
private settle(job: Job, reply: FerratasResponse): void {
const at = (this.deps.now ?? Date.now)();
for (const ferrata of reply.ferratas) this.byId.set(ferrata.id, ferrata);
for (const key of job.keys) {
this.loaded.add(key);
this.pending.delete(key);
this.waiting.delete(key);
}
if (job.priority === 'visible') {
this.failure = null;
this.retryAttempt = 0;
this.truncated = reply.truncated;
}
const persist = this.deps.persist;
if (persist) {
void persist(ferrataRowsOf(job.range, reply.ferratas, at)).catch(() => {
this.persistFailures += 1;
});
}
this.emit();
this.pump();
}
private fail(job: Job, failure: FerrataTileFailure): void {
for (const key of job.keys) this.pending.delete(key);
if (job.priority === 'visible') {
// The view's tiles are no longer on their way: the status reads the failure instead.
for (const key of job.keys) this.waiting.delete(key);
this.failure = failure;
const delay = (this.deps.retryDelaysMs ?? FERRATA_RETRY_DELAYS_MS)[this.retryAttempt];
if (failure === 'unavailable' && delay !== undefined) {
this.retryAttempt += 1;
const tiles = ferrataTilesOf(job.range);
this.clearRetry();
this.retryTimer = setTimeout(() => {
this.retryTimer = null;
this.want(tiles, 'visible');
}, delay);
}
}
this.emit();
this.pump();
}
private build(): FerrataTileSnapshot {
return {
loaded: new Set(this.loaded),
ferratas: [...this.byId.values()],
waiting: new Set(this.waiting),
failure: this.failure,
truncated: this.truncated,
hydrated: this.hydrated,
};
}
private emit(): void {
const next = this.build();
const prev = this.snapshot;
// An unchanged list keeps its reference, so what is drawn from it is not rebuilt.
const sameFerratas =
prev.ferratas.length === next.ferratas.length &&
prev.ferratas.every((ferrata, index) => ferrata === next.ferratas[index]);
const same =
sameFerratas &&
prev.failure === next.failure &&
prev.truncated === next.truncated &&
prev.hydrated === next.hydrated &&
sameSet(prev.loaded, next.loaded) &&
sameSet(prev.waiting, next.waiting);
if (same) return;
this.snapshot = sameFerratas ? { ...next, ferratas: prev.ferratas } : next;
for (const listener of [...this.listeners]) listener();
}
}
function sameSet(a: ReadonlySet<string>, b: ReadonlySet<string>): boolean {
if (a.size !== b.size) return false;
for (const value of a) if (!b.has(value)) return false;
return true;
}
@@ -1,12 +1,16 @@
/** /**
* FE-FERRATA-GL-001 to FE-FERRATA-GL-008: the via ferrata layer on a * FE-FERRATA-GL-001 to FE-FERRATA-GL-012: the via ferrata layer on a
* MapLibre or Mapbox GL map. * MapLibre or Mapbox GL map.
* *
* Pinned: the layers go in once per map (and wait for `idle` when tiles are * Pinned: the layers go in once per map (and wait for `idle` when tiles are
* still loading, the failure the hazard overlay had), new data replaces the * still loading, the failure the hazard overlay had), the line is the dark
* source's contents instead of rebuilding the layers, a click on the hit line * dashed content colour and never a grade colour, new data replaces the
* hands the ferrata to `onSelect`, a theme change repaints the installed * sources' contents instead of rebuilding the layers, a click on the hit line
* layers in place, and an unmount leaves nothing behind. * hands the ferrata and the way to `onSelect`, hover and selection highlight
* the whole ferrata (solid, on a wider casing) and name it above the pointer,
* the 3D terrain draws wider lines, the route icon joins once its image is drawn, a theme
* change repaints the installed layers in place, and an unmount leaves nothing
* behind.
*/ */
import { act, render, renderHook, waitFor } from '@testing-library/react'; import { act, render, renderHook, waitFor } from '@testing-library/react';
import type { Map as MapboxMap, MapMouseEvent } from 'mapbox-gl'; import type { Map as MapboxMap, MapMouseEvent } from 'mapbox-gl';
@@ -15,32 +19,54 @@ import { describe, expect, it, vi } from 'vitest';
import type { Ferrata } from '@trek/shared'; import type { Ferrata } from '@trek/shared';
import FerrataLayerGL from './FerrataLayerGL'; import FerrataLayerGL from './FerrataLayerGL';
import { groupFerratas } from './ferrataGroups';
import { useFerrataLayerGL } from './useFerrataLayerGL'; import { useFerrataLayerGL } from './useFerrataLayerGL';
const SOURCE = 'trek-ferratas'; vi.mock('./ferrataIcon', async (importOriginal) => ({
const LAYERS = ['trek-ferratas-casing', 'trek-ferratas-line', 'trek-ferratas-hit']; ...(await importOriginal<typeof import('./ferrataIcon')>()),
loadFerrataIcon: vi.fn(() => Promise.resolve({ width: 44, height: 44 })),
}));
const ferrata = (id: string, level: number | null = 2): Ferrata => ({ const SOURCE = 'trek-ferratas';
const LABEL_SOURCE = 'trek-ferratas-labels';
const CASING = 'trek-ferratas-casing';
const LINE = 'trek-ferratas-line';
const HIGHLIGHT_CASING = 'trek-ferratas-highlight-casing';
const HIGHLIGHT = 'trek-ferratas-highlight';
const HIT = 'trek-ferratas-hit';
const LABEL = 'trek-ferratas-label';
const LAYERS = [CASING, LINE, HIGHLIGHT_CASING, HIGHLIGHT, HIT];
const ferrata = (id: string, lat: number, name: string): Ferrata => ({
id, id,
name: `Ferrata ${id}`, name,
grade: { raw: level === null ? null : String(level), level, plus: null }, grade: { raw: '2', level: 2, plus: null },
routeName: null, routeName: null,
geometry: [ geometry: [
[46.5, 11.7], [lat, 11.7],
[46.51, 11.71], [lat + 0.01, 11.71],
], ],
osmUrl: `https://www.openstreetmap.org/${id}`, osmUrl: `https://www.openstreetmap.org/${id}`,
}); });
/** Two ferratas: way/1 and way/2 are one (same name, touching), way/3 another. */
const GROUPS = groupFerratas([
ferrata('way/1', 46.5, 'Via A'),
ferrata('way/2', 46.51, 'Via A'),
ferrata('way/3', 47, 'Via B'),
]);
type Handler = (event?: unknown) => void; type Handler = (event?: unknown) => void;
function fakeGlMap(styleLoaded = true) { function fakeGlMap(styleLoaded = true) {
const sources = new Map<string, { setData: ReturnType<typeof vi.fn> }>(); const sources = new Map<string, { setData: ReturnType<typeof vi.fn> }>();
const layers = new Set<string>(); const layers = new Set<string>();
const images = new Set<string>();
const handlers = new Map<string, Set<Handler>>(); const handlers = new Map<string, Set<Handler>>();
const once = new Map<string, Set<Handler>>(); const once = new Map<string, Set<Handler>>();
const state = { styleLoaded, hit: null as null | { properties: { id: string } } }; const state = { styleLoaded, hit: null as null | { properties: { id: string; group: string } } };
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
const container = document.createElement('div');
const keyOf = (type: string, layer?: unknown) => (typeof layer === 'string' ? `${type}:${layer}` : type); const keyOf = (type: string, layer?: unknown) => (typeof layer === 'string' ? `${type}:${layer}` : type);
const map = { const map = {
isStyleLoaded: vi.fn(() => state.styleLoaded), isStyleLoaded: vi.fn(() => state.styleLoaded),
@@ -59,8 +85,17 @@ function fakeGlMap(styleLoaded = true) {
layers.delete(id); layers.delete(id);
}), }),
setPaintProperty: vi.fn(), setPaintProperty: vi.fn(),
setFilter: vi.fn(),
hasImage: vi.fn((id: string) => images.has(id)),
addImage: vi.fn((id: string) => {
images.add(id);
}),
removeImage: vi.fn((id: string) => {
images.delete(id);
}),
queryRenderedFeatures: vi.fn(() => (state.hit ? [state.hit] : [])), queryRenderedFeatures: vi.fn(() => (state.hit ? [state.hit] : [])),
getCanvas: vi.fn(() => canvas), getCanvas: vi.fn(() => canvas),
getContainer: vi.fn(() => container),
on: vi.fn((type: string, layerOrFn: unknown, fn?: Handler) => { on: vi.fn((type: string, layerOrFn: unknown, fn?: Handler) => {
const key = keyOf(type, layerOrFn); const key = keyOf(type, layerOrFn);
const set = handlers.get(key) ?? new Set<Handler>(); const set = handlers.get(key) ?? new Set<Handler>();
@@ -85,7 +120,9 @@ function fakeGlMap(styleLoaded = true) {
state, state,
sources, sources,
layers, layers,
images,
canvas, canvas,
container,
emit, emit,
listeners: () => [...handlers.values()].reduce((sum, set) => sum + set.size, 0), listeners: () => [...handlers.values()].reduce((sum, set) => sum + set.size, 0),
emitIdle: () => { emitIdle: () => {
@@ -98,62 +135,82 @@ function fakeGlMap(styleLoaded = true) {
const click = { point: { x: 4, y: 4 }, lngLat: { lng: 11.705, lat: 46.505 } } as unknown as MapMouseEvent; const click = { point: { x: 4, y: 4 }, lngLat: { lng: 11.705, lat: 46.505 } } as unknown as MapMouseEvent;
const addedIds = (gl: ReturnType<typeof fakeGlMap>) => gl.spies.addLayer.mock.calls.map(([layer]) => layer.id);
const spec = (gl: ReturnType<typeof fakeGlMap>, id: string) =>
gl.spies.addLayer.mock.calls.map(([layer]) => layer).find((layer) => layer.id === id) as unknown as {
paint: Record<string, unknown>;
filter?: unknown;
minzoom?: number;
layout?: Record<string, unknown>;
};
const filterOf = (ids: string[]) => ['in', ['get', 'group'], ['literal', ids]];
describe('useFerrataLayerGL', () => { describe('useFerrataLayerGL', () => {
it('FE-FERRATA-GL-001: adds one source and the casing, line and hit layers, in that order', () => { it('FE-FERRATA-GL-001: adds the line and label sources and the casing, line, highlight casing, highlight and hit layers, in that order', () => {
const gl = fakeGlMap(); const gl = fakeGlMap();
renderHook(() => useFerrataLayerGL(gl.map, [ferrata('way/1')], vi.fn())); renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
expect(gl.spies.addSource).toHaveBeenCalledWith(SOURCE, expect.objectContaining({ type: 'geojson' })); expect(gl.spies.addSource).toHaveBeenCalledWith(SOURCE, expect.objectContaining({ type: 'geojson' }));
expect(gl.spies.addLayer.mock.calls.map(([spec]) => spec.id)).toEqual(LAYERS); expect(gl.spies.addSource).toHaveBeenCalledWith(LABEL_SOURCE, expect.objectContaining({ type: 'geojson' }));
expect(addedIds(gl)).toEqual(LAYERS);
}); });
it('FE-FERRATA-GL-002: colours the line by difficulty band through a match on the tone', () => { it('FE-FERRATA-GL-002: draws a dark dashed line in the content colour on a light casing, never by grade', () => {
const root = document.documentElement;
root.style.setProperty('--text-primary', 'black');
root.style.setProperty('--bg-primary', 'white');
const gl = fakeGlMap(); const gl = fakeGlMap();
renderHook(() => useFerrataLayerGL(gl.map, [ferrata('way/1')], vi.fn())); renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
const line = gl.spies.addLayer.mock.calls const line = spec(gl, LINE);
.map(([spec]) => spec) expect(line.paint['line-color']).toBe('black');
.find((spec) => spec.id === LAYERS[1]) as unknown as { expect(line.paint['line-dasharray']).toEqual([2, 1.5]);
paint: { 'line-color': unknown[] }; expect(spec(gl, CASING).paint['line-color']).toBe('white');
}; root.style.removeProperty('--text-primary');
expect(line.paint['line-color'].slice(0, 2)).toEqual(['match', ['get', 'tone']]); root.style.removeProperty('--bg-primary');
}); });
it('FE-FERRATA-GL-003: waits for idle when the style is still loading tiles', () => { it('FE-FERRATA-GL-003: waits for idle when the style is still loading tiles', () => {
const gl = fakeGlMap(false); const gl = fakeGlMap(false);
renderHook(() => useFerrataLayerGL(gl.map, [ferrata('way/1')], vi.fn())); renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
expect(gl.spies.addSource).not.toHaveBeenCalled(); expect(gl.spies.addSource).not.toHaveBeenCalled();
gl.state.styleLoaded = true; gl.state.styleLoaded = true;
gl.emitIdle(); gl.emitIdle();
expect(gl.spies.addLayer).toHaveBeenCalledTimes(3); expect(addedIds(gl)).toEqual(LAYERS);
}); });
it('FE-FERRATA-GL-004: new ferratas replace the source data without rebuilding the layers', () => { it('FE-FERRATA-GL-004: new ferratas replace the sources data without rebuilding the layers', () => {
const gl = fakeGlMap(); const gl = fakeGlMap();
const { rerender } = renderHook(({ list }) => useFerrataLayerGL(gl.map, list, vi.fn()), { const { rerender } = renderHook(({ groups }) => useFerrataLayerGL(gl.map, groups, vi.fn()), {
initialProps: { list: [ferrata('way/1')] }, initialProps: { groups: GROUPS.slice(0, 1) },
}); });
rerender({ list: [ferrata('way/1'), ferrata('way/2', 5)] }); rerender({ groups: GROUPS });
expect(gl.spies.addLayer).toHaveBeenCalledTimes(3); expect(addedIds(gl)).toEqual(LAYERS);
const setData = gl.sources.get(SOURCE)?.setData; expect(gl.sources.get(SOURCE)?.setData).toHaveBeenLastCalledWith(
expect(setData).toHaveBeenLastCalledWith(
expect.objectContaining({ expect.objectContaining({
features: [expect.anything(), expect.objectContaining({ properties: { id: 'way/2', tone: 'hard' } })], features: [
expect.objectContaining({ properties: { id: 'way/1', group: 'group/way/1' } }),
expect.objectContaining({ properties: { id: 'way/2', group: 'group/way/1' } }),
expect.objectContaining({ properties: { id: 'way/3', group: 'group/way/3' } }),
],
}) })
); );
expect(gl.sources.get(LABEL_SOURCE)?.setData).toHaveBeenLastCalledWith(
expect.objectContaining({ features: [expect.anything(), expect.anything()] })
);
}); });
it('FE-FERRATA-GL-005: a click on the hit line hands the ferrata and the point to onSelect', () => { it('FE-FERRATA-GL-005: a click on the hit line hands the ferrata, the way and the point to onSelect', () => {
const gl = fakeGlMap(); const gl = fakeGlMap();
const onSelect = vi.fn(); const onSelect = vi.fn();
const target = ferrata('way/2'); renderHook(() => useFerrataLayerGL(gl.map, GROUPS, onSelect));
renderHook(() => useFerrataLayerGL(gl.map, [ferrata('way/1'), target], onSelect));
gl.state.hit = { properties: { id: 'way/2' } }; gl.state.hit = { properties: { id: 'way/2', group: 'group/way/1' } };
gl.emit('click', click); gl.emit('click', click);
expect(onSelect).toHaveBeenCalledWith(target, { lng: 11.705, lat: 46.505 }); expect(onSelect).toHaveBeenCalledWith(GROUPS[0], GROUPS[0]?.segments[1], { lng: 11.705, lat: 46.505 });
onSelect.mockClear(); onSelect.mockClear();
gl.state.hit = null; gl.state.hit = null;
@@ -161,43 +218,102 @@ describe('useFerrataLayerGL', () => {
expect(onSelect).not.toHaveBeenCalled(); expect(onSelect).not.toHaveBeenCalled();
}); });
it('FE-FERRATA-GL-006: the pointer turns into a hand over the hit line', () => { it('FE-FERRATA-GL-006: the pointer turns into a hand over a line, and the whole ferrata under it lights up', () => {
const gl = fakeGlMap(); const gl = fakeGlMap();
renderHook(() => useFerrataLayerGL(gl.map, [ferrata('way/1')], vi.fn())); renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
expect(spec(gl, HIGHLIGHT).filter).toEqual(filterOf([]));
gl.emit(`mouseenter:${LAYERS[2]}`); gl.emit(`mouseenter:${HIT}`);
expect(gl.canvas.style.cursor).toBe('pointer'); expect(gl.canvas.style.cursor).toBe('pointer');
gl.emit(`mouseleave:${LAYERS[2]}`); gl.state.hit = { properties: { id: 'way/2', group: 'group/way/1' } };
act(() => gl.emit(`mousemove:${HIT}`, click));
expect(gl.spies.setFilter).toHaveBeenLastCalledWith(HIGHLIGHT, filterOf(['group/way/1']));
act(() => gl.emit(`mouseleave:${HIT}`));
expect(gl.canvas.style.cursor).toBe(''); expect(gl.canvas.style.cursor).toBe('');
expect(gl.spies.setFilter).toHaveBeenLastCalledWith(HIGHLIGHT, filterOf([]));
}); });
it('FE-FERRATA-GL-007: unmounting removes the layers, the source and every listener; no map, no work', () => { it('FE-FERRATA-GL-007: the ferrata open in the card stays highlighted', () => {
const gl = fakeGlMap(); const gl = fakeGlMap();
const { unmount } = render(<FerrataLayerGL map={gl.map} ferratas={[ferrata('way/1')]} onSelect={vi.fn()} />); const { rerender } = renderHook(({ selected }) => useFerrataLayerGL(gl.map, GROUPS, vi.fn(), selected), {
initialProps: { selected: null as string | null },
});
rerender({ selected: 'group/way/3' });
expect(gl.spies.setFilter).toHaveBeenLastCalledWith(HIGHLIGHT, filterOf(['group/way/3']));
});
it('FE-FERRATA-GL-008: adds the route icon layer from the zoom it shows at, once its image is drawn', async () => {
const gl = fakeGlMap();
renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
await waitFor(() => expect(gl.layers.has(LABEL)).toBe(true));
expect(gl.images.has('trek-ferrata-icon')).toBe(true);
const label = spec(gl, LABEL);
expect(label.minzoom).toBe(13);
expect(label.layout?.['icon-image']).toBe('trek-ferrata-icon');
});
it('FE-FERRATA-GL-009: unmounting removes the layers, the sources, the icon and every listener; no map, no work', async () => {
const gl = fakeGlMap();
const { unmount } = render(<FerrataLayerGL map={gl.map} groups={GROUPS} onSelect={vi.fn()} />);
await waitFor(() => expect(gl.layers.has(LABEL)).toBe(true));
unmount(); unmount();
expect(gl.layers.size).toBe(0); expect(gl.layers.size).toBe(0);
expect(gl.sources.size).toBe(0); expect(gl.sources.size).toBe(0);
expect(gl.images.size).toBe(0);
expect(gl.listeners()).toBe(0); expect(gl.listeners()).toBe(0);
expect(() => renderHook(() => useFerrataLayerGL(null, [ferrata('way/1')], vi.fn()))).not.toThrow(); expect(() => renderHook(() => useFerrataLayerGL(null, GROUPS, vi.fn()))).not.toThrow();
}); });
it('FE-FERRATA-GL-008: a theme change repaints the installed layers without rebuilding them', async () => { it('FE-FERRATA-GL-010: a theme change repaints the installed layers without rebuilding them', async () => {
const root = document.documentElement; const root = document.documentElement;
root.style.setProperty('--success', 'green'); root.style.setProperty('--text-primary', 'black');
const gl = fakeGlMap(); const gl = fakeGlMap();
renderHook(() => useFerrataLayerGL(gl.map, [ferrata('way/1')], vi.fn())); renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
expect(gl.spies.setPaintProperty).not.toHaveBeenCalled(); expect(gl.spies.setPaintProperty).not.toHaveBeenCalled();
await act(async () => { await act(async () => {
root.style.setProperty('--success', 'lime'); root.style.setProperty('--text-primary', 'ivory');
await Promise.resolve(); await Promise.resolve();
}); });
await waitFor(() => await waitFor(() => expect(gl.spies.setPaintProperty).toHaveBeenCalledWith(LINE, 'line-color', 'ivory'));
expect(gl.spies.setPaintProperty).toHaveBeenCalledWith(LAYERS[1], 'line-color', expect.arrayContaining(['lime'])) expect(addedIds(gl).filter((id) => LAYERS.includes(id))).toEqual(LAYERS);
); root.style.removeProperty('--text-primary');
expect(gl.spies.addLayer).toHaveBeenCalledTimes(3); });
root.style.removeProperty('--success'); it('FE-FERRATA-GL-011: names the ferrata under the pointer with its grade, above the pointer, and hides it on leave', () => {
const gl = fakeGlMap();
const { unmount } = renderHook(() => useFerrataLayerGL(gl.map, GROUPS, vi.fn()));
gl.state.hit = { properties: { id: 'way/2', group: 'group/way/1' } };
act(() => gl.emit(`mousemove:${HIT}`, click));
const label = gl.container.querySelector<HTMLElement>('[data-testid="ferrata-hover-label"]');
expect(label?.hidden).toBe(false);
expect(label?.textContent).toBe('Via AK2');
expect(label?.querySelector('span')?.textContent).toBe('K2');
expect(label?.style.transform).toContain('translate(4px, -8px)');
expect(label).toHaveClass('bg-surface-card', 'text-content', 'border-edge-faint');
act(() => gl.emit(`mouseleave:${HIT}`));
expect(label?.hidden).toBe(true);
unmount();
expect(gl.container.querySelector('[data-testid="ferrata-hover-label"]')).toBeNull();
});
it('FE-FERRATA-GL-012: lights a ferrata solid on a wider casing, and draws wider lines on the 3D terrain', () => {
const flat = fakeGlMap();
renderHook(() => useFerrataLayerGL(flat.map, GROUPS, vi.fn()));
expect(spec(flat, CASING).paint['line-width']).toBe(6);
expect(spec(flat, LINE).paint['line-width']).toBe(2.5);
expect(spec(flat, HIGHLIGHT_CASING).paint['line-width']).toBe(9);
expect(spec(flat, HIGHLIGHT).paint['line-width']).toBe(4);
expect(spec(flat, HIGHLIGHT).paint['line-dasharray']).toBeUndefined();
const terrain = fakeGlMap();
renderHook(() => useFerrataLayerGL(terrain.map, GROUPS, vi.fn(), null, 'terrain'));
expect(spec(terrain, CASING).paint['line-width']).toBe(8);
expect(spec(terrain, LINE).paint['line-width']).toBe(3.5);
}); });
}); });
@@ -1,17 +1,46 @@
import type { ExpressionSpecification, GeoJSONSource, Map, MapMouseEvent } from 'mapbox-gl'; import type { ExpressionSpecification, GeoJSONSource, Map, MapMouseEvent } from 'mapbox-gl';
import { useEffect, useMemo, useRef } from 'react'; import { useEffect, useMemo, useRef, useState } from 'react';
import type { Ferrata } from '@trek/shared'; import type { Ferrata } from '@trek/shared';
import { ferrataFeatureCollection } from './ferrataModel'; import { useTranslation } from '../../i18n';
import { type FerrataGroup, ferrataLabel } from './ferrataGroups';
import { ferrataHoverLabel } from './ferrataHoverLabel';
import { FERRATA_ICON_IMAGE, FERRATA_ICON_MIN_ZOOM, FERRATA_ICON_PIXEL_RATIO, loadFerrataIcon } from './ferrataIcon';
import { ferrataFeatureCollection, ferrataLabelCollection } from './ferrataModel';
import type { FerrataPaint } from './ferrataPaint';
import { useFerrataPaint } from './useFerrataPaint'; import { useFerrataPaint } from './useFerrataPaint';
const SOURCE = 'trek-ferratas'; const SOURCE = 'trek-ferratas';
const LABEL_SOURCE = `${SOURCE}-labels`;
const CASING = `${SOURCE}-casing`; const CASING = `${SOURCE}-casing`;
const LINE = `${SOURCE}-line`; const LINE = `${SOURCE}-line`;
/** The wider casing under a lit ferrata, so it stands out by its shape and not only its colour. */
const HIGHLIGHT_CASING = `${SOURCE}-highlight-casing`;
/** The whole ferrata under the pointer or open in the card, drawn solid over its own line. */
const HIGHLIGHT = `${SOURCE}-highlight`;
/** A wide, invisible line on top, so a thin dashed route is still easy to tap. */ /** A wide, invisible line on top, so a thin dashed route is still easy to tap. */
const HIT = `${SOURCE}-hit`; const HIT = `${SOURCE}-hit`;
const LAYERS = [CASING, LINE, HIT]; const LABEL = `${SOURCE}-label`;
const LAYERS = [CASING, LINE, HIGHLIGHT_CASING, HIGHLIGHT, HIT, LABEL];
const LIT_LAYERS = [HIGHLIGHT_CASING, HIGHLIGHT];
/**
* The line widths, in pixels. On the 3D terrain the map is seen at a slant
* and over a hillshade, so the lines there are drawn wider to stay readable.
*/
interface FerrataWidths {
casing: number;
line: number;
litCasing: number;
lit: number;
}
const FLAT_WIDTHS: FerrataWidths = { casing: 6, line: 2.5, litCasing: 9, lit: 4 };
const TERRAIN_WIDTHS: FerrataWidths = { casing: 8, line: 3.5, litCasing: 11, lit: 5 };
/** Where the layer is drawn: the flat map, or the 3D terrain, which draws its lines wider. */
type FerrataSurface = 'flat' | 'terrain';
/** Where on the map a ferrata was clicked, for a popup or card anchored there. */ /** Where on the map a ferrata was clicked, for a popup or card anchored there. */
interface FerrataClickPoint { interface FerrataClickPoint {
@@ -19,51 +48,142 @@ interface FerrataClickPoint {
lat: number; lat: number;
} }
export type FerrataSelectHandler = (ferrata: Ferrata, at: FerrataClickPoint) => void; /** A click on a line: the ferrata it belongs to, the OSM way under the pointer and where it landed. */
export type FerrataSelectHandler = (group: FerrataGroup, segment: Ferrata, at: FerrataClickPoint) => void;
type Paint = ReturnType<typeof useFerrataPaint>; /** The groups to highlight, as a GL filter on the features' `group`. */
function highlightFilter(ids: readonly string[]): ExpressionSpecification {
return ['in', ['get', 'group'], ['literal', [...ids]]];
}
/** The line colour per difficulty band, as a GL `match` on the feature's `tone`. */ const LINE_LAYOUT = { 'line-cap': 'round', 'line-join': 'round' } as const;
function toneColor(paint: Paint): ExpressionSpecification {
return [ /**
'match', * The two sources and the five line layers, bottom to top: casing, dashed
['get', 'tone'], * line, the lit ferrata's wider casing and its solid line, hit line.
'easy', */
paint.tone.easy, function addFerrataLayers(
'medium', map: Map,
paint.tone.medium, paint: FerrataPaint,
'hard', widths: FerrataWidths,
paint.tone.hard, data: GeoJSON.GeoJSON,
paint.tone.unknown, labels: GeoJSON.GeoJSON
]; ): void {
map.addSource(SOURCE, { type: 'geojson', data });
map.addSource(LABEL_SOURCE, { type: 'geojson', data: labels });
map.addLayer({
id: CASING,
type: 'line',
source: SOURCE,
layout: LINE_LAYOUT,
paint: { 'line-color': paint.casing, 'line-width': widths.casing, 'line-opacity': 0.9 },
});
map.addLayer({
id: LINE,
type: 'line',
source: SOURCE,
layout: LINE_LAYOUT,
paint: { 'line-color': paint.line, 'line-width': widths.line, 'line-dasharray': [2, 1.5] },
});
map.addLayer({
id: HIGHLIGHT_CASING,
type: 'line',
source: SOURCE,
layout: LINE_LAYOUT,
filter: highlightFilter([]),
paint: { 'line-color': paint.casing, 'line-width': widths.litCasing },
});
map.addLayer({
id: HIGHLIGHT,
type: 'line',
source: SOURCE,
layout: LINE_LAYOUT,
filter: highlightFilter([]),
paint: { 'line-color': paint.highlight, 'line-width': widths.lit },
});
map.addLayer({
id: HIT,
type: 'line',
source: SOURCE,
layout: LINE_LAYOUT,
paint: { 'line-color': paint.casing, 'line-width': 16, 'line-opacity': 0 },
});
} }
/** /**
* The via ferrata layer on a MapLibre or Mapbox GL map: one GeoJSON source, * Draws the route icon in `paint` and, the first time, the symbol layer that
* a casing, the dashed line coloured by difficulty band, and a hit line that * shows it from FERRATA_ICON_MIN_ZOOM. Done once the image is decoded; a
* turns a click into `onSelect(ferrata, point)`. * layer torn down meanwhile (`gone`) is left alone.
*/
function addFerrataIcon(map: Map, paint: FerrataPaint, gone: () => boolean): void {
void loadFerrataIcon(paint).then(
(image) => {
if (gone() || !map.getSource(LABEL_SOURCE)) return;
if (map.hasImage(FERRATA_ICON_IMAGE)) map.removeImage(FERRATA_ICON_IMAGE);
map.addImage(FERRATA_ICON_IMAGE, image, { pixelRatio: FERRATA_ICON_PIXEL_RATIO });
if (map.getLayer(LABEL)) return;
map.addLayer({
id: LABEL,
type: 'symbol',
source: LABEL_SOURCE,
minzoom: FERRATA_ICON_MIN_ZOOM,
layout: { 'icon-image': FERRATA_ICON_IMAGE, 'icon-allow-overlap': false, 'icon-ignore-placement': false },
});
},
// Undecodable: the lines simply go without their icons.
() => undefined
);
}
function removeFerrataLayers(map: Map): void {
for (const layer of [...LAYERS].reverse()) if (map.getLayer(layer)) map.removeLayer(layer);
for (const source of [LABEL_SOURCE, SOURCE]) if (map.getSource(source)) map.removeSource(source);
if (map.hasImage(FERRATA_ICON_IMAGE)) map.removeImage(FERRATA_ICON_IMAGE);
}
/**
* The via ferrata layer on a MapLibre or Mapbox GL map: a source of the OSM
* ways and one of a point per ferrata, a light casing, the dark dashed line,
* the highlight of the ferrata under the pointer or `selectedId` (solid and
* wider, on a wider casing), a hit line that turns a click into
* `onSelect(group, way, point)` and shows the name and grade above the
* pointer, and from FERRATA_ICON_MIN_ZOOM a small route icon halfway along
* each ferrata. `surface` 'terrain' draws the lines wider for the 3D map.
* *
* The layers are installed once per map and again after a style swap (a * The layers are installed once per map and again after a style swap (a
* basemap or theme change drops every custom layer); new ferratas only replace * basemap or theme change drops every custom layer); new ferratas only replace
* the source's data, so a refetch after a pan does not rebuild the layers. Like * the sources' data, so tiles loading after a pan do not rebuild the layers.
* the hazard overlay, a draw that finds the style busy loading tiles waits for * Like the hazard overlay, a draw that finds the style busy loading tiles
* `idle` rather than for a `style.load` that may never come. A theme change * waits for `idle` rather than for a `style.load` that may never come. A theme
* repaints the installed layers in place instead of rebuilding them. * change repaints the installed layers in place and redraws the icon.
*/ */
export function useFerrataLayerGL(map: Map | null, ferratas: readonly Ferrata[], onSelect: FerrataSelectHandler) { export function useFerrataLayerGL(
const data = useMemo(() => ferrataFeatureCollection(ferratas), [ferratas]); map: Map | null,
groups: readonly FerrataGroup[],
onSelect: FerrataSelectHandler,
selectedId: string | null = null,
surface: FerrataSurface = 'flat'
) {
const { t } = useTranslation();
const data = useMemo(() => ferrataFeatureCollection(groups), [groups]);
const labels = useMemo(() => ferrataLabelCollection(groups), [groups]);
const paint = useFerrataPaint(); const paint = useFerrataPaint();
const [hovered, setHovered] = useState<string | null>(null);
// The handlers read the newest props without reinstalling the layers on every render. // The handlers read the newest props without reinstalling the layers on every render.
const latest = useRef({ ferratas, data, onSelect, paint }); const latest = useRef({ groups, data, labels, onSelect, paint, t, surface });
// The paint the installed layers were last drawn in, so a mount does not repaint what it just drew. // The paint the installed layers were last drawn in, so a mount does not repaint what it just drew.
const painted = useRef<Paint | null>(null); const painted = useRef<FerrataPaint | null>(null);
// Set by the install effect: redraws the icon in the current paint on a theme change.
const iconRepaint = useRef<(() => void) | null>(null);
useEffect(() => { useEffect(() => {
latest.current = { ferratas, data, onSelect, paint }; latest.current = { groups, data, labels, onSelect, paint, t, surface };
}); });
useEffect(() => { useEffect(() => {
if (!map) return; if (!map) return;
let waiting = false; let waiting = false;
let disposed = false;
const hoverLabel = ferrataHoverLabel(map);
const draw = () => { const draw = () => {
if (map.getSource(SOURCE)) return; if (map.getSource(SOURCE)) return;
if (!map.isStyleLoaded()) { if (!map.isStyleLoaded()) {
@@ -73,80 +193,90 @@ export function useFerrataLayerGL(map: Map | null, ferratas: readonly Ferrata[],
} }
return; return;
} }
const paint = latest.current.paint; const { paint, data, labels } = latest.current;
painted.current = paint; painted.current = paint;
const lineLayout = { 'line-cap': 'round', 'line-join': 'round' } as const; const widths = latest.current.surface === 'terrain' ? TERRAIN_WIDTHS : FLAT_WIDTHS;
map.addSource(SOURCE, { type: 'geojson', data: latest.current.data }); addFerrataLayers(map, paint, widths, data, labels);
map.addLayer({ addFerrataIcon(map, paint, () => disposed);
id: CASING,
type: 'line',
source: SOURCE,
layout: lineLayout,
paint: { 'line-color': paint.casing, 'line-width': 6, 'line-opacity': 0.75 },
});
map.addLayer({
id: LINE,
type: 'line',
source: SOURCE,
layout: lineLayout,
paint: {
'line-color': toneColor(paint),
'line-width': 3,
'line-dasharray': [2, 1.5],
},
});
map.addLayer({
id: HIT,
type: 'line',
source: SOURCE,
layout: lineLayout,
paint: { 'line-color': paint.casing, 'line-width': 16, 'line-opacity': 0 },
});
}; };
const retry = () => { const retry = () => {
waiting = false; waiting = false;
draw(); draw();
}; };
const click = (event: MapMouseEvent) => { const featureAt = (event: MapMouseEvent) => {
if (!map.getLayer(HIT)) return; if (!map.getLayer(HIT)) return null;
const hit = map.queryRenderedFeatures(event.point, { layers: [HIT] })[0]; const hit = map.queryRenderedFeatures(event.point, { layers: [HIT] })[0];
const id: unknown = hit?.properties?.id; return hit?.properties ?? null;
const ferrata = latest.current.ferratas.find((candidate) => candidate.id === id); };
if (ferrata) latest.current.onSelect(ferrata, { lng: event.lngLat.lng, lat: event.lngLat.lat }); const click = (event: MapMouseEvent) => {
const id: unknown = featureAt(event)?.id;
for (const group of latest.current.groups) {
const segment = group.segments.find((candidate) => candidate.id === id);
if (segment) {
latest.current.onSelect(group, segment, { lng: event.lngLat.lng, lat: event.lngLat.lat });
return;
}
}
};
const move = (event: MapMouseEvent) => {
const id: unknown = featureAt(event)?.group;
const group = typeof id === 'string' ? latest.current.groups.find((candidate) => candidate.id === id) : undefined;
setHovered(group ? group.id : null);
if (group) hoverLabel.show(ferrataLabel(group, latest.current.t), event.point);
else hoverLabel.hide();
}; };
const enter = () => { const enter = () => {
map.getCanvas().style.cursor = 'pointer'; map.getCanvas().style.cursor = 'pointer';
}; };
const leave = () => { const leave = () => {
map.getCanvas().style.cursor = ''; map.getCanvas().style.cursor = '';
setHovered(null);
hoverLabel.hide();
}; };
const repaintIcon = () => addFerrataIcon(map, latest.current.paint, () => disposed);
draw(); draw();
map.on('style.load', draw); map.on('style.load', draw);
map.on('click', click); map.on('click', click);
map.on('mousemove', HIT, move);
map.on('mouseenter', HIT, enter); map.on('mouseenter', HIT, enter);
map.on('mouseleave', HIT, leave); map.on('mouseleave', HIT, leave);
iconRepaint.current = repaintIcon;
return () => { return () => {
disposed = true;
iconRepaint.current = null;
map.off('style.load', draw); map.off('style.load', draw);
map.off('idle', retry); map.off('idle', retry);
map.off('click', click); map.off('click', click);
map.off('mousemove', HIT, move);
map.off('mouseenter', HIT, enter); map.off('mouseenter', HIT, enter);
map.off('mouseleave', HIT, leave); map.off('mouseleave', HIT, leave);
for (const layer of [...LAYERS].reverse()) if (map.getLayer(layer)) map.removeLayer(layer); hoverLabel.remove();
if (map.getSource(SOURCE)) map.removeSource(SOURCE); removeFerrataLayers(map);
}; };
}, [map]); }, [map]);
useEffect(() => { useEffect(() => {
const source = map?.getSource(SOURCE) as GeoJSONSource | undefined; (map?.getSource(SOURCE) as GeoJSONSource | undefined)?.setData(data);
source?.setData(data); (map?.getSource(LABEL_SOURCE) as GeoJSONSource | undefined)?.setData(labels);
}, [map, data]); }, [map, data, labels]);
const highlighted = useMemo(
() => [...new Set([hovered, selectedId].filter((id): id is string => !!id))],
[hovered, selectedId]
);
useEffect(() => {
for (const layer of LIT_LAYERS) if (map?.getLayer(layer)) map.setFilter(layer, highlightFilter(highlighted));
}, [map, highlighted]);
useEffect(() => { useEffect(() => {
if (!map || painted.current === paint || !map.getLayer(LINE)) return; if (!map || painted.current === paint || !map.getLayer(LINE)) return;
painted.current = paint; painted.current = paint;
if (map.getLayer(CASING)) map.setPaintProperty(CASING, 'line-color', paint.casing); for (const layer of [CASING, HIGHLIGHT_CASING])
if (map.getLayer(LINE)) map.setPaintProperty(LINE, 'line-color', toneColor(paint)); if (map.getLayer(layer)) map.setPaintProperty(layer, 'line-color', paint.casing);
map.setPaintProperty(LINE, 'line-color', paint.line);
if (map.getLayer(HIGHLIGHT)) map.setPaintProperty(HIGHLIGHT, 'line-color', paint.highlight);
iconRepaint.current?.();
}, [map, paint]); }, [map, paint]);
} }
@@ -173,7 +303,7 @@ export function isFerrataHit<Point>(map: HitTestMap<Point>, point: Point): boole
return !!map.getLayer(HIT) && map.queryRenderedFeatures(point, { layers: [HIT] }).length > 0; return !!map.getLayer(HIT) && map.queryRenderedFeatures(point, { layers: [HIT] }).length > 0;
} }
const NO_FERRATAS: readonly Ferrata[] = []; const NO_GROUPS: readonly FerrataGroup[] = [];
const IGNORE_SELECT: FerrataSelectHandler = () => {}; const IGNORE_SELECT: FerrataSelectHandler = () => {};
/** /**
@@ -182,7 +312,16 @@ const IGNORE_SELECT: FerrataSelectHandler = () => {};
*/ */
export function useFerrataMapLayerGL( export function useFerrataMapLayerGL(
map: Map | null, map: Map | null,
layer: { active: boolean; ferratas: readonly Ferrata[]; onSelect: FerrataSelectHandler } | undefined layer:
| { active: boolean; groups: readonly FerrataGroup[]; onSelect: FerrataSelectHandler; selectedId: string | null }
| undefined,
surface: FerrataSurface = 'flat'
) { ) {
useFerrataLayerGL(layer?.active ? map : null, layer?.ferratas ?? NO_FERRATAS, layer?.onSelect ?? IGNORE_SELECT); useFerrataLayerGL(
layer?.active ? map : null,
layer?.groups ?? NO_GROUPS,
layer?.onSelect ?? IGNORE_SELECT,
layer?.selectedId ?? null,
surface
);
} }
@@ -1,55 +1,55 @@
/** /**
* FE-FERRATA-MAPLAYER-001 to FE-FERRATA-MAPLAYER-005: the via ferrata layer as * FE-FERRATA-MAPLAYER-001 to FE-FERRATA-MAPLAYER-008: the via ferrata layer as
* one map surface holds it: off by default, the area the map reports, the card * one map surface holds it: off by default, the area the map reports, the
* a click opens. * trip's area to prefetch, the ferratas grouped from OSM's segments with their
* count in view, and the card a click opens.
* *
* useTripFerratas is mocked: what it asks the server is its own suite's * useTripFerratas is mocked: what it asks the server is its own suite's
* subject. This one checks what the layer hands it (the area, and whether it * subject. This one checks what the layer hands it (the view, whether it is on
* is on at all) and what it hands the map back. * at all, the trip's area) and what it hands the map back.
*/ */
import { act, renderHook } from '@testing-library/react'; import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Ferrata, FerrataBbox } from '@trek/shared'; import type { Ferrata, FerrataBbox } from '@trek/shared';
import { useFerrataMapLayer } from './useFerrataMapLayer'; import { ferrataAreaOf, useFerrataMapLayer } from './useFerrataMapLayer';
import { useTripFerratas } from './useTripFerratas'; import { useTripFerratas } from './useTripFerratas';
vi.mock('./useTripFerratas', () => ({ useTripFerratas: vi.fn() })); vi.mock('./useTripFerratas', () => ({ useTripFerratas: vi.fn() }));
const BOX: FerrataBbox = { south: 46.5, west: 11.7, north: 46.6, east: 11.9 }; const BOX: FerrataBbox = { south: 46.5, west: 11.7, north: 46.6, east: 11.9 };
const OTHER: FerrataBbox = { south: 46.4, west: 11.6, north: 46.5, east: 11.8 }; const OTHER: FerrataBbox = { south: 47.4, west: 11.6, north: 47.5, east: 11.8 };
const FERRATA: Ferrata = { const segment = (id: number, lat: number): Ferrata => ({
id: 'way/42', id: `way/${id}`,
name: 'Via delle Trincee', name: 'Via delle Trincee',
grade: { raw: '4', level: 4, plus: null }, grade: { raw: String(id), level: id, plus: null },
routeName: null, routeName: null,
geometry: [ geometry: [
[46.52, 11.8], [lat, 11.8],
[46.53, 11.81], [lat + 0.001, 11.8],
], ],
osmUrl: 'https://www.openstreetmap.org/way/42', osmUrl: `https://www.openstreetmap.org/way/${id}`,
}; });
// Two touching segments of one ferrata.
const LOWER = segment(2, 46.52);
const UPPER = segment(4, 46.521);
const lastCall = () => vi.mocked(useTripFerratas).mock.lastCall; const lastCall = () => vi.mocked(useTripFerratas).mock.lastCall;
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
vi.mocked(useTripFerratas).mockReturnValue({ vi.mocked(useTripFerratas).mockReturnValue({ ferratas: [LOWER, UPPER], status: 'ready', truncated: false });
ferratas: [FERRATA],
status: 'ready',
truncated: false,
attribution: null,
});
}); });
describe('useFerrataMapLayer', () => { describe('useFerrataMapLayer', () => {
it('FE-FERRATA-MAPLAYER-001: starts off, so nothing is asked for', () => { it('FE-FERRATA-MAPLAYER-001: starts off, so nothing is asked for', () => {
const { result } = renderHook(() => useFerrataMapLayer(7)); const { result } = renderHook(() => useFerrataMapLayer(7));
expect(result.current.layer.active).toBe(false); expect(result.current.layer.active).toBe(false);
expect(result.current.layer.count).toBe(0);
expect(result.current.selected).toBeNull(); expect(result.current.selected).toBeNull();
expect(lastCall()).toEqual([7, null, false]); expect(lastCall()).toEqual([7, null, false, null]);
}); });
it('FE-FERRATA-MAPLAYER-002: an area reported while off is remembered without a re-render, and used once it is on', () => { it('FE-FERRATA-MAPLAYER-002: an area reported while off is remembered without a re-render, and used once it is on', () => {
@@ -61,33 +61,82 @@ describe('useFerrataMapLayer', () => {
act(() => result.current.layer.onToggle()); act(() => result.current.layer.onToggle());
expect(result.current.layer.active).toBe(true); expect(result.current.layer.active).toBe(true);
expect(lastCall()).toEqual([7, BOX, true]); expect(lastCall()?.slice(0, 3)).toEqual([7, BOX, true]);
}); });
it('FE-FERRATA-MAPLAYER-003: while on, every new area reaches the request', () => { it('FE-FERRATA-MAPLAYER-003: while on, every new area reaches the request', () => {
const { result } = renderHook(() => useFerrataMapLayer(7)); const { result } = renderHook(() => useFerrataMapLayer(7));
act(() => result.current.layer.onToggle()); act(() => result.current.layer.onToggle());
act(() => result.current.onViewportChange(OTHER)); act(() => result.current.onViewportChange(OTHER));
expect(lastCall()).toEqual([7, OTHER, true]); expect(lastCall()?.slice(0, 3)).toEqual([7, OTHER, true]);
}); });
it('FE-FERRATA-MAPLAYER-004: a click opens the card, close shuts it, and the map gets what the request found', () => { it("FE-FERRATA-MAPLAYER-004: hands the trip's area around its lines to the loader", () => {
const lines = [
[[46.5, 11.7]],
[
[46.7, 11.6],
[46.6, 11.9],
],
] as Array<Array<[number, number]>>;
renderHook(() => useFerrataMapLayer(7, lines));
expect(lastCall()?.[3]).toEqual({ south: 46.5, west: 11.6, north: 46.7, east: 11.9 });
expect(ferrataAreaOf([])).toBeNull();
expect(ferrataAreaOf([[[Number.NaN, 1]]])).toBeNull();
});
it('FE-FERRATA-MAPLAYER-005: groups the segments into one ferrata and counts the ferratas in view', () => {
const { result } = renderHook(() => useFerrataMapLayer(7));
act(() => result.current.onViewportChange(BOX));
act(() => result.current.layer.onToggle());
expect(result.current.layer.groups).toHaveLength(1);
expect(result.current.layer.groups[0]?.segments).toEqual([LOWER, UPPER]);
expect(result.current.layer.count).toBe(1);
act(() => result.current.onViewportChange(OTHER));
expect(result.current.layer.count).toBe(0);
// Out of view, still drawn.
expect(result.current.layer.groups).toHaveLength(1);
});
it('FE-FERRATA-MAPLAYER-006: a click opens the whole ferrata in the card, highlighted, and close shuts it', () => {
const { result } = renderHook(() => useFerrataMapLayer(7)); const { result } = renderHook(() => useFerrataMapLayer(7));
act(() => result.current.layer.onToggle()); act(() => result.current.layer.onToggle());
expect(result.current.layer.ferratas).toEqual([FERRATA]); const group = result.current.layer.groups[0];
expect(result.current.layer.status).toBe('ready'); if (!group) throw new Error('no group');
act(() => result.current.layer.onSelect(FERRATA, { lat: 46.52, lng: 11.8 })); act(() => result.current.layer.onSelect(group, UPPER, { lat: 46.521, lng: 11.8 }));
expect(result.current.selected).toBe(FERRATA); expect(result.current.selected?.group.id).toBe(group.id);
expect(result.current.selected?.segment).toBe(UPPER);
expect(result.current.layer.selectedId).toBe(group.id);
act(() => result.current.close()); act(() => result.current.close());
expect(result.current.selected).toBeNull(); expect(result.current.selected).toBeNull();
expect(result.current.layer.selectedId).toBeNull();
}); });
it('FE-FERRATA-MAPLAYER-005: switching the layer off closes the card and stops asking', () => { it('FE-FERRATA-MAPLAYER-007: the card follows its ferrata when more of its segments load', () => {
const { result, rerender } = renderHook(() => useFerrataMapLayer(7));
act(() => result.current.layer.onToggle());
const group = result.current.layer.groups[0];
if (!group) throw new Error('no group');
act(() => result.current.layer.onSelect(group, UPPER, { lat: 46.521, lng: 11.8 }));
// A lower way loads and joins the ferrata; its id is now the group's.
const lowest = segment(1, 46.519);
vi.mocked(useTripFerratas).mockReturnValue({ ferratas: [LOWER, UPPER, lowest], status: 'ready', truncated: false });
rerender();
expect(result.current.selected?.group.segments).toHaveLength(3);
expect(result.current.selected?.segment).toBe(UPPER);
expect(result.current.layer.selectedId).toBe('group/way/1');
});
it('FE-FERRATA-MAPLAYER-008: switching the layer off closes the card and stops asking', () => {
const { result } = renderHook(() => useFerrataMapLayer(7)); const { result } = renderHook(() => useFerrataMapLayer(7));
act(() => result.current.layer.onToggle()); act(() => result.current.layer.onToggle());
act(() => result.current.layer.onSelect(FERRATA, { lat: 46.52, lng: 11.8 })); const group = result.current.layer.groups[0];
if (!group) throw new Error('no group');
act(() => result.current.layer.onSelect(group, LOWER, { lat: 46.52, lng: 11.8 }));
act(() => result.current.layer.onToggle()); act(() => result.current.layer.onToggle());
expect(result.current.layer.active).toBe(false); expect(result.current.layer.active).toBe(false);
@@ -2,62 +2,102 @@ import { useCallback, useMemo, useRef, useState } from 'react';
import type { Ferrata, FerrataBbox } from '@trek/shared'; import type { Ferrata, FerrataBbox } from '@trek/shared';
import {
type FerrataGroup,
ferrataGroupOf,
ferrataGroupsInView,
ferrataPointsBbox,
groupFerratas,
} from './ferrataGroups';
import type { FerrataSelectHandler } from './useFerrataLayerGL'; import type { FerrataSelectHandler } from './useFerrataLayerGL';
import { type FerrataStatus, useTripFerratas } from './useTripFerratas'; import { type FerrataStatus, useTripFerratas } from './useTripFerratas';
/** /**
* The via ferrata layer as a map hands it around: whether it is on, the switch * The via ferrata layer as a map hands it around: whether it is on, the switch
* the layer pill shows, what the status pill says, the lines to draw and what * the layer pill shows with its count badge, the ferratas to draw (grouped
* a click on one of them does. Both flat renderers and the 3D terrain map take * from OSM's segments), the one open in the card, and what a click on a line
* this one object, so the three cannot drift apart on what the layer is. * does. Both flat renderers and the 3D terrain map take this one object, so
* the three cannot drift apart on what the layer is.
*/ */
export interface FerrataMapLayer { export interface FerrataMapLayer {
active: boolean; active: boolean;
onToggle: () => void; onToggle: () => void;
status: FerrataStatus; status: FerrataStatus;
ferratas: readonly Ferrata[]; /** How many ferratas reach into the view on screen. */
count: number;
groups: readonly FerrataGroup[];
/** The ferrata open in the card, drawn highlighted. */
selectedId: string | null;
onSelect: FerrataSelectHandler; onSelect: FerrataSelectHandler;
} }
/** The ferrata open in the card, and the OSM way that was picked on it. */
export interface FerrataSelection {
group: FerrataGroup;
segment: Ferrata;
}
/** The box around every point of every line the stage hands over, or null for none. */
export function ferrataAreaOf(lines: ReadonlyArray<ReadonlyArray<readonly [number, number]>>): FerrataBbox | null {
return ferrataPointsBbox(lines.flat());
}
/** /**
* The via ferrata layer of one map surface: off by default, and nothing is * The via ferrata layer of one map surface: off by default, and nothing is
* asked for while it is off. * read or asked for while it is off.
* *
* `onViewportChange` takes the visible area from whichever map is on screen * `onViewportChange` takes the visible area from whichever map is on screen
* (the flat planner map or the 3D terrain). While the layer is off the area is * (the flat planner map or the 3D terrain). While the layer is off the area is
* only remembered, not put into state, so panning a map with the layer off * only remembered, not put into state, so panning a map with the layer off
* re-renders nothing; switching it on starts from the last area the map * re-renders nothing; switching it on starts from the last area the map
* reported instead of waiting for the next pan. `selected` is the ferrata * reported instead of waiting for the next pan. `areaLines` are the lines the
* whose card is open, closed again when the layer is switched off. * trip's area is made of (tour routes, waypoints, places), prefetched once
* the layer is on. The selection names the OSM way that was clicked, so it
* follows its ferrata when more segments load and join it, and closes when the
* layer is switched off.
*/ */
export function useFerrataMapLayer(tripId: number | null | undefined) { export function useFerrataMapLayer(
tripId: number | null | undefined,
areaLines: ReadonlyArray<ReadonlyArray<readonly [number, number]>> = []
) {
const [active, setActive] = useState(false); const [active, setActive] = useState(false);
const [bbox, setBbox] = useState<FerrataBbox | null>(null); const [bbox, setBbox] = useState<FerrataBbox | null>(null);
const [selected, setSelected] = useState<Ferrata | null>(null); const [selectedSegment, setSelectedSegment] = useState<string | null>(null);
const lastArea = useRef<FerrataBbox | null>(null); const lastArea = useRef<FerrataBbox | null>(null);
const activeRef = useRef(false); const activeRef = useRef(false);
const { ferratas, status } = useTripFerratas(tripId, bbox, active); const area = ferrataAreaOf(areaLines);
const { ferratas, status } = useTripFerratas(tripId, bbox, active, area);
const groups = useMemo(() => groupFerratas(ferratas), [ferratas]);
const count = active ? ferrataGroupsInView(groups, bbox) : 0;
const onViewportChange = useCallback((area: FerrataBbox) => { const onViewportChange = useCallback((next: FerrataBbox) => {
lastArea.current = area; lastArea.current = next;
if (activeRef.current) setBbox(area); if (activeRef.current) setBbox(next);
}, []); }, []);
const onToggle = useCallback(() => { const onToggle = useCallback(() => {
const next = !activeRef.current; const next = !activeRef.current;
activeRef.current = next; activeRef.current = next;
setActive(next); setActive(next);
setSelected(null); setSelectedSegment(null);
if (next) setBbox(lastArea.current); if (next) setBbox(lastArea.current);
}, []); }, []);
const close = useCallback(() => setSelected(null), []); const close = useCallback(() => setSelectedSegment(null), []);
const onSelect = useCallback<FerrataSelectHandler>((ferrata) => setSelected(ferrata), []); const onSelect = useCallback<FerrataSelectHandler>((_group, segment) => setSelectedSegment(segment.id), []);
const selected = useMemo<FerrataSelection | null>(() => {
if (!active) return null;
const group = ferrataGroupOf(groups, selectedSegment);
const segment = group?.segments.find((candidate) => candidate.id === selectedSegment);
return group && segment ? { group, segment } : null;
}, [active, groups, selectedSegment]);
const selectedId = selected?.group.id ?? null;
const layer = useMemo<FerrataMapLayer>( const layer = useMemo<FerrataMapLayer>(
() => ({ active, onToggle, status, ferratas, onSelect }), () => ({ active, onToggle, status, count, groups, selectedId, onSelect }),
[active, onToggle, status, ferratas, onSelect] [active, onToggle, status, count, groups, selectedId, onSelect]
); );
return { layer, onViewportChange, selected: active ? selected : null, close }; return { layer, onViewportChange, selected, close };
} }
@@ -7,33 +7,33 @@ import { useFerrataPaint } from './useFerrataPaint';
const root = document.documentElement; const root = document.documentElement;
afterEach(() => { afterEach(() => {
root.style.removeProperty('--success'); root.style.removeProperty('--text-primary');
root.classList.remove('dark', 'unrelated'); root.classList.remove('dark', 'unrelated');
root.removeAttribute('data-scheme'); root.removeAttribute('data-scheme');
}); });
describe('useFerrataPaint', () => { describe('useFerrataPaint', () => {
it('FE-FERRATA-USEPAINT-001: starts with the colours the tokens resolve to', () => { it('FE-FERRATA-USEPAINT-001: starts with the colours the tokens resolve to', () => {
root.style.setProperty('--success', 'green'); root.style.setProperty('--text-primary', 'black');
const { result } = renderHook(() => useFerrataPaint()); const { result } = renderHook(() => useFerrataPaint());
expect(result.current.tone.easy).toBe('green'); expect(result.current.line).toBe('black');
}); });
it('FE-FERRATA-USEPAINT-002: resolves again when the scheme or a token on the root changes', async () => { it('FE-FERRATA-USEPAINT-002: resolves again when the scheme or a token on the root changes', async () => {
root.style.setProperty('--success', 'green'); root.style.setProperty('--text-primary', 'black');
const { result } = renderHook(() => useFerrataPaint()); const { result } = renderHook(() => useFerrataPaint());
await act(async () => { await act(async () => {
root.setAttribute('data-scheme', 'forest'); root.setAttribute('data-scheme', 'forest');
root.style.setProperty('--success', 'teal'); root.style.setProperty('--text-primary', 'ivory');
await Promise.resolve(); await Promise.resolve();
}); });
await waitFor(() => expect(result.current.tone.easy).toBe('teal')); await waitFor(() => expect(result.current.line).toBe('ivory'));
}); });
it('FE-FERRATA-USEPAINT-003: keeps the same reference when a change leaves every colour as it was', async () => { it('FE-FERRATA-USEPAINT-003: keeps the same reference when a change leaves every colour as it was', async () => {
root.style.setProperty('--success', 'green'); root.style.setProperty('--text-primary', 'black');
const { result } = renderHook(() => useFerrataPaint()); const { result } = renderHook(() => useFerrataPaint());
const first = result.current; const first = result.current;
@@ -1,38 +1,26 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { ferrataPaint } from './ferrataPaint'; import { type FerrataPaint, ferrataPaint, sameFerrataPaint } from './ferrataPaint';
/** The attributes `applyAppearance()` writes on `<html>` that change what the status tokens resolve to. */ /** The attributes `applyAppearance()` writes on `<html>` that change what the tokens resolve to. */
const THEME_ATTRIBUTES = ['class', 'style', 'data-scheme']; const THEME_ATTRIBUTES = ['class', 'style', 'data-scheme'];
type Paint = ReturnType<typeof ferrataPaint>;
function samePaint(a: Paint, b: Paint): boolean {
return (
a.casing === b.casing &&
a.tone.easy === b.tone.easy &&
a.tone.medium === b.tone.medium &&
a.tone.hard === b.tone.hard &&
a.tone.unknown === b.tone.unknown
);
}
/** /**
* The line colours, resolved again whenever the light/dark class, the colour * The line colours, resolved again whenever the light/dark class, the colour
* scheme or a custom accent on `<html>` changes. Map paint holds resolved * scheme or a custom accent on `<html>` changes. Map paint holds resolved
* strings rather than `var(--token)`, so without this a theme toggle would leave * strings rather than `var(--token)`, so without this a theme toggle would leave
* the lines in the old scheme's colours until the next refetch. Same observer * the lines in the old scheme's colours. Same observer approach as `useIsDark`,
* approach as `useIsDark`, which only watches the class. The reference only * which only watches the class. The reference only changes when a colour does,
* changes when a colour does, so a memo keyed on it does not churn. * so a memo keyed on it does not churn.
*/ */
export function useFerrataPaint(): Paint { export function useFerrataPaint(): FerrataPaint {
const [paint, setPaint] = useState(ferrataPaint); const [paint, setPaint] = useState(ferrataPaint);
useEffect(() => { useEffect(() => {
if (typeof document === 'undefined') return; if (typeof document === 'undefined') return;
const observer = new MutationObserver(() => { const observer = new MutationObserver(() => {
const next = ferrataPaint(); const next = ferrataPaint();
setPaint((current) => (samePaint(current, next) ? current : next)); setPaint((current) => (sameFerrataPaint(current, next) ? current : next));
}); });
observer.observe(document.documentElement, { attributes: true, attributeFilter: THEME_ATTRIBUTES }); observer.observe(document.documentElement, { attributes: true, attributeFilter: THEME_ATTRIBUTES });
return () => observer.disconnect(); return () => observer.disconnect();
@@ -0,0 +1,34 @@
// FE-FERRATA-GUARD-001: the press that closed a ferrata's card is kept from also acting on the map.
import { act, fireEvent, renderHook } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';
import { useOutsidePressGuard } from './useOutsidePressGuard';
describe('useOutsidePressGuard', () => {
it('FE-FERRATA-GUARD-001: the guard drops the one map click after the closing press and is off once that click went by', () => {
vi.useFakeTimers();
try {
const handler = vi.fn();
const { result } = renderHook(() => useOutsidePressGuard());
const guarded = result.current.guard(handler);
expect(result.current.guard(undefined)).toBeUndefined();
act(() => result.current.arm());
guarded?.('first');
expect(handler).not.toHaveBeenCalled();
guarded?.('second');
expect(handler).toHaveBeenCalledWith('second');
// Armed, but the click lands somewhere that is not the map: the guard comes off right after it.
act(() => result.current.arm());
fireEvent.click(document.body);
act(() => {
vi.runAllTimers();
});
guarded?.('third');
expect(handler).toHaveBeenCalledWith('third');
} finally {
vi.useRealTimers();
}
});
});
@@ -0,0 +1,61 @@
import { useCallback, useEffect, useRef } from 'react';
/**
* Keeps the press that closed the card from also acting on the map.
*
* `arm` goes to the overlay's `onOutsidePress`; `guard` wraps the map's own
* click handlers (adding a waypoint, say). The click that ends the closing
* press is dropped once, and the guard is off again as soon as that click has
* gone by, or at the next press when the first one never became a click (a
* pan that ended off the page). A press on another ferrata keeps working: its
* click opens that ferrata's card, which never runs a guarded handler.
*/
export function useOutsidePressGuard() {
const armed = useRef(false);
const disarm = useRef<(() => void) | null>(null);
useEffect(() => () => disarm.current?.(), []);
const arm = useCallback(() => {
disarm.current?.();
armed.current = true;
let timer: number | undefined;
const off = () => {
if (timer !== undefined) window.clearTimeout(timer);
document.removeEventListener('click', afterClick, true);
document.removeEventListener('pointerdown', nextPress, true);
disarm.current = null;
};
// The map's handler runs during this click's dispatch; the guard comes off right after it.
const afterClick = () => {
document.removeEventListener('click', afterClick, true);
timer = window.setTimeout(() => {
armed.current = false;
off();
}, 0);
};
const nextPress = () => {
armed.current = false;
off();
};
document.addEventListener('click', afterClick, true);
// Listened for from the next task on, so the press being dispatched now does not count.
window.setTimeout(() => {
if (armed.current && disarm.current === off) document.addEventListener('pointerdown', nextPress, true);
}, 0);
disarm.current = off;
}, []);
const guard = useCallback(<A extends unknown[]>(handler: ((...args: A) => void) | undefined) => {
if (!handler) return undefined;
return (...args: A) => {
if (armed.current) {
armed.current = false;
return;
}
handler(...args);
};
}, []);
return { arm, guard };
}
@@ -1,11 +1,12 @@
/** /**
* FE-FERRATA-HOOK-001 to FE-FERRATA-HOOK-010: when the via ferrata layer asks * FE-FERRATA-HOOK-001 to FE-FERRATA-HOOK-010: when the via ferrata layer asks
* the server, and what it reports while it does. * the server, what it keeps, and what it reports while it does.
* *
* The request goes to a public Overpass mirror behind the server, so the hook * The requests reach a public Overpass mirror behind the server, so the hook
* has to be frugal: nothing while the layer is off, one request per settled * has to be frugal: nothing while the layer is off, a trip's cached tiles
* map area, the old one aborted when the area moves on, and none at all for an * first, then only the tiles of the shared grid that are not loaded yet, never
* area the server would refuse or a device that is offline. * one twice, and none at all for an area the server would refuse or a device
* that is offline.
*/ */
import { act, renderHook } from '@testing-library/react'; import { act, renderHook } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -14,13 +15,21 @@ import type { Ferrata, FerrataBbox, FerratasResponse } from '@trek/shared';
import { useNetworkMode } from '../../hooks/useNetworkMode'; import { useNetworkMode } from '../../hooks/useNetworkMode';
import { ferrataRepo } from '../../repo/ferrataRepo'; import { ferrataRepo } from '../../repo/ferrataRepo';
import { useTripFerratas } from './useTripFerratas'; import { FERRATA_AREA_PREFETCH_MAX_TILES, useTripFerratas } from './useTripFerratas';
vi.mock('../../repo/ferrataRepo', () => ({ ferrataRepo: { list: vi.fn() } })); vi.mock('../../repo/ferrataRepo', () => ({
ferrataRepo: { list: vi.fn(), cachedTiles: vi.fn(), saveTiles: vi.fn(), cacheName: vi.fn(() => 'trek-offline') },
}));
vi.mock('../../hooks/useNetworkMode', () => ({ useNetworkMode: vi.fn() })); vi.mock('../../hooks/useNetworkMode', () => ({ useNetworkMode: vi.fn() }));
const BOX: FerrataBbox = { south: 46.5, west: 11.7, north: 46.6, east: 11.9 }; /** Exactly the tile 930:236. */
const OTHER: FerrataBbox = { south: 46.4, west: 11.6, north: 46.5, east: 11.8 }; const TILE: FerrataBbox = { south: 46.5, west: 11.8, north: 46.55, east: 11.85 };
const TILE_PARAM = '46.500,11.800,46.550,11.850';
/** The first block of the ring around it, south west of it: the ring goes out a 0.1 degree block at a time. */
const RING_FIRST_PARAM = '46.450,11.750,46.500,11.800';
/** A view inside the same tile, after a small pan. */
const INSIDE: FerrataBbox = { south: 46.51, west: 11.81, north: 46.54, east: 11.84 };
const WIDE: FerrataBbox = { south: 46, west: 11, north: 47, east: 12 };
const FERRATA: Ferrata = { const FERRATA: Ferrata = {
id: 'way/42', id: 'way/42',
@@ -28,15 +37,15 @@ const FERRATA: Ferrata = {
grade: { raw: '4', level: 4, plus: null }, grade: { raw: '4', level: 4, plus: null },
routeName: null, routeName: null,
geometry: [ geometry: [
[46.52, 11.8], [46.52, 11.82],
[46.53, 11.81], [46.53, 11.82],
], ],
osmUrl: 'https://www.openstreetmap.org/way/42', osmUrl: 'https://www.openstreetmap.org/way/42',
}; };
const reply = (ferratas: Ferrata[] = [FERRATA], truncated = false): FerratasResponse => ({ const reply = (ferratas: Ferrata[] = [FERRATA]): FerratasResponse => ({
ferratas, ferratas,
truncated, truncated: false,
source: 'openstreetmap', source: 'openstreetmap',
attribution: '© OpenStreetMap contributors', attribution: '© OpenStreetMap contributors',
}); });
@@ -44,152 +53,162 @@ const reply = (ferratas: Ferrata[] = [FERRATA], truncated = false): FerratasResp
const network = (offline: boolean) => const network = (offline: boolean) =>
vi.mocked(useNetworkMode).mockReturnValue({ offline, forced: false, setForced: vi.fn() }); vi.mocked(useNetworkMode).mockReturnValue({ offline, forced: false, setForced: vi.fn() });
const params = () => vi.mocked(ferrataRepo.list).mock.calls.map(([, bbox]) => bbox);
beforeEach(() => { beforeEach(() => {
vi.useFakeTimers(); vi.useFakeTimers();
vi.clearAllMocks(); vi.clearAllMocks();
network(false); network(false);
vi.mocked(ferrataRepo.list).mockResolvedValue(reply()); vi.mocked(ferrataRepo.list).mockResolvedValue(reply());
vi.mocked(ferrataRepo.cachedTiles).mockResolvedValue([]);
vi.mocked(ferrataRepo.saveTiles).mockResolvedValue(undefined);
}); });
afterEach(() => { afterEach(() => {
vi.useRealTimers(); vi.useRealTimers();
}); });
/** Lets the debounce run out and the repo's promise settle. */ /** Lets the cache read and settled requests run their handlers. */
async function settle() { async function drain() {
await act(async () => { await act(async () => {
await vi.advanceTimersByTimeAsync(400); // Enough turns for the prefetches, which go out one after another, to settle too.
for (let i = 0; i < 40; i += 1) await Promise.resolve();
}); });
} }
/** Lets the debounce run out, then everything it started settle. */
async function settle(ms = 200) {
await act(async () => {
await vi.advanceTimersByTimeAsync(ms);
});
await drain();
}
describe('useTripFerratas', () => { describe('useTripFerratas', () => {
it('FE-FERRATA-HOOK-001: asks for nothing while the layer is off', async () => { it('FE-FERRATA-HOOK-001: reads and asks for nothing while the layer is off', async () => {
const { result } = renderHook(() => useTripFerratas(7, BOX, false)); const { result } = renderHook(() => useTripFerratas(7, TILE, false));
await settle(); await settle(1_000);
expect(ferrataRepo.cachedTiles).not.toHaveBeenCalled();
expect(ferrataRepo.list).not.toHaveBeenCalled(); expect(ferrataRepo.list).not.toHaveBeenCalled();
expect(result.current).toEqual({ ferratas: [], status: 'idle', truncated: false, attribution: null }); expect(result.current.status).toBe('idle');
}); });
it('FE-FERRATA-HOOK-002: waits for the area to settle, then asks once for the rounded box', async () => { it('FE-FERRATA-HOOK-002: switched on, asks for the missing tiles of the view after the debounce, then the ring', async () => {
const { result } = renderHook(() => useTripFerratas(7, BOX, true)); const { result } = renderHook(() => useTripFerratas(7, TILE, true));
await drain();
expect(ferrataRepo.cachedTiles).toHaveBeenCalledWith(7);
expect(ferrataRepo.list).not.toHaveBeenCalled();
expect(result.current.status).toBe('loading'); expect(result.current.status).toBe('loading');
expect(ferrataRepo.list).not.toHaveBeenCalled();
await settle(); await settle();
expect(params()[0]).toBe(TILE_PARAM);
expect(ferrataRepo.list).toHaveBeenCalledTimes(1); expect(params().slice(1)).toContain(RING_FIRST_PARAM);
expect(ferrataRepo.list).toHaveBeenCalledWith(7, '46.500,11.700,46.600,11.900', expect.any(AbortSignal)); expect(vi.mocked(ferrataRepo.list).mock.calls[0]?.[0]).toBe(7);
expect(result.current).toEqual({
ferratas: [FERRATA],
status: 'ready',
truncated: false,
attribution: '© OpenStreetMap contributors',
});
});
it('FE-FERRATA-HOOK-003: a pan before the debounce runs out sends only the last area', async () => {
const { rerender } = renderHook(({ box }) => useTripFerratas(7, box, true), { initialProps: { box: BOX } });
await act(async () => {
await vi.advanceTimersByTimeAsync(200);
});
rerender({ box: OTHER });
await settle();
expect(ferrataRepo.list).toHaveBeenCalledTimes(1);
expect(vi.mocked(ferrataRepo.list).mock.calls[0]?.[1]).toBe('46.400,11.600,46.500,11.800');
});
it('FE-FERRATA-HOOK-004: a new area aborts the request in flight and keeps the old lines while it loads', async () => {
let first: AbortSignal | undefined;
vi.mocked(ferrataRepo.list).mockImplementationOnce((_trip, _bbox, signal) => {
first = signal;
return Promise.resolve(reply());
});
const { result, rerender } = renderHook(({ box }) => useTripFerratas(7, box, true), {
initialProps: { box: BOX },
});
await settle();
expect(result.current.status).toBe('ready'); expect(result.current.status).toBe('ready');
expect(result.current.ferratas).toEqual([FERRATA]);
expect(ferrataRepo.saveTiles).toHaveBeenCalledWith(7, expect.any(Array), 'trek-offline');
});
vi.mocked(ferrataRepo.list).mockReturnValueOnce(new Promise(() => {})); it('FE-FERRATA-HOOK-003: draws the cached tiles at once and does not ask for them again', async () => {
rerender({ box: OTHER }); vi.mocked(ferrataRepo.cachedTiles).mockResolvedValue([{ tile: '930:236', fetchedAt: 1, ferratas: [FERRATA] }]);
expect(first?.aborted).toBe(true); const { result } = renderHook(() => useTripFerratas(7, TILE, true));
expect(result.current.status).toBe('loading'); await drain();
expect(result.current.status).toBe('ready');
expect(result.current.ferratas).toEqual([FERRATA]);
await settle();
expect(params()).not.toContain(TILE_PARAM);
});
it('FE-FERRATA-HOOK-004: a pan inside loaded tiles asks for nothing, and keeps every line drawn', async () => {
const { result, rerender } = renderHook(({ view }) => useTripFerratas(7, view, true), {
initialProps: { view: TILE },
});
await settle();
const asked = params().length;
rerender({ view: INSIDE });
await settle();
expect(params()).toHaveLength(asked);
expect(result.current.ferratas).toEqual([FERRATA]);
expect(result.current.status).toBe('ready');
});
it('FE-FERRATA-HOOK-005: a fast pan ends in one request for where the map stopped', async () => {
const { rerender } = renderHook(({ view }) => useTripFerratas(7, view, true), {
initialProps: { view: { south: 47, west: 11.8, north: 47.05, east: 11.85 } },
});
await act(async () => {
await vi.advanceTimersByTimeAsync(100);
});
rerender({ view: TILE });
await settle();
expect(params()[0]).toBe(TILE_PARAM);
});
it('FE-FERRATA-HOOK-006: offline it asks for nothing and still draws what the device kept', async () => {
network(true);
vi.mocked(ferrataRepo.cachedTiles).mockResolvedValue([{ tile: '930:236', fetchedAt: 1, ferratas: [FERRATA] }]);
const { result } = renderHook(() => useTripFerratas(7, TILE, true));
await settle(1_000);
expect(ferrataRepo.list).not.toHaveBeenCalled();
expect(result.current.status).toBe('offline');
expect(result.current.ferratas).toEqual([FERRATA]); expect(result.current.ferratas).toEqual([FERRATA]);
}); });
it('FE-FERRATA-HOOK-005: a failed request reports an error and draws nothing', async () => { it('FE-FERRATA-HOOK-007: a view wider than the server takes reads as zoom in and asks for nothing', async () => {
vi.mocked(ferrataRepo.list).mockRejectedValue(Object.assign(new Error('502'), { response: { status: 502 } })); const { result } = renderHook(() => useTripFerratas(7, WIDE, true));
const { result } = renderHook(() => useTripFerratas(7, BOX, true)); await settle(1_000);
await settle();
expect(result.current.status).toBe('error');
expect(result.current.ferratas).toEqual([]);
});
it('FE-FERRATA-HOOK-006: an area wider than the server takes asks the user to zoom in, without a request', async () => {
const { result } = renderHook(() => useTripFerratas(7, { south: 46, west: 11, north: 47, east: 12 }, true));
await settle();
expect(ferrataRepo.list).not.toHaveBeenCalled();
expect(result.current.status).toBe('tooLarge'); expect(result.current.status).toBe('tooLarge');
expect(ferrataRepo.list).not.toHaveBeenCalled();
}); });
it('FE-FERRATA-HOOK-007: offline it says so and sends nothing', async () => { it('FE-FERRATA-HOOK-008: reports the mirrors failing as an error and a refused box as zoom in', async () => {
network(true); vi.mocked(ferrataRepo.list).mockRejectedValue(new Error('502'));
const { result } = renderHook(() => useTripFerratas(7, BOX, true)); const { result, rerender } = renderHook(({ view }) => useTripFerratas(7, view, true), {
initialProps: { view: TILE },
});
await settle(); await settle();
expect(result.current.status).toBe('error');
expect(result.current.status).toBe('offline'); vi.mocked(ferrataRepo.list).mockRejectedValue({ response: { status: 400 } });
expect(ferrataRepo.list).not.toHaveBeenCalled(); rerender({ view: { south: 47, west: 11.8, north: 47.05, east: 11.85 } });
await settle();
expect(result.current.status).toBe('tooLarge');
}); });
it('FE-FERRATA-HOOK-008: switching the layer off aborts and clears, and a truncated answer is passed on', async () => { it('FE-FERRATA-HOOK-009: switched off and on again asks for no tile it already has', async () => {
vi.mocked(ferrataRepo.list).mockResolvedValue(reply([FERRATA], true)); const { result, rerender } = renderHook(({ on }) => useTripFerratas(7, TILE, on), {
const { result, rerender } = renderHook(({ on }) => useTripFerratas(7, BOX, on), { initialProps: { on: true } }); initialProps: { on: true },
});
await settle(); await settle();
expect(result.current.truncated).toBe(true); const asked = params().length;
rerender({ on: false }); rerender({ on: false });
expect(result.current).toEqual({ ferratas: [], status: 'idle', truncated: false, attribution: null }); expect(result.current.status).toBe('idle');
expect(renderHook(() => useTripFerratas(undefined, BOX, true)).result.current.status).toBe('idle'); rerender({ on: true });
expect(renderHook(() => useTripFerratas(7, null, true)).result.current.status).toBe('idle');
});
it('FE-FERRATA-HOOK-009: a 400 reads as "zoom in" and is not asked again', async () => {
vi.mocked(ferrataRepo.list).mockRejectedValue(Object.assign(new Error('400'), { response: { status: 400 } }));
const { result } = renderHook(() => useTripFerratas(7, BOX, true));
await settle(); await settle();
expect(result.current.status).toBe('tooLarge'); expect(params()).toHaveLength(asked);
expect(ferrataRepo.cachedTiles).toHaveBeenCalledTimes(1);
await act(async () => {
await vi.advanceTimersByTimeAsync(60_000);
});
expect(ferrataRepo.list).toHaveBeenCalledTimes(1);
});
it('FE-FERRATA-HOOK-010: an outage asks again for the same area after a backoff and recovers', async () => {
vi.mocked(ferrataRepo.list)
.mockRejectedValueOnce(Object.assign(new Error('502'), { response: { status: 502 } }))
.mockResolvedValueOnce(reply());
const { result } = renderHook(() => useTripFerratas(7, BOX, true));
await settle();
expect(result.current.status).toBe('error');
await act(async () => {
await vi.advanceTimersByTimeAsync(4_000);
});
expect(ferrataRepo.list).toHaveBeenCalledTimes(1);
expect(result.current.status).toBe('error');
await act(async () => {
await vi.advanceTimersByTimeAsync(1_000);
});
await settle();
expect(ferrataRepo.list).toHaveBeenCalledTimes(2);
expect(vi.mocked(ferrataRepo.list).mock.calls[1]?.[1]).toBe('46.500,11.700,46.600,11.900');
expect(result.current.status).toBe('ready');
expect(result.current.ferratas).toEqual([FERRATA]); expect(result.current.ferratas).toEqual([FERRATA]);
expect(result.current.status).toBe('ready');
});
it("FE-FERRATA-HOOK-010: prefetches the trip's area in the background, but not one wider than the cap", async () => {
const area: FerrataBbox = { south: 46.0, west: 11.0, north: 46.1, east: 11.1 };
const first = renderHook(() => useTripFerratas(7, TILE, true, area));
await settle();
expect(params()).toContain('46.000,11.000,46.100,11.100');
first.unmount();
vi.mocked(ferrataRepo.list).mockClear();
// 2 by 2 degrees is 1600 tiles, past FERRATA_AREA_PREFETCH_MAX_TILES.
expect(FERRATA_AREA_PREFETCH_MAX_TILES).toBeLessThan(1600);
renderHook(() => useTripFerratas(8, null, true, { south: 44, west: 10, north: 46, east: 12 }));
await settle(1_000);
expect(ferrataRepo.list).not.toHaveBeenCalled();
}); });
}); });
+129 -103
View File
@@ -1,134 +1,160 @@
import { useEffect, useState } from 'react'; import { useEffect, useMemo, useSyncExternalStore } from 'react';
import type { Ferrata, FerrataBbox, FerratasResponse } from '@trek/shared'; import {
ferrataTileCount,
ferrataTileKey,
ferrataTileRange,
ferrataTilesOf,
growFerrataTileRange,
type Ferrata,
type FerrataBbox,
type FerrataTileRange,
} from '@trek/shared';
import { useNetworkMode } from '../../hooks/useNetworkMode'; import { useNetworkMode } from '../../hooks/useNetworkMode';
import { ferrataRepo } from '../../repo/ferrataRepo'; import { ferrataRepo } from '../../repo/ferrataRepo';
import { ferrataBboxFits, ferrataBboxParam, roundFerrataBbox } from './ferrataModel'; import { ferrataBboxFits, ferrataBboxParam } from './ferrataModel';
import { FerrataTileStore, type FerrataTileSnapshot } from './ferrataTileStore';
/** /**
* Where the layer stands. `tooLarge` and `offline` are answers to "why are * Where the layer stands for the view on screen. `tooLarge` and `offline` are
* there no lines" that need no request at all: the server refuses a box wider * answers to "why are there no new lines" that need no request at all: the
* than its limit, and an offline device should not fire one. * server refuses a box wider than its limit, and an offline device should not
* fire one. Whatever was loaded before stays drawn in both.
*/ */
export type FerrataStatus = 'idle' | 'loading' | 'ready' | 'error' | 'tooLarge' | 'offline'; export type FerrataStatus = 'idle' | 'loading' | 'ready' | 'error' | 'tooLarge' | 'offline';
/** Long enough that a pan or a zoom gesture settles before the request goes out. */ /** Long enough that a pan or a zoom gesture settles before the missing tiles are asked for. */
const DEBOUNCE_MS = 400; const DEBOUNCE_MS = 200;
/** The ring of tiles around the view that loads behind it, so a short pan finds its lines already there. */
const PREFETCH_RINGS = 1;
/** /**
* How long the layer waits before asking again for the same area after the * The trip's area is prefetched up to this many tiles (about 0.4 by 0.4
* mirrors failed it. Past the last step it stays on the error until the map * degree, at most sixteen requests of 0.1 degree); a trip spread wider loads
* moves or the layer is switched off and on. * as the map gets there.
*/ */
const RETRY_DELAYS_MS = [5_000, 15_000, 45_000]; export const FERRATA_AREA_PREFETCH_MAX_TILES = 64;
const NO_FERRATAS: Ferrata[] = []; const NO_TILES: readonly string[] = [];
/** const EMPTY_SNAPSHOT: FerrataTileSnapshot = {
* What came back: the answer, a 400 (`refused`: the server would not take the loaded: new Set(),
* box, which reads as "zoom in" rather than as an outage) or anything else ferratas: [],
* (`unavailable`: the Overpass mirrors, worth another try). waiting: new Set(),
*/ failure: null,
type Outcome = FerratasResponse | 'refused' | 'unavailable'; truncated: false,
hydrated: false,
};
const subscribeNothing = () => () => {};
const emptySnapshot = () => EMPTY_SNAPSHOT;
interface Answer { /** A range as a dependency-friendly string, and back. */
key: string; const rangeKeyOf = (range: FerrataTileRange) => `${range.minRow},${range.maxRow},${range.minCol},${range.maxCol}`;
outcome: Outcome; function rangeOfKey(key: string): FerrataTileRange {
const [minRow = 0, maxRow = 0, minCol = 0, maxCol = 0] = key.split(',').map(Number);
return { minRow, maxRow, minCol, maxCol };
} }
function failureOf(error: unknown): 'refused' | 'unavailable' { /** `missing`: some tile of the view is not loaded yet. */
const status = (error as { response?: { status?: unknown } } | null)?.response?.status; function statusOf(
return status === 400 ? 'refused' : 'unavailable'; active: boolean,
} offline: boolean,
fits: boolean,
/** `current` is the outcome for the area on screen, or null while that one is still on its way. */ missing: boolean,
function statusOf(active: boolean, offline: boolean, fits: boolean, current: Outcome | null): FerrataStatus { snapshot: FerrataTileSnapshot
): FerrataStatus {
if (!active) return 'idle'; if (!active) return 'idle';
if (offline) return 'offline'; if (offline) return 'offline';
if (!fits || current === 'refused') return 'tooLarge'; if (!fits || (missing && snapshot.failure === 'refused')) return 'tooLarge';
if (!current) return 'loading'; if (!missing) return 'ready';
return current === 'unavailable' ? 'error' : 'ready'; return snapshot.failure === 'unavailable' ? 'error' : 'loading';
} }
/** The area on screen as the request grid sees it, and the request it makes (null: none). */ /** One tile store per trip; it lives as long as the map surface does, so 2D, 3D and a toggle share it. */
function requestOf(tripId: number | null | undefined, bbox: FerrataBbox | null, enabled: boolean, offline: boolean) { function useTileStore(tripId: number | null | undefined): FerrataTileStore | null {
const area = bbox ? roundFerrataBbox(bbox) : null; return useMemo(() => {
const fits = !!area && ferrataBboxFits(area); if (!tripId) return null;
const active = enabled && !!tripId && !!area; const cacheName = ferrataRepo.cacheName();
const param = active && fits && !offline && area ? ferrataBboxParam(area) : null; return new FerrataTileStore({
return { fits, active, param, key: param ? `${tripId}:${param}` : null }; fetch: (bbox, signal) => ferrataRepo.list(tripId, ferrataBboxParam(bbox), signal),
} persist: (rows) => ferrataRepo.saveTiles(tripId, rows, cacheName),
});
/** The answer that speaks for this attempt; while a retry is in flight, the failure it follows keeps the error up. */ }, [tripId]);
function answerFor(answer: Answer | null, key: string | null, attempt: number): Answer | null {
if (!answer || !key) return null;
if (answer.key === `${key}#${attempt}`) return answer;
return attempt > 0 && answer.key === `${key}#${attempt - 1}` ? answer : null;
}
function view(answer: Answer | null, status: FerrataStatus) {
// The last good answer stays up while the next area loads; any other state draws nothing.
const last = answer && typeof answer.outcome === 'object' ? answer.outcome : null;
const shown = status === 'ready' || status === 'loading' ? last : null;
return {
ferratas: shown?.ferratas ?? NO_FERRATAS,
status,
truncated: shown?.truncated ?? false,
attribution: shown?.attribution ?? null,
};
} }
/** /**
* The via ferratas inside the visible map area of a trip, for the map layer. * The via ferratas of a trip, for the map layer, loaded per tile of the shared
* 0.05 degree grid (see `FerrataTileStore`).
* *
* Online only (see repo/ferrataRepo.ts). Nothing is requested while the layer is * Nothing is read or requested while the layer is off. Switched on, the trip's
* off. A new area waits DEBOUNCE_MS before it is asked for, and a newer area, * cached tiles come back from the device first and draw at once, offline too;
* a closed layer or an unmount aborts the request in flight, so a fast pan * then the view's missing tiles are asked for DEBOUNCE_MS after the map
* across the Alps ends in one request for where the map stopped. While the * settles, the ring around them behind that, and `area` (the trip's own
* next area loads, the lines of the last one stay drawn instead of blinking out. * extent) in the background once, after the same pause, so the view's tiles
* When the mirrors fail an area, it is asked for again after each of * are always asked for first. Every ferrata loaded stays drawn: a pan, a
* RETRY_DELAYS_MS; a 400 is not retried, it reads as "zoom in". * zoom, a switch between 2D and 3D or the layer off and on again never drops
* one, and never asks again for a tile it has.
*/ */
export function useTripFerratas(tripId: number | null | undefined, bbox: FerrataBbox | null, enabled: boolean) { export function useTripFerratas(
tripId: number | null | undefined,
view: FerrataBbox | null,
enabled: boolean,
area: FerrataBbox | null = null
): { ferratas: readonly Ferrata[]; status: FerrataStatus; truncated: boolean } {
const { offline } = useNetworkMode(); const { offline } = useNetworkMode();
const { fits, active, param, key } = requestOf(tripId, bbox, enabled, offline); const store = useTileStore(tripId);
const [answer, setAnswer] = useState<Answer | null>(null); const snapshot = useSyncExternalStore(
// Retries count per area: a new area starts again at zero without a reset. store ? store.subscribe : subscribeNothing,
const [retry, setRetry] = useState<{ key: string | null; attempt: number }>({ key: null, attempt: 0 }); store ? store.getSnapshot : emptySnapshot
const attempt = retry.key === key ? retry.attempt : 0; );
const requestKey = key ? `${key}#${attempt}` : null; const active = enabled && !!store;
const fits = !!view && ferrataBboxFits(view);
const viewKey = view && fits ? rangeKeyOf(ferrataTileRange(view)) : null;
const areaRange = area && ferrataBboxFitsArea(area) ? ferrataTileRange(area) : null;
const areaKey =
areaRange && ferrataTileCount(areaRange) <= FERRATA_AREA_PREFETCH_MAX_TILES ? rangeKeyOf(areaRange) : null;
const visibleKeys = useMemo(
() => (viewKey ? ferrataTilesOf(rangeOfKey(viewKey)).map(ferrataTileKey) : NO_TILES),
[viewKey]
);
useEffect(() => { useEffect(() => {
if (!tripId || !param || !requestKey) return; if (!active || !store || !tripId) return;
const areaKey = `${tripId}:${param}`; if (store.beginHydration()) {
const controller = new AbortController(); void ferrataRepo.cachedTiles(tripId).then(
let retryTimer: number | undefined; (rows) => store.hydrate(rows),
const timer = window.setTimeout(() => { // An unreadable cache is an empty one: the tiles load from the network instead.
ferrataRepo.list(tripId, param, controller.signal).then( () => store.hydrate([])
(reply) => {
if (!controller.signal.aborted) setAnswer({ key: requestKey, outcome: reply });
},
(error: unknown) => {
if (controller.signal.aborted) return;
// The pill reports it ("Overpass unreachable" or "zoom in"); the error
// itself carries nothing more the user could act on.
const failure = failureOf(error);
setAnswer({ key: requestKey, outcome: failure });
const delay = RETRY_DELAYS_MS[attempt];
if (failure === 'unavailable' && delay !== undefined) {
retryTimer = window.setTimeout(() => setRetry({ key: areaKey, attempt: attempt + 1 }), delay);
}
}
); );
}, DEBOUNCE_MS); }
return () => { return () => store.stop();
window.clearTimeout(timer); }, [active, store, tripId]);
window.clearTimeout(retryTimer);
controller.abort();
};
}, [tripId, param, requestKey, attempt]);
const status = statusOf(active, offline, fits, answerFor(answer, key, attempt)?.outcome ?? null); useEffect(() => {
return view(answer, status); if (!active || !store || offline || !viewKey) return;
const timer = window.setTimeout(() => {
const range = rangeOfKey(viewKey);
store.want(ferrataTilesOf(range), 'visible');
store.want(ferrataTilesOf(growFerrataTileRange(range, PREFETCH_RINGS)), 'background');
}, DEBOUNCE_MS);
return () => window.clearTimeout(timer);
}, [active, store, offline, viewKey]);
// Declared after the view's effect, so with both due its timer fires second.
useEffect(() => {
if (!active || !store || offline || !areaKey) return;
const timer = window.setTimeout(() => {
store.want(ferrataTilesOf(rangeOfKey(areaKey)), 'background');
}, DEBOUNCE_MS);
return () => window.clearTimeout(timer);
}, [active, store, offline, areaKey]);
const missing = visibleKeys.some((key) => !snapshot.loaded.has(key));
const status = statusOf(active, offline, fits, missing, snapshot);
return { ferratas: snapshot.ferratas, status, truncated: active && snapshot.truncated };
}
/** A trip's area is any real box; only its tile count limits the prefetch. */
function ferrataBboxFitsArea(bbox: FerrataBbox): boolean {
return bbox.north >= bbox.south && bbox.east >= bbox.west;
} }
@@ -1,4 +1,4 @@
// FE-COMP-MAPLAYER-001 to FE-COMP-MAPLAYER-014 // FE-COMP-MAPLAYER-001 to FE-COMP-MAPLAYER-016
import React from 'react' import React from 'react'
import { describe, it, expect, vi } from 'vitest' import { describe, it, expect, vi } from 'vitest'
import { render, screen, fireEvent } from '../../../tests/helpers/render' import { render, screen, fireEvent } from '../../../tests/helpers/render'
@@ -103,13 +103,15 @@ const ferrataLayer = (overrides: Partial<FerrataMapLayer> = {}): FerrataMapLayer
active: false, active: false,
onToggle: vi.fn(), onToggle: vi.fn(),
status: 'idle', status: 'idle',
ferratas: [], count: 0,
groups: [],
selectedId: null,
onSelect: vi.fn(), onSelect: vi.fn(),
...overrides, ...overrides,
}) })
describe('TourMapLayerSwitcher via ferrata switch', () => { describe('TourMapLayerSwitcher via ferrata switch', () => {
it('FE-COMP-MAPLAYER-011: offers no via ferrata switch without a layer, and no status row either', () => { it('FE-COMP-MAPLAYER-011: offers no via ferrata switch without a layer', () => {
const { container } = render(<TourMapLayerSwitcher active="default" onChange={() => {}} />) const { container } = render(<TourMapLayerSwitcher active="default" onChange={() => {}} />)
expect(screen.queryByRole('button', { name: 'Via ferratas' })).toBeNull() expect(screen.queryByRole('button', { name: 'Via ferratas' })).toBeNull()
expect(container.firstElementChild).toHaveAttribute('role', 'group') expect(container.firstElementChild).toHaveAttribute('role', 'group')
@@ -130,29 +132,51 @@ describe('TourMapLayerSwitcher via ferrata switch', () => {
expect(on).toHaveClass('bg-accent', 'text-accent-text') expect(on).toHaveClass('bg-accent', 'text-accent-text')
}) })
it('FE-COMP-MAPLAYER-013: the status pill sits in the same row, right of the pill, and says nothing while the layer is off', () => { it('FE-COMP-MAPLAYER-013: the count rides in the switch as a badge, nothing beside the pill, and none while off', () => {
const { rerender } = render(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer()} />) const { container, rerender } = render(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ count: 4 })} />)
expect(screen.queryByTestId('ferrata-count-badge')).toBeNull()
expect(container.firstElementChild).toHaveAttribute('role', 'group')
rerender(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ active: true, status: 'ready', count: 4 })} />)
const badge = screen.getByTestId('ferrata-count-badge')
expect(badge).toHaveTextContent('4')
expect(badge).toHaveAttribute('data-tone', 'default')
expect(screen.getByRole('button', { name: 'Via ferratas' })).toContainElement(badge)
expect(screen.getByRole('status')).toHaveTextContent('4 via ferrata routes')
expect(screen.queryByTestId('ferrata-status-pill')).toBeNull() expect(screen.queryByTestId('ferrata-status-pill')).toBeNull()
})
it('FE-COMP-MAPLAYER-014: a tiny spinner in the badge while tiles load', () => {
render(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ active: true, status: 'loading', count: 2 })} />)
const badge = screen.getByTestId('ferrata-count-badge')
expect(badge).toContainElement(screen.getByTestId('ferrata-count-spinner'))
expect(badge).not.toHaveTextContent('2')
expect(screen.getByRole('status')).toHaveTextContent('Loading via ferratas…')
})
it('FE-COMP-MAPLAYER-015: offline, the mirrors down or the view too wide: a warning tone, the reason in the tooltip', async () => {
const { rerender } = render(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ active: true, status: 'offline', count: 1 })} />)
expect(screen.getByTestId('ferrata-count-badge')).toHaveAttribute('data-tone', 'warning')
expect(screen.getByTestId('ferrata-count-badge')).toHaveClass('bg-warning-soft', 'text-warning')
expect(screen.getByRole('status')).toHaveTextContent('Via ferratas need a connection')
rerender(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ active: true, status: 'tooLarge' })} />) rerender(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ active: true, status: 'tooLarge' })} />)
const pill = screen.getByTestId('ferrata-status-pill') fireEvent.mouseEnter(screen.getByRole('button', { name: 'Via ferratas' }))
expect(pill).toHaveTextContent('Zoom in to load via ferratas') expect(await screen.findByRole('tooltip')).toHaveTextContent('Zoom in to load via ferratas')
const row = pill.parentElement as HTMLElement
expect(row.style.display).toBe('flex') rerender(<TourMapLayerSwitcher active="default" onChange={() => {}} ferrataLayer={ferrataLayer({ active: true, status: 'error' })} />)
expect(row.firstElementChild).toHaveAttribute('role', 'group') expect(screen.getByTestId('ferrata-count-badge')).toHaveAttribute('data-tone', 'warning')
expect(row.lastElementChild).toBe(pill) expect(screen.getByRole('status')).toHaveTextContent('Overpass unreachable')
}) })
it('FE-COMP-MAPLAYER-014: sits beside the 3D switch behind one divider, and counts what the layer found', () => { it('FE-COMP-MAPLAYER-016: sits beside the 3D switch behind one divider', () => {
const found = [{ id: 'way/1' }, { id: 'way/2' }] as unknown as FerrataMapLayer['ferratas']
render( render(
<TourMapLayerSwitcher active="default" onChange={() => {}} onToggleTerrain={() => {}} <TourMapLayerSwitcher active="default" onChange={() => {}} onToggleTerrain={() => {}}
ferrataLayer={ferrataLayer({ active: true, status: 'ready', ferratas: found })} /> ferrataLayer={ferrataLayer({ active: true, status: 'ready', count: 2 })} />
) )
const group = screen.getByRole('group') const group = screen.getByRole('group')
expect(group.querySelectorAll('span[aria-hidden]')).toHaveLength(1) expect(group.querySelectorAll('span.bg-edge')).toHaveLength(1)
const buttons = Array.from(group.querySelectorAll('button')).map((button) => button.textContent) const buttons = Array.from(group.querySelectorAll('button')).map((button) => button.getAttribute('aria-label') ?? button.textContent)
expect(buttons.slice(-2)).toEqual(['Via ferratas', '3D']) expect(buttons.slice(-2)).toEqual(['Via ferratas', '3D'])
expect(screen.getByTestId('ferrata-status-pill')).toHaveTextContent('2 via ferrata routes')
}) })
}) })
+94 -40
View File
@@ -1,8 +1,9 @@
import { Map as MapIcon, Mountain, MountainSnow, Rotate3d, Satellite } from 'lucide-react' import { Loader2, Map as MapIcon, Mountain, MountainSnow, Rotate3d, Satellite } from 'lucide-react'
import type { CSSProperties } from 'react' import type { CSSProperties } from 'react'
import { useTranslation } from '../../i18n' import { useTranslation } from '../../i18n'
import FerrataStatusPill from '../Ferratas/FerrataStatusPill'
import type { FerrataMapLayer } from '../Ferratas/useFerrataMapLayer' import type { FerrataMapLayer } from '../Ferratas/useFerrataMapLayer'
import type { FerrataStatus } from '../Ferratas/useTripFerratas'
import { fs } from '../shared/DialogShell'
import { Tooltip } from '../shared/Tooltip' import { Tooltip } from '../shared/Tooltip'
import { MAP_CONTROL_SHADOW } from './mapControlShadow' import { MAP_CONTROL_SHADOW } from './mapControlShadow'
@@ -82,59 +83,121 @@ const segmentStyle = (filled: boolean): CSSProperties => ({
fontSize: 'calc(11px * var(--fs-scale-caption, 1))', fontWeight: 600, fontSize: 'calc(11px * var(--fs-scale-caption, 1))', fontWeight: 600,
}) })
/** Per-view Tours control. It never reads or writes the global map setting. */ type Translate = (key: string, params?: Record<string, string | number>) => string
/** What the via ferrata layer is doing, in words, for the toggle's tooltip and the screen reader. */
function ferrataStatusLabel(status: Exclude<FerrataStatus, 'idle'>, count: number, t: Translate): string {
switch (status) {
case 'loading':
return t('ferratas.loading')
case 'error':
return t('ferratas.error')
case 'offline':
return t('ferratas.offline')
case 'tooLarge':
return t('ferratas.zoomIn')
case 'ready':
return t('ferratas.count', { count })
}
}
/**
* The via ferrata switch with its count as a badge: how many ferratas reach
* into the view, a small spinner while tiles load, and the warning tone when
* there is a reason it cannot load (offline, the mirrors down, the view too
* wide), which the tooltip names. Nothing beside the switch, so the bottom row
* stays one pill wide. `compact` drops the word and keeps the icon and the
* badge; the tooltip and the accessible name still say what it is.
*/
function FerrataToggle({ layer, compact = false }: { layer: FerrataMapLayer; compact?: boolean }) {
const { t } = useTranslation()
const { active, status, count } = layer
const live = active && status !== 'idle' ? ferrataStatusLabel(status, count, t) : null
const problem = status === 'error' || status === 'offline' || status === 'tooLarge'
return (
<>
<Tooltip label={live ?? t('ferratas.layer')} placement="top">
<button
type="button"
onClick={layer.onToggle}
aria-label={t('ferratas.layer')}
aria-pressed={active}
className={active ? 'bg-accent text-accent-text' : 'text-content-muted'}
style={segmentStyle(active)}
>
<MountainSnow size={15} strokeWidth={2} />
{!compact && <span>{t('ferratas.layer')}</span>}
{live && (
<span
aria-hidden
data-testid="ferrata-count-badge"
data-tone={problem ? 'warning' : 'default'}
className={`inline-flex h-[18px] min-w-[18px] items-center justify-center rounded-full px-1 font-semibold tabular-nums leading-none ${
problem ? 'bg-warning-soft text-warning' : 'bg-accent-text text-accent'
}`}
style={fs(10)}
>
{status === 'loading'
? <Loader2 size={10} strokeWidth={2.5} className="motion-safe:animate-spin" data-testid="ferrata-count-spinner" />
: count}
</span>
)}
</button>
</Tooltip>
{/* Polite, so a screen reader hears the count arrive without being interrupted. */}
<span role="status" aria-live="polite" className="sr-only">{live ?? ''}</span>
</>
)
}
/**
* Per-view Tours control. It never reads or writes the global map setting.
* `compact` shows the base layers and the via ferrata switch by their icons
* alone, each named by its tooltip, for a bottom row that has to share its
* width (the 3D map's, beside the flyover dock).
*/
export function TourMapLayerSwitcher({ export function TourMapLayerSwitcher({
active, active,
onChange, onChange,
onToggleTerrain, onToggleTerrain,
terrainActive = false, terrainActive = false,
ferrataLayer, ferrataLayer,
compact = false,
}: { }: {
active: TourBaseLayer active: TourBaseLayer
onChange: (layer: TourBaseLayer) => void onChange: (layer: TourBaseLayer) => void
/** Offers the 3D terrain switch beside the base layers. */ /** Offers the 3D terrain switch beside the base layers. */
onToggleTerrain?: () => void onToggleTerrain?: () => void
terrainActive?: boolean terrainActive?: boolean
/** /** Offers the via ferrata switch, with its count badge, beside the base layers. */
* Offers the via ferrata switch beside the base layers, and puts the layer's
* status pill in the same row, right of this one.
*/
ferrataLayer?: FerrataMapLayer ferrataLayer?: FerrataMapLayer
compact?: boolean
}) { }) {
const { t } = useTranslation() const { t } = useTranslation()
const pill = ( return (
<div style={{ ...FROSTED_SHELL, gap: 2 }} role="group" aria-label={t('settings.mapStyle')}> <div style={{ ...FROSTED_SHELL, gap: 2 }} role="group" aria-label={t('settings.mapStyle')}>
{TOUR_LAYERS.map(({ id, labelKey, Icon }) => { {TOUR_LAYERS.map(({ id, labelKey, Icon }) => {
const selected = active === id const selected = active === id
const label = labelKey ? t(labelKey) : 'Topo' const label = labelKey ? t(labelKey) : 'Topo'
return ( return (
<button <Tooltip key={id} label={label} placement="top" disabled={!compact}>
key={id} <button
type="button" type="button"
onClick={() => onChange(id)} onClick={() => onChange(id)}
aria-pressed={selected} aria-pressed={selected}
className={selected ? 'bg-accent-soft text-accent' : 'text-content-muted'} aria-label={compact ? label : undefined}
style={segmentStyle(selected)} className={selected ? 'bg-accent-soft text-accent' : 'text-content-muted'}
> style={segmentStyle(selected)}
<Icon size={15} strokeWidth={2} /> >
<span>{label}</span> <Icon size={15} strokeWidth={2} />
</button> {!compact && <span>{label}</span>}
</button>
</Tooltip>
) )
})} })}
{(ferrataLayer || onToggleTerrain) && <span aria-hidden className="mx-0.5 h-5 w-px bg-edge" />} {(ferrataLayer || onToggleTerrain) && <span aria-hidden className="mx-0.5 h-5 w-px bg-edge" />}
{ferrataLayer && ( {ferrataLayer && <FerrataToggle layer={ferrataLayer} compact={compact} />}
<button
type="button"
onClick={ferrataLayer.onToggle}
aria-pressed={ferrataLayer.active}
className={ferrataLayer.active ? 'bg-accent text-accent-text' : 'text-content-muted'}
style={segmentStyle(ferrataLayer.active)}
>
<MountainSnow size={15} strokeWidth={2} />
<span>{t('ferratas.layer')}</span>
</button>
)}
{onToggleTerrain && ( {onToggleTerrain && (
<button <button
type="button" type="button"
@@ -149,13 +212,4 @@ export function TourMapLayerSwitcher({
)} )}
</div> </div>
) )
if (!ferrataLayer) return pill
// The status pill keeps the column gap the lock pill keeps above, and says nothing while the layer is off.
return (
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
{pill}
<FerrataStatusPill status={ferrataLayer.status} count={ferrataLayer.ferratas.length} />
</div>
)
} }
@@ -26,8 +26,8 @@ vi.mock('../../hooks/useGeolocation', () => ({
// What the via ferrata layer draws is FerrataLayerLeaflet's own suite; here only what the // What the via ferrata layer draws is FerrataLayerLeaflet's own suite; here only what the
// renderer hands it. jsdom has no SVG renderer for Leaflet to draw real lines into. // renderer hands it. jsdom has no SVG renderer for Leaflet to draw real lines into.
vi.mock('../Ferratas/FerrataLayerLeaflet', () => ({ vi.mock('../Ferratas/FerrataLayerLeaflet', () => ({
FerrataMapLayerLeaflet: ({ layer }: { layer?: { active: boolean; ferratas: readonly unknown[] } }) => FerrataMapLayerLeaflet: ({ layer }: { layer?: { active: boolean; groups: readonly unknown[] } }) =>
layer?.active ? <div data-testid="ferrata-leaflet-layer" data-count={layer.ferratas.length} /> : null, layer?.active ? <div data-testid="ferrata-leaflet-layer" data-count={layer.groups.length} /> : null,
})) }))
vi.mock('../../services/photoService', () => ({ vi.mock('../../services/photoService', () => ({
@@ -41,6 +41,8 @@ vi.mock('../../services/photoService', () => ({
import L from 'leaflet' import L from 'leaflet'
import { MapView } from './MapView' import { MapView } from './MapView'
import { ContextMenu } from '../shared/ContextMenu' import { ContextMenu } from '../shared/ContextMenu'
import type { Ferrata } from '@trek/shared'
import { groupFerratas } from '../Ferratas/ferrataGroups'
// Every map this file builds, newest last, for the tests that reach past the component. // Every map this file builds, newest last, for the tests that reach past the component.
const maps: L.Map[] = [] const maps: L.Map[] = []
@@ -457,9 +459,12 @@ describe('MapView on a real Leaflet map', () => {
}) })
it('RC-10: the tour layer pill offers the via ferrata switch, and the layer is drawn only while it is on', async () => { it('RC-10: the tour layer pill offers the via ferrata switch, and the layer is drawn only while it is on', async () => {
const ferrata = { id: 'way/42', name: 'Via delle Trincee', grade: { raw: '4', level: 4, plus: null }, routeName: null, const ferrata: Ferrata = { id: 'way/42', name: 'Via delle Trincee', grade: { raw: '4', level: 4, plus: null }, routeName: null,
geometry: [[46.52, 11.8], [46.53, 11.81]], osmUrl: 'https://www.openstreetmap.org/way/42' } geometry: [[46.52, 11.8], [46.53, 11.81]], osmUrl: 'https://www.openstreetmap.org/way/42' }
const layer = { active: false, onToggle: vi.fn(), status: 'idle' as const, ferratas: [ferrata], onSelect: vi.fn() } const layer = {
active: false, onToggle: vi.fn(), status: 'idle' as const, count: 0, groups: groupFerratas([ferrata]),
selectedId: null, onSelect: vi.fn(),
}
const view = render( const view = render(
<MapView places={[]} center={[46.52, 11.8]} zoom={13} tileUrl={RASTER} <MapView places={[]} center={[46.52, 11.8]} zoom={13} tileUrl={RASTER}
viewBaseLayer="topo" onViewBaseLayerChange={vi.fn()} ferrataLayer={layer} />, viewBaseLayer="topo" onViewBaseLayerChange={vi.fn()} ferrataLayer={layer} />,
@@ -472,10 +477,10 @@ describe('MapView on a real Leaflet map', () => {
view.rerender( view.rerender(
<MapView places={[]} center={[46.52, 11.8]} zoom={13} tileUrl={RASTER} <MapView places={[]} center={[46.52, 11.8]} zoom={13} tileUrl={RASTER}
viewBaseLayer="topo" onViewBaseLayerChange={vi.fn()} ferrataLayer={{ ...layer, active: true, status: 'ready' }} />, viewBaseLayer="topo" onViewBaseLayerChange={vi.fn()} ferrataLayer={{ ...layer, active: true, status: 'ready', count: 1 }} />,
) )
await waitFor(() => expect(view.getByTestId('ferrata-leaflet-layer')).toHaveAttribute('data-count', '1')) await waitFor(() => expect(view.getByTestId('ferrata-leaflet-layer')).toHaveAttribute('data-count', '1'))
expect(view.getByRole('button', { name: 'Via ferratas' })).toHaveAttribute('aria-pressed', 'true') expect(view.getByRole('button', { name: 'Via ferratas' })).toHaveAttribute('aria-pressed', 'true')
expect(view.getByTestId('ferrata-status-pill')).toHaveTextContent('1 via ferrata route') expect(view.getByTestId('ferrata-count-badge')).toHaveTextContent('1')
}) })
}) })
@@ -36,6 +36,10 @@ const glMap = vi.hoisted(() => ({
removeLayer: vi.fn(), removeLayer: vi.fn(),
removeSource: vi.fn(), removeSource: vi.fn(),
setLayoutProperty: vi.fn(), setLayoutProperty: vi.fn(),
setFilter: vi.fn(),
hasImage: vi.fn().mockReturnValue(false),
addImage: vi.fn(),
removeImage: vi.fn(),
getStyle: vi.fn().mockReturnValue({ layers: [] }), getStyle: vi.fn().mockReturnValue({ layers: [] }),
isStyleLoaded: vi.fn().mockReturnValue(true), isStyleLoaded: vi.fn().mockReturnValue(true),
getCanvasContainer: vi.fn(() => glCanvasContainer), getCanvasContainer: vi.fn(() => glCanvasContainer),
@@ -227,6 +231,7 @@ vi.mock('../../services/photoService', () => ({
import mapboxgl from 'mapbox-gl'; import mapboxgl from 'mapbox-gl';
import * as photoService from '../../services/photoService'; import * as photoService from '../../services/photoService';
import { groupFerratas } from '../Ferratas/ferrataGroups';
import { MapViewGL as MapViewGLWithEngine } from './MapViewGL'; import { MapViewGL as MapViewGLWithEngine } from './MapViewGL';
import * as mapboxSetup from './mapboxSetup'; import * as mapboxSetup from './mapboxSetup';
@@ -489,11 +494,14 @@ describe('MapViewGL', () => {
] as [number, number][], ] as [number, number][],
osmUrl: 'https://www.openstreetmap.org/way/42', osmUrl: 'https://www.openstreetmap.org/way/42',
}; };
const FERRATA_GROUPS = groupFerratas([FERRATA]);
const ferrataLayer = (active: boolean) => ({ const ferrataLayer = (active: boolean) => ({
active, active,
onToggle: vi.fn(), onToggle: vi.fn(),
status: active ? ('ready' as const) : ('idle' as const), status: active ? ('ready' as const) : ('idle' as const),
ferratas: [FERRATA], count: active ? 1 : 0,
groups: FERRATA_GROUPS,
selectedId: null,
onSelect: vi.fn(), onSelect: vi.fn(),
}); });
const addedFerratas = () => glMap.addSource.mock.calls.filter(([id]) => id === 'trek-ferratas'); const addedFerratas = () => glMap.addSource.mock.calls.filter(([id]) => id === 'trek-ferratas');
@@ -529,7 +537,7 @@ describe('MapViewGL', () => {
await act(async () => {}); await act(async () => {});
expect(addedFerratas()).toHaveLength(1); expect(addedFerratas()).toHaveLength(1);
expect(view.getByRole('button', { name: 'Via ferratas' })).toHaveAttribute('aria-pressed', 'true'); expect(view.getByRole('button', { name: 'Via ferratas' })).toHaveAttribute('aria-pressed', 'true');
expect(view.getByTestId('ferrata-status-pill')).toHaveTextContent('1 via ferrata route'); expect(view.getByTestId('ferrata-count-badge')).toHaveTextContent('1');
}); });
it('RC-10: a click on a drawn ferrata opens it and is not a map click, so no control point lands under it', async () => { it('RC-10: a click on a drawn ferrata opens it and is not a map click, so no control point lands under it', async () => {
@@ -562,6 +570,6 @@ describe('MapViewGL', () => {
}; };
act(() => mapClicks.forEach((handler) => handler(click))); act(() => mapClicks.forEach((handler) => handler(click)));
expect(onMapClick).not.toHaveBeenCalled(); expect(onMapClick).not.toHaveBeenCalled();
expect(layer.onSelect).toHaveBeenCalledWith(FERRATA, { lat: 46.52, lng: 11.8 }); expect(layer.onSelect).toHaveBeenCalledWith(FERRATA_GROUPS[0], FERRATA, { lat: 46.52, lng: 11.8 });
}); });
}); });
+4 -3
View File
@@ -120,9 +120,10 @@ export interface MapViewProps {
/** Offers the 3D terrain switch in the tour layer pill. */ /** Offers the 3D terrain switch in the tour layer pill. */
onToggleTerrain3d?: () => void; onToggleTerrain3d?: () => void;
/** /**
* The via ferrata layer (RC-10): its switch and status pill join the tour layer * The via ferrata layer (RC-10): its switch, with the count as a badge, joins
* pill, and while it is on its lines are drawn and a click on one hands the * the tour layer pill, and while it is on its lines are drawn and a click on
* ferrata to `onSelect`. Both renderers mount it through the Ferratas modules. * one hands the ferrata to `onSelect`. Both renderers mount it through the
* Ferratas modules.
*/ */
ferrataLayer?: FerrataMapLayer; ferrataLayer?: FerrataMapLayer;
/** /**
+1 -1
View File
@@ -100,7 +100,7 @@ export function TimePill({ children, className = '', quiet = false }: { children
return <WhiteBadge className={className} quiet={quiet} icon={<Clock size={10} strokeWidth={2.2} className="flex-none text-content-faint" />}>{children}</WhiteBadge> return <WhiteBadge className={className} quiet={quiet} icon={<Clock size={10} strokeWidth={2.2} className="flex-none text-content-faint" />}>{children}</WhiteBadge>
} }
export type PillTone = 'neutral' | 'success' | 'warning' | 'danger' | 'info' | 'accent' type PillTone = 'neutral' | 'success' | 'warning' | 'danger' | 'info' | 'accent'
// White badges on whatever tint the row has; the tone only colours the words and the icon. // White badges on whatever tint the row has; the tone only colours the words and the icon.
const PILL_TONES: Record<PillTone, string> = { const PILL_TONES: Record<PillTone, string> = {
@@ -1,6 +1,6 @@
import type { FerrataBbox } from '@trek/shared'; import type { FerrataBbox } from '@trek/shared';
import { ChevronDown, ChevronUp, Gauge, Palette, Pause, Play, RotateCcw, Scan, TrendingUp } from 'lucide-react'; import { ChevronDown, ChevronUp, Gauge, Palette, Pause, Play, RotateCcw, Scan, TrendingUp } from 'lucide-react';
import { useMemo, useState, type ReactNode } from 'react'; import { useEffect, useMemo, useState, type ReactNode } from 'react';
import { useTranslation } from '../../../i18n'; import { useTranslation } from '../../../i18n';
import type { DistanceUnit } from '../../../types'; import type { DistanceUnit } from '../../../types';
import type { DistanceIndexedProfileSample, RouteProfileFocus } from '../../../utils/routeGeometry'; import type { DistanceIndexedProfileSample, RouteProfileFocus } from '../../../utils/routeGeometry';
@@ -12,6 +12,7 @@ import { SoftPill } from '../../Planner/planParts';
import { fs } from '../../shared/DialogShell'; import { fs } from '../../shared/DialogShell';
import { Tooltip } from '../../shared/Tooltip'; import { Tooltip } from '../../shared/Tooltip';
import { GRADE_BANDS, terrainStats, type GradeBandKey, type TerrainStats } from './tourTerrainModel'; import { GRADE_BANDS, terrainStats, type GradeBandKey, type TerrainStats } from './tourTerrainModel';
import { useControlRowFit } from './useControlRowFit';
import type { TerrainWaypoints } from './useTerrainPins'; import type { TerrainWaypoints } from './useTerrainPins';
import { useTourTerrain } from './useTourTerrain'; import { useTourTerrain } from './useTourTerrain';
@@ -34,6 +35,8 @@ export interface TourTerrainMapProps {
ferrataLayer?: FerrataMapLayer; ferrataLayer?: FerrataMapLayer;
/** The visible area, for the via ferrata layer to ask for. */ /** The visible area, for the via ferrata layer to ask for. */
onViewportChange?: (bbox: FerrataBbox) => void; onViewportChange?: (bbox: FerrataBbox) => void;
/** How high the controls along the bottom edge reach, every row of them, for what floats above them. */
onControlsHeightChange?: (px: number) => void;
} }
const SPEEDS = [1, 2, 4] as const; const SPEEDS = [1, 2, 4] as const;
@@ -53,47 +56,61 @@ const GLASS = {
*/ */
export default function TourTerrainMap(props: TourTerrainMapProps) { export default function TourTerrainMap(props: TourTerrainMapProps) {
const { samples, leftWidth, rightWidth, baseLayer, onBaseLayerChange, onExit, unit = 'metric' } = props; const { samples, leftWidth, rightWidth, baseLayer, onBaseLayerChange, onExit, unit = 'metric' } = props;
const { onControlsHeightChange } = props;
const { setContainer, ...terrain } = useTourTerrain({ ...props, panels: { leftWidth, rightWidth } }); const { setContainer, ...terrain } = useTourTerrain({ ...props, panels: { leftWidth, rightWidth } });
const stats = useMemo(() => terrainStats(samples), [samples]); const stats = useMemo(() => terrainStats(samples), [samples]);
const { fit, height: controlsHeight, attachRow, attachStack } = useControlRowFit();
const legend = terrain.canReplay ? <GradeLegend stats={stats} unit={unit} /> : null;
const legendAbove = fit === 'stacked';
useEffect(() => {
if (controlsHeight !== null) onControlsHeightChange?.(controlsHeight);
}, [controlsHeight, onControlsHeightChange]);
return ( return (
<div className="absolute inset-0"> <div className="absolute inset-0">
{/* MapLibre makes its container position: relative, so it is sized, not inset. */} {/* MapLibre makes its container position: relative, so it is sized, not inset. */}
<div ref={setContainer} className="h-full w-full bg-surface" /> <div ref={setContainer} className="h-full w-full bg-surface" />
{/* One row along the bottom edge, between the panels: the flat map's layer pill at {/* One row along the bottom edge, between the panels and the same inset from both:
the same spot with 3D switched on (and the via ferrata status beside it), then the the flat map's layer pill at the same spot with 3D switched on (the via ferrata
flyover controls and the grade legend. The pill takes the width it needs, so a count rides inside its switch), then the flyover controls and the grade legend.
longer pill moves the dock along instead of running under it. */} Where the row does not fit, the pill goes to its icons first, then the legend
moves onto a row of its own above, so nothing runs under a panel. */}
<div <div
className="pointer-events-none absolute bottom-3 z-[19] flex items-end gap-3" ref={attachStack}
className="pointer-events-none absolute bottom-3 z-[19] flex flex-col items-end gap-3"
style={{ style={{
insetInlineStart: leftWidth + MAP_LAYER_SWITCHER_INSET, insetInlineStart: leftWidth + MAP_LAYER_SWITCHER_INSET,
insetInlineEnd: rightWidth + MAP_LAYER_SWITCHER_INSET, insetInlineEnd: rightWidth + MAP_LAYER_SWITCHER_INSET,
}} }}
> >
<div className="flex-none"> {legendAbove && legend}
<TourMapLayerSwitcher <div ref={attachRow} className="flex w-full items-end gap-3" data-fit={fit} data-testid="terrain-control-row">
active={baseLayer} <div className="flex-none">
onChange={onBaseLayerChange} <TourMapLayerSwitcher
terrainActive active={baseLayer}
onToggleTerrain={onExit} onChange={onBaseLayerChange}
ferrataLayer={props.ferrataLayer} terrainActive
/> onToggleTerrain={onExit}
</div> ferrataLayer={props.ferrataLayer}
<div className="grid min-w-0 flex-1 grid-cols-[1fr_auto_1fr] items-end gap-3"> compact={fit !== 'full'}
<span /> />
{terrain.canReplay ? ( </div>
<TerrainDock terrain={terrain} /> <div className="grid min-w-0 flex-1 grid-cols-[1fr_auto_1fr] items-end gap-3">
) : ( <span />
<div {terrain.canReplay ? (
className="pointer-events-auto rounded-full px-4 py-2.5 text-content-muted" <TerrainDock terrain={terrain} />
style={{ ...GLASS, ...fs(12, 'body') }} ) : (
> <div
<PickHint editing={!!props.waypoints.onAdd} /> className="pointer-events-auto rounded-full px-4 py-2.5 text-content-muted"
</div> style={{ ...GLASS, ...fs(12, 'body') }}
)} >
<div className="flex justify-end">{terrain.canReplay && <GradeLegend stats={stats} unit={unit} />}</div> <PickHint editing={!!props.waypoints.onAdd} />
</div>
)}
<div className="flex justify-end">{!legendAbove && legend}</div>
</div>
</div> </div>
</div> </div>
</div> </div>
@@ -0,0 +1,52 @@
// FE-TERRAIN-FIT-001 to FE-TERRAIN-FIT-002: the 3D map's bottom row steps down to fit between the panels.
import { act, renderHook } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { type ControlRowFit, useControlRowFit } from './useControlRowFit';
/** What the row's content needs at each step, in pixels. */
const NEEDS: Record<ControlRowFit, number> = { full: 700, compact: 480, stacked: 400 };
function fakeRow(room: { width: number }, fit: () => ControlRowFit): HTMLDivElement {
const row = document.createElement('div');
Object.defineProperty(row, 'clientWidth', { get: () => room.width });
Object.defineProperty(row, 'scrollWidth', { get: () => Math.max(room.width, NEEDS[fit()]) });
return row;
}
describe('useControlRowFit', () => {
it('FE-TERRAIN-FIT-001: keeps every label while the row fits, and steps down only as far as it must', () => {
const room = { width: 800 };
const { result, rerender } = renderHook(() => useControlRowFit());
const row = fakeRow(room, () => result.current.fit);
act(() => result.current.attachRow(row));
expect(result.current.fit).toBe('full');
room.width = 500;
rerender();
expect(result.current.fit).toBe('compact');
room.width = 450;
rerender();
expect(result.current.fit).toBe('stacked');
});
it('FE-TERRAIN-FIT-002: steps back up only once the room holds what the larger step needed', () => {
const room = { width: 450 };
const { result, rerender } = renderHook(() => useControlRowFit());
const row = fakeRow(room, () => result.current.fit);
act(() => result.current.attachRow(row));
rerender();
expect(result.current.fit).toBe('stacked');
room.width = 600;
rerender();
expect(result.current.fit).toBe('compact');
room.width = 699;
rerender();
expect(result.current.fit).toBe('compact');
room.width = 700;
rerender();
expect(result.current.fit).toBe('full');
});
});
@@ -0,0 +1,68 @@
import { useCallback, useLayoutEffect, useRef, useState } from 'react';
/**
* How the 3D map's bottom row fits between the side panels: `full` with every
* label, `compact` with the layer pill on its icons, and `stacked` with the
* grade legend on a row of its own above the dock as well.
*/
export type ControlRowFit = 'full' | 'compact' | 'stacked';
const STEPS: readonly ControlRowFit[] = ['full', 'compact', 'stacked'];
/**
* Steps the row down while its content is wider than the room between the
* panels, and back up once the room has grown to what the larger step
* needed, so it never flips back and forth at one width. `height` is the
* height the controls take from the bottom edge, every row of them, for what
* floats above them (the via ferrata card).
*
* `row` is the one row that must not overflow; `stack` wraps everything the
* fit shows, the legend's own row included.
*/
export function useControlRowFit() {
const [row, setRow] = useState<HTMLDivElement | null>(null);
const [stack, setStack] = useState<HTMLDivElement | null>(null);
const [fit, setFit] = useState<ControlRowFit>('full');
const [height, setHeight] = useState<number | null>(null);
// The width the content needed at each step when it had to step down from it.
const needed = useRef<Partial<Record<ControlRowFit, number>>>({});
const check = useCallback(() => {
if (!row) return;
setFit((current) => {
const index = STEPS.indexOf(current);
const room = row.clientWidth;
const larger = STEPS[index - 1];
const neededLarger = larger ? needed.current[larger] : undefined;
if (larger && neededLarger !== undefined && room >= neededLarger) return larger;
const smaller = STEPS[index + 1];
if (smaller && row.scrollWidth > room + 1) {
needed.current[current] = row.scrollWidth;
return smaller;
}
return current;
});
}, [row]);
useLayoutEffect(() => {
check();
});
useLayoutEffect(() => {
if (!row || typeof ResizeObserver === 'undefined') return;
const observer = new ResizeObserver(() => check());
observer.observe(row);
return () => observer.disconnect();
}, [row, check]);
useLayoutEffect(() => {
if (!stack || typeof ResizeObserver === 'undefined') return;
const measure = () => setHeight(stack.offsetHeight || null);
measure();
const observer = new ResizeObserver(measure);
observer.observe(stack);
return () => observer.disconnect();
}, [stack]);
return { fit, height, attachRow: setRow, attachStack: setStack };
}
@@ -39,5 +39,6 @@ export function useTerrainFerratas(
}; };
}, [gl, onViewportChange]); }, [gl, onViewportChange]);
useFerrataMapLayerGL(gl ? (gl.map as unknown as FerrataGlMap) : null, layer); // Wider lines than the flat map's: seen at a slant over the hillshade, the thin ones get lost.
useFerrataMapLayerGL(gl ? (gl.map as unknown as FerrataGlMap) : null, layer, 'terrain');
} }
+22 -1
View File
@@ -1,4 +1,4 @@
import type { RoadtripPreferences, TourListItem, TourWaypoint } from '@trek/shared'; import type { Ferrata, RoadtripPreferences, TourListItem, TourWaypoint } from '@trek/shared';
import Dexie, { type Table } from 'dexie'; import Dexie, { type Table } from 'dexie';
import type { Trip, Day, Place, PackingItem, TodoItem, BudgetItem, Reservation, TripFile, Accommodation, TripMember, Tag, Category } from '../types'; import type { Trip, Day, Place, PackingItem, TodoItem, BudgetItem, Reservation, TripFile, Accommodation, TripMember, Tag, Category } from '../types';
@@ -28,6 +28,20 @@ export interface CachedBudgetSettlement {
settlement: unknown; settlement: unknown;
} }
/**
* One 0.05 degree tile of the via ferrata layer (see `ferrataGrid` in
* `@trek/shared`) as last loaded for a trip: the ferratas that reach into it,
* possibly none. Read straight from OpenStreetMap, not trip data; kept so the
* layer draws at once and offline, and refreshed after a week.
*/
interface CachedFerrataTile {
tripId: number;
/** The tile's key, `<row>:<col>`. */
tile: string;
fetchedAt: number;
ferratas: Ferrata[];
}
// ── Queue + sync types ──────────────────────────────────────────────────────── // ── Queue + sync types ────────────────────────────────────────────────────────
// 'conflict' is terminal-until-resolved: the server rejected the replay because // 'conflict' is terminal-until-resolved: the server rejected the replay because
@@ -234,6 +248,7 @@ export class TrekOfflineDb extends Dexie {
areaPlaces!: Table<CachedAreaPlace, [string, number]>; areaPlaces!: Table<CachedAreaPlace, [string, number]>;
tours!: Table<CachedTour, number>; tours!: Table<CachedTour, number>;
budgetSettlements!: Table<CachedBudgetSettlement, number>; budgetSettlements!: Table<CachedBudgetSettlement, number>;
ferrataTiles!: Table<CachedFerrataTile, [number, string]>;
constructor(name: string = ANON_DB_NAME) { constructor(name: string = ANON_DB_NAME) {
super(name); super(name);
@@ -308,6 +323,10 @@ export class TrekOfflineDb extends Dexie {
// v10: the Costs settlement, one per trip, so the balances read offline. // v10: the Costs settlement, one per trip, so the balances read offline.
this.version(10).stores({ budgetSettlements: 'tripId' }); this.version(10).stores({ budgetSettlements: 'tripId' });
// v11: the via ferrata layer's loaded tiles, per trip, so the lines draw at
// once and offline; `tripId` so they drop with the trip.
this.version(11).stores({ ferrataTiles: '[tripId+tile], tripId, fetchedAt' });
} }
} }
@@ -590,6 +609,7 @@ export async function clearTripData(tripId: number): Promise<void> {
offlineDb.roadtripPreferences, offlineDb.roadtripPreferences,
offlineDb.tours, offlineDb.tours,
offlineDb.budgetSettlements, offlineDb.budgetSettlements,
offlineDb.ferrataTiles,
], ],
async () => { async () => {
await offlineDb.roadtripPreferences.delete(tripId); await offlineDb.roadtripPreferences.delete(tripId);
@@ -611,6 +631,7 @@ export async function clearTripData(tripId: number): Promise<void> {
// answering offline searches — and nothing else ever deleted them, so they // answering offline searches — and nothing else ever deleted them, so they
// accumulated for the life of the install. // accumulated for the life of the install.
await offlineDb.areaPlaces.where('tripId').equals(tripId).delete(); await offlineDb.areaPlaces.where('tripId').equals(tripId).delete();
await offlineDb.ferrataTiles.where('tripId').equals(tripId).delete();
}, },
); );
// Remove the trip row itself outside the transaction since it's a separate table // Remove the trip row itself outside the transaction since it's a separate table
+5
View File
@@ -1199,6 +1199,11 @@ body {
.leaflet-tooltip-top.map-tooltip::before { .leaflet-tooltip-top.map-tooltip::before {
display: none; display: none;
} }
/* A clicked via ferrata opens a card with its own focus; the path's focus ring would only linger on the map. */
.leaflet-interactive.trek-ferrata-hit:focus,
.leaflet-interactive.trek-ferrata-hit:focus-visible {
outline: none;
}
/* Scrollbars — styled on desktop, hidden on mobile */ /* Scrollbars — styled on desktop, hidden on mobile */
::-webkit-scrollbar { ::-webkit-scrollbar {
@@ -1,9 +1,10 @@
// FE-PAGE-TOURSTAGE-001 to FE-PAGE-TOURSTAGE-009 // FE-PAGE-TOURSTAGE-001 to FE-PAGE-TOURSTAGE-012
import type { Ferrata } from '@trek/shared'; import type { Ferrata } from '@trek/shared';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { act, fireEvent, render, screen, waitFor } from '../../../tests/helpers/render'; import { act, fireEvent, render, screen, waitFor } from '../../../tests/helpers/render';
import { useTripFerratas } from '../../components/Ferratas/useTripFerratas'; import { useTripFerratas } from '../../components/Ferratas/useTripFerratas';
import type { MapViewProps } from '../../components/Map/mapViewContract'; import type { MapViewProps } from '../../components/Map/mapViewContract';
import { useSettingsStore } from '../../store/settingsStore';
import { TourPlannerMapStage, type TourPlannerMapStageProps } from './TourPlannerMapStage'; import { TourPlannerMapStage, type TourPlannerMapStageProps } from './TourPlannerMapStage';
type Handler = (event?: unknown) => void; type Handler = (event?: unknown) => void;
@@ -42,7 +43,14 @@ const stage = vi.hoisted(() => {
}), }),
}; };
const fire = (type: string, event?: unknown) => (listeners.get(type) ?? []).forEach((fn) => fn(event)); const fire = (type: string, event?: unknown) => (listeners.get(type) ?? []).forEach((fn) => fn(event));
return { map, engine, listeners, fire, flatMap: { props: null as MapViewProps | null } }; return {
map,
engine,
listeners,
fire,
flatMap: { props: null as MapViewProps | null },
search: { props: null as Record<string, unknown> | null },
};
}); });
vi.mock('../../components/Map/engines/maplibre', () => ({ default: stage.engine })); vi.mock('../../components/Map/engines/maplibre', () => ({ default: stage.engine }));
@@ -56,7 +64,12 @@ vi.mock('../../components/Map/MapViewAuto', () => ({
); );
}, },
})); }));
vi.mock('../../components/Planner/LocationSelect', () => ({ default: () => <input aria-label="location search" /> })); vi.mock('../../components/Planner/LocationSelect', () => ({
default: (props: Record<string, unknown>) => {
stage.search.props = props;
return <input aria-label="location search" />;
},
}));
// What the layer asks the server is useTripFerratas' own suite; here it answers from memory. // What the layer asks the server is useTripFerratas' own suite; here it answers from memory.
vi.mock('../../components/Ferratas/useTripFerratas', () => ({ useTripFerratas: vi.fn() })); vi.mock('../../components/Ferratas/useTripFerratas', () => ({ useTripFerratas: vi.fn() }));
@@ -122,12 +135,12 @@ async function openTerrain() {
beforeEach(() => { beforeEach(() => {
stage.listeners.clear(); stage.listeners.clear();
stage.flatMap.props = null; stage.flatMap.props = null;
stage.search.props = null;
vi.clearAllMocks(); vi.clearAllMocks();
vi.mocked(useTripFerratas).mockImplementation((_tripId, _bbox, enabled) => ({ vi.mocked(useTripFerratas).mockImplementation((_tripId, _bbox, enabled) => ({
ferratas: enabled ? [FERRATA] : [], ferratas: enabled ? [FERRATA] : [],
status: enabled ? 'ready' : 'idle', status: enabled ? 'ready' : 'idle',
truncated: false, truncated: false,
attribution: null,
})); }));
vi.stubGlobal( vi.stubGlobal(
'fetch', 'fetch',
@@ -202,55 +215,106 @@ describe('TourPlannerMapStage', () => {
expect(props?.ferrataLayer?.active).toBe(false); expect(props?.ferrataLayer?.active).toBe(false);
expect(props?.ferrataLayer?.status).toBe('idle'); expect(props?.ferrataLayer?.status).toBe('idle');
expect(typeof props?.onViewportChange).toBe('function'); expect(typeof props?.onViewportChange).toBe('function');
expect(vi.mocked(useTripFerratas).mock.lastCall).toEqual([7, null, false]); // Off: no view, and the trip's area (here its one framed point) only rides along.
expect(vi.mocked(useTripFerratas).mock.lastCall).toEqual([
7,
null,
false,
{ south: 46, west: 7, north: 46, east: 7 },
]);
}); });
it('FE-PAGE-TOURSTAGE-007: switched on, the layer asks for the area the flat map reported and draws what it found', () => { it('FE-PAGE-TOURSTAGE-007: switched on, the layer asks for the area the flat map reported and draws what it found, grouped', () => {
renderStage(planner('view-saved')); renderStage(planner('view-saved'));
const area = { south: 46.5, west: 11.7, north: 46.6, east: 11.9 }; const area = { south: 46.5, west: 11.7, north: 46.6, east: 11.9 };
act(() => stage.flatMap.props?.onViewportChange?.(area)); act(() => stage.flatMap.props?.onViewportChange?.(area));
act(() => stage.flatMap.props?.ferrataLayer?.onToggle()); act(() => stage.flatMap.props?.ferrataLayer?.onToggle());
expect(vi.mocked(useTripFerratas).mock.lastCall).toEqual([7, area, true]); expect(vi.mocked(useTripFerratas).mock.lastCall?.slice(0, 3)).toEqual([7, area, true]);
expect(stage.flatMap.props?.ferrataLayer?.active).toBe(true); expect(stage.flatMap.props?.ferrataLayer?.active).toBe(true);
expect(stage.flatMap.props?.ferrataLayer?.ferratas).toEqual([FERRATA]); expect(stage.flatMap.props?.ferrataLayer?.groups[0]?.segments).toEqual([FERRATA]);
expect(stage.flatMap.props?.ferrataLayer?.count).toBe(1);
}); });
it('FE-PAGE-TOURSTAGE-008: a picked ferrata opens its card bottom right, clear of the right panel, and Escape closes it', () => { it('FE-PAGE-TOURSTAGE-008: a picked ferrata opens its card at the inline end, clear of the right panel, and Escape closes it', () => {
renderStage(planner('view-saved')); renderStage(planner('view-saved'));
act(() => stage.flatMap.props?.ferrataLayer?.onToggle()); act(() => stage.flatMap.props?.ferrataLayer?.onToggle());
expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull(); expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull();
act(() => stage.flatMap.props?.ferrataLayer?.onSelect(FERRATA, { lat: 46.52, lng: 11.8 })); const group = stage.flatMap.props?.ferrataLayer?.groups[0];
if (!group) throw new Error('no group');
act(() => stage.flatMap.props?.ferrataLayer?.onSelect(group, FERRATA, { lat: 46.52, lng: 11.8 }));
const overlay = screen.getByTestId('ferrata-detail-overlay'); const overlay = screen.getByTestId('ferrata-detail-overlay');
// The right panel's 380px plus the 20px the layer pill keeps to the left one. // The right panel's 380px plus the 20px the layer pill keeps to the left one.
expect(overlay.style.right).toBe('400px'); expect(overlay.style.insetInlineEnd).toBe('400px');
// Above the bottom row: its 12px gap, the 42px pill, the same gap again. // Above the bottom row: its 12px gap, the 42px pill, the same gap again.
expect(overlay.style.bottom).toBe('66px'); expect(overlay.style.bottom).toBe('66px');
expect(overlay).toHaveTextContent('Via delle Trincee'); expect(overlay).toHaveTextContent('Via delle Trincee');
expect(stage.flatMap.props?.ferrataLayer?.selectedId).toBe(group.id);
fireEvent.keyDown(document, { key: 'Escape' }); fireEvent.keyDown(document, { key: 'Escape' });
expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull(); expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull();
}); });
it('FE-PAGE-TOURSTAGE-009: in 3D the switch sits in the terrain layer pill, the layer stays on and the terrain reports its area', async () => { it('FE-PAGE-TOURSTAGE-009: in 3D the switch sits in the terrain layer pill with its count, the layer stays on and the terrain reports its area', async () => {
renderStage(planner('view-saved')); renderStage(planner('view-saved'));
act(() => stage.flatMap.props?.ferrataLayer?.onToggle()); act(() => stage.flatMap.props?.ferrataLayer?.onToggle());
await openTerrain(); await openTerrain();
const toggle = screen.getByRole('button', { name: 'Via ferratas' }); const toggle = screen.getByRole('button', { name: 'Via ferratas' });
expect(toggle).toHaveAttribute('aria-pressed', 'true'); expect(toggle).toHaveAttribute('aria-pressed', 'true');
expect(screen.getByTestId('ferrata-status-pill')).toHaveTextContent('1 via ferrata route'); expect(screen.queryByTestId('ferrata-status-pill')).toBeNull();
act(() => stage.fire('load')); act(() => stage.fire('load'));
expect(vi.mocked(useTripFerratas).mock.lastCall).toEqual([ expect(vi.mocked(useTripFerratas).mock.lastCall?.slice(0, 3)).toEqual([
7, 7,
{ south: 46.5, west: 11.7, north: 46.6, east: 11.9 }, { south: 46.5, west: 11.7, north: 46.6, east: 11.9 },
true, true,
]); ]);
expect(screen.getByTestId('ferrata-count-badge')).toHaveTextContent('1');
fireEvent.click(toggle); fireEvent.click(toggle);
expect(screen.getByRole('button', { name: 'Via ferratas' })).toHaveAttribute('aria-pressed', 'false'); expect(screen.getByRole('button', { name: 'Via ferratas' })).toHaveAttribute('aria-pressed', 'false');
expect(screen.queryByTestId('ferrata-status-pill')).toBeNull(); expect(screen.queryByTestId('ferrata-count-badge')).toBeNull();
});
it('FE-PAGE-TOURSTAGE-010: the map search is the floating variant, with the short placeholder', () => {
renderStage(planner('view-saved'));
expect(stage.search.props).toMatchObject({
floating: true,
placeholder: 'tours.planner.mapSearch',
ariaLabel: 'tours.planner.mapSearch',
});
});
it('FE-PAGE-TOURSTAGE-011: right to left, the card sits at the inline end over the left panel', () => {
useSettingsStore.setState((state) => ({ settings: { ...state.settings, language: 'ar' } }));
try {
renderStage(planner('view-saved'));
act(() => stage.flatMap.props?.ferrataLayer?.onToggle());
const group = stage.flatMap.props?.ferrataLayer?.groups[0];
if (!group) throw new Error('no group');
act(() => stage.flatMap.props?.ferrataLayer?.onSelect(group, FERRATA, { lat: 46.52, lng: 11.8 }));
// The left panel's 320px plus the pill's 20px.
expect(screen.getByTestId('ferrata-detail-overlay').style.insetInlineEnd).toBe('340px');
} finally {
useSettingsStore.setState((state) => ({ settings: { ...state.settings, language: 'en' } }));
}
});
it('FE-PAGE-TOURSTAGE-012: the press that closes a ferrata card does not also add a waypoint, the next click does', () => {
const tourPlanner = planner('new-draft');
renderStage(tourPlanner);
act(() => stage.flatMap.props?.ferrataLayer?.onToggle());
const group = stage.flatMap.props?.ferrataLayer?.groups[0];
if (!group) throw new Error('no group');
act(() => stage.flatMap.props?.ferrataLayer?.onSelect(group, FERRATA, { lat: 46.52, lng: 11.8 }));
fireEvent.pointerDown(screen.getByRole('button', { name: 'flat map' }));
expect(screen.queryByTestId('ferrata-detail-overlay')).toBeNull();
act(() => stage.flatMap.props?.onMapClick?.({ latlng: { lat: 46.2, lng: 7.2 } } as never));
expect(tourPlanner.addWaypoint).not.toHaveBeenCalled();
act(() => stage.flatMap.props?.onMapClick?.({ latlng: { lat: 46.3, lng: 7.3 } } as never));
expect(tourPlanner.addWaypoint).toHaveBeenCalledWith(46.3, 7.3);
}); });
}); });
@@ -1,9 +1,11 @@
import { lazy, Suspense, useState, type ReactElement } from 'react'; import { lazy, Suspense, useMemo, useState, type ReactElement } from 'react';
import FerrataDetailOverlay from '../../components/Ferratas/FerrataDetailOverlay'; import FerrataDetailOverlay from '../../components/Ferratas/FerrataDetailOverlay';
import { useFerrataMapLayer } from '../../components/Ferratas/useFerrataMapLayer'; import { useFerrataMapLayer } from '../../components/Ferratas/useFerrataMapLayer';
import { useOutsidePressGuard } from '../../components/Ferratas/useOutsidePressGuard';
import { MAP_LAYER_SWITCHER_INSET, MAP_ROUND_CONTROL_SIZE } from '../../components/Map/MapLayerSwitcher'; import { MAP_LAYER_SWITCHER_INSET, MAP_ROUND_CONTROL_SIZE } from '../../components/Map/MapLayerSwitcher';
import { MapViewAuto as MapView } from '../../components/Map/MapViewAuto'; import { MapViewAuto as MapView } from '../../components/Map/MapViewAuto';
import LocationSelect from '../../components/Planner/LocationSelect'; import LocationSelect from '../../components/Planner/LocationSelect';
import { isRtlLanguage, useTranslation } from '../../i18n';
import { useSettingsStore } from '../../store/settingsStore'; import { useSettingsStore } from '../../store/settingsStore';
import type { useTripPlanner } from './useTripPlanner'; import type { useTripPlanner } from './useTripPlanner';
import type { useTripPlannerPage } from './useTripPlannerPage'; import type { useTripPlannerPage } from './useTripPlannerPage';
@@ -49,8 +51,18 @@ export function TourPlannerMapStage({
rightPanelPx, rightPanelPx,
}: Readonly<TourPlannerMapStageProps>): ReactElement { }: Readonly<TourPlannerMapStageProps>): ReactElement {
const [terrain3d, setTerrain3d] = useState(false); const [terrain3d, setTerrain3d] = useState(false);
// How high the 3D map's controls reach: one row, or two once the grade legend moved above the dock.
const [terrainControlsPx, setTerrainControlsPx] = useState(MAP_ROUND_CONTROL_SIZE);
// One via ferrata layer for both modes: it stays on, and keeps its card, across 2D and 3D. // One via ferrata layer for both modes: it stays on, and keeps its card, across 2D and 3D.
const ferrata = useFerrataMapLayer(tripId); // The trip's area for the layer to prefetch: the framed points and the tour's line.
const ferrataArea = useMemo(
() => [tourMap.focusPoints, ...(tourMap.route ?? [])],
[tourMap.focusPoints, tourMap.route]
);
const ferrata = useFerrataMapLayer(tripId, ferrataArea);
// The click that closes a ferrata's card must not also add a waypoint under it.
const closingPress = useOutsidePressGuard();
const { language } = useTranslation();
const distanceUnit = useSettingsStore((s) => s.settings.distance_unit); const distanceUnit = useSettingsStore((s) => s.settings.distance_unit);
const analysis = tourPlanner.mode.type === 'view-gpx' ? tourPlanner.readOnlyGpxAnalysis : tourPlanner.routeAnalysis; const analysis = tourPlanner.mode.type === 'view-gpx' ? tourPlanner.readOnlyGpxAnalysis : tourPlanner.routeAnalysis;
const editing = tourPlanner.mode.type === 'new-draft' || tourPlanner.mode.type === 'edit-saved'; const editing = tourPlanner.mode.type === 'new-draft' || tourPlanner.mode.type === 'edit-saved';
@@ -60,7 +72,7 @@ export function TourPlannerMapStage({
selectedId: editing ? tourPlanner.selectedWaypointId : null, selectedId: editing ? tourPlanner.selectedWaypointId : null,
onSelect: tourPlanner.setSelectedWaypointId, onSelect: tourPlanner.setSelectedWaypointId,
onMove: canEditRoute ? tourPlanner.setWaypointPosition : undefined, onMove: canEditRoute ? tourPlanner.setWaypointPosition : undefined,
onAdd: canEditRoute ? tourPlanner.addWaypoint : undefined, onAdd: closingPress.guard(canEditRoute ? tourPlanner.addWaypoint : undefined),
}; };
// What each panel covers of the map, a collapsed one its rail. // What each panel covers of the map, a collapsed one its rail.
const leftInset = leftHidden ? 56 : leftPanelPx; const leftInset = leftHidden ? 56 : leftPanelPx;
@@ -85,6 +97,7 @@ export function TourPlannerMapStage({
framePoints={tourMap.focusPoints} framePoints={tourMap.focusPoints}
ferrataLayer={ferrata.layer} ferrataLayer={ferrata.layer}
onViewportChange={ferrata.onViewportChange} onViewportChange={ferrata.onViewportChange}
onControlsHeightChange={setTerrainControlsPx}
/> />
</Suspense> </Suspense>
) : ( ) : (
@@ -93,12 +106,12 @@ export function TourPlannerMapStage({
places={[]} places={[]}
route={tourMap.route} route={tourMap.route}
followSelection={false} followSelection={false}
onMapClick={ onMapClick={closingPress.guard(
canEditRoute canEditRoute
? ({ latlng }: { latlng: { lat: number; lng: number } }) => ? ({ latlng }: { latlng: { lat: number; lng: number } }) =>
tourPlanner.addWaypoint(latlng.lat, latlng.lng) tourPlanner.addWaypoint(latlng.lat, latlng.lng)
: undefined : undefined
} )}
tileUrl={mapTileUrl} tileUrl={mapTileUrl}
leftWidth={leftPanelPx} leftWidth={leftPanelPx}
rightWidth={rightPanelPx} rightWidth={rightPanelPx}
@@ -116,11 +129,14 @@ export function TourPlannerMapStage({
onViewportChange={ferrata.onViewportChange} onViewportChange={ferrata.onViewportChange}
/> />
)} )}
{/* The card sits at the inline end; the panels keep their physical sides, so in a
right-to-left language that end is the left panel's. */}
<FerrataDetailOverlay <FerrataDetailOverlay
ferrata={ferrata.selected} selection={ferrata.selected}
onClose={ferrata.close} onClose={ferrata.close}
insetRight={rightInset + MAP_LAYER_SWITCHER_INSET} onOutsidePress={closingPress.arm}
bottom={FERRATA_CARD_BOTTOM} insetEnd={(isRtlLanguage(language) ? leftInset : rightInset) + MAP_LAYER_SWITCHER_INSET}
bottom={terrain3d ? CONTROL_GAP + terrainControlsPx + CONTROL_GAP : FERRATA_CARD_BOTTOM}
/> />
{/* Centred over the map between the two panels, which sit on fixed physical sides. */} {/* Centred over the map between the two panels, which sit on fixed physical sides. */}
<div <div
@@ -144,8 +160,9 @@ export function TourPlannerMapStage({
<LocationSelect <LocationSelect
value={locationSearch.value} value={locationSearch.value}
onChange={locationSearch.onChange} onChange={locationSearch.onChange}
placeholder={t('reservations.searchLocation')} placeholder={t('tours.planner.mapSearch')}
ariaLabel={t('reservations.searchLocation')} ariaLabel={t('tours.planner.mapSearch')}
floating
showSearchStatus showSearchStatus
style={{ width: '100%' }} style={{ width: '100%' }}
/> />
+68 -2
View File
@@ -1,9 +1,13 @@
// FE-REPO-FERRATA-001 to FE-REPO-FERRATA-003: the online-only read behind the via ferrata layer. // FE-REPO-FERRATA-001 to FE-REPO-FERRATA-007: the network read behind the via ferrata layer and its tiles kept on the device.
import 'fake-indexeddb/auto';
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Ferrata } from '@trek/shared';
import { ferratasApi } from '../api/ferratas'; import { ferratasApi } from '../api/ferratas';
import { clearAll, clearTripData, offlineDb } from '../db/offlineDb';
import { isEffectivelyOffline } from '../sync/networkMode'; import { isEffectivelyOffline } from '../sync/networkMode';
import { ferrataRepo } from './ferrataRepo'; import { FERRATA_TILE_FRESH_MS, ferrataRepo } from './ferrataRepo';
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => false) })); vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => false) }));
vi.mock('../api/ferratas', () => ({ ferratasApi: { list: vi.fn() } })); vi.mock('../api/ferratas', () => ({ ferratasApi: { list: vi.fn() } }));
@@ -57,3 +61,65 @@ describe('ferrataRepo.list', () => {
await expect(ferrataRepo.list(7, BBOX, new AbortController().signal)).rejects.toThrow(); await expect(ferrataRepo.list(7, BBOX, new AbortController().signal)).rejects.toThrow();
}); });
}); });
describe('ferrataRepo tiles', () => {
const NOW = 10 * FERRATA_TILE_FRESH_MS;
const FERRATA: Ferrata = {
id: 'way/42',
name: 'Via delle Trincee',
grade: { raw: '4', level: 4, plus: null },
routeName: null,
geometry: [
[46.52, 11.8],
[46.53, 11.81],
],
osmUrl: 'https://www.openstreetmap.org/way/42',
};
beforeEach(async () => {
await clearAll();
});
it('FE-REPO-FERRATA-004: keeps tiles per trip and hands back the fresh ones', async () => {
await ferrataRepo.saveTiles(7, [
{ tile: '930:236', fetchedAt: NOW - 1_000, ferratas: [FERRATA] },
{ tile: '930:237', fetchedAt: NOW - 2_000, ferratas: [] },
]);
await ferrataRepo.saveTiles(8, [{ tile: '930:236', fetchedAt: NOW, ferratas: [] }]);
const rows = await ferrataRepo.cachedTiles(7, NOW);
expect(rows.map((row) => row.tile).sort()).toEqual(['930:236', '930:237']);
expect(rows.find((row) => row.tile === '930:236')?.ferratas).toEqual([FERRATA]);
expect(await ferrataRepo.cachedTiles(8, NOW)).toHaveLength(1);
});
it('FE-REPO-FERRATA-005: a tile older than a week is dropped instead of drawn', async () => {
await ferrataRepo.saveTiles(7, [
{ tile: '930:236', fetchedAt: NOW - FERRATA_TILE_FRESH_MS - 1, ferratas: [FERRATA] },
{ tile: '930:237', fetchedAt: NOW - FERRATA_TILE_FRESH_MS + 1, ferratas: [] },
]);
expect((await ferrataRepo.cachedTiles(7, NOW)).map((row) => row.tile)).toEqual(['930:237']);
expect(await offlineDb.ferrataTiles.get([7, '930:236'])).toBeUndefined();
});
it('FE-REPO-FERRATA-006: a cached ferrata off the contract, or a row under a bad key, is left out', async () => {
await offlineDb.ferrataTiles.bulkPut([
{ tripId: 7, tile: '930:236', fetchedAt: NOW, ferratas: [FERRATA, { ...FERRATA, id: 'node/1' }] },
{ tripId: 7, tile: 'nonsense', fetchedAt: NOW, ferratas: [] },
]);
const rows = await ferrataRepo.cachedTiles(7, NOW);
expect(rows).toHaveLength(1);
expect(rows[0]?.ferratas).toEqual([FERRATA]);
});
it('FE-REPO-FERRATA-007: skips a save for another account, and the tiles go with their trip', async () => {
await ferrataRepo.saveTiles(7, [{ tile: '930:236', fetchedAt: NOW, ferratas: [] }], 'trek-offline-u999');
expect(await offlineDb.ferrataTiles.count()).toBe(0);
await ferrataRepo.saveTiles(7, [{ tile: '930:236', fetchedAt: NOW, ferratas: [] }], ferrataRepo.cacheName());
expect(await offlineDb.ferrataTiles.count()).toBe(1);
await clearTripData(7);
expect(await offlineDb.ferrataTiles.count()).toBe(0);
});
});
+70 -9
View File
@@ -1,24 +1,85 @@
import { type FerratasResponse, ferratasResponseSchema } from '@trek/shared'; import {
type Ferrata,
type FerratasResponse,
ferrataSchema,
ferratasResponseSchema,
parseFerrataTileKey,
} from '@trek/shared';
import { ferratasApi } from '../api/ferratas'; import { ferratasApi } from '../api/ferratas';
import { offlineDb } from '../db/offlineDb';
import { isEffectivelyOffline } from '../sync/networkMode'; import { isEffectivelyOffline } from '../sync/networkMode';
/** How long a loaded tile stays good on this device; past it the tile is asked for again. */
export const FERRATA_TILE_FRESH_MS = 7 * 24 * 60 * 60 * 1000;
/** One loaded tile of the layer: its key, when it was loaded and the ferratas that reach into it. */
export interface FerrataTileRow {
tile: string;
fetchedAt: number;
ferratas: Ferrata[];
}
/** The cached ferratas that still match the contract; a row an older bundle wrote off it is left out, not drawn. */
function validFerratas(rows: unknown[]): Ferrata[] {
return rows.flatMap((row) => {
const parsed = ferrataSchema.safeParse(row);
return parsed.success ? [parsed.data] : [];
});
}
/** /**
* Via ferratas from OpenStreetMap, for the map layer. * Via ferratas from OpenStreetMap, for the map layer.
* *
* **Deliberately online-only, and no Dexie table.** The answer is a live read * Read through the network per tile of the shared 0.05 degree grid, and kept
* of OpenStreetMap through Overpass for whatever area the map shows; a cached * on the device per trip in `ferrataTiles` (Dexie), so a trip's lines draw at
* copy would be stale data for an area the user has long panned away from, and * once on the next visit and offline. The copy is read-only map data, not trip
* the server keeps nothing either. Same shape and reason as * data, so there is no write queue: a tile is good for FERRATA_TILE_FRESH_MS
* `roadtripHazardsRepo` and `pluginPoiRepo`. * and then loaded again.
* *
* Offline (by circumstance or by the manual switch) the request is not made at * Offline (by circumstance or by the manual switch) no request is made at all.
* all. The answer is parsed against the shared contract rather than cast, * Every answer is parsed against the shared contract rather than cast, because
* because its names and grades go on to be drawn on the map. * its names and grades go on to be drawn on the map.
*/ */
export const ferrataRepo = { export const ferrataRepo = {
async list(tripId: number, bbox: string, signal: AbortSignal): Promise<FerratasResponse> { async list(tripId: number, bbox: string, signal: AbortSignal): Promise<FerratasResponse> {
if (isEffectivelyOffline()) throw new Error('Via ferratas need a connection'); if (isEffectivelyOffline()) throw new Error('Via ferratas need a connection');
return ferratasResponseSchema.parse(await ferratasApi.list(tripId, bbox, signal)); return ferratasResponseSchema.parse(await ferratasApi.list(tripId, bbox, signal));
}, },
/** The trip's tiles loaded within the last FERRATA_TILE_FRESH_MS; older ones are dropped on the way. */
async cachedTiles(tripId: number, now: number = Date.now()): Promise<FerrataTileRow[]> {
const rows = await offlineDb.ferrataTiles.where('tripId').equals(tripId).toArray();
const fresh: FerrataTileRow[] = [];
const stale: Array<[number, string]> = [];
for (const row of rows) {
const current = now - row.fetchedAt < FERRATA_TILE_FRESH_MS && parseFerrataTileKey(row.tile) !== null;
if (!current) stale.push([tripId, row.tile]);
else
fresh.push({
tile: row.tile,
fetchedAt: row.fetchedAt,
ferratas: validFerratas(Array.isArray(row.ferratas) ? row.ferratas : []),
});
}
if (stale.length) await offlineDb.ferrataTiles.bulkDelete(stale);
return fresh;
},
/** Which account's database the cache is in now, to hand back to `saveTiles` once an answer arrives. */
cacheName(): string {
return offlineDb.name;
},
/**
* Keeps loaded tiles for the trip. Skipped when the account changed since
* the answer was asked for, so one user's map data never lands in another's
* database.
*/
async saveTiles(tripId: number, rows: readonly FerrataTileRow[], cacheName: string = offlineDb.name): Promise<void> {
if (rows.length === 0 || offlineDb.name !== cacheName) return;
await offlineDb.ferrataTiles.bulkPut(
rows.map((row) => ({ tripId, tile: row.tile, fetchedAt: row.fetchedAt, ferratas: row.ferratas }))
);
},
}; };
@@ -53,6 +53,7 @@ const HISTORY: Record<number, StoreSpec> = {
8: { areaPlaces: '[gers+tripId], gers, tripId, searchName' }, 8: { areaPlaces: '[gers+tripId], gers, tripId, searchName' },
9: { tours: 'place_id, trip_id' }, 9: { tours: 'place_id, trip_id' },
10: { budgetSettlements: 'tripId' }, 10: { budgetSettlements: 'tripId' },
11: { ferrataTiles: '[tripId+tile], tripId, fetchedAt' },
}; };
const HISTORY_VERSIONS = Object.keys(HISTORY) const HISTORY_VERSIONS = Object.keys(HISTORY)
@@ -130,6 +131,7 @@ function rowsAt(version: number): Record<string, Record<string, unknown>> {
roadtripPreferences: { tripId: 1, preferences: { avoid_tolls: true } }, roadtripPreferences: { tripId: 1, preferences: { avoid_tolls: true } },
tours: { place_id: 21, trip_id: 1, name: 'Old town loop', distance: 4200 }, tours: { place_id: 21, trip_id: 1, name: 'Old town loop', distance: 4200 },
budgetSettlements: { tripId: 1, base: 'EUR', settlement: { balances: [], flows: [] } }, budgetSettlements: { tripId: 1, base: 'EUR', settlement: { balances: [], flows: [] } },
ferrataTiles: { tripId: 1, tile: '930:236', fetchedAt: 11, ferratas: [] },
}; };
const tables = schemaAt(version); const tables = schemaAt(version);
return Object.fromEntries(Object.entries(all).filter(([table]) => table in tables)); return Object.fromEntries(Object.entries(all).filter(([table]) => table in tables));
@@ -291,6 +293,21 @@ const UPGRADE_CASES: Record<number, UpgradeCase> = {
expect(await db.budgetSettlements.get(3)).toMatchObject({ base: 'USD' }); expect(await db.budgetSettlements.get(3)).toMatchObject({ base: 'USD' });
}, },
}, },
11: {
intent: 'adds the via ferrata tiles per trip and keeps the Costs settlement',
check: async (db) => {
expect(await db.budgetSettlements.get(1)).toMatchObject({ base: 'EUR' });
await db.table('ferrataTiles').bulkPut([
{ tripId: 3, tile: '930:236', fetchedAt: 5, ferratas: [] },
{ tripId: 3, tile: '930:237', fetchedAt: 6, ferratas: [] },
// The same tile for another trip is its own row.
{ tripId: 4, tile: '930:236', fetchedAt: 7, ferratas: [] },
]);
expect(await db.ferrataTiles.where('tripId').equals(3).count()).toBe(2);
expect(await db.ferrataTiles.get([4, '930:236'])).toMatchObject({ fetchedAt: 7 });
expect(await db.ferrataTiles.where('fetchedAt').below(6).count()).toBe(1);
},
},
}; };
describe('offlineDb upgrades', () => { describe('offlineDb upgrades', () => {
+1
View File
@@ -31,6 +31,7 @@
"DocumentProviderFields": "doc-sync", "DocumentProviderFields": "doc-sync",
"DocumentProviders": "doc-sync", "DocumentProviders": "doc-sync",
"DocumentSyncItems": "doc-sync", "DocumentSyncItems": "doc-sync",
"FerrataTiles": "ferratas",
"FileLinks": "files", "FileLinks": "files",
"GoogleApiUsage": "google-quota", "GoogleApiUsage": "google-quota",
"GooglePlacePhotoMeta": "place-photos", "GooglePlacePhotoMeta": "place-photos",
@@ -0,0 +1,20 @@
import { EntityRepositoryType, PrimaryKeyProp, defineEntity, p } from '@mikro-orm/core';
import { FerrataTilesRepository } from '../repositories/FerrataTiles.repository';
export class FerrataTiles {
[EntityRepositoryType]?: FerrataTilesRepository;
[PrimaryKeyProp]?: 'tile_key';
tile_key!: string;
payload!: string;
fetched_at!: number;
}
export const FerrataTilesSchema = defineEntity({
class: FerrataTiles,
repository: () => FerrataTilesRepository,
properties: {
tile_key: p.text().primary(),
payload: p.text(),
fetched_at: p.integer(),
},
});
+2
View File
@@ -31,6 +31,7 @@ import { DocumentConnectionsSchema } from './DocumentConnections.entity';
import { DocumentProviderFieldsSchema } from './DocumentProviderFields.entity'; import { DocumentProviderFieldsSchema } from './DocumentProviderFields.entity';
import { DocumentProvidersSchema } from './DocumentProviders.entity'; import { DocumentProvidersSchema } from './DocumentProviders.entity';
import { DocumentSyncItemsSchema } from './DocumentSyncItems.entity'; import { DocumentSyncItemsSchema } from './DocumentSyncItems.entity';
import { FerrataTilesSchema } from './FerrataTiles.entity';
import { FileLinksSchema } from './FileLinks.entity'; import { FileLinksSchema } from './FileLinks.entity';
import { GoogleApiUsageSchema } from './GoogleApiUsage.entity'; import { GoogleApiUsageSchema } from './GoogleApiUsage.entity';
import { GooglePlacePhotoMetaSchema } from './GooglePlacePhotoMeta.entity'; import { GooglePlacePhotoMetaSchema } from './GooglePlacePhotoMeta.entity';
@@ -165,6 +166,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
DocumentProviderFieldsSchema, DocumentProviderFieldsSchema,
DocumentProvidersSchema, DocumentProvidersSchema,
DocumentSyncItemsSchema, DocumentSyncItemsSchema,
FerrataTilesSchema,
FileLinksSchema, FileLinksSchema,
GoogleApiUsageSchema, GoogleApiUsageSchema,
GooglePlacePhotoMetaSchema, GooglePlacePhotoMetaSchema,
+18
View File
@@ -1,4 +1,5 @@
import type { JsonColumn } from '../utils/json-column'; import type { JsonColumn } from '../utils/json-column';
import { FERRATAS_RESULT_CAP, ferrataSchema, type Ferrata } from '@trek/shared';
import { z } from 'zod'; import { z } from 'zod';
@@ -106,3 +107,20 @@ export const BUDGET_ITEM_TICKET: JsonColumn<{ items: { price?: string | number;
.loose(), .loose(),
fallback: () => null, fallback: () => null,
}; };
/** What one cell of the via ferrata grid holds: its ferratas, and whether the Overpass answer they came from was cut. */
export interface FerrataTilePayload {
ferratas: Ferrata[];
truncated: boolean;
}
/**
* One via ferrata grid cell, as Overpass answered it. Checked against the wire
* contract, so a stored row can go out as it is. A broken row reads as a cell
* never fetched (null), and the next request asks Overpass for it again.
*/
export const FERRATA_TILE_PAYLOAD: JsonColumn<FerrataTilePayload | null> = {
column: 'ferrata_tiles.payload',
schema: z.object({ ferratas: z.array(ferrataSchema).max(FERRATAS_RESULT_CAP), truncated: z.boolean() }).nullable(),
fallback: () => null,
};
@@ -0,0 +1,8 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
export interface FerrataTilesTable {
tile_key: string;
payload: string;
fetched_at: number;
}
+2
View File
@@ -34,6 +34,7 @@ import type { DocumentConnectionsTable } from './DocumentConnections.table';
import type { DocumentProviderFieldsTable } from './DocumentProviderFields.table'; import type { DocumentProviderFieldsTable } from './DocumentProviderFields.table';
import type { DocumentProvidersTable } from './DocumentProviders.table'; import type { DocumentProvidersTable } from './DocumentProviders.table';
import type { DocumentSyncItemsTable } from './DocumentSyncItems.table'; import type { DocumentSyncItemsTable } from './DocumentSyncItems.table';
import type { FerrataTilesTable } from './FerrataTiles.table';
import type { FileLinksTable } from './FileLinks.table'; import type { FileLinksTable } from './FileLinks.table';
import type { GoogleApiUsageTable } from './GoogleApiUsage.table'; import type { GoogleApiUsageTable } from './GoogleApiUsage.table';
import type { GooglePlacePhotoMetaTable } from './GooglePlacePhotoMeta.table'; import type { GooglePlacePhotoMetaTable } from './GooglePlacePhotoMeta.table';
@@ -174,6 +175,7 @@ export interface DB {
document_provider_fields: DocumentProviderFieldsTable; document_provider_fields: DocumentProviderFieldsTable;
document_providers: DocumentProvidersTable; document_providers: DocumentProvidersTable;
document_sync_items: DocumentSyncItemsTable; document_sync_items: DocumentSyncItemsTable;
ferrata_tiles: FerrataTilesTable;
file_links: FileLinksTable; file_links: FileLinksTable;
google_api_usage: GoogleApiUsageTable; google_api_usage: GoogleApiUsageTable;
google_place_photo_meta: GooglePlacePhotoMetaTable; google_place_photo_meta: GooglePlacePhotoMetaTable;
@@ -0,0 +1,27 @@
import { Migration } from '@mikro-orm/migrations';
/**
* `ferrata_tiles` keeps what Overpass answered for one cell of the via
* ferrata grid (0.05 degrees a side), so a restart does not send every map
* pan back to the public mirrors.
*
* `tile_key` names the cell by its grid indices (`"<lat index>,<lng index>"`),
* `payload` is the JSON of the cell's ferratas and whether the answer was cut,
* and `fetched_at` is the epoch of that answer in seconds (not ms, so it fits
* the 32-bit integer this column is on Postgres). Nothing references the
* table and nothing deletes from it: an old cell is served and refreshed, not
* dropped.
*/
export class Migration20200101043200_via_ferrata_tiles_outlive_a_restart extends Migration {
override name = 'Migration20200101043200_via_ferrata_tiles_outlive_a_restart';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS ferrata_tiles (
tile_key TEXT PRIMARY KEY NOT NULL,
payload TEXT NOT NULL,
fetched_at INTEGER NOT NULL
)
`);
}
}
@@ -0,0 +1,30 @@
import type { FerrataTiles } from '../entities/FerrataTiles.entity';
import { TrekRepository } from './_shared/trek-repository';
export interface FerrataTileRow {
tile_key: string;
payload: string;
/** When Overpass answered for the cell, as an epoch in seconds. */
fetched_at: number;
}
export class FerrataTilesRepository extends TrekRepository<FerrataTiles> {
/** The stored cells among `keys`, in no particular order; a key never stored is simply absent. */
async findByKeys(keys: readonly string[]): Promise<FerrataTileRow[]> {
if (keys.length === 0) return [];
const rows = await this.find({ tile_key: { $in: [...keys] } }, { fields: ['tile_key', 'payload', 'fetched_at'] });
return rows.map((row) => ({ tile_key: row.tile_key, payload: row.payload, fetched_at: row.fetched_at }));
}
/**
* Writes every cell, replacing the payload and fetch time of one already
* stored. The caller wraps it in a transaction, so a batch lands whole.
*/
async upsertTiles(rows: readonly FerrataTileRow[]): Promise<void> {
if (rows.length === 0) return;
await this.upsertMany(
rows.map((row) => ({ ...row })),
{ onConflictFields: ['tile_key'], onConflictAction: 'merge' },
);
}
}
+109 -8
View File
@@ -1,9 +1,15 @@
import { FERRATAS_RESULT_CAP, type Ferrata, type FerrataBbox, type FerrataGrade } from '@trek/shared'; import {
FERRATAS_RESULT_CAP,
ferrataTileBlockKey,
type Ferrata,
type FerrataBbox,
type FerrataGrade,
} from '@trek/shared';
import { z } from 'zod'; import { z } from 'zod';
/** /**
* Pure pieces of the via ferrata lookup: the Overpass query, the cache key, the * Pure pieces of the via ferrata lookup: the Overpass query, the tile grid, the
* grade parser and the mapping from Overpass elements to the wire rows. Kept * grade parser and the mapping from Overpass elements to the wire rows. Kept
* free of I/O so the service and its tests share them. * free of I/O so the service and its tests share them.
*/ */
@@ -11,7 +17,7 @@ import { z } from 'zod';
/** How long Overpass itself may work on one query, in seconds (`[timeout:N]`). */ /** How long Overpass itself may work on one query, in seconds (`[timeout:N]`). */
export const FERRATA_QUERY_TIMEOUT_S = 15; export const FERRATA_QUERY_TIMEOUT_S = 15;
/** The grid the box is snapped outwards to, so nearby requests share one cache entry. */ /** The grid the box is snapped outwards to; one cell of it is the unit the answers are stored by. */
const GRID_DEG = 0.05; const GRID_DEG = 0.05;
const GRADE_RE = /^([0-6])([+-])?$/; const GRADE_RE = /^([0-6])([+-])?$/;
@@ -37,9 +43,8 @@ const snapUp = (v: number) => Math.ceil(v / GRID_DEG - 1e-9) * GRID_DEG;
const fixed = (v: number) => Number(v.toFixed(2)); const fixed = (v: number) => Number(v.toFixed(2));
/** /**
* The box grown outwards to the 0.05 degree grid and clamped to the globe. * The box grown outwards to the 0.05 degree grid and clamped to the globe,
* The query asks for the snapped box, so a cache entry always covers every * so it is made of whole cells.
* request that maps onto it.
*/ */
export function snapFerrataBbox(bbox: FerrataBbox): FerrataBbox { export function snapFerrataBbox(bbox: FerrataBbox): FerrataBbox {
return { return {
@@ -50,8 +55,104 @@ export function snapFerrataBbox(bbox: FerrataBbox): FerrataBbox {
}; };
} }
export function ferrataCacheKey(snapped: FerrataBbox): string { /**
return `${snapped.south},${snapped.west},${snapped.north},${snapped.east}`; * The grid cells a box covers, as tile keys (`"<lat index>,<lng index>"`, the
* cell's south-west corner divided by the grid), south to north and west to
* east. The box is snapped outwards first, so every cell is whole. `null` when
* the box covers more than `max` cells: the caller refuses it rather than
* walking a grid it never meant to ask for.
*/
export function ferrataTileKeysFor(bbox: FerrataBbox, max: number): string[] | null {
const snapped = snapFerrataBbox(bbox);
const lat0 = Math.round(snapped.south / GRID_DEG);
const lat1 = Math.round(snapped.north / GRID_DEG);
const lng0 = Math.round(snapped.west / GRID_DEG);
const lng1 = Math.round(snapped.east / GRID_DEG);
if ((lat1 - lat0) * (lng1 - lng0) > max) return null;
const keys: string[] = [];
for (let lat = lat0; lat < lat1; lat += 1) {
for (let lng = lng0; lng < lng1; lng += 1) keys.push(tileKey(lat, lng));
}
return keys;
}
const tileKey = (lat: number, lng: number) => `${lat},${lng}`;
function tileIndices(key: string): [number, number] {
const parts = key.split(',').map(Number);
const lat = parts[0] ?? Number.NaN;
const lng = parts[1] ?? Number.NaN;
if (parts.length !== 2 || !Number.isInteger(lat) || !Number.isInteger(lng)) {
throw new Error(`Not a ferrata tile key: ${key}`);
}
return [lat, lng];
}
/** The one box around every given cell, which is what a batch asks Overpass for. */
export function ferrataTilesBbox(keys: readonly string[]): FerrataBbox {
if (keys.length === 0) throw new Error('No ferrata tiles to bound');
let south = Infinity;
let west = Infinity;
let north = -Infinity;
let east = -Infinity;
for (const key of keys) {
const [lat, lng] = tileIndices(key);
south = Math.min(south, lat);
west = Math.min(west, lng);
north = Math.max(north, lat + 1);
east = Math.max(east, lng + 1);
}
return {
south: fixed(Math.max(-90, south * GRID_DEG)),
west: fixed(Math.max(-180, west * GRID_DEG)),
north: fixed(Math.min(90, north * GRID_DEG)),
east: fixed(Math.min(180, east * GRID_DEG)),
};
}
/**
* The cells grouped into the grid-aligned blocks of FERRATA_TILES_PER_REQUEST_SIDE
* cells a side that one Overpass query covers (the blocks the client asks in),
* each block's cells in the order given.
*/
export function ferrataTileBlocks(keys: readonly string[]): string[][] {
const blocks = new Map<string, string[]>();
for (const key of keys) {
const [lat, lng] = tileIndices(key);
const block = ferrataTileBlockKey(lat, lng);
const cells = blocks.get(block);
if (cells) cells.push(key);
else blocks.set(block, [key]);
}
return [...blocks.values()];
}
/** The grid rows (or columns) a coordinate lies in: two when it sits on the line between them. */
function gridIndices(value: number): number[] {
const steps = value / GRID_DEG;
const nearest = Math.round(steps);
return Math.abs(steps - nearest) < 1e-9 ? [nearest - 1, nearest] : [Math.floor(steps)];
}
/**
* The ferratas of one Overpass answer, handed to every cell among `keys` that
* a point of their line lies in (a point on a cell edge counts for both
* cells). A cell no ferrata touches gets an empty list, which is an answer
* too: it is stored, so the cell is not asked for again.
*/
export function splitFerratasByTile(ferratas: readonly Ferrata[], keys: ReadonlySet<string>): Map<string, Ferrata[]> {
const byTile = new Map<string, Ferrata[]>();
for (const key of keys) byTile.set(key, []);
for (const ferrata of ferratas) {
const touched = new Set<string>();
for (const [lat, lng] of ferrata.geometry) {
for (const row of gridIndices(lat)) {
for (const column of gridIndices(lng)) touched.add(tileKey(row, column));
}
}
for (const key of touched) byTile.get(key)?.push(ferrata);
}
return byTile;
} }
/** /**
+6 -2
View File
@@ -1,16 +1,20 @@
import { FerrataTiles } from '../../db/entities/FerrataTiles.entity';
import { PermissionsModule } from '../permissions/permissions.module'; import { PermissionsModule } from '../permissions/permissions.module';
import { FerratasOverpassClient } from './ferratas-overpass.client'; import { FerratasOverpassClient } from './ferratas-overpass.client';
import { FerratasController } from './ferratas.controller'; import { FerratasController } from './ferratas.controller';
import { FerratasService } from './ferratas.service'; import { FerratasService } from './ferratas.service';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
/** /**
* Via ferratas around a trip, read from OpenStreetMap through Overpass. * Via ferratas around a trip, read from OpenStreetMap through Overpass.
* PermissionsModule brings TripAccessGuard; RuntimeEnvService (the Overpass * PermissionsModule brings TripAccessGuard; RuntimeEnvService (the Overpass
* endpoints and deadline) comes from the global AppConfigModule. * endpoints and deadline) comes from the global AppConfigModule. The answers
* are kept per grid cell in `ferrata_tiles`; UnitOfWork and MikroORM come from
* the global OrmModule.
*/ */
@Module({ @Module({
imports: [PermissionsModule], imports: [PermissionsModule, MikroOrmModule.forFeature([FerrataTiles])],
controllers: [FerratasController], controllers: [FerratasController],
providers: [FerratasService, FerratasOverpassClient], providers: [FerratasService, FerratasOverpassClient],
}) })
+269 -59
View File
@@ -1,99 +1,309 @@
import { DomainError } from '../common/domain-error'; import { FerrataTiles } from '../../db/entities/FerrataTiles.entity';
import { FERRATA_TILE_PAYLOAD, type FerrataTilePayload } from '../../db/json-columns';
import type { FerrataTileRow, FerrataTilesRepository } from '../../db/repositories/FerrataTiles.repository';
import { decodeJsonResult, encodeJson, logJsonFailure } from '../../utils/json-column';
import { logWarn } from '../audit/audit-log.logger';
import { badRequest, DomainError } from '../common/domain-error';
import { withRequestContext } from '../database/request-context';
import { UnitOfWork } from '../database/unit-of-work';
import { FerratasOverpassClient } from './ferratas-overpass.client'; import { FerratasOverpassClient } from './ferratas-overpass.client';
import { buildFerrataQuery, ferrataCacheKey, snapFerrataBbox, toFerratas } from './ferratas.helpers'; import {
buildFerrataQuery,
ferrataTileBlocks,
ferrataTileKeysFor,
ferrataTilesBbox,
splitFerratasByTile,
toFerratas,
} from './ferratas.helpers';
import { MikroORM } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { FERRATAS_ATTRIBUTION, FERRATAS_UNAVAILABLE_CODE, type FerrataBbox, type FerratasResponse } from '@trek/shared'; import {
FERRATA_BBOX_TOO_LARGE_MESSAGE,
FERRATAS_ATTRIBUTION,
FERRATAS_RESULT_CAP,
FERRATAS_UNAVAILABLE_CODE,
type Ferrata,
type FerrataBbox,
type FerratasResponse,
} from '@trek/shared';
/** Via ferratas change on the scale of seasons; a day keeps Overpass out of every map pan. */ /**
export const FERRATA_CACHE_TTL_MS = 24 * 60 * 60 * 1000; * Via ferratas change on the scale of seasons. A cell older than this is still
* served at once, and asked for again in the background.
*/
export const FERRATA_TILE_STALE_MS = 30 * 24 * 60 * 60 * 1000;
/** The most boxes held at once; the oldest goes first, so roaming the globe cannot grow the map without bound. */ /** The most cells held in memory in front of the table; the least recently used goes first. */
export const FERRATA_CACHE_MAX = 300; const FERRATA_TILE_MEMORY_MAX = 2000;
/**
* The most cells one request may cover. The contract's span cap already keeps
* a box to 11 by 11 cells; this holds the service to a bound of its own.
*/
const FERRATA_TILES_MAX_PER_REQUEST = 200;
/**
* The most Overpass queries this process runs at once. A box is queried in
* blocks of 0.1 degree, so a wide one turns into several queries; they wait
* their turn here rather than all hitting the public mirrors together.
*/
const FERRATA_QUERIES_AT_ONCE = 2;
/** `fetched_at` is kept in seconds, so the epoch fits a 32-bit integer column on Postgres too. */
const MS_PER_S = 1000;
/**
* `fetched_at` of a cell whose answer was cut at the cap: the oldest possible,
* so a restart serves it and asks again rather than keeping a partial cell for
* FERRATA_TILE_STALE_MS. This process keeps it for what it is meanwhile.
*/
const FERRATA_CUT_TILE_FETCHED_AT = 0;
export const FERRATAS_UNAVAILABLE_MESSAGE = 'Via ferrata data is unavailable right now'; export const FERRATAS_UNAVAILABLE_MESSAGE = 'Via ferrata data is unavailable right now';
interface CacheEntry { interface StoredTile {
at: number; fetchedAt: number;
value: FerratasResponse; payload: FerrataTilePayload;
} }
type TileBatch = Promise<ReadonlyMap<string, StoredTile>>;
/** /**
* The via ferratas inside a box, from OpenStreetMap. * The via ferratas inside a box, from OpenStreetMap.
* *
* Read-only and per process: answers are cached by the box snapped to a 0.05 * The unit is one cell of the 0.05 degree grid. A box is resolved into its
* degree grid, and concurrent requests for the same snapped box share one * cells, each served from `ferrata_tiles` (with a small LRU in front), and
* Overpass call. A failure is never cached, so the next request tries again; * Overpass is asked only for the cells never fetched: one query per
* it reaches the client as a 502 `{ error, code }` it can show, never as a * grid-aligned block of FERRATA_TILES_PER_REQUEST_SIDE cells a side (0.1
* 500 with a stack. * degree), at most FERRATA_QUERIES_AT_ONCE at a time, each answer split back
* per cell and stored. A block whose answer was cut at the cap is asked again
* cell by cell, and a cell still cut on its own is kept but stored as old, so
* it is not served as complete for a month. A cell older than
* FERRATA_TILE_STALE_MS is served as it is and refreshed in the background,
* in queries of its own, so a refresh can never fail the request that saw
* it; nothing is deleted. Concurrent requests for a cell share one call.
*
* A request fails only when a cell it needs has never been fetched and
* Overpass fails too: the client gets a 502 `{ error, code }` it can show,
* never a 500 with a stack. A failed background refresh is logged and leaves
* the old cell in place.
*/ */
@Injectable() @Injectable()
export class FerratasService { export class FerratasService {
private readonly cache = new Map<string, CacheEntry>(); private readonly memory = new Map<string, StoredTile>();
private readonly pending = new Map<string, Promise<FerratasResponse>>(); private readonly pending = new Map<string, TileBatch>();
/** Overpass queries running now, and the ones waiting for a turn. */
private running = 0;
private readonly waiting: Array<() => void> = [];
constructor(private readonly overpass: FerratasOverpassClient) {} constructor(
private readonly overpass: FerratasOverpassClient,
@InjectRepository(FerrataTiles) private readonly tiles: FerrataTilesRepository,
private readonly uow: UnitOfWork,
private readonly orm: MikroORM,
) {}
/** The via ferratas that reach into `bbox`; the cache holds the wider snapped box. */ /** The via ferratas that reach into `bbox`, gathered from the cells it covers. */
async list(bbox: FerrataBbox, now: number = Date.now()): Promise<FerratasResponse> { async list(bbox: FerrataBbox, now: number = Date.now()): Promise<FerratasResponse> {
return withinBbox(await this.listSnapped(bbox, now), bbox); const keys = ferrataTileKeysFor(bbox, FERRATA_TILES_MAX_PER_REQUEST);
if (keys === null) throw badRequest(FERRATA_BBOX_TOO_LARGE_MESSAGE);
const tiles = new Map(await this.stored(keys));
const stale = keys.filter((key) => {
const tile = tiles.get(key);
return tile !== undefined && now - tile.fetchedAt >= FERRATA_TILE_STALE_MS && !this.pending.has(key);
});
const missing = keys.filter((key) => !tiles.has(key));
if (stale.length > 0) this.refreshInBackground(stale, now);
if (missing.length > 0) {
for (const [key, tile] of await this.fetchMissing(missing, now)) tiles.set(key, tile);
}
return gather(keys, tiles, bbox);
} }
private async listSnapped(bbox: FerrataBbox, now: number): Promise<FerratasResponse> { /** The cells among `keys` that were ever fetched: memory first, the table for the rest. */
const snapped = snapFerrataBbox(bbox); private async stored(keys: readonly string[]): Promise<Map<string, StoredTile>> {
const key = ferrataCacheKey(snapped); const found = new Map<string, StoredTile>();
const hit = this.cache.get(key); const unread: string[] = [];
if (hit && now - hit.at < FERRATA_CACHE_TTL_MS) return hit.value; for (const key of keys) {
if (hit) this.cache.delete(key); const hit = this.memory.get(key);
if (hit) {
this.remember(key, hit);
found.set(key, hit);
} else {
unread.push(key);
}
}
for (const row of await this.tiles.findByKeys(unread)) {
const decoded = decodeJsonResult(FERRATA_TILE_PAYLOAD, row.payload);
if (!decoded.ok || decoded.value === null) {
logJsonFailure(FERRATA_TILE_PAYLOAD, decoded.ok ? 'schema' : decoded.reason, row.tile_key);
continue;
}
const tile = { fetchedAt: row.fetched_at * MS_PER_S, payload: decoded.value };
this.remember(row.tile_key, tile);
found.set(row.tile_key, tile);
}
return found;
}
const inFlight = this.pending.get(key); /**
if (inFlight !== undefined) return inFlight; * The cells never fetched, awaited. A cell another request is already
// Memoised in-flight load, not a missing await: concurrent callers join the * fetching joins that batch; the rest go out one batch per block.
// promise above, and this frame awaits it below. */
const load = this.load(snapped); private async fetchMissing(missing: readonly string[], now: number): Promise<Map<string, StoredTile>> {
this.pending.set(key, load); const tiles = new Map<string, StoredTile>();
try { const batches = new Set<TileBatch>();
const value = await load; const own: string[] = [];
this.remember(key, { at: now, value }); for (const key of missing) {
return value; const hit = this.memory.get(key);
} finally { const inFlight = this.pending.get(key);
this.pending.delete(key); if (hit) tiles.set(key, hit);
else if (inFlight !== undefined) batches.add(inFlight);
else own.push(key);
}
for (const block of ferrataTileBlocks(own)) batches.add(this.batch(block, now));
for (const answer of await Promise.all(batches)) {
for (const [key, tile] of answer) tiles.set(key, tile);
}
return tiles;
}
/** Refreshes old cells without holding up the request, a batch per block; a failure only costs the refresh. */
private refreshInBackground(keys: readonly string[], now: number): void {
for (const block of ferrataTileBlocks(keys)) {
this.batch(block, now).catch((err: unknown) => {
const reason = err instanceof Error ? err.message : String(err);
logWarn(`[Ferratas] background refresh of ${block.length} tile(s) failed: ${reason}`);
});
} }
} }
private async load(snapped: FerrataBbox): Promise<FerratasResponse> { /**
let elements: unknown[]; * One Overpass query for `keys`, shared with every request that needs one of
* them until it settles. It runs in a request context of its own: it can
* outlive the request that started it.
*/
private batch(keys: readonly string[], now: number): TileBatch {
const run = withRequestContext(this.orm, () => this.fetchAndStore(keys, now));
for (const key of keys) this.pending.set(key, run);
const settle = () => {
for (const key of keys) {
if (this.pending.get(key) === run) this.pending.delete(key);
}
};
void run.then(settle, settle);
return run;
}
/**
* Asks Overpass for `keys` and stores the answer per cell. An answer cut at
* the cap is not stored for a block: its cells are asked again one by one,
* which is the most a cut answer can be narrowed. A single cell still cut is
* kept in memory as it is, and stored as old, so the next start refreshes it.
*/
private async fetchAndStore(keys: readonly string[], now: number): Promise<Map<string, StoredTile>> {
const elements = await this.queryOverpass(keys);
const { ferratas, truncated } = toFerratas(elements);
if (truncated && keys.length > 1) {
const tiles = new Map<string, StoredTile>();
const answers = await Promise.all(keys.map((key) => this.fetchAndStore([key], now)));
for (const answer of answers) for (const [key, tile] of answer) tiles.set(key, tile);
return tiles;
}
const byTile = splitFerratasByTile(ferratas, new Set(keys));
const storedAt = truncated ? FERRATA_CUT_TILE_FETCHED_AT : Math.floor(now / MS_PER_S);
const tiles = new Map<string, StoredTile>();
const rows: FerrataTileRow[] = [];
for (const key of keys) {
const payload: FerrataTilePayload = { ferratas: byTile.get(key) ?? [], truncated };
tiles.set(key, { fetchedAt: now, payload });
rows.push({ tile_key: key, payload: encodeJson(FERRATA_TILE_PAYLOAD, payload), fetched_at: storedAt });
}
try { try {
elements = await this.overpass.query(buildFerrataQuery(snapped)); await this.uow.transactional(() => this.tiles.upsertTiles(rows));
} catch (err) {
// The answer is still good for this process; the next start asks again.
const reason = err instanceof Error ? err.message : String(err);
logWarn(`[Ferratas] could not store ${rows.length} tile(s): ${reason}`);
}
for (const [key, tile] of tiles) this.remember(key, tile);
return tiles;
}
/** One Overpass query for the box around `keys`, once a turn is free; a failure becomes the 502 the client reads. */
private async queryOverpass(keys: readonly string[]): Promise<unknown[]> {
await this.turn();
try {
return await this.overpass.query(buildFerrataQuery(ferrataTilesBbox(keys)));
} catch { } catch {
// The client already logged why each mirror failed. // The client already logged why each mirror failed.
throw new DomainError(502, FERRATAS_UNAVAILABLE_MESSAGE, { throw new DomainError(502, FERRATAS_UNAVAILABLE_MESSAGE, {
code: FERRATAS_UNAVAILABLE_CODE, code: FERRATAS_UNAVAILABLE_CODE,
details: { code: FERRATAS_UNAVAILABLE_CODE }, details: { code: FERRATAS_UNAVAILABLE_CODE },
}); });
} finally {
// The turn passes straight to the next query waiting, or is given back.
const next = this.waiting.shift();
if (next) next();
else this.running -= 1;
} }
const { ferratas, truncated } = toFerratas(elements);
return { ferratas, truncated, source: 'openstreetmap', attribution: FERRATAS_ATTRIBUTION };
} }
private remember(key: string, entry: CacheEntry): void { /** Resolves once the caller holds one of FERRATA_QUERIES_AT_ONCE turns, handed over by a query that finished. */
this.cache.delete(key); private async turn(): Promise<void> {
while (this.cache.size >= FERRATA_CACHE_MAX) { if (this.running < FERRATA_QUERIES_AT_ONCE) {
const oldest = this.cache.keys().next(); this.running += 1;
if (oldest.done) break; return;
this.cache.delete(oldest.value);
} }
this.cache.set(key, entry); await new Promise<void>((resolve) => this.waiting.push(resolve));
}
private remember(key: string, tile: StoredTile): void {
this.memory.delete(key);
while (this.memory.size >= FERRATA_TILE_MEMORY_MAX) {
const oldest = this.memory.keys().next();
if (oldest.done) break;
this.memory.delete(oldest.value);
}
this.memory.set(key, tile);
} }
} }
/** A ferrata belongs to the box when any point of its line lies inside it. */ /** A ferrata belongs to the box when any point of its line lies inside it. */
function withinBbox(response: FerratasResponse, bbox: FerrataBbox): FerratasResponse { function reachesInto(ferrata: Ferrata, bbox: FerrataBbox): boolean {
const inside = ([lat, lng]: readonly unknown[]) => return ferrata.geometry.some(
typeof lat === 'number' && ([lat, lng]) => lat >= bbox.south && lat <= bbox.north && lng >= bbox.west && lng <= bbox.east,
typeof lng === 'number' && );
lat >= bbox.south && }
lat <= bbox.north &&
lng >= bbox.west && /**
lng <= bbox.east; * The answer out of the cells: every ferrata once, however many cells it
return { ...response, ferratas: response.ferratas.filter((ferrata) => ferrata.geometry.some(inside)) }; * crosses, kept when it reaches into the asked box, cut at the contract's cap.
* The answer is marked cut when any cell came from a cut Overpass answer.
*/
function gather(keys: readonly string[], tiles: ReadonlyMap<string, StoredTile>, bbox: FerrataBbox): FerratasResponse {
const seen = new Set<string>();
const ferratas: Ferrata[] = [];
let truncated = false;
for (const key of keys) {
const tile = tiles.get(key);
if (!tile) continue;
truncated ||= tile.payload.truncated;
for (const ferrata of tile.payload.ferratas) {
if (seen.has(ferrata.id)) continue;
seen.add(ferrata.id);
if (reachesInto(ferrata, bbox)) ferratas.push(ferrata);
}
}
return {
ferratas: ferratas.slice(0, FERRATAS_RESULT_CAP),
truncated: truncated || ferratas.length > FERRATAS_RESULT_CAP,
source: 'openstreetmap',
attribution: FERRATAS_ATTRIBUTION,
};
} }
+16
View File
@@ -3,10 +3,12 @@
* TripAccessGuard over a migrated snapshot db. Only Overpass is faked, by stubbing * TripAccessGuard over a migrated snapshot db. Only Overpass is faked, by stubbing
* the global fetch; the response contract is checked on every 200 (NODE_ENV=test). * the global fetch; the response contract is checked on every 200 (NODE_ENV=test).
*/ */
import { FerrataTiles } from '../../src/db/entities/FerrataTiles.entity';
import { AppConfigModule } from '../../src/nest/app-config/app-config.module'; import { AppConfigModule } from '../../src/nest/app-config/app-config.module';
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter'; import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
import { FerratasModule } from '../../src/nest/ferratas/ferratas.module'; import { FerratasModule } from '../../src/nest/ferratas/ferratas.module';
import { createSnapshotTestDb } from '../helpers/db-mock'; import { createSnapshotTestDb } from '../helpers/db-mock';
import { countRows } from '../helpers/factories/rows';
import { makeTrip } from '../helpers/factories/trips'; import { makeTrip } from '../helpers/factories/trips';
import { makeUser } from '../helpers/factories/users'; import { makeUser } from '../helpers/factories/users';
import { createTestMikroOrmModule } from '../helpers/test-orm'; import { createTestMikroOrmModule } from '../helpers/test-orm';
@@ -159,6 +161,20 @@ describe('Ferratas e2e (real guards + snapshot SQLite)', () => {
}); });
}); });
it('200 from the stored cells for the same box, without asking Overpass again', async () => {
const fetcher = vi.fn();
vi.stubGlobal('fetch', fetcher);
const res = await request(server)
.get(url())
.set('Cookie', sessionCookie(ownerId))
.query({ bbox: '46.5,12.05,46.6,12.2' });
expect(res.status).toBe(200);
expect(res.body.ferratas.map((f: { id: string }) => f.id)).toEqual(['way/4242']);
expect(fetcher).not.toHaveBeenCalled();
// 46.5 to 46.6 by 12.05 to 12.2: two rows of three cells.
expect(await countRows(app.get(MikroORM), FerrataTiles)).toBe(6);
});
it('502 { error, code } when no Overpass mirror answers', async () => { it('502 { error, code } when no Overpass mirror answers', async () => {
vi.spyOn(console, 'warn').mockImplementation(() => {}); vi.spyOn(console, 'warn').mockImplementation(() => {});
vi.stubGlobal( vi.stubGlobal(
+5 -4
View File
@@ -30,19 +30,20 @@ describe('ALL_ENTITIES', () => {
}); });
/** /**
* Plan 2 Task 3: every one of the 132 entities the migrated schema * Plan 2 Task 3: every one of the 133 entities the migrated schema
* describes (120 rewritten + the 5 document-sync tables added this task, * describes (120 rewritten + the 5 document-sync tables added this task,
* + push_subscriptions and google_api_usage from legacy steps 245 and 254, * + push_subscriptions and google_api_usage from legacy steps 245 and 254,
* + tour_types, tours and tour_waypoints from the Tours migration, * + tour_types, tours and tour_waypoints from the Tours migration,
* + scheduler_leases from the cron lease migration * + scheduler_leases from the cron lease migration
* + user_sessions, the session rows behind revocable session tokens) * + user_sessions, the session rows behind revocable session tokens
* + ferrata_tiles, the stored via ferrata grid cells)
* is registered, and no two share a class name (a silent duplicate would * is registered, and no two share a class name (a silent duplicate would
* shadow one of them in `orm.getMetadata()`, exactly the kind of drift * shadow one of them in `orm.getMetadata()`, exactly the kind of drift
* `entity-schema-parity.test.ts` can't see — it iterates the metadata map, * `entity-schema-parity.test.ts` can't see — it iterates the metadata map,
* which de-duplicates by class name). * which de-duplicates by class name).
*/ */
it('ENT-003: exactly 132 entities, every class name unique', () => { it('ENT-003: exactly 133 entities, every class name unique', () => {
expect(ALL_ENTITIES.length).toBe(132); expect(ALL_ENTITIES.length).toBe(133);
const names = ALL_ENTITIES.map((s) => s.meta.className); const names = ALL_ENTITIES.map((s) => s.meta.className);
expect(new Set(names).size).toBe(names.length); expect(new Set(names).size).toBe(names.length);
}); });
@@ -15,7 +15,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
* `scripts/generate-entities.ts`'s `RULE1d_fixNaNNumericDefaults` and * `scripts/generate-entities.ts`'s `RULE1d_fixNaNNumericDefaults` and
* `tests/unit/db/generate-entities.test.ts`'s `RULE1D-*` fixture tests for * `tests/unit/db/generate-entities.test.ts`'s `RULE1D-*` fixture tests for
* the mechanism). This file pins the OUTPUT of that rule against the real, * the mechanism). This file pins the OUTPUT of that rule against the real,
* committed entities (every one of the 132, not a sample) and reproduces * committed entities (every one of the 133, not a sample) and reproduces
* the reviewer's own insert probe so a future regenerate/hand-edit that * the reviewer's own insert probe so a future regenerate/hand-edit that
* reintroduces a `NaN` initialiser fails loudly here, not silently at a * reintroduces a `NaN` initialiser fails loudly here, not silently at a
* caller's first `em.create()`. * caller's first `em.create()`.
@@ -28,7 +28,7 @@ import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const KNOWN_UNINITIALISED_NOT_NULL_SCALARS = 446; const KNOWN_UNINITIALISED_NOT_NULL_SCALARS = 446;
describe('entity class fields never carry a NaN initialiser (task-4-review-shape.md, Important 1)', () => { describe('entity class fields never carry a NaN initialiser (task-4-review-shape.md, Important 1)', () => {
it('CLASSFIELD-001: new X() has no own property whose value is NaN, across all 132 entities', () => { it('CLASSFIELD-001: new X() has no own property whose value is NaN, across all 133 entities', () => {
const failures: string[] = []; const failures: string[] = [];
for (const schema of ALL_ENTITIES) { for (const schema of ALL_ENTITIES) {
const Ctor = schema.class as new () => Record<string, unknown>; const Ctor = schema.class as new () => Record<string, unknown>;
@@ -0,0 +1,67 @@
/**
* `Migration20200101043200_via_ferrata_tiles_outlive_a_restart`: the table
* that keeps the via ferrata grid cells exists only from this step on, keyed
* by the cell, with every column required.
*/
import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step';
import type { MikroORM } from '@mikro-orm/sqlite';
import { describe, expect, it } from 'vitest';
const TARGET = 'Migration20200101043200_via_ferrata_tiles_outlive_a_restart';
async function ormBeforeTarget(): Promise<MikroORM> {
const orm = await createMigrationOrm();
const names = await pendingNames(orm);
const index = names.indexOf(TARGET);
expect(index).toBeGreaterThan(0);
await migrateTo(orm, names[index - 1]);
return orm;
}
const tables = (orm: MikroORM) =>
rawQuery<{ name: string }>(orm, "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'ferrata_tiles'");
describe('Via ferrata tiles migration', () => {
it('FERTILEMIG-001: creates ferrata_tiles with a required key, payload and fetch time', async () => {
const orm = await ormBeforeTarget();
try {
expect(await tables(orm)).toEqual([]);
await migrateTo(orm, TARGET);
expect(await tables(orm)).toEqual([{ name: 'ferrata_tiles' }]);
const columns = await rawQuery<{ name: string; type: string; notnull: number; pk: number }>(
orm,
'SELECT name, type, "notnull", pk FROM pragma_table_info(\'ferrata_tiles\') ORDER BY cid',
);
expect(columns).toEqual([
{ name: 'tile_key', type: 'TEXT', notnull: 1, pk: 1 },
{ name: 'payload', type: 'TEXT', notnull: 1, pk: 0 },
{ name: 'fetched_at', type: 'INTEGER', notnull: 1, pk: 0 },
]);
} finally {
await orm.close(true);
}
}, 30000);
it('FERTILEMIG-002: holds one row per cell and refuses a row without a payload', async () => {
const orm = await ormBeforeTarget();
try {
await migrateTo(orm, TARGET);
await rawExec(orm, "INSERT INTO ferrata_tiles (tile_key, payload, fetched_at) VALUES ('942,224', '{}', 1)");
await expect(
rawExec(orm, "INSERT INTO ferrata_tiles (tile_key, payload, fetched_at) VALUES ('942,224', '{}', 2)"),
).rejects.toThrow(/UNIQUE|PRIMARY KEY/);
await expect(
rawExec(orm, "INSERT INTO ferrata_tiles (tile_key, payload, fetched_at) VALUES ('942,225', NULL, 2)"),
).rejects.toThrow(/NOT NULL/);
await expect(
rawExec(orm, "INSERT INTO ferrata_tiles (tile_key, payload, fetched_at) VALUES (NULL, '{}', 2)"),
).rejects.toThrow(/NOT NULL/);
} finally {
await orm.close(true);
}
}, 30000);
});
@@ -14,6 +14,7 @@ describe('known migrations', () => {
expect(known.has('Migration20200101042900_tour_planned_total_duration')).toBe(true); expect(known.has('Migration20200101042900_tour_planned_total_duration')).toBe(true);
expect(known.has('Migration20200101043000_tour_break_additional_duration')).toBe(true); expect(known.has('Migration20200101043000_tour_break_additional_duration')).toBe(true);
expect(known.has('Migration20200101043100_an_imported_tour_has_no_hiking_difficulty')).toBe(true); expect(known.has('Migration20200101043100_an_imported_tour_has_no_hiking_difficulty')).toBe(true);
expect(known.has('Migration20200101043200_via_ferrata_tiles_outlive_a_restart')).toBe(true);
expect(known.size).toBe(fs.readdirSync(path.join(__dirname, '../../../src/db/migrations')).length); expect(known.size).toBe(fs.readdirSync(path.join(__dirname, '../../../src/db/migrations')).length);
}); });
@@ -43,6 +43,7 @@ const UNNUMBERED = [
'Migration20200101042900_tour_planned_total_duration', 'Migration20200101042900_tour_planned_total_duration',
'Migration20200101043000_tour_break_additional_duration', 'Migration20200101043000_tour_break_additional_duration',
'Migration20200101043100_an_imported_tour_has_no_hiking_difficulty', 'Migration20200101043100_an_imported_tour_has_no_hiking_difficulty',
'Migration20200101043200_via_ferrata_tiles_outlive_a_restart',
]; ];
// The last step the positional runner ever had; planLegacyBaseline refuses a // The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it. // schema_version past it.
@@ -0,0 +1,98 @@
/**
* FerrataTilesRepository: the cells of the via ferrata grid that
* FerratasService keeps between restarts, read by key and written as a batch.
*/
import { FerrataTiles } from '../../../../src/db/entities/FerrataTiles.entity';
import type { FerrataTilesRepository } from '../../../../src/db/repositories/FerrataTiles.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { findRow, findRows, insertRows } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let tiles: FerrataTilesRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
tiles = t.repo(FerrataTiles);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
const empty = '{"ferratas":[],"truncated":false}';
describe('FerrataTilesRepository.findByKeys', () => {
it('FERTILEREPO-001: answers the stored cells among the keys and leaves the others out', async () => {
await insertRows(t, FerrataTiles, [
{ tile_key: '942,224', payload: empty, fetched_at: 100 },
{ tile_key: '942,225', payload: '{"ferratas":[],"truncated":true}', fetched_at: 200 },
{ tile_key: '950,230', payload: empty, fetched_at: 300 },
]);
const rows = await tiles.findByKeys(['942,224', '942,225', '943,224']);
expect(rows.sort((a, b) => a.tile_key.localeCompare(b.tile_key))).toEqual([
{ tile_key: '942,224', payload: empty, fetched_at: 100 },
{ tile_key: '942,225', payload: '{"ferratas":[],"truncated":true}', fetched_at: 200 },
]);
});
it('FERTILEREPO-002: no keys asks for nothing', async () => {
await insertRows(t, FerrataTiles, [{ tile_key: '942,224', payload: empty, fetched_at: 100 }]);
expect(await tiles.findByKeys([])).toEqual([]);
});
it('FERTILEREPO-003: a cell on the other side of the globe keeps its negative key', async () => {
await insertRows(t, FerrataTiles, [{ tile_key: '-1800,-3600', payload: empty, fetched_at: 5 }]);
expect(await tiles.findByKeys(['-1800,-3600'])).toEqual([
{ tile_key: '-1800,-3600', payload: empty, fetched_at: 5 },
]);
});
});
describe('FerrataTilesRepository.upsertTiles', () => {
it('FERTILEREPO-004: writes a row per new cell', async () => {
await tiles.upsertTiles([
{ tile_key: '942,224', payload: empty, fetched_at: 100 },
{ tile_key: '942,225', payload: empty, fetched_at: 100 },
]);
const rows = await findRows(t, FerrataTiles, {}, { tile_key: 'asc' });
expect(rows).toEqual([
{ tile_key: '942,224', payload: empty, fetched_at: 100 },
{ tile_key: '942,225', payload: empty, fetched_at: 100 },
]);
});
it('FERTILEREPO-005: a cell stored before gets the new payload and fetch time, still as one row', async () => {
await insertRows(t, FerrataTiles, [{ tile_key: '942,224', payload: empty, fetched_at: 100 }]);
const refreshed = '{"ferratas":[],"truncated":true}';
await tiles.upsertTiles([
{ tile_key: '942,224', payload: refreshed, fetched_at: 900 },
{ tile_key: '942,225', payload: empty, fetched_at: 900 },
]);
expect(await findRows(t, FerrataTiles, { tile_key: '942,224' })).toHaveLength(1);
expect(await findRow(t, FerrataTiles, { tile_key: '942,224' })).toEqual({
tile_key: '942,224',
payload: refreshed,
fetched_at: 900,
});
expect(await findRow(t, FerrataTiles, { tile_key: '942,225' })).toMatchObject({ fetched_at: 900 });
});
it('FERTILEREPO-006: an empty batch writes nothing', async () => {
await tiles.upsertTiles([]);
expect(await findRows(t, FerrataTiles)).toEqual([]);
});
});
+118 -8
View File
@@ -1,13 +1,16 @@
import { import {
FERRATA_QUERY_TIMEOUT_S, FERRATA_QUERY_TIMEOUT_S,
buildFerrataQuery, buildFerrataQuery,
ferrataCacheKey, ferrataTileBlocks,
ferrataTileKeysFor,
ferrataTilesBbox,
overpassFerrataAnswerSchema, overpassFerrataAnswerSchema,
parseFerrataGrade, parseFerrataGrade,
snapFerrataBbox, snapFerrataBbox,
splitFerratasByTile,
toFerratas, toFerratas,
} from '../../../src/nest/ferratas/ferratas.helpers'; } from '../../../src/nest/ferratas/ferratas.helpers';
import { FERRATAS_RESULT_CAP } from '@trek/shared'; import { FERRATAS_RESULT_CAP, type Ferrata } from '@trek/shared';
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
@@ -48,7 +51,7 @@ describe('parseFerrataGrade', () => {
}); });
}); });
describe('snapFerrataBbox and ferrataCacheKey', () => { describe('snapFerrataBbox', () => {
it('grows the box outwards to the 0.05 degree grid', () => { it('grows the box outwards to the 0.05 degree grid', () => {
expect(snapFerrataBbox({ south: 47.12, west: 11.21, north: 47.38, east: 11.57 })).toEqual({ expect(snapFerrataBbox({ south: 47.12, west: 11.21, north: 47.38, east: 11.57 })).toEqual({
south: 47.1, south: 47.1,
@@ -71,12 +74,119 @@ describe('snapFerrataBbox and ferrataCacheKey', () => {
east: -179.9, east: -179.9,
}); });
}); });
});
it('gives two nearby requests the same key', () => { describe('ferrataTileKeysFor', () => {
const a = ferrataCacheKey(snapFerrataBbox({ south: 47.11, west: 11.21, north: 47.39, east: 11.59 })); it('names every whole cell the snapped box covers, south to north and west to east', () => {
const b = ferrataCacheKey(snapFerrataBbox({ south: 47.12, west: 11.22, north: 47.38, east: 11.58 })); expect(ferrataTileKeysFor({ south: 47.12, west: 11.21, north: 47.18, east: 11.29 }, 200)).toEqual([
expect(a).toBe(b); '942,224',
expect(a).toBe('47.1,11.2,47.4,11.6'); '942,225',
'943,224',
'943,225',
]);
});
it('gives two nearby requests the same cells', () => {
const a = ferrataTileKeysFor({ south: 47.11, west: 11.21, north: 47.39, east: 11.59 }, 200);
const b = ferrataTileKeysFor({ south: 47.12, west: 11.22, north: 47.38, east: 11.58 }, 200);
expect(a).toEqual(b);
expect(a).toHaveLength(6 * 8);
});
it('a box on the grid covers exactly its own cells', () => {
expect(ferrataTileKeysFor({ south: 47.1, west: 11.2, north: 47.15, east: 11.25 }, 200)).toEqual(['942,224']);
});
it('keys cells south of the equator and west of Greenwich by negative indices', () => {
expect(ferrataTileKeysFor({ south: -0.04, west: -0.04, north: 0.04, east: 0.04 }, 200)).toEqual([
'-1,-1',
'-1,0',
'0,-1',
'0,0',
]);
});
it('answers null for a box over the cell cap instead of walking it', () => {
expect(ferrataTileKeysFor({ south: 47, west: 11, north: 48, east: 12 }, 200)).toBeNull();
expect(ferrataTileKeysFor({ south: 47, west: 11, north: 47.5, east: 11.5 }, 100)).toHaveLength(100);
expect(ferrataTileKeysFor({ south: 47, west: 11, north: 47.5, east: 11.5 }, 99)).toBeNull();
});
});
describe('ferrataTileBlocks', () => {
it('groups cells into the grid-aligned blocks of two by two that one query covers', () => {
expect(ferrataTileBlocks(['942,224', '942,225', '943,224', '944,224', '942,226', '-1,-1', '-2,-1'])).toEqual([
['942,224', '942,225', '943,224'],
['944,224'],
['942,226'],
['-1,-1', '-2,-1'],
]);
expect(ferrataTileBlocks([])).toEqual([]);
});
it('refuses a key that is not a cell', () => {
expect(() => ferrataTileBlocks(['nope'])).toThrow(/tile key/);
});
});
describe('ferrataTilesBbox', () => {
it('bounds one cell by its own edges', () => {
expect(ferrataTilesBbox(['942,224'])).toEqual({ south: 47.1, west: 11.2, north: 47.15, east: 11.25 });
});
it('bounds scattered cells by one box around all of them', () => {
expect(ferrataTilesBbox(['942,230', '945,224', '-1,-1'])).toEqual({
south: -0.05,
west: -0.05,
north: 47.3,
east: 11.55,
});
});
it('refuses no cells and a key that is not a cell', () => {
expect(() => ferrataTilesBbox([])).toThrow();
expect(() => ferrataTilesBbox(['947.1,11'])).toThrow(/tile key/);
expect(() => ferrataTilesBbox(['1,2,3'])).toThrow(/tile key/);
});
});
describe('splitFerratasByTile', () => {
const ferrata = (id: string, geometry: [number, number][]): Ferrata => ({
id,
name: null,
grade: { raw: null, level: null, plus: null },
routeName: null,
geometry,
osmUrl: `https://www.openstreetmap.org/${id}`,
});
it('hands a ferrata to every asked cell a point of its line lies in', () => {
const crossing = ferrata('way/1', [
[47.12, 11.21],
[47.17, 11.21],
]);
const byTile = splitFerratasByTile([crossing], new Set(['942,224', '943,224', '942,225']));
expect(byTile.get('942,224')).toEqual([crossing]);
expect(byTile.get('943,224')).toEqual([crossing]);
expect(byTile.get('942,225')).toEqual([]);
});
it('counts a point on a cell edge for the cells on both sides', () => {
const onCorner = ferrata('way/2', [
[47.15, 11.25],
[47.15, 11.25],
]);
const byTile = splitFerratasByTile([onCorner], new Set(['942,224', '942,225', '943,224', '943,225']));
expect([...byTile.values()].every((list) => list.length === 1)).toBe(true);
});
it('gives every asked cell a list, empty where nothing lies, and leaves cells it was not asked about out', () => {
const elsewhere = ferrata('way/3', [
[46.51, 12.06],
[46.52, 12.07],
]);
const byTile = splitFerratasByTile([elsewhere], new Set(['942,224']));
expect([...byTile.entries()]).toEqual([['942,224', []]]);
}); });
}); });
+395 -58
View File
@@ -1,117 +1,454 @@
/**
* FerratasService over a real snapshot db: the box resolved into grid cells,
* the cells served from `ferrata_tiles` (and the memory in front of it), and
* Overpass, faked, asked once per 0.1 degree block for the cells never fetched.
*/
import { FerrataTiles } from '../../../src/db/entities/FerrataTiles.entity';
import { FerrataTilesRepository } from '../../../src/db/repositories/FerrataTiles.repository';
import { DomainError } from '../../../src/nest/common/domain-error'; import { DomainError } from '../../../src/nest/common/domain-error';
import { FerratasOverpassClient } from '../../../src/nest/ferratas/ferratas-overpass.client'; import { FerratasOverpassClient } from '../../../src/nest/ferratas/ferratas-overpass.client';
import { FerratasModule } from '../../../src/nest/ferratas/ferratas.module';
import { import {
FERRATA_CACHE_MAX, FERRATA_TILE_STALE_MS,
FERRATA_CACHE_TTL_MS,
FERRATAS_UNAVAILABLE_MESSAGE, FERRATAS_UNAVAILABLE_MESSAGE,
FerratasService, FerratasService,
} from '../../../src/nest/ferratas/ferratas.service'; } from '../../../src/nest/ferratas/ferratas.service';
import { Test } from '@nestjs/testing'; import { createSnapshotTestDb } from '../../helpers/db-mock';
import { FERRATAS_ATTRIBUTION, FERRATAS_UNAVAILABLE_CODE, ferratasResponseSchema } from '@trek/shared'; import { countRows, deleteRows, findRow, findRows, insertRows } from '../../helpers/factories/rows';
import { resetTestDb } from '../../helpers/test-db';
import { createTestModule, type TestModule } from '../../helpers/test-module';
import { MikroORM } from '@mikro-orm/core';
import {
FERRATA_BBOX_TOO_LARGE_MESSAGE,
FERRATAS_ATTRIBUTION,
FERRATAS_RESULT_CAP,
FERRATAS_UNAVAILABLE_CODE,
ferratasResponseSchema,
type Ferrata,
} from '@trek/shared';
import { beforeEach, describe, expect, it, vi } from 'vitest'; import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const bbox = { south: 47.12, west: 11.21, north: 47.38, east: 11.57 }; const db = createSnapshotTestDb();
const NOW = 1_800_000_000_000;
/** What `fetched_at` holds for NOW: the table keeps seconds. */
const NOW_S = NOW / 1000;
const seconds = (ms: number) => Math.floor(ms / 1000);
/** 47.1 to 47.2 north, 11.2 to 11.3 east once snapped: 2 by 2 cells, one block of one query. */
const bbox = { south: 47.12, west: 11.21, north: 47.18, east: 11.29 };
/** 47.1 to 47.4 north, 11.2 to 11.6 east once snapped: 6 by 8 cells, 3 by 4 blocks. */
const wide = { south: 47.12, west: 11.21, north: 47.38, east: 11.57 };
const element = { const element = {
type: 'way', type: 'way',
id: 42, id: 42,
tags: { highway: 'via_ferrata', name: 'Klettersteig', via_ferrata_scale: '2+' }, tags: { highway: 'via_ferrata', name: 'Klettersteig', via_ferrata_scale: '2+' },
geometry: [ geometry: [
{ lat: 47.2, lon: 11.3 }, { lat: 47.15, lon: 11.25 },
{ lat: 47.21, lon: 11.31 }, { lat: 47.16, lon: 11.26 },
], ],
}; };
const stored = (id: string, geometry: [number, number][]): Ferrata => ({
id,
name: 'Stored',
grade: { raw: null, level: null, plus: null },
routeName: null,
geometry,
osmUrl: `https://www.openstreetmap.org/${id}`,
});
const payload = (ferratas: Ferrata[], truncated = false) => JSON.stringify({ ferratas, truncated });
const query = vi.fn<(q: string) => Promise<unknown[]>>(); const query = vi.fn<(q: string) => Promise<unknown[]>>();
let t: TestModule | undefined;
let service: FerratasService; let service: FerratasService;
const orm = () => {
if (!t) throw new Error('No container booted');
return t.get(MikroORM);
};
/** A fresh container on the same db: an empty memory, what a restart looks like. */
async function boot(): Promise<void> {
await t?.close();
t = await createTestModule({
db,
imports: [FerratasModule],
overrides: [{ provide: FerratasOverpassClient, useValue: { query } }],
});
service = t.get(FerratasService);
}
function deferred(): { promise: Promise<unknown[]>; resolve: (v: unknown[]) => void; reject: (e: Error) => void } {
let resolve: (v: unknown[]) => void = () => {};
let reject: (e: Error) => void = () => {};
const promise = new Promise<unknown[]>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
beforeEach(async () => { beforeEach(async () => {
resetTestDb(db);
query.mockReset(); query.mockReset();
query.mockResolvedValue([element]); query.mockResolvedValue([element]);
const moduleRef = await Test.createTestingModule({ await boot();
providers: [FerratasService, { provide: FerratasOverpassClient, useValue: { query } }],
}).compile();
service = moduleRef.get(FerratasService);
}); });
describe('FerratasService.list', () => { afterEach(() => {
it('answers the ferratas with source and attribution, inside the response contract', async () => { vi.restoreAllMocks();
const res = await service.list(bbox); });
afterAll(async () => {
await t?.close();
db.close();
});
describe('FerratasService.list: cells fetched for the first time', () => {
it('FERSVC-001: answers the ferratas with source and attribution, inside the response contract', async () => {
const res = await service.list(bbox, NOW);
expect(res).toMatchObject({ truncated: false, source: 'openstreetmap', attribution: FERRATAS_ATTRIBUTION }); expect(res).toMatchObject({ truncated: false, source: 'openstreetmap', attribution: FERRATAS_ATTRIBUTION });
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']); expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(ferratasResponseSchema.safeParse(res).success).toBe(true); expect(ferratasResponseSchema.safeParse(res).success).toBe(true);
}); });
it('answers only the ferratas that reach into the asked box, not the whole snapped box', async () => { it('FERSVC-002: asks Overpass once for a box of one block', async () => {
await service.list(bbox, NOW);
expect(query).toHaveBeenCalledTimes(1);
expect(query.mock.calls[0]?.[0]).toContain('(47.1,11.2,47.2,11.3)');
});
it('FERSVC-023: asks a wider box block by block, never more than 0.1 degree a side, at most two at once', async () => {
const gates = Array.from({ length: 12 }, () => deferred());
let next = 0;
query.mockImplementation(() => (gates[next++] ?? deferred()).promise);
const answer = service.list(wide, NOW);
await vi.waitFor(() => expect(query).toHaveBeenCalledTimes(2));
gates[0]?.resolve([element]);
await vi.waitFor(() => expect(query).toHaveBeenCalledTimes(3));
for (const gate of gates) gate.resolve([element]);
const res = await answer;
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(query).toHaveBeenCalledTimes(12);
for (const [text] of query.mock.calls) {
const [south, west, north, east] = (/\(([\d.,-]+)\)/.exec(text)?.[1] ?? '').split(',').map(Number);
expect((north ?? 0) - (south ?? 0)).toBeLessThanOrEqual(0.1 + 1e-9);
expect((east ?? 0) - (west ?? 0)).toBeLessThanOrEqual(0.1 + 1e-9);
}
expect(await countRows(orm(), FerrataTiles)).toBe(6 * 8);
});
it('FERSVC-003: stores every cell, each with the ferratas whose line touches it, at the time in seconds', async () => {
await service.list(bbox, NOW);
expect(await countRows(orm(), FerrataTiles)).toBe(2 * 2);
// 47.15 and 11.25 lie on cell edges, so way 42 belongs to the four cells around that corner.
const holding = (await findRows(orm(), FerrataTiles, {}, { tile_key: 'asc' }))
.filter((row) => row.payload.includes('way/42'))
.map((row) => row.tile_key);
expect(holding).toEqual(['942,224', '942,225', '943,224', '943,225']);
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toMatchObject({ fetched_at: NOW_S });
// The next block north holds none of it: its cells are stored empty.
await service.list({ south: 47.21, west: 11.21, north: 47.29, east: 11.29 }, NOW);
expect(await findRow(orm(), FerrataTiles, { tile_key: '944,224' })).toEqual({
tile_key: '944,224',
payload: payload([]),
fetched_at: NOW_S,
});
});
it('FERSVC-004: answers a ferrata crossing several cells once', async () => {
const res = await service.list(bbox, NOW);
expect(res.ferratas).toHaveLength(1);
});
it('FERSVC-005: answers only the ferratas that reach into the asked box, not the whole cells', async () => {
const outside = { const outside = {
...element, ...element,
id: 43, id: 43,
geometry: [ geometry: [
{ lat: 47.1, lon: 11.3 }, { lat: 47.1, lon: 11.22 },
{ lat: 47.11, lon: 11.31 }, { lat: 47.11, lon: 11.23 },
], ],
}; };
query.mockResolvedValue([element, outside]); query.mockResolvedValue([element, outside]);
const res = await service.list(bbox); const res = await service.list(bbox, NOW);
expect(res.ferratas.map((ferrata) => ferrata.id)).toEqual(['way/42']); expect(res.ferratas.map((ferrata) => ferrata.id)).toEqual(['way/42']);
// The cell still keeps it, for the next box that does reach that far south.
expect((await findRow(orm(), FerrataTiles, { tile_key: '942,224' }))?.payload).toContain('way/43');
}); });
it('asks Overpass for the box snapped to the grid', async () => { it('FERSVC-006: shares one Overpass call between concurrent identical requests', async () => {
await service.list(bbox); const pending = deferred();
expect(query).toHaveBeenCalledTimes(1); query.mockReturnValueOnce(pending.promise);
expect(query.mock.calls[0]?.[0]).toContain('(47.1,11.2,47.4,11.6)'); const both = Promise.all([service.list(bbox, NOW), service.list(bbox, NOW)]);
}); await vi.waitFor(() => expect(query).toHaveBeenCalledTimes(1));
pending.resolve([element]);
it('serves a nearby request from the cache inside the TTL', async () => {
await service.list(bbox, 1_000);
await service.list({ south: 47.11, west: 11.22, north: 47.39, east: 11.59 }, 1_000 + FERRATA_CACHE_TTL_MS - 1);
expect(query).toHaveBeenCalledTimes(1);
});
it('asks again once the entry is older than the TTL', async () => {
await service.list(bbox, 1_000);
await service.list(bbox, 1_000 + FERRATA_CACHE_TTL_MS);
expect(query).toHaveBeenCalledTimes(2);
});
it('shares one Overpass call between concurrent identical requests', async () => {
let release: (v: unknown[]) => void = () => {};
query.mockReturnValueOnce(new Promise((resolve) => (release = resolve)));
const both = Promise.all([service.list(bbox), service.list(bbox)]);
release([element]);
const [a, b] = await both; const [a, b] = await both;
expect(a).toBe(b); expect(a).toEqual(b);
expect(query).toHaveBeenCalledTimes(1); expect(query).toHaveBeenCalledTimes(1);
}); });
it('turns an Overpass failure into a 502 with a code, and does not cache it', async () => { it('FERSVC-007: asks a cut block again cell by cell, and stores a cell still cut as old so a restart asks again', async () => {
const many = Array.from({ length: FERRATAS_RESULT_CAP + 1 }, (_, i) => ({ ...element, id: 1000 + i }));
query.mockResolvedValue(many);
const res = await service.list(bbox, NOW);
expect(res.truncated).toBe(true);
expect(res.ferratas).toHaveLength(FERRATAS_RESULT_CAP);
expect(ferratasResponseSchema.safeParse(res).success).toBe(true);
// The block, then each of its four cells on its own.
expect(query).toHaveBeenCalledTimes(1 + 4);
expect(query.mock.calls[1]?.[0]).toContain('(47.1,11.2,47.15,11.25)');
const row = await findRow(orm(), FerrataTiles, { tile_key: '942,224' });
expect(row?.fetched_at).toBe(0);
expect(row?.payload).toContain('"truncated":true');
// This process keeps the cut cells; a restart serves them and asks again in the background.
expect((await service.list(bbox, NOW + 1)).truncated).toBe(true);
expect(query).toHaveBeenCalledTimes(5);
await boot();
query.mockReset();
query.mockResolvedValue([element]);
expect((await service.list(bbox, NOW)).truncated).toBe(true);
expect(query).toHaveBeenCalledTimes(1);
await vi.waitFor(async () =>
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toMatchObject({ fetched_at: NOW_S }),
);
expect((await service.list(bbox, NOW + 1)).truncated).toBe(false);
});
it('FERSVC-024: a block cut at the cap whose cells each fit is stored whole, and not marked cut', async () => {
const many = Array.from({ length: FERRATAS_RESULT_CAP + 1 }, (_, i) => ({ ...element, id: 1000 + i }));
query.mockResolvedValueOnce(many).mockResolvedValue([element]);
const res = await service.list(bbox, NOW);
expect(res.truncated).toBe(false);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(query).toHaveBeenCalledTimes(5);
const rows = await findRows(orm(), FerrataTiles, {}, { tile_key: 'asc' });
expect(rows.map((r) => [r.tile_key, r.fetched_at, r.payload.includes('"truncated":false')])).toEqual([
['942,224', NOW_S, true],
['942,225', NOW_S, true],
['943,224', NOW_S, true],
['943,225', NOW_S, true],
]);
});
it('FERSVC-008: refuses a box over the cell cap before asking anyone', async () => {
const err = await service.list({ south: 47, west: 11, north: 48, east: 12 }, NOW).catch((e: unknown) => e);
expect(err).toBeInstanceOf(DomainError);
expect((err as DomainError).getStatus()).toBe(400);
expect((err as DomainError).getResponse()).toEqual({ error: FERRATA_BBOX_TOO_LARGE_MESSAGE });
expect(query).not.toHaveBeenCalled();
});
});
describe('FerratasService.list: cells already stored', () => {
it('FERSVC-009: serves a nearby request from the cells, without Overpass', async () => {
await service.list(bbox, NOW);
const res = await service.list({ south: 47.11, west: 11.22, north: 47.19, east: 11.28 }, NOW + 1);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(query).toHaveBeenCalledTimes(1);
});
it('FERSVC-010: serves the cells from the table after a restart', async () => {
await service.list(bbox, NOW);
await boot();
const res = await service.list(bbox, NOW + 1);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(query).toHaveBeenCalledTimes(1);
});
it('FERSVC-011: serves the cells from memory without reading the table again', async () => {
await service.list(bbox, NOW);
await deleteRows(orm(), FerrataTiles);
const res = await service.list(bbox, NOW + 1);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(query).toHaveBeenCalledTimes(1);
});
it('FERSVC-012: asks Overpass only for the cells not stored yet', async () => {
await service.list({ south: 47.1, west: 11.2, north: 47.2, east: 11.3 }, NOW);
query.mockClear();
await service.list({ south: 47.1, west: 11.2, north: 47.3, east: 11.3 }, NOW);
expect(query).toHaveBeenCalledTimes(1);
expect(query.mock.calls[0]?.[0]).toContain('(47.2,11.2,47.3,11.3)');
expect(await countRows(orm(), FerrataTiles)).toBe(4 * 2);
});
it('FERSVC-013: reads a stored cell that does not decode as never fetched, and fetches it again', async () => {
await insertRows(orm(), FerrataTiles, [{ tile_key: '942,224', payload: '{"ferratas":"nope"}', fetched_at: NOW_S }]);
query.mockResolvedValue([]);
await service.list({ south: 47.1, west: 11.2, north: 47.15, east: 11.25 }, NOW + 1000);
expect(query).toHaveBeenCalledTimes(1);
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toEqual({
tile_key: '942,224',
payload: payload([]),
fetched_at: NOW_S + 1,
});
});
});
describe('FerratasService.list: old cells', () => {
const cell = { south: 47.1, west: 11.2, north: 47.15, east: 11.25 };
const old = stored('way/7', [
[47.11, 11.21],
[47.12, 11.22],
]);
const seedOld = () =>
insertRows(orm(), FerrataTiles, [
{ tile_key: '942,224', payload: payload([old]), fetched_at: seconds(NOW - FERRATA_TILE_STALE_MS) },
]);
it('FERSVC-014: serves an old cell at once and refreshes it in the background', async () => {
await seedOld();
const pending = deferred();
query.mockReturnValueOnce(pending.promise);
const res = await service.list(cell, NOW);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/7']);
expect(query).toHaveBeenCalledTimes(1);
expect(query.mock.calls[0]?.[0]).toContain('(47.1,11.2,47.15,11.25)');
pending.resolve([]);
await vi.waitFor(async () =>
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toEqual({
tile_key: '942,224',
payload: payload([]),
fetched_at: NOW_S,
}),
);
expect((await service.list(cell, NOW + 1)).ferratas).toEqual([]);
expect(query).toHaveBeenCalledTimes(1);
});
it('FERSVC-015: a cell just under the age limit is not refreshed', async () => {
await insertRows(orm(), FerrataTiles, [
{ tile_key: '942,224', payload: payload([old]), fetched_at: seconds(NOW - FERRATA_TILE_STALE_MS) + 1 },
]);
await service.list(cell, NOW);
expect(query).not.toHaveBeenCalled();
});
it('FERSVC-016: starts one refresh for an old cell, however many requests see it', async () => {
await seedOld();
const pending = deferred();
query.mockReturnValueOnce(pending.promise);
await service.list(cell, NOW);
await service.list(cell, NOW);
expect(query).toHaveBeenCalledTimes(1);
pending.resolve([]);
await vi.waitFor(async () =>
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toMatchObject({ fetched_at: NOW_S }),
);
});
it('FERSVC-017: a failed refresh keeps the old cell, never reaches the client, and is tried again later', async () => {
await seedOld();
query.mockRejectedValueOnce(new Error('Could not reach any Overpass endpoint')); query.mockRejectedValueOnce(new Error('Could not reach any Overpass endpoint'));
const err = await service.list(bbox).catch((e: unknown) => e);
const res = await service.list(cell, NOW);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/7']);
// Let the rejected refresh settle; an unhandled rejection would fail the run.
await new Promise((resolve) => setTimeout(resolve, 0));
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toMatchObject({
fetched_at: seconds(NOW - FERRATA_TILE_STALE_MS),
});
query.mockResolvedValueOnce([]);
expect((await service.list(cell, NOW + 1000)).ferratas.map((f) => f.id)).toEqual(['way/7']);
expect(query).toHaveBeenCalledTimes(2);
await vi.waitFor(async () =>
expect(await findRow(orm(), FerrataTiles, { tile_key: '942,224' })).toMatchObject({ fetched_at: NOW_S + 1 }),
);
});
it('FERSVC-018: an old cell is refreshed in a query of its own, so its failure cannot fail the new cells', async () => {
await seedOld();
query.mockImplementation((text) =>
text.includes('(47.1,11.2,47.15,11.25)') ? Promise.reject(new Error('timeout')) : Promise.resolve([]),
);
const res = await service.list({ south: 47.1, west: 11.2, north: 47.2, east: 11.25 }, NOW);
expect(query).toHaveBeenCalledTimes(2);
expect(query.mock.calls.map(([text]) => text.match(/\(([\d.,]+)\)/)?.[1]).sort()).toEqual([
'47.1,11.2,47.15,11.25',
'47.15,11.2,47.2,11.25',
]);
// The old cell is served as it was; the new one came back on its own.
expect(res.ferratas.map((f) => f.id)).toEqual(['way/7']);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(await findRows(orm(), FerrataTiles, {}, { tile_key: 'asc' })).toEqual([
{ tile_key: '942,224', payload: payload([old]), fetched_at: seconds(NOW - FERRATA_TILE_STALE_MS) },
{ tile_key: '943,224', payload: payload([]), fetched_at: NOW_S },
]);
});
});
describe('FerratasService.list: Overpass failing', () => {
it('FERSVC-019: turns a failure for a cell never fetched into a 502 with a code, and stores nothing', async () => {
query.mockRejectedValueOnce(new Error('Could not reach any Overpass endpoint'));
const err = await service.list(bbox, NOW).catch((e: unknown) => e);
expect(err).toBeInstanceOf(DomainError); expect(err).toBeInstanceOf(DomainError);
const domain = err as DomainError; const domain = err as DomainError;
expect(domain.getStatus()).toBe(502); expect(domain.getStatus()).toBe(502);
expect(domain.code).toBe(FERRATAS_UNAVAILABLE_CODE); expect(domain.code).toBe(FERRATAS_UNAVAILABLE_CODE);
expect(domain.getResponse()).toEqual({ error: FERRATAS_UNAVAILABLE_MESSAGE, code: FERRATAS_UNAVAILABLE_CODE }); expect(domain.getResponse()).toEqual({ error: FERRATAS_UNAVAILABLE_MESSAGE, code: FERRATAS_UNAVAILABLE_CODE });
expect(await countRows(orm(), FerrataTiles)).toBe(0);
const retry = await service.list(bbox); const retry = await service.list(bbox, NOW);
expect(retry.ferratas).toHaveLength(1); expect(retry.ferratas).toHaveLength(1);
expect(query).toHaveBeenCalledTimes(2); expect(query).toHaveBeenCalledTimes(2);
}); });
it('evicts the oldest box once the cache is full', async () => { it('FERSVC-020: fails every request that joined the failed batch', async () => {
const boxAt = (i: number) => ({ south: 10 + i * 0.1, west: 10, north: 10.04 + i * 0.1, east: 10.04 }); const pending = deferred();
// One after another, so the insertion order is the call order. query.mockReturnValueOnce(pending.promise);
await Array.from({ length: FERRATA_CACHE_MAX + 1 }, (_, i) => i).reduce<Promise<unknown>>(async (previous, i) => { const both = Promise.allSettled([service.list(bbox, NOW), service.list(bbox, NOW)]);
await previous; await vi.waitFor(() => expect(query).toHaveBeenCalledTimes(1));
return service.list(boxAt(i), 1_000); pending.reject(new Error('down'));
}, Promise.resolve()); const results = await both;
expect(query).toHaveBeenCalledTimes(FERRATA_CACHE_MAX + 1); expect(results.map((r) => r.status)).toEqual(['rejected', 'rejected']);
expect(query).toHaveBeenCalledTimes(1);
});
await service.list(boxAt(FERRATA_CACHE_MAX), 1_000); it('FERSVC-021: needs no Overpass at all while every cell is stored and fresh', async () => {
expect(query).toHaveBeenCalledTimes(FERRATA_CACHE_MAX + 1); await service.list(bbox, NOW);
await service.list(boxAt(0), 1_000); query.mockRejectedValue(new Error('down'));
expect(query).toHaveBeenCalledTimes(FERRATA_CACHE_MAX + 2); const res = await service.list(bbox, NOW + 1);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(query).toHaveBeenCalledTimes(1);
});
it('FERSVC-022: an answer the table refuses to store is still served, and kept in memory', async () => {
vi.spyOn(FerrataTilesRepository.prototype, 'upsertTiles').mockRejectedValueOnce(new Error('disk full'));
const res = await service.list(bbox, NOW);
expect(res.ferratas.map((f) => f.id)).toEqual(['way/42']);
expect(await countRows(orm(), FerrataTiles)).toBe(0);
await service.list(bbox, NOW + 1);
expect(query).toHaveBeenCalledTimes(1);
}); });
}); });
+124
View File
@@ -0,0 +1,124 @@
import {
FERRATA_TILE_DEG,
FERRATA_TILES_PER_REQUEST_SIDE,
ferrataTileAt,
ferrataTileCount,
ferrataTileKey,
ferrataTileRange,
ferrataTileRangeBbox,
ferrataTileBlockKey,
ferrataTileRequests,
ferrataTilesOf,
growFerrataTileRange,
parseFerrataTileKey,
snapFerrataBbox,
} from './ferrataGrid';
import { ferrataBboxSpanOk } from './ferratas.schema';
import { describe, expect, it } from 'vitest';
describe('ferrataGrid', () => {
it('uses one 0.05 degree cell and two cells per request side', () => {
expect(FERRATA_TILE_DEG).toBe(0.05);
expect(FERRATA_TILES_PER_REQUEST_SIDE).toBe(2);
});
it('snaps a box outwards to the grid and clamps it to the globe', () => {
expect(snapFerrataBbox({ south: 46.51, west: 11.72, north: 46.58, east: 11.81 })).toEqual({
south: 46.5,
west: 11.7,
north: 46.6,
east: 11.85,
});
// An edge already on a grid line stays where it is, float noise or not.
expect(snapFerrataBbox({ south: 46.15, west: 11.7, north: 46.2, east: 11.75 })).toEqual({
south: 46.15,
west: 11.7,
north: 46.2,
east: 11.75,
});
expect(snapFerrataBbox({ south: -90, west: -180, north: 90, east: 180 })).toEqual({
south: -90,
west: -180,
north: 90,
east: 180,
});
});
it('names the tile a point lies in, a point on a grid line in the tile north east of it', () => {
expect(ferrataTileAt(46.52, 11.81)).toEqual({ row: 930, col: 236 });
expect(ferrataTileAt(46.15, 11.7)).toEqual({ row: 923, col: 234 });
expect(ferrataTileAt(-0.01, -0.01)).toEqual({ row: -1, col: -1 });
// The poles and the antimeridian stay inside the grid.
expect(ferrataTileAt(90, 180)).toEqual({ row: 1799, col: 3599 });
});
it('round-trips a tile through its key and refuses anything else', () => {
expect(parseFerrataTileKey(ferrataTileKey({ row: -12, col: 340 }))).toEqual({ row: -12, col: 340 });
expect(parseFerrataTileKey('930:236')).toEqual({ row: 930, col: 236 });
expect(parseFerrataTileKey('930,236')).toBeNull();
expect(parseFerrataTileKey('99999:1')).toBeNull();
expect(parseFerrataTileKey('')).toBeNull();
});
it('covers a box with the tiles it touches, not the next row on a grid line', () => {
const range = ferrataTileRange({ south: 46.5, west: 11.7, north: 46.6, east: 11.81 });
expect(range).toEqual({ minRow: 930, maxRow: 931, minCol: 234, maxCol: 236 });
expect(ferrataTileCount(range)).toBe(6);
expect(ferrataTilesOf(range).map(ferrataTileKey)).toEqual([
'930:234',
'930:235',
'930:236',
'931:234',
'931:235',
'931:236',
]);
expect(ferrataTileRangeBbox(range)).toEqual({ south: 46.5, west: 11.7, north: 46.6, east: 11.85 });
});
it('grows a range by a ring on every side', () => {
const range = { minRow: 930, maxRow: 931, minCol: 234, maxCol: 236 };
expect(growFerrataTileRange(range, 1)).toEqual({ minRow: 929, maxRow: 932, minCol: 233, maxCol: 237 });
expect(growFerrataTileRange({ minRow: 1799, maxRow: 1799, minCol: 0, maxCol: 0 }, 1).maxRow).toBe(1799);
});
it('asks for tiles of one grid-aligned block with one box around them', () => {
const requests = ferrataTileRequests([
{ row: 930, col: 234 },
{ row: 931, col: 235 },
]);
expect(requests).toEqual([
{
bbox: { south: 46.5, west: 11.7, north: 46.6, east: 11.8 },
range: { minRow: 930, maxRow: 931, minCol: 234, maxCol: 235 },
},
]);
expect(ferrataTileRequests([])).toEqual([]);
});
it('splits tiles that straddle a block line along it, so each request is one block of the server', () => {
const requests = ferrataTileRequests([
{ row: 931, col: 235 },
{ row: 932, col: 236 },
]);
expect(requests.map(({ range }) => range)).toEqual([
{ minRow: 931, maxRow: 931, minCol: 235, maxCol: 235 },
{ minRow: 932, maxRow: 932, minCol: 236, maxCol: 236 },
]);
expect(ferrataTileBlockKey(931, 235)).toBe('465:117');
expect(ferrataTileBlockKey(-1, -3)).toBe('-1:-2');
});
it('cuts a wider set into boxes of at most 0.1 degree a side, each covering only tiles it holds', () => {
const tiles = ferrataTilesOf({ minRow: 900, maxRow: 911, minCol: 200, maxCol: 202 });
const requests = ferrataTileRequests(tiles);
expect(requests).toHaveLength(12);
for (const { bbox } of requests) {
expect(bbox.north - bbox.south).toBeLessThanOrEqual(0.1 + 1e-9);
expect(bbox.east - bbox.west).toBeLessThanOrEqual(0.1 + 1e-9);
}
for (const { bbox } of requests) expect(ferrataBboxSpanOk(bbox)).toBe(true);
const covered = requests.flatMap(({ range }) => ferrataTilesOf(range).map(ferrataTileKey)).sort();
expect(covered).toEqual(tiles.map(ferrataTileKey).sort());
});
});
+187
View File
@@ -0,0 +1,187 @@
import type { FerrataBbox } from './ferratas.schema';
/**
* The tile grid of the via ferrata layer, shared by both ends.
*
* The server caches Overpass answers per box snapped outwards to this grid,
* and the client loads, keeps and persists the layer per tile of it. One cell
* size on both sides means a box the client asks for is always a whole
* number of cells, which is exactly a cache entry on the server.
*
* A tile is named by its row and column: row = floor(lat / 0.05), column =
* floor(lng / 0.05). Pure, no I/O.
*/
/** The cell size, in degrees, on both axes. */
export const FERRATA_TILE_DEG = 0.05;
/**
* The most tiles one Overpass query covers per side: two, a box of 0.1 degree.
* A box of that size answers in a second or two, where one of half a degree
* often ran into the mirrors' timeout. The client asks in blocks of this size
* and the server queries Overpass in the same blocks, both aligned to the
* grid (a block starts at a row and a column divisible by it), so one request
* of the client is exactly one query of the server.
*/
export const FERRATA_TILES_PER_REQUEST_SIDE = 2;
/** Room for float noise when an edge sits exactly on a grid line (46.15 / 0.05 is 922.9999999999999). */
const GRID_EPSILON = 1e-9;
const MIN_ROW = Math.round(-90 / FERRATA_TILE_DEG);
const MAX_ROW = Math.round(90 / FERRATA_TILE_DEG) - 1;
const MIN_COL = Math.round(-180 / FERRATA_TILE_DEG);
const MAX_COL = Math.round(180 / FERRATA_TILE_DEG) - 1;
export interface FerrataTile {
row: number;
col: number;
}
/** A rectangle of tiles, both ends inclusive. */
export interface FerrataTileRange {
minRow: number;
maxRow: number;
minCol: number;
maxCol: number;
}
const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value));
const snapDown = (v: number) => Math.floor(v / FERRATA_TILE_DEG + GRID_EPSILON) * FERRATA_TILE_DEG;
const snapUp = (v: number) => Math.ceil(v / FERRATA_TILE_DEG - GRID_EPSILON) * FERRATA_TILE_DEG;
/** Every grid line is a multiple of 0.05, so two decimals write it exactly. */
const fixed = (v: number) => Number(v.toFixed(2));
/**
* The box grown outwards to the grid and clamped to the globe. The server
* queries Overpass for this box and caches the answer under it.
*/
export function snapFerrataBbox(bbox: FerrataBbox): FerrataBbox {
return {
south: fixed(Math.max(-90, snapDown(bbox.south))),
west: fixed(Math.max(-180, snapDown(bbox.west))),
north: fixed(Math.min(90, snapUp(bbox.north))),
east: fixed(Math.min(180, snapUp(bbox.east))),
};
}
/** The tile a point lies in; a point on a grid line belongs to the tile north and east of it. */
export function ferrataTileAt(lat: number, lng: number): FerrataTile {
return {
row: clamp(Math.floor(lat / FERRATA_TILE_DEG + GRID_EPSILON), MIN_ROW, MAX_ROW),
col: clamp(Math.floor(lng / FERRATA_TILE_DEG + GRID_EPSILON), MIN_COL, MAX_COL),
};
}
/** The tile's stable name, as it is kept in memory and in IndexedDB. */
export function ferrataTileKey(tile: FerrataTile): string {
return `${tile.row}:${tile.col}`;
}
/** The tile a key names, or null for anything that is not one. */
export function parseFerrataTileKey(key: string): FerrataTile | null {
const match = /^(-?\d+):(-?\d+)$/.exec(key);
if (!match) return null;
const row = Number(match[1]);
const col = Number(match[2]);
if (row < MIN_ROW || row > MAX_ROW || col < MIN_COL || col > MAX_COL) return null;
return { row, col };
}
/** The tiles a box touches, as a range. A box on a grid line does not reach into the next row or column. */
export function ferrataTileRange(bbox: FerrataBbox): FerrataTileRange {
const sw = ferrataTileAt(bbox.south, bbox.west);
const lastRow = Math.ceil(bbox.north / FERRATA_TILE_DEG - GRID_EPSILON) - 1;
const lastCol = Math.ceil(bbox.east / FERRATA_TILE_DEG - GRID_EPSILON) - 1;
return {
minRow: sw.row,
maxRow: clamp(Math.max(sw.row, lastRow), MIN_ROW, MAX_ROW),
minCol: sw.col,
maxCol: clamp(Math.max(sw.col, lastCol), MIN_COL, MAX_COL),
};
}
/** The range grown by `rings` tiles on every side, inside the globe. */
export function growFerrataTileRange(range: FerrataTileRange, rings: number): FerrataTileRange {
return {
minRow: clamp(range.minRow - rings, MIN_ROW, MAX_ROW),
maxRow: clamp(range.maxRow + rings, MIN_ROW, MAX_ROW),
minCol: clamp(range.minCol - rings, MIN_COL, MAX_COL),
maxCol: clamp(range.maxCol + rings, MIN_COL, MAX_COL),
};
}
/** How many tiles a range holds. */
export function ferrataTileCount(range: FerrataTileRange): number {
return (range.maxRow - range.minRow + 1) * (range.maxCol - range.minCol + 1);
}
/** Every tile of a range, row by row from the south west. */
export function ferrataTilesOf(range: FerrataTileRange): FerrataTile[] {
const tiles: FerrataTile[] = [];
for (let row = range.minRow; row <= range.maxRow; row += 1) {
for (let col = range.minCol; col <= range.maxCol; col += 1) tiles.push({ row, col });
}
return tiles;
}
/** The box a range of tiles covers, edges on the grid. */
export function ferrataTileRangeBbox(range: FerrataTileRange): FerrataBbox {
return {
south: fixed(Math.max(-90, range.minRow * FERRATA_TILE_DEG)),
west: fixed(Math.max(-180, range.minCol * FERRATA_TILE_DEG)),
north: fixed(Math.min(90, (range.maxRow + 1) * FERRATA_TILE_DEG)),
east: fixed(Math.min(180, (range.maxCol + 1) * FERRATA_TILE_DEG)),
};
}
/** The smallest range holding every tile, or null for none. */
function enclosing(tiles: readonly FerrataTile[]): FerrataTileRange | null {
if (tiles.length === 0) return null;
let minRow = Infinity;
let maxRow = -Infinity;
let minCol = Infinity;
let maxCol = -Infinity;
for (const { row, col } of tiles) {
minRow = Math.min(minRow, row);
maxRow = Math.max(maxRow, row);
minCol = Math.min(minCol, col);
maxCol = Math.max(maxCol, col);
}
return { minRow, maxRow, minCol, maxCol };
}
/** One request: the box to ask for and the range of tiles its answer completes. */
export interface FerrataTileRequest {
bbox: FerrataBbox;
range: FerrataTileRange;
}
/** The grid-aligned block of FERRATA_TILES_PER_REQUEST_SIDE tiles a side that a tile falls in. */
export function ferrataTileBlockKey(row: number, col: number): string {
const side = FERRATA_TILES_PER_REQUEST_SIDE;
return `${Math.floor(row / side)}:${Math.floor(col / side)}`;
}
/**
* The requests that load `tiles`: one per grid-aligned block of
* FERRATA_TILES_PER_REQUEST_SIDE tiles a side that holds any of them, each box
* shrunk to the tiles it holds. Every box is a whole number of tiles and well
* within the server's span limit, so none is refused, each answer completes
* every tile its box covers, and the server answers it with one query.
*/
export function ferrataTileRequests(tiles: readonly FerrataTile[]): FerrataTileRequest[] {
const blocks = new Map<string, FerrataTile[]>();
for (const tile of tiles) {
const key = ferrataTileBlockKey(tile.row, tile.col);
const block = blocks.get(key);
if (block) block.push(tile);
else blocks.set(key, [tile]);
}
const requests: FerrataTileRequest[] = [];
for (const block of blocks.values()) {
const range = enclosing(block);
if (range) requests.push({ bbox: ferrataTileRangeBbox(range), range });
}
return requests;
}
+1
View File
@@ -13,6 +13,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'قرّب الخريطة لتحميل مسارات التسلق الحديدية', 'ferratas.zoomIn': 'قرّب الخريطة لتحميل مسارات التسلق الحديدية',
'ferratas.offline': 'تحتاج مسارات التسلق الحديدية إلى اتصال', 'ferratas.offline': 'تحتاج مسارات التسلق الحديدية إلى اتصال',
'ferratas.unknownGrade': 'درجة الصعوبة غير معروفة', 'ferratas.unknownGrade': 'درجة الصعوبة غير معروفة',
'ferratas.gradeRange': '{from} إلى {to}',
'ferratas.unnamed': 'مسار تسلق حديدي بلا اسم', 'ferratas.unnamed': 'مسار تسلق حديدي بلا اسم',
'ferratas.viewInOsm': 'عرض في OpenStreetMap', 'ferratas.viewInOsm': 'عرض في OpenStreetMap',
'ferratas.attribution': 'البيانات © مساهمو OpenStreetMap', 'ferratas.attribution': 'البيانات © مساهمو OpenStreetMap',
+1
View File
@@ -65,6 +65,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'اسم الجولة', 'tours.planner.name': 'اسم الجولة',
'tours.planner.namePlaceholder': 'جولة مشي بلا اسم', 'tours.planner.namePlaceholder': 'جولة مشي بلا اسم',
'tours.planner.mapHint': 'انقر على الخريطة لإضافة نقاط.', 'tours.planner.mapHint': 'انقر على الخريطة لإضافة نقاط.',
'tours.planner.mapSearch': 'ابحث عن مكان أو عنوان',
'tours.planner.waypoints': 'نقاط المرور', 'tours.planner.waypoints': 'نقاط المرور',
'tours.planner.waypointLabel': 'نقطة المرور {n}', 'tours.planner.waypointLabel': 'نقطة المرور {n}',
'tours.durationMinutes': '{count} د', 'tours.durationMinutes': '{count} د',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Via ferrata marşrutlarını yükləmək üçün yaxınlaşdırın', 'ferratas.zoomIn': 'Via ferrata marşrutlarını yükləmək üçün yaxınlaşdırın',
'ferratas.offline': 'Via ferrata marşrutları üçün bağlantı lazımdır', 'ferratas.offline': 'Via ferrata marşrutları üçün bağlantı lazımdır',
'ferratas.unknownGrade': 'Çətinlik naməlumdur', 'ferratas.unknownGrade': 'Çətinlik naməlumdur',
'ferratas.gradeRange': '{from} - {to}',
'ferratas.unnamed': 'Adsız via ferrata marşrutu', 'ferratas.unnamed': 'Adsız via ferrata marşrutu',
'ferratas.viewInOsm': 'OpenStreetMap-də bax', 'ferratas.viewInOsm': 'OpenStreetMap-də bax',
'ferratas.attribution': 'Məlumatlar © OpenStreetMap iştirakçıları', 'ferratas.attribution': 'Məlumatlar © OpenStreetMap iştirakçıları',
+1
View File
@@ -63,6 +63,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Turun adı', 'tours.planner.name': 'Turun adı',
'tours.planner.namePlaceholder': 'Adsız yürüş', 'tours.planner.namePlaceholder': 'Adsız yürüş',
'tours.planner.mapHint': 'Nöqtə əlavə etmək üçün xəritəyə klikləyin.', 'tours.planner.mapHint': 'Nöqtə əlavə etmək üçün xəritəyə klikləyin.',
'tours.planner.mapSearch': 'Yer və ya ünvan axtar',
'tours.planner.waypoints': 'Yol nöqtələri', 'tours.planner.waypoints': 'Yol nöqtələri',
'tours.planner.waypointLabel': 'Yol nöqtəsi {n}', 'tours.planner.waypointLabel': 'Yol nöqtəsi {n}',
'tours.durationMinutes': '{count} dəq.', 'tours.durationMinutes': '{count} dəq.',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Aproxime o mapa para carregar vias ferratas', 'ferratas.zoomIn': 'Aproxime o mapa para carregar vias ferratas',
'ferratas.offline': 'As vias ferratas precisam de conexão', 'ferratas.offline': 'As vias ferratas precisam de conexão',
'ferratas.unknownGrade': 'Dificuldade desconhecida', 'ferratas.unknownGrade': 'Dificuldade desconhecida',
'ferratas.gradeRange': '{from} a {to}',
'ferratas.unnamed': 'Via ferrata sem nome', 'ferratas.unnamed': 'Via ferrata sem nome',
'ferratas.viewInOsm': 'Ver no OpenStreetMap', 'ferratas.viewInOsm': 'Ver no OpenStreetMap',
'ferratas.attribution': 'Dados © colaboradores do OpenStreetMap', 'ferratas.attribution': 'Dados © colaboradores do OpenStreetMap',
+1
View File
@@ -64,6 +64,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Nome do passeio', 'tours.planner.name': 'Nome do passeio',
'tours.planner.namePlaceholder': 'Trilha sem título', 'tours.planner.namePlaceholder': 'Trilha sem título',
'tours.planner.mapHint': 'Clique no mapa para adicionar pontos.', 'tours.planner.mapHint': 'Clique no mapa para adicionar pontos.',
'tours.planner.mapSearch': 'Buscar lugar ou endereço',
'tours.planner.waypoints': 'Pontos de passagem', 'tours.planner.waypoints': 'Pontos de passagem',
'tours.planner.waypointLabel': 'Ponto de passagem {n}', 'tours.planner.waypointLabel': 'Ponto de passagem {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Apropa el mapa per carregar vies ferrades', 'ferratas.zoomIn': 'Apropa el mapa per carregar vies ferrades',
'ferratas.offline': 'Les vies ferrades necessiten connexió', 'ferratas.offline': 'Les vies ferrades necessiten connexió',
'ferratas.unknownGrade': 'Dificultat desconeguda', 'ferratas.unknownGrade': 'Dificultat desconeguda',
'ferratas.gradeRange': '{from} a {to}',
'ferratas.unnamed': 'Via ferrada sense nom', 'ferratas.unnamed': 'Via ferrada sense nom',
'ferratas.viewInOsm': 'Mostra a OpenStreetMap', 'ferratas.viewInOsm': 'Mostra a OpenStreetMap',
'ferratas.attribution': "Dades © col·laboradors d'OpenStreetMap", 'ferratas.attribution': "Dades © col·laboradors d'OpenStreetMap",
+1
View File
@@ -63,6 +63,7 @@ const tours: TranslationStrings = {
'tours.planner.name': "Nom de l'excursió", 'tours.planner.name': "Nom de l'excursió",
'tours.planner.namePlaceholder': 'Ruta de senderisme sense títol', 'tours.planner.namePlaceholder': 'Ruta de senderisme sense títol',
'tours.planner.mapHint': 'Fes clic al mapa per afegir punts.', 'tours.planner.mapHint': 'Fes clic al mapa per afegir punts.',
'tours.planner.mapSearch': 'Cerca un lloc o una adreça',
'tours.planner.waypoints': 'Punts de pas', 'tours.planner.waypoints': 'Punts de pas',
'tours.planner.waypointLabel': 'Punt de pas {n}', 'tours.planner.waypointLabel': 'Punt de pas {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -10,6 +10,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Přibližte mapu pro načtení ferrat', 'ferratas.zoomIn': 'Přibližte mapu pro načtení ferrat',
'ferratas.offline': 'Ferraty vyžadují připojení', 'ferratas.offline': 'Ferraty vyžadují připojení',
'ferratas.unknownGrade': 'Obtížnost neznámá', 'ferratas.unknownGrade': 'Obtížnost neznámá',
'ferratas.gradeRange': '{from} až {to}',
'ferratas.unnamed': 'Ferrata bez názvu', 'ferratas.unnamed': 'Ferrata bez názvu',
'ferratas.viewInOsm': 'Zobrazit v OpenStreetMap', 'ferratas.viewInOsm': 'Zobrazit v OpenStreetMap',
'ferratas.attribution': 'Data © přispěvatelé OpenStreetMap', 'ferratas.attribution': 'Data © přispěvatelé OpenStreetMap',
+1
View File
@@ -62,6 +62,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Název výletu', 'tours.planner.name': 'Název výletu',
'tours.planner.namePlaceholder': 'Túra bez názvu', 'tours.planner.namePlaceholder': 'Túra bez názvu',
'tours.planner.mapHint': 'Kliknutím do mapy přidáte body.', 'tours.planner.mapHint': 'Kliknutím do mapy přidáte body.',
'tours.planner.mapSearch': 'Hledat místo nebo adresu',
'tours.planner.waypoints': 'Průjezdní body', 'tours.planner.waypoints': 'Průjezdní body',
'tours.planner.waypointLabel': 'Bod {n}', 'tours.planner.waypointLabel': 'Bod {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Für Klettersteige näher heranzoomen', 'ferratas.zoomIn': 'Für Klettersteige näher heranzoomen',
'ferratas.offline': 'Klettersteige brauchen eine Verbindung', 'ferratas.offline': 'Klettersteige brauchen eine Verbindung',
'ferratas.unknownGrade': 'Schwierigkeit unbekannt', 'ferratas.unknownGrade': 'Schwierigkeit unbekannt',
'ferratas.gradeRange': '{from} bis {to}',
'ferratas.unnamed': 'Klettersteig ohne Namen', 'ferratas.unnamed': 'Klettersteig ohne Namen',
'ferratas.viewInOsm': 'In OpenStreetMap ansehen', 'ferratas.viewInOsm': 'In OpenStreetMap ansehen',
'ferratas.attribution': 'Daten © OpenStreetMap-Mitwirkende', 'ferratas.attribution': 'Daten © OpenStreetMap-Mitwirkende',
+1
View File
@@ -63,6 +63,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Name der Tour', 'tours.planner.name': 'Name der Tour',
'tours.planner.namePlaceholder': 'Wanderung ohne Titel', 'tours.planner.namePlaceholder': 'Wanderung ohne Titel',
'tours.planner.mapHint': 'Klicke auf die Karte, um Punkte zu setzen.', 'tours.planner.mapHint': 'Klicke auf die Karte, um Punkte zu setzen.',
'tours.planner.mapSearch': 'Ort oder Adresse suchen',
'tours.planner.waypoints': 'Wegpunkte', 'tours.planner.waypoints': 'Wegpunkte',
'tours.planner.waypointLabel': 'Wegpunkt {n}', 'tours.planner.waypointLabel': 'Wegpunkt {n}',
'tours.durationMinutes': '{count} Min.', 'tours.durationMinutes': '{count} Min.',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Zoom in to load via ferratas', 'ferratas.zoomIn': 'Zoom in to load via ferratas',
'ferratas.offline': 'Via ferratas need a connection', 'ferratas.offline': 'Via ferratas need a connection',
'ferratas.unknownGrade': 'Grade unknown', 'ferratas.unknownGrade': 'Grade unknown',
'ferratas.gradeRange': '{from} to {to}',
'ferratas.unnamed': 'Unnamed via ferrata', 'ferratas.unnamed': 'Unnamed via ferrata',
'ferratas.viewInOsm': 'View in OpenStreetMap', 'ferratas.viewInOsm': 'View in OpenStreetMap',
'ferratas.attribution': 'Data © OpenStreetMap contributors', 'ferratas.attribution': 'Data © OpenStreetMap contributors',
+1
View File
@@ -62,6 +62,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Tour name', 'tours.planner.name': 'Tour name',
'tours.planner.namePlaceholder': 'Untitled hike', 'tours.planner.namePlaceholder': 'Untitled hike',
'tours.planner.mapHint': 'Click the map to add points.', 'tours.planner.mapHint': 'Click the map to add points.',
'tours.planner.mapSearch': 'Search place or address',
'tours.planner.waypoints': 'Waypoints', 'tours.planner.waypoints': 'Waypoints',
'tours.planner.waypointLabel': 'Waypoint {n}', 'tours.planner.waypointLabel': 'Waypoint {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Acerca el mapa para cargar vías ferratas', 'ferratas.zoomIn': 'Acerca el mapa para cargar vías ferratas',
'ferratas.offline': 'Las vías ferratas necesitan conexión', 'ferratas.offline': 'Las vías ferratas necesitan conexión',
'ferratas.unknownGrade': 'Dificultad desconocida', 'ferratas.unknownGrade': 'Dificultad desconocida',
'ferratas.gradeRange': '{from} a {to}',
'ferratas.unnamed': 'Vía ferrata sin nombre', 'ferratas.unnamed': 'Vía ferrata sin nombre',
'ferratas.viewInOsm': 'Ver en OpenStreetMap', 'ferratas.viewInOsm': 'Ver en OpenStreetMap',
'ferratas.attribution': 'Datos © colaboradores de OpenStreetMap', 'ferratas.attribution': 'Datos © colaboradores de OpenStreetMap',
+1
View File
@@ -63,6 +63,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Nombre de la excursión', 'tours.planner.name': 'Nombre de la excursión',
'tours.planner.namePlaceholder': 'Ruta de senderismo sin título', 'tours.planner.namePlaceholder': 'Ruta de senderismo sin título',
'tours.planner.mapHint': 'Haz clic en el mapa para añadir puntos.', 'tours.planner.mapHint': 'Haz clic en el mapa para añadir puntos.',
'tours.planner.mapSearch': 'Buscar lugar o dirección',
'tours.planner.waypoints': 'Puntos de paso', 'tours.planner.waypoints': 'Puntos de paso',
'tours.planner.waypointLabel': 'Punto de paso {n}', 'tours.planner.waypointLabel': 'Punto de paso {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Via ferrata radade laadimiseks suumi lähemale', 'ferratas.zoomIn': 'Via ferrata radade laadimiseks suumi lähemale',
'ferratas.offline': 'Via ferrata rajad vajavad ühendust', 'ferratas.offline': 'Via ferrata rajad vajavad ühendust',
'ferratas.unknownGrade': 'Raskusaste teadmata', 'ferratas.unknownGrade': 'Raskusaste teadmata',
'ferratas.gradeRange': '{from} kuni {to}',
'ferratas.unnamed': 'Nimeta via ferrata', 'ferratas.unnamed': 'Nimeta via ferrata',
'ferratas.viewInOsm': 'Vaata OpenStreetMapis', 'ferratas.viewInOsm': 'Vaata OpenStreetMapis',
'ferratas.attribution': 'Andmed © OpenStreetMapi kaastöötajad', 'ferratas.attribution': 'Andmed © OpenStreetMapi kaastöötajad',
+1
View File
@@ -62,6 +62,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Retke nimi', 'tours.planner.name': 'Retke nimi',
'tours.planner.namePlaceholder': 'Nimetu matk', 'tours.planner.namePlaceholder': 'Nimetu matk',
'tours.planner.mapHint': 'Klõpsa kaardil punktide lisamiseks.', 'tours.planner.mapHint': 'Klõpsa kaardil punktide lisamiseks.',
'tours.planner.mapSearch': 'Otsi kohta või aadressi',
'tours.planner.waypoints': 'Teekonnapunktid', 'tours.planner.waypoints': 'Teekonnapunktid',
'tours.planner.waypointLabel': 'Teekonnapunkt {n}', 'tours.planner.waypointLabel': 'Teekonnapunkt {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Zoomez pour charger les via ferrata', 'ferratas.zoomIn': 'Zoomez pour charger les via ferrata',
'ferratas.offline': 'Les via ferrata nécessitent une connexion', 'ferratas.offline': 'Les via ferrata nécessitent une connexion',
'ferratas.unknownGrade': 'Difficulté inconnue', 'ferratas.unknownGrade': 'Difficulté inconnue',
'ferratas.gradeRange': '{from} à {to}',
'ferratas.unnamed': 'Via ferrata sans nom', 'ferratas.unnamed': 'Via ferrata sans nom',
'ferratas.viewInOsm': 'Voir dans OpenStreetMap', 'ferratas.viewInOsm': 'Voir dans OpenStreetMap',
'ferratas.attribution': 'Données © contributeurs OpenStreetMap', 'ferratas.attribution': 'Données © contributeurs OpenStreetMap',
+1
View File
@@ -65,6 +65,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Nom de la sortie', 'tours.planner.name': 'Nom de la sortie',
'tours.planner.namePlaceholder': 'Randonnée sans titre', 'tours.planner.namePlaceholder': 'Randonnée sans titre',
'tours.planner.mapHint': 'Cliquez sur la carte pour ajouter des points.', 'tours.planner.mapHint': 'Cliquez sur la carte pour ajouter des points.',
'tours.planner.mapSearch': 'Rechercher un lieu ou une adresse',
'tours.planner.waypoints': 'Points de passage', 'tours.planner.waypoints': 'Points de passage',
'tours.planner.waypointLabel': 'Point de passage {n}', 'tours.planner.waypointLabel': 'Point de passage {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Μεγεθύνετε για να φορτωθούν διαδρομές via ferrata', 'ferratas.zoomIn': 'Μεγεθύνετε για να φορτωθούν διαδρομές via ferrata',
'ferratas.offline': 'Οι διαδρομές via ferrata χρειάζονται σύνδεση', 'ferratas.offline': 'Οι διαδρομές via ferrata χρειάζονται σύνδεση',
'ferratas.unknownGrade': 'Άγνωστη δυσκολία', 'ferratas.unknownGrade': 'Άγνωστη δυσκολία',
'ferratas.gradeRange': '{from} έως {to}',
'ferratas.unnamed': 'Διαδρομή via ferrata χωρίς όνομα', 'ferratas.unnamed': 'Διαδρομή via ferrata χωρίς όνομα',
'ferratas.viewInOsm': 'Προβολή στο OpenStreetMap', 'ferratas.viewInOsm': 'Προβολή στο OpenStreetMap',
'ferratas.attribution': 'Δεδομένα © συντελεστές του OpenStreetMap', 'ferratas.attribution': 'Δεδομένα © συντελεστές του OpenStreetMap',
+1
View File
@@ -65,6 +65,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Όνομα εκδρομής', 'tours.planner.name': 'Όνομα εκδρομής',
'tours.planner.namePlaceholder': 'Πεζοπορία χωρίς όνομα', 'tours.planner.namePlaceholder': 'Πεζοπορία χωρίς όνομα',
'tours.planner.mapHint': 'Κάντε κλικ στον χάρτη για να προσθέσετε σημεία.', 'tours.planner.mapHint': 'Κάντε κλικ στον χάρτη για να προσθέσετε σημεία.',
'tours.planner.mapSearch': 'Αναζήτηση τόπου ή διεύθυνσης',
'tours.planner.waypoints': 'Σημεία διέλευσης', 'tours.planner.waypoints': 'Σημεία διέλευσης',
'tours.planner.waypointLabel': 'Σημείο διέλευσης {n}', 'tours.planner.waypointLabel': 'Σημείο διέλευσης {n}',
'tours.durationMinutes': '{count} λεπτά', 'tours.durationMinutes': '{count} λεπτά',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Nagyíts rá a vasalt utak betöltéséhez', 'ferratas.zoomIn': 'Nagyíts rá a vasalt utak betöltéséhez',
'ferratas.offline': 'A vasalt utakhoz kapcsolat szükséges', 'ferratas.offline': 'A vasalt utakhoz kapcsolat szükséges',
'ferratas.unknownGrade': 'Ismeretlen nehézség', 'ferratas.unknownGrade': 'Ismeretlen nehézség',
'ferratas.gradeRange': '{from}–{to}',
'ferratas.unnamed': 'Névtelen vasalt út', 'ferratas.unnamed': 'Névtelen vasalt út',
'ferratas.viewInOsm': 'Megtekintés az OpenStreetMapen', 'ferratas.viewInOsm': 'Megtekintés az OpenStreetMapen',
'ferratas.attribution': 'Adatok © OpenStreetMap-közreműködők', 'ferratas.attribution': 'Adatok © OpenStreetMap-közreműködők',
+1
View File
@@ -62,6 +62,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Túra neve', 'tours.planner.name': 'Túra neve',
'tours.planner.namePlaceholder': 'Névtelen gyalogtúra', 'tours.planner.namePlaceholder': 'Névtelen gyalogtúra',
'tours.planner.mapHint': 'Kattints a térképre pontok hozzáadásához.', 'tours.planner.mapHint': 'Kattints a térképre pontok hozzáadásához.',
'tours.planner.mapSearch': 'Hely vagy cím keresése',
'tours.planner.waypoints': 'Útvonalpontok', 'tours.planner.waypoints': 'Útvonalpontok',
'tours.planner.waypointLabel': '{n}. útvonalpont', 'tours.planner.waypointLabel': '{n}. útvonalpont',
'tours.durationMinutes': '{count} perc', 'tours.durationMinutes': '{count} perc',
+1
View File
@@ -8,6 +8,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Perbesar peta untuk memuat jalur via ferrata', 'ferratas.zoomIn': 'Perbesar peta untuk memuat jalur via ferrata',
'ferratas.offline': 'Jalur via ferrata memerlukan koneksi', 'ferratas.offline': 'Jalur via ferrata memerlukan koneksi',
'ferratas.unknownGrade': 'Tingkat kesulitan tidak diketahui', 'ferratas.unknownGrade': 'Tingkat kesulitan tidak diketahui',
'ferratas.gradeRange': '{from} hingga {to}',
'ferratas.unnamed': 'Via ferrata tanpa nama', 'ferratas.unnamed': 'Via ferrata tanpa nama',
'ferratas.viewInOsm': 'Lihat di OpenStreetMap', 'ferratas.viewInOsm': 'Lihat di OpenStreetMap',
'ferratas.attribution': 'Data © kontributor OpenStreetMap', 'ferratas.attribution': 'Data © kontributor OpenStreetMap',
+1
View File
@@ -62,6 +62,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'Nama tur', 'tours.planner.name': 'Nama tur',
'tours.planner.namePlaceholder': 'Pendakian tanpa judul', 'tours.planner.namePlaceholder': 'Pendakian tanpa judul',
'tours.planner.mapHint': 'Klik peta untuk menambahkan titik.', 'tours.planner.mapHint': 'Klik peta untuk menambahkan titik.',
'tours.planner.mapSearch': 'Cari tempat atau alamat',
'tours.planner.waypoints': 'Titik lintasan', 'tours.planner.waypoints': 'Titik lintasan',
'tours.planner.waypointLabel': 'Titik lintasan {n}', 'tours.planner.waypointLabel': 'Titik lintasan {n}',
'tours.durationMinutes': '{count} menit', 'tours.durationMinutes': '{count} menit',
+1
View File
@@ -9,6 +9,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'Ingrandisci per caricare le vie ferrate', 'ferratas.zoomIn': 'Ingrandisci per caricare le vie ferrate',
'ferratas.offline': 'Le vie ferrate richiedono una connessione', 'ferratas.offline': 'Le vie ferrate richiedono una connessione',
'ferratas.unknownGrade': 'Difficoltà sconosciuta', 'ferratas.unknownGrade': 'Difficoltà sconosciuta',
'ferratas.gradeRange': 'da {from} a {to}',
'ferratas.unnamed': 'Via ferrata senza nome', 'ferratas.unnamed': 'Via ferrata senza nome',
'ferratas.viewInOsm': 'Vedi su OpenStreetMap', 'ferratas.viewInOsm': 'Vedi su OpenStreetMap',
'ferratas.attribution': 'Dati © contributori di OpenStreetMap', 'ferratas.attribution': 'Dati © contributori di OpenStreetMap',
+1
View File
@@ -64,6 +64,7 @@ const tours: TranslationStrings = {
'tours.planner.name': "Nome dell'escursione", 'tours.planner.name': "Nome dell'escursione",
'tours.planner.namePlaceholder': 'Escursione a piedi senza titolo', 'tours.planner.namePlaceholder': 'Escursione a piedi senza titolo',
'tours.planner.mapHint': 'Fai clic sulla mappa per aggiungere punti.', 'tours.planner.mapHint': 'Fai clic sulla mappa per aggiungere punti.',
'tours.planner.mapSearch': 'Cerca luogo o indirizzo',
'tours.planner.waypoints': 'Punti di passaggio', 'tours.planner.waypoints': 'Punti di passaggio',
'tours.planner.waypointLabel': 'Punto di passaggio {n}', 'tours.planner.waypointLabel': 'Punto di passaggio {n}',
'tours.durationMinutes': '{count} min', 'tours.durationMinutes': '{count} min',
+1
View File
@@ -8,6 +8,7 @@ const ferratas: TranslationStrings = {
'ferratas.zoomIn': 'ヴィア・フェラータを読み込むには拡大してください', 'ferratas.zoomIn': 'ヴィア・フェラータを読み込むには拡大してください',
'ferratas.offline': 'ヴィア・フェラータの表示には接続が必要です', 'ferratas.offline': 'ヴィア・フェラータの表示には接続が必要です',
'ferratas.unknownGrade': '難易度不明', 'ferratas.unknownGrade': '難易度不明',
'ferratas.gradeRange': '{from}〜{to}',
'ferratas.unnamed': '名称未設定のヴィア・フェラータ', 'ferratas.unnamed': '名称未設定のヴィア・フェラータ',
'ferratas.viewInOsm': 'OpenStreetMap で表示', 'ferratas.viewInOsm': 'OpenStreetMap で表示',
'ferratas.attribution': 'データ © OpenStreetMap 貢献者', 'ferratas.attribution': 'データ © OpenStreetMap 貢献者',
+1
View File
@@ -60,6 +60,7 @@ const tours: TranslationStrings = {
'tours.planner.name': 'ツアー名', 'tours.planner.name': 'ツアー名',
'tours.planner.namePlaceholder': '名前のないハイキング', 'tours.planner.namePlaceholder': '名前のないハイキング',
'tours.planner.mapHint': '地図をクリックして地点を追加します。', 'tours.planner.mapHint': '地図をクリックして地点を追加します。',
'tours.planner.mapSearch': '場所や住所を検索',
'tours.planner.waypoints': 'ルート上の地点', 'tours.planner.waypoints': 'ルート上の地点',
'tours.planner.waypointLabel': '地点{n}', 'tours.planner.waypointLabel': '地点{n}',
'tours.durationMinutes': '{count}分', 'tours.durationMinutes': '{count}分',

Some files were not shown because too many files have changed in this diff Show More