import React, { useMemo, useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import { Popover, RadioGroup } from '@headlessui/react'; import { usePopper } from 'react-popper'; import { MapIcon } from '@heroicons/react/outline'; import { Slider } from '@reach/slider'; import '@reach/slider/styles.css'; import { getMapStyle, getLayerName, NBS } from './styles'; const STYLES = { ortho: 'Satellite', vector: 'Vectoriel', ign: 'Carte IGN' }; function optionalLayersMap(optionalLayers) { return Object.fromEntries( optionalLayers .filter((layer) => layer != 'cadastres') .map((layer) => [ layer, { enabled: true, opacity: 70, name: getLayerName(layer) } ]) ); } export function useMapStyle( optionalLayers, { onStyleChange, cadastreEnabled } ) { const [styleId, setStyle] = useState('ortho'); const [layers, setLayers] = useState(() => optionalLayersMap(optionalLayers)); const setLayerEnabled = (layer, enabled) => setLayers((optionalLayers) => { optionalLayers[layer].enabled = enabled; return { ...optionalLayers }; }); const setLayerOpacity = (layer, opacity) => setLayers((optionalLayers) => { optionalLayers[layer].opacity = opacity; return { ...optionalLayers }; }); const enabledLayers = Object.entries(layers).filter( ([, { enabled }]) => enabled ); const style = useMemo( () => getMapStyle( styleId, enabledLayers.map(([layer]) => layer), Object.fromEntries( enabledLayers.map(([layer, { opacity }]) => [layer, opacity]) ) ), [ styleId, enabledLayers.map(([layer, { opacity }]) => `${layer}-${opacity}`) ] ); useEffect(() => onStyleChange(), [styleId, cadastreEnabled]); return { style, layers, setStyle, setLayerEnabled, setLayerOpacity }; } function MapStyleControl({ style, layers, setStyle, setLayerEnabled, setLayerOpacity }) { const [buttonElement, setButtonElement] = useState(); const [panelElement, setPanelElement] = useState(); const { styles, attributes } = usePopper(buttonElement, panelElement, { placement: 'bottom-end' }); return (