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(null) // eslint-disable-next-line @typescript-eslint/no-explicit-any const mapRef = useRef(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 (
Enter a Mapbox access token to preview
) } return
}