Files
TREK/client/src/components/Tours/terrain/useTerrainMap.ts
T
Maurice e28bd2fc99 fix(maps): draw with Leaflet wherever WebGL2 is missing
MapLibre 6 and Mapbox GL 3 both throw out of their Map constructor without
WebGL2. Every place that builds a GL map now asks one helper first,
components/Map/glSupport.ts, and a browser without WebGL2 gets the Leaflet
map with the user's tiles instead of a map that fails to start:

- the trip map (MapViewAuto) and the journey maps (JourneyMapAuto) pick
  their renderer through useGlRenderer, which folds in the Mapbox token
  rule they each had;
- the settings preview, desktop and phone, shows the Leaflet preview
  through useMapSettingsForm and says why under it;
- the tour planner keeps the 2D map when 3D is asked for and says that
  3D needs WebGL2;
- the PDF route map and the Leaflet vector basemap already fell back and
  now report a GPU failure too.

Behind the probe, buildGlMap wraps each constructor (MapViewGL,
JourneyMapGL, the settings preview, the terrain map, the PDF map). A GPU
that claims WebGL2 and still refuses the context is reported through
utils/webgl, which turns the answer to no and tells every mounted map,
so they switch to Leaflet without a reload; any other error is still
thrown. Two new strings, map.webgl2.fallback and map.webgl2.terrain, in
all 27 locales.
2026-10-24 22:41:56 +00:00

144 lines
4.7 KiB
TypeScript

import type { GeoJSONSource, Map as MapLibreMap } from 'maplibre-gl';
import { useCallback, useEffect, useRef, useState, type RefObject } from 'react';
import type { TourBaseLayer } from '../../Map/MapLayerSwitcher';
import { buildGlMap } from '../../Map/glSupport';
import { isTourLayerHit } from '../../Map/mapRasterOverlay';
import {
addTerrainLayers,
DEM_SOURCE,
frameBounds,
PITCH,
sidePadding,
styleFor,
TRACK_SOURCE,
type PanelWidths,
type TrackData,
} from './terrainLayers';
type MapLibreEngine = typeof import('../../Map/engines/maplibre').default;
/** The GL map and the engine it came from, there once the engine has loaded. */
export interface TerrainGl {
map: MapLibreMap;
engine: MapLibreEngine;
}
/** What the map's own event handlers read at the moment they fire, not when they were bound. */
export interface TerrainScene {
data: TrackData;
exaggeration: number;
panels: PanelWidths;
baseLayer: TourBaseLayer;
trackColor: string;
pins: ReadonlyArray<{ lat: number; lng: number }>;
framePoints: [number, number][];
onAdd?: (lat: number, lng: number) => void;
}
/** Frames the route (or the pins, or what the flat map framed) between the side panels. */
export function frameCamera(map: MapLibreMap, scene: TerrainScene, duration: number): boolean {
const target = frameBounds(scene.data, scene.pins, scene.framePoints);
if (!target) return false;
map.fitBounds(target, {
padding: { top: 70, bottom: 150, ...sidePadding(scene.panels, 40) },
pitch: PITCH,
bearing: -20,
duration,
});
return true;
}
interface MapHandlers {
onReady: () => void;
onStyleIdle: () => void;
}
function createTerrainMap(
engine: MapLibreEngine,
container: HTMLDivElement,
scene: RefObject<TerrainScene>,
handlers: MapHandlers
): MapLibreMap {
const map = new engine.Map({
container,
style: styleFor(scene.current.baseLayer),
pitch: PITCH,
maxPitch: 75,
fadeDuration: 0,
renderWorldCopies: false,
maxTileCacheSize: 2000,
attributionControl: { compact: true },
});
// Clicking the terrain adds a control point, the same as on the flat planner map;
// a click on a via ferrata or a paddle obstacle opens its card instead.
map.on('click', (event) => {
if (isTourLayerHit(map, event.point)) return;
scene.current.onAdd?.(event.lngLat.lat, event.lngLat.lng);
});
map.on('style.load', () => {
addTerrainLayers(map, scene.current.data, scene.current.exaggeration);
handlers.onReady();
// Vector sources know their tile URLs only once their TileJSON is in.
void map.once('idle', handlers.onStyleIdle);
});
void map.once('load', () => frameCamera(map, scene.current, 0));
return map;
}
interface Options {
scene: RefObject<TerrainScene>;
baseLayer: TourBaseLayer;
data: TrackData;
exaggeration: number;
}
/**
* One GL map for as long as 3D mode is on, its engine loaded on demand. The base layer
* swaps the style in place, and the track and relief follow without a rebuild.
*/
export function useTerrainMap({ scene, baseLayer, data, exaggeration }: Options) {
const [container, setContainer] = useState<HTMLDivElement | null>(null);
const [gl, setGl] = useState<TerrainGl | null>(null);
const [ready, setReady] = useState(false);
/** Counts the styles that finished loading their sources, so the prefetch knows the tile URLs. */
const [styleGeneration, setStyleGeneration] = useState(0);
const markReady = useCallback(() => setReady(true), []);
const countStyle = useCallback(() => setStyleGeneration((n) => n + 1), []);
useEffect(() => {
if (!container) return;
let cancelled = false;
let map: MapLibreMap | null = null;
void import('../../Map/engines/maplibre').then(({ default: engine }) => {
if (cancelled) return;
// Null when the GPU refused WebGL2. The report switches the planner back to the 2D map.
map = buildGlMap(() =>
createTerrainMap(engine, container, scene, { onReady: markReady, onStyleIdle: countStyle })
);
if (map) setGl({ map, engine });
});
return () => {
cancelled = true;
map?.remove();
};
}, [container, scene, markReady, countStyle]);
const appliedLayer = useRef(baseLayer);
useEffect(() => {
if (!gl || !ready || appliedLayer.current === baseLayer) return;
appliedLayer.current = baseLayer;
setReady(false);
gl.map.setStyle(styleFor(baseLayer));
}, [gl, ready, baseLayer]);
useEffect(() => {
if (gl && ready) gl.map.getSource<GeoJSONSource>(TRACK_SOURCE)?.setData(data);
}, [gl, ready, data]);
useEffect(() => {
if (gl && ready) gl.map.setTerrain({ source: DEM_SOURCE, exaggeration });
}, [gl, ready, exaggeration]);
return { setContainer, gl, ready, styleGeneration };
}