import * as React from "react"; import type { FeatureCollection } from "geojson"; import L, { LatLngLiteral } from "leaflet"; import "leaflet-defaulticon-compatibility"; import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css"; import "leaflet-draw/dist/leaflet.draw.css"; import "leaflet/dist/leaflet.css"; import { FeatureGroup, MapContainer, TileLayer, ZoomControl, } from "react-leaflet"; import { EditControl } from "react-leaflet-draw"; import MapEvents from "../MapEvents"; const center: LatLngLiteral = { lat: 47.0844, lng: 2.3964 }; export type ZoneEditorProps = { value: FeatureCollection; onChange: (geoJson: FeatureCollection) => void; }; export const ZoneEditor = ({ value, onChange }: ZoneEditorProps) => { const ref = React.useRef(null); React.useEffect(() => { if (ref.current?.getLayers().length === 0 && value) { L.geoJSON(value).eachLayer((layer) => { if ( layer instanceof L.Polyline || layer instanceof L.Polygon || layer instanceof L.Marker ) { if (layer?.feature?.properties.radius && ref.current) { new L.Circle(layer.feature.geometry.coordinates.slice().reverse(), { radius: layer.feature?.properties.radius, }).addTo(ref.current); } else { ref.current?.addLayer(layer); } } }); } }, [value]); const handleChange = () => { const geoJson = ref.current?.toGeoJSON(); if (geoJson?.type === "FeatureCollection") { onChange(geoJson); } }; return ( ); }; export default ZoneEditor;