feat(tours): OpenHikingMap as a hiking base map of the tour planner (MAP-01)

While a hike is planned, the planner's map switch offers "Hiking" beside the
map, topo and satellite: OpenHikingMap, with marked trails over the terrain.
It is a choice of the planner's view alone, like topo: the global map
setting, the tour and its days stay as they are. With no tour open, over an
import viewed as it came in and for a paddle tour it is not offered, and a
choice made earlier falls back to the standard map there.

openmaps.fr's tile usage policy is followed: tiles load only as the map
shows them, with the browser's Referer, from the server's own caching; no
service worker rule keeps them and the 3D flyover's warm-up passes their
source by. Its credit and donation link are on the map; the 3D map, whose
attribution is folded away under the panel, shows them as a line of its own
above its controls.

The service is best effort. A run of failed tiles while it is shown marks
the switch, with a tooltip and a status for screen readers that say it is
not answering and that the tour is untouched; one tile arriving clears it.
The tile health the seamark overlay had is now useTileHealth, shared by both.

The source is free for non-commercial installs up to 500,000 tiles a month.
tours_hiking_map_enabled is an admin switch under Tours (admin tours-hiking-map,
audited), on by default, for an install that is commercial or larger to
switch off until it has an agreement with the operator. /api/addons carries
it as toursHikingMap, true only while Tours is on.

Leaflet, GL and the 3D map read one table of the planner's raster bases
(tourRasterBases), so topo and the hiking map are drawn the same way.
tile.openmaps.fr is on the CSP's connect-src for the GL maps.
This commit is contained in:
Maurice
2026-10-19 13:07:36 +02:00
parent d322363e53
commit 06e6e25b25
90 changed files with 839 additions and 117 deletions
@@ -0,0 +1,44 @@
import { renderHook } from '@testing-library/react';
import type { TourListItem } from '@trek/shared';
import { beforeEach, describe, expect, it } from 'vitest';
import type { TourPlannerController } from '../../components/Tours/planner/useTourPlanner';
import { useAddonStore } from '../../store/addonStore';
import { useTourStageLayers } from './useTourStageLayers';
type Planner = Parameters<typeof useTourStageLayers>[0];
type Ferrata = Parameters<typeof useTourStageLayers>[1];
const ferrata = { layer: undefined, crossings: [], selected: null } as unknown as Ferrata;
function planner(mode: TourPlannerController['mode'], tourType: Planner['tourType'] = 'hike'): Planner {
return { mode, tourType, mapBaseLayer: 'hiking', mapOverlays: new Set(), toggleMapOverlay: () => {} };
}
const basesOf = (p: Planner) => renderHook(() => useTourStageLayers(p, ferrata)).result.current;
describe('useTourStageLayers: OpenHikingMap (MAP-01)', () => {
beforeEach(() => useAddonStore.setState({ toursHikingMap: true }));
it('offers it while a hike is planned, as a draft or a saved tour in the editor', () => {
for (const mode of [{ type: 'new-draft' }, { type: 'edit-saved', placeId: 4 }] as const) {
const layers = basesOf(planner(mode));
expect(layers.tourLayers.bases).toContain('hiking');
expect(layers.baseLayer).toBe('hiking');
}
});
it('not with no tour open, over an import viewed as it came in, or for a paddle tour', () => {
const viewed = { type: 'view-gpx', placeId: 9, tour: { place_id: 9, tour_type: 'hike' } as TourListItem } as const;
for (const p of [planner({ type: 'neutral' }), planner(viewed), planner({ type: 'new-draft' }, 'kayak')]) {
const layers = basesOf(p);
expect(layers.tourLayers.bases).not.toContain('hiking');
// The choice is kept; the map shows the type's default until a hike is planned again.
expect(layers.baseLayer).toBe('default');
}
});
it('not where the admin withdrew it', () => {
useAddonStore.setState({ toursHikingMap: false });
expect(basesOf(planner({ type: 'new-draft' })).tourLayers.bases).not.toContain('hiking');
});
});
@@ -1,11 +1,12 @@
import { useCallback, useMemo } from 'react';
import { useCallback, useEffect, useMemo } from 'react';
import { useShallow } from 'zustand/react/shallow';
import type { useFerrataMapLayer } from '../../components/Ferratas/useFerrataMapLayer';
import { paddleMapItems, selectedPaddleKey } from '../../components/Tours/paddle/paddleMapLayer';
import { usePaddlePaint } from '../../components/Tours/paddle/paddlePaint';
import type { TourPlannerController } from '../../components/Tours/planner/useTourPlanner';
import { baseLayerFor, SEAMARK_OVERLAY, tourMapLayersFor } from '../../components/Tours/tourMapLayers';
import { useTourRasterOverlay } from '../../components/Tours/useTourRasterOverlay';
import { useTileHealth, useTourRasterOverlay } from '../../components/Tours/useTourRasterOverlay';
import { useAddonStore } from '../../store/addonStore';
import { selectPaddleObstacle, usePaddleObstacleStore } from '../../store/paddleObstacleStore';
const NO_CROSSINGS: never[] = [];
@@ -17,14 +18,29 @@ type FerrataLayerState = ReturnType<typeof useFerrataMapLayer>;
* and overlays of its type, an import with its own type's and a draft with the
* one chosen. A type without the via ferrata layer, or no tour open at all,
* neither draws nor warns about ferratas, even if the layer was switched on before; a base layer it
* does not offer falls back to its default without changing the choice kept.
* does not offer falls back to its default without changing the choice kept,
* which is also what an install whose admin withdrew OpenHikingMap sees.
*/
export function useTourStageLayers(
tourPlanner: Pick<TourPlannerController, 'mode' | 'tourType' | 'mapBaseLayer' | 'mapOverlays' | 'toggleMapOverlay'>,
ferrata: FerrataLayerState
) {
const { mode } = tourPlanner;
const layerSet = tourMapLayersFor(mode.type === 'view-gpx' ? mode.tour.tour_type : tourPlanner.tourType);
// MAP-01: OpenHikingMap while a hike is being planned (a draft or a saved tour in the editor),
// never over an import viewed as it came in or with no tour open, and only while the admin offers it.
const hikingMapAllowed = useAddonStore((state) => state.toursHikingMap);
const planning = mode.type === 'new-draft' || mode.type === 'edit-saved';
const layerSet = tourMapLayersFor(mode.type === 'view-gpx' ? mode.tour.tour_type : tourPlanner.tourType, {
hikingMap: hikingMapAllowed && planning,
});
const baseLayer = baseLayerFor(layerSet, tourPlanner.mapBaseLayer);
// Whether OpenHikingMap answers, asked afresh each time it becomes the base map.
const hikingMap = useTileHealth();
const { reset: resetHikingMap } = hikingMap;
const showsHikingMap = baseLayer === 'hiking';
useEffect(() => {
if (showsHikingMap) resetHikingMap();
}, [showsHikingMap, resetHikingMap]);
// With no tour open nothing is a hike yet: the ferratas wait for one, as the seamarks wait for a paddle tour.
const offersFerratas = mode.type !== 'neutral' && layerSet.overlays.includes('ferratas');
const { toggleMapOverlay } = tourPlanner;
@@ -54,11 +70,16 @@ export function useTourStageLayers(
[paddleItems, features, selectedRef]
);
const tourLayers = useMemo(
() => ({ bases: layerSet.bases, overlays: layerSet.overlays.includes('seamarks') ? [seamarks] : [], paddle }),
[layerSet, seamarks, paddle]
() => ({
bases: layerSet.bases,
overlays: layerSet.overlays.includes('seamarks') ? [seamarks] : [],
paddle,
hikingMap: showsHikingMap ? hikingMap : undefined,
}),
[layerSet, seamarks, paddle, showsHikingMap, hikingMap]
);
return {
baseLayer: baseLayerFor(layerSet, tourPlanner.mapBaseLayer),
baseLayer,
tourLayers,
ferrataLayer: offersFerratas ? ferrata.layer : undefined,
ferrataCrossings: offersFerratas ? ferrata.crossings : NO_CROSSINGS,