import 'maplibre-gl/dist/maplibre-gl.css'; import '../switcher/switcher.css'; import maplibregl from 'maplibre-gl'; import React, { useRef, useLayoutEffect, useEffect, useState, } from 'react'; import { SwitcherControl } from '../switcher/switcher'; import { useAttributePreference } from '../../common/util/preferences'; import usePersistedState, { savePersistedState } from '../../common/util/usePersistedState'; import { mapImages } from './preloadImages'; import useMapStyles from './useMapStyles'; const element = document.createElement('div'); element.style.width = '100%'; element.style.height = '100%'; element.style.boxSizing = 'initial'; export const map = new maplibregl.Map({ container: element, }); let ready = false; const readyListeners = new Set(); const addReadyListener = (listener) => { readyListeners.add(listener); listener(ready); }; const removeReadyListener = (listener) => { readyListeners.delete(listener); }; const updateReadyValue = (value) => { ready = value; readyListeners.forEach((listener) => listener(value)); }; const initMap = async () => { if (ready) return; if (!map.hasImage('background')) { Object.entries(mapImages).forEach(([key, value]) => { map.addImage(key, value, { pixelRatio: window.devicePixelRatio, }); }); } updateReadyValue(true); }; map.addControl(new maplibregl.NavigationControl()); const switcher = new SwitcherControl( () => updateReadyValue(false), (layerId) => savePersistedState('mapLayer', layerId), () => { map.once('styledata', () => { const waiting = () => { if (!map.loaded()) { setTimeout(waiting, 33); } else { initMap(); } }; waiting(); }); }, ); map.addControl(switcher); const MapView = ({ children }) => { const containerEl = useRef(null); const [mapReady, setMapReady] = useState(false); const [defaultMapLayer] = usePersistedState('mapLayer', 'locationIqStreets'); const mapboxAccessToken = useAttributePreference('mapboxAccessToken'); const mapStyles = useMapStyles(); useEffect(() => { maplibregl.accessToken = mapboxAccessToken; }, [mapboxAccessToken]); useEffect(() => { switcher.updateStyles(mapStyles, defaultMapLayer); }, [mapStyles, defaultMapLayer]); useEffect(() => { const listener = (ready) => setMapReady(ready); addReadyListener(listener); return () => { removeReadyListener(listener); }; }, []); useLayoutEffect(() => { const currentEl = containerEl.current; currentEl.appendChild(element); map.resize(); return () => { currentEl.removeChild(element); }; }, [containerEl]); return (
{mapReady && children}
); }; export default MapView;