Files
TREK/client/src/components/Tours/terrain/useTerrainPrefetch.ts
T
Maurice 98cc57942b feat(tours): 3D terrain view with flyover and grade colours, imported tours carry no hiking difficulty
The tours map gets a 3D mode on the same map surface between the side panels:
a "3D" switch in the layer pill drapes the route over open Terrarium elevation
tiles (AWS Open Data), colours it by grade, plays a flyover tied to the
elevation profile in the rail, and keeps planning working by clicking and
dragging waypoints on the terrain. The corridor around the route is preloaded
before the flyover so the landscape never loads in front of the camera.

GPX imports no longer get a made-up hiking difficulty: max_hiking_difficulty
becomes nullable (new migration, waypoints kept), the import writes null and
the client shows no T badge for it. Drawn tours still default to T2.

Info hints in the planner show their tooltip only, and the discard
confirmation reads "Discard changes".
2026-10-14 14:02:27 +02:00

36 lines
1.6 KiB
TypeScript

import type { Map as MapLibreMap } from 'maplibre-gl';
import { useEffect, useMemo, useState } from 'react';
import type { DistanceIndexedProfileSample } from '../../../utils/routeGeometry';
import { corridorPoints, loadedTemplates } from './terrainLayers';
import { corridorTiles, prefetchTiles, tileUrls } from './terrainPrefetch';
/**
* The corridor around the route, warmed up front so the flyover never waits on a tile.
* Returns the share already in the browser cache: 0 until the style knows its tile URLs,
* 1 once the flyover can run from the cache (or there is no corridor to load).
*/
export function useTerrainPrefetch(
map: MapLibreMap | null,
styleGeneration: number,
samples: DistanceIndexedProfileSample[]
): number {
const points = useMemo(() => corridorPoints(samples), [samples]);
// One run per route and loaded style; a progress report from an older run never shows.
const run = useMemo(() => ({ points, styleGeneration }), [points, styleGeneration]);
const [progress, setProgress] = useState<{ run: typeof run | null; share: number }>({ run: null, share: 0 });
useEffect(() => {
if (!map || run.styleGeneration === 0 || !run.points.length) return;
const controller = new AbortController();
const urls = tileUrls(loadedTemplates(map), corridorTiles(run.points));
void prefetchTiles(urls, controller.signal, (share) => {
if (!controller.signal.aborted) setProgress({ run, share });
});
return () => controller.abort();
}, [map, run]);
if (styleGeneration === 0) return 0;
if (!points.length) return 1;
return progress.run === run ? progress.share : 0;
}