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