import { TileLayer, MapContainer, useMapEvents } from "react-leaflet"; import "leaflet/dist/leaflet.css"; import * as React from "react"; import { Item, Tag, ItemsApi, LayerProps, UtopiaMapProps } from "../../types" import "./UtopiaMap.css" import { LatLng } from "leaflet"; import MarkerClusterGroup from 'react-leaflet-cluster' import AddButton from "./Subcomponents/AddButton"; import { useState } from "react"; import { ItemFormPopupProps } from "./Subcomponents/ItemFormPopup"; import { ItemsProvider } from "./hooks/useItems"; import { TagsProvider } from "./hooks/useTags"; import { LayersProvider } from "./hooks/useLayers"; export interface MapEventListenerProps { selectMode: LayerProps | null, setSelectMode: React.Dispatch, setItemFormPopup: React.Dispatch> } function MapEventListener(props: MapEventListenerProps) { useMapEvents({ click: (e) => { console.log(e.latlng.lat + ',' + e.latlng.lng); if (props.selectMode != null) { props.setItemFormPopup({ layer: props.selectMode, position: e.latlng }) props.setSelectMode(null) } }, resize: () => { console.log("resize"); } }) return null } // for refreshing map on resize (needs to be implemented) const mapDivRef = React.createRef(); function UtopiaMap({ height = "500px", width = "100%", center = new LatLng(50.6, 9.5), zoom = 10, children } : UtopiaMapProps) { const [selectMode, setSelectMode] = useState(null); const [itemFormPopup, setItemFormPopup] = useState(null); return (
{ React.Children.toArray(children).map((child) => React.isValidElement<{ setItemFormPopup: React.Dispatch>, itemFormPopup: ItemFormPopupProps | null }>(child) ? React.cloneElement(child, { setItemFormPopup: setItemFormPopup, itemFormPopup: itemFormPopup }) : child ) } {selectMode != null &&
Select {selectMode.name} position!
}
); } export { UtopiaMap, Item, Tag, ItemsApi };