Files
TREK/client/src/components/Settings/MapboxPreview.tsx
T
Maurice 69eacd5316 feat(settings): pick your own map template or the theme style for MapLibre
The MapLibre style picker offers two more entries after the OpenFreeMap
styles: OpenFreeMap that follows the light or dark theme (trek:ofm-auto),
and the user's own map template (trek:template), which brings the template
field under the picker, and the CARTO key for a CARTO template. Where the
trip map will draw the template with the classic 2D map (Amap, a host the
CSP does not name) the form says so and the preview shows that map. The
preview draws exactly what the trip map will.

Every place the forms load or save a MapLibre style keeps a keyword now,
where it used to turn it into Liberty on the first unchanged save: the
user form on desktop and phone (load, save, the phone's mirrored field,
a provider switch there and back) and the admin defaults (provider switch,
preset pick, typed style). The rules live in mapSettingsModel, so both
shells render from one decision:

- the template keyword with an empty template saves Liberty;
- switching from Leaflet with a template and an empty MapLibre slot
  preselects the template, shown and not saved until Save;
- Leaflet never writes a keyword into the Mapbox slot, and Mapbox shows its
  default for one.

New strings in all locales.
2026-11-30 16:22:53 +01:00

94 lines
3.5 KiB
TypeScript

import { useEffect, useRef } from 'react'
import { isStandardFamily } from '../Map/mapboxSetup'
import { applyGl3d, glMapOptions } from '../Map/glMapOptions'
import type { GlStyle } from '../Map/glBasemap'
import { normalizeStyleForProvider, type GlMapProvider } from '../Map/glProviders'
import { buildGlMap } from '../Map/glSupport'
import { setBasemapConfig } from '../Map/useStandardBasemapConfig'
import { fs } from '../shared/DialogShell'
interface Props {
provider?: GlMapProvider
token?: string
/** A style URL, or the style a MapLibre keyword resolved to (useMapSettingsForm). */
style: GlStyle
lat: number
lng: number
zoom: number
enable3d: boolean
quality?: boolean
/** Leaves out Mapbox Standard's own points of interest, as the trip map does (#1761). */
hidePois?: boolean
onClick?: (latlng: { lat: number; lng: number }) => void
/**
* The GL engine, injected instead of imported. Both SDKs used to be pulled in
* statically here, so a single 2.8 MB chunk carried mapbox-gl and maplibre-gl
* together and every map user downloaded both while only one ever ran.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
gl: any
}
export default function GlMapPreview({ provider = 'mapbox-gl', token = '', style, lat, lng, zoom, enable3d, quality = false, hidePois = false, onClick, gl }: Props) {
const containerRef = useRef<HTMLDivElement>(null)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mapRef = useRef<any | null>(null)
const onClickRef = useRef(onClick)
onClickRef.current = onClick
const isMapLibre = provider === 'maplibre-gl'
const glStyle = typeof style === 'string' ? normalizeStyleForProvider(provider, style) : style
const enableMapbox3d = !isMapLibre && enable3d
useEffect(() => {
if (!containerRef.current || (!isMapLibre && !token)) return
if (!isMapLibre) gl.accessToken = token
const mapOptions = glMapOptions({
provider,
container: containerRef.current,
style: glStyle,
center: [lng, lat],
zoom,
pitch: enableMapbox3d ? 45 : 0,
attributionControl: true,
quality,
})
// Null when the GPU refused WebGL2: the settings form has switched the preview to Leaflet by then.
const map = buildGlMap(() => new gl.Map(mapOptions as any))
if (!map) return
mapRef.current = map
map.on('load', () => {
// The trip map's 3D, terrain rule included, so the preview shows what the trip will.
applyGl3d(map, glStyle, enableMapbox3d, document.documentElement.classList.contains('dark'))
if (hidePois && !isMapLibre && isStandardFamily(glStyle)) setBasemapConfig(map, 'showPointOfInterestLabels', false)
})
map.on('click', (e) => {
onClickRef.current?.({ lat: e.lngLat.lat, lng: e.lngLat.lng })
})
return () => {
try { map.remove() } catch { /* noop */ }
mapRef.current = null
}
}, [provider, token, glStyle, enableMapbox3d, quality, hidePois])
// Recenter without rebuilding the map when lat/lng/zoom change externally
useEffect(() => {
if (!mapRef.current) return
try { mapRef.current.jumpTo({ center: [lng, lat], zoom }) } catch { /* noop */ }
}, [lat, lng, zoom])
if (!isMapLibre && !token) {
return (
<div className="flex h-full items-center justify-center bg-surface-tertiary px-4 text-center font-medium text-content-muted" style={fs(12)}>
Enter a Mapbox access token to preview
</div>
)
}
return <div ref={containerRef} className="h-full w-full overflow-hidden" />
}