2021-05-07 14:30:12 +00:00
|
|
|
import React, { useCallback, useState, useRef, useMemo, useEffect } from "react"
|
2021-04-30 20:10:00 +00:00
|
|
|
import BoxFull from "../base/BoxFull"
|
|
|
|
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"
|
|
|
|
import { faMapPin, faPlus } from "@fortawesome/free-solid-svg-icons"
|
|
|
|
import FormInline from "../base/FormInline"
|
|
|
|
import Style from "./BoxConditionMap.module.css"
|
|
|
|
import ButtonIconOnly from "../base/ButtonIconOnly"
|
2021-05-01 00:33:46 +00:00
|
|
|
import { MapContainer, Marker, TileLayer } from "react-leaflet"
|
2021-04-30 20:10:00 +00:00
|
|
|
import useRepositoryEditor from "../../hooks/useRepositoryEditor"
|
2021-05-07 03:02:20 +00:00
|
|
|
import Condition from "../../utils/Condition"
|
2021-04-30 20:10:00 +00:00
|
|
|
|
|
|
|
|
2021-05-07 14:30:12 +00:00
|
|
|
const STARTING_POSITION = {lat: 41.89309, lng: 12.48289}
|
2021-05-01 00:33:46 +00:00
|
|
|
const STARTING_ZOOM = 3
|
|
|
|
|
2021-05-07 14:30:12 +00:00
|
|
|
// FIXME: this only works correctly at the equator!
|
|
|
|
/**
|
|
|
|
* https://wiki.openstreetmap.org/wiki/Zoom_levels
|
|
|
|
*/
|
|
|
|
const MPIXEL = [
|
|
|
|
156412,
|
|
|
|
78206,
|
|
|
|
39103,
|
|
|
|
19551,
|
|
|
|
9776,
|
|
|
|
4888,
|
|
|
|
2444,
|
|
|
|
1222,
|
|
|
|
610.984,
|
|
|
|
305.492,
|
|
|
|
152.746,
|
|
|
|
76.373,
|
|
|
|
38.187,
|
|
|
|
19.093,
|
|
|
|
9.547,
|
|
|
|
4.773,
|
|
|
|
2.387,
|
|
|
|
1.193,
|
|
|
|
0.596,
|
|
|
|
0.298,
|
|
|
|
0.149,
|
|
|
|
]
|
|
|
|
|
2021-05-01 00:33:46 +00:00
|
|
|
|
2021-04-30 20:10:00 +00:00
|
|
|
/**
|
|
|
|
* A {@link BoxFull} that allows the user to select a geographical location to use to filter tweets.
|
|
|
|
*
|
|
|
|
* @param props - Additional props to pass to the box.
|
|
|
|
* @returns {JSX.Element}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
export default function BoxConditionMap({ ...props }) {
|
2021-05-07 14:30:12 +00:00
|
|
|
const [position, setPosition] = useState(STARTING_POSITION)
|
|
|
|
const [zoom, setZoom] = useState(STARTING_ZOOM)
|
|
|
|
const [map, setMap] = useState(null)
|
2021-05-07 03:02:20 +00:00
|
|
|
const {addCondition} = useRepositoryEditor()
|
2021-04-30 20:10:00 +00:00
|
|
|
|
2021-05-07 14:30:12 +00:00
|
|
|
const onMove = useCallback(
|
|
|
|
() => {
|
|
|
|
setPosition(map.getCenter())
|
|
|
|
},
|
|
|
|
[map]
|
|
|
|
)
|
|
|
|
|
|
|
|
const onZoom = useCallback(
|
|
|
|
() => {
|
|
|
|
setZoom(map.getZoom())
|
|
|
|
},
|
|
|
|
[map]
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => {
|
|
|
|
if(map === null) return
|
|
|
|
|
|
|
|
map.on("move", onMove)
|
|
|
|
map.on("zoom", onZoom)
|
|
|
|
return () => {
|
|
|
|
map.off("move", onMove)
|
|
|
|
map.off("zoom", onZoom)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[map]
|
2021-05-07 03:02:20 +00:00
|
|
|
)
|
2021-04-30 20:10:00 +00:00
|
|
|
|
2021-05-07 03:02:20 +00:00
|
|
|
const onButtonClick = () => {
|
2021-05-07 14:30:12 +00:00
|
|
|
const mapSize = map.getSize()
|
|
|
|
const minSize = Math.min(mapSize.x, mapSize.y)
|
|
|
|
const radius = minSize * MPIXEL[zoom]
|
|
|
|
|
2021-05-07 03:02:20 +00:00
|
|
|
addCondition(new Condition(
|
|
|
|
"COORDINATES",
|
2021-05-07 14:30:12 +00:00
|
|
|
`< ${radius} ${position.lat} ${position.lng}`
|
2021-05-07 03:02:20 +00:00
|
|
|
))
|
2021-05-07 14:30:12 +00:00
|
|
|
setPosition(STARTING_POSITION)
|
2021-04-30 20:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2021-05-01 00:33:46 +00:00
|
|
|
<BoxFull
|
2021-05-07 14:30:12 +00:00
|
|
|
header={
|
|
|
|
<span>Search by <FontAwesomeIcon icon={faMapPin}/> zone</span>
|
|
|
|
}
|
2021-05-01 00:33:46 +00:00
|
|
|
childrenClassName={Style.BoxConditionMapContents}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
<MapContainer
|
|
|
|
center={STARTING_POSITION}
|
|
|
|
zoom={STARTING_ZOOM}
|
|
|
|
className={Style.MapContainer}
|
2021-05-07 14:30:12 +00:00
|
|
|
whenCreated={setMap}
|
2021-05-01 00:33:46 +00:00
|
|
|
>
|
2021-04-30 20:10:00 +00:00
|
|
|
<TileLayer
|
|
|
|
attribution='(c) <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
|
|
|
|
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
|
|
|
/>
|
2021-05-07 14:30:12 +00:00
|
|
|
<div className={"leaflet-top leaflet-right"}>
|
|
|
|
<div className={"leaflet-control"}>
|
|
|
|
<ButtonIconOnly
|
|
|
|
className={Style.Button}
|
|
|
|
icon={faPlus}
|
|
|
|
color={"Green"}
|
|
|
|
onClick={onButtonClick}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-30 20:10:00 +00:00
|
|
|
</MapContainer>
|
|
|
|
</BoxFull>
|
|
|
|
)
|
|
|
|
}
|