import { useEffect, useState } from "react";
import { Polyline } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import { CustomButton } from "@/components/button";
import { ReorderList } from "@/components/list";
import { CustomMapContainer, MapEventListener } from "@/components/map";
import { TextInput } from "@/components/input";
import { Node, LabeledPolygon } from "@/components/layer";
import useAdmin from "@/hook/useAdmin";
import useMapPolygonDraw from "@/hook/usePolygonDraw";
import useLocalVariable from "@/hook/useLocalVariable";
import { defaultZoneSettings } from "@/util/configurations";
import { ZoneTypes } from "@/util/types";
function Drawings({ localZoneSettings, addZone, removeZone }) {
const [polygons, setPolygons] = useState([]);
const { currentPolygon, highlightNodes, handleLeftClick, handleRightClick, handleMouseMove } = useMapPolygonDraw(polygons, addZone, removeZone);
useEffect(() => {
if (localZoneSettings.type == ZoneTypes.POLYGON) {
setPolygons(localZoneSettings.polygons.map(zone => zone.polygon));
}
}, [localZoneSettings]);
return (<>
{localZoneSettings.polygons.map((zone, i) => )}
{ currentPolygon.length > 0 && <>
>}
{highlightNodes.map((node, i) => )}
>);
}
export default function PolygonZoneSelector() {
const defaultDuration = 10;
const {zoneSettings, outOfZoneDelay, updateSettings} = useAdmin();
const [localZoneSettings, setLocalZoneSettings, applyLocalZoneSettings] = useLocalVariable(zoneSettings, (e) => updateSettings({zone: e}));
const [localOutOfZoneDelay, setLocalOutOfZoneDelay, applyLocalOutOfZoneDelay] = useLocalVariable(outOfZoneDelay, (e) => updateSettings({outOfZoneDelay: e}));
useEffect(() => {
if (localZoneSettings.type != ZoneTypes.POLYGON) {
setLocalZoneSettings(defaultZoneSettings.polygon);
}
}, [localZoneSettings]);
function getNewPolygonName() {
const existingIds = new Set(localZoneSettings.polygons.map(zone => zone.id));
for (let i = 0; i < 26; i++) {
const letter = String.fromCharCode(65 + i);
if (!existingIds.has(letter)) {
return letter;
}
}
return "XXX";
}
function setLocalZoneSettingsPolygons(polygons) {
setLocalZoneSettings({type: localZoneSettings.type, polygons: polygons});
}
function addZone(polygon) {
setLocalZoneSettingsPolygons([...localZoneSettings.polygons, {id: getNewPolygonName(), polygon: polygon, duration: defaultDuration}]);
}
function removeZone(i) {
setLocalZoneSettingsPolygons(localZoneSettings.polygons.filter((_, index) => index !== i));
}
function updateDuration(id, duration) {
setLocalZoneSettingsPolygons(localZoneSettings.polygons.map(zone => zone.id === id ? {...zone, duration: duration} : zone));
}
function handleSubmit() {
applyLocalZoneSettings();
applyLocalOutOfZoneDelay();
}
function customStringToInt(e) {
return parseInt(e, 10) || null;
}
return (
{localZoneSettings.type == ZoneTypes.POLYGON && <>
Reduction order
{ (zone) =>
Zone {zone.id}
updateDuration(zone.id, customStringToInt(e.target.value))}/>
}
Timeout
setLocalOutOfZoneDelay(customStringToInt(e.target.value))}/>
Apply
>}
);
}