import { useEffect, useMemo } from "react";
import { Polyline } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import { ReorderList } from "@/components/list";
import { CustomMapContainer, MapEventListener } from "@/components/map";
import { NumberInput } from "@/components/input";
import { Node, PolygonZone, Label } from "@/components/layer";
import useMapPolygonDraw from "@/hook/usePolygonDraw";
import useLocalVariable from "@/hook/useLocalVariable";
import { defaultZoneSettings } from "@/config/configurations";
import { ZoneTypes } from "@/config/types";
import { emitSettings } from "@/services/socket/emitters";
import { useAdmin } from "@/context/adminContext";
function Drawings({ localZoneSettings, addZone, removeZone }) {
const polygons = useMemo(() => {
return localZoneSettings.type == ZoneTypes.POLYGON ? localZoneSettings.polygons.map(zone => zone.polygon) : [];
}, [localZoneSettings]);
const { currentPolygon, highlightNodes, handleLeftClick, handleRightClick, handleMouseMove } = useMapPolygonDraw(polygons, addZone, removeZone);
function getLabelPosition(polygon) {
const sum = polygon.reduce(
(acc, coord) => ({
lat: acc.lat + coord.lat,
lng: acc.lng + coord.lng
}),
{ lat: 0, lng: 0 }
);
// The calculated mean point can be out of the polygon
// Idea : take the mean point of the largest convex subpolygon
return {lat: sum.lat / polygon.length, lng: sum.lng / polygon.length};
}
return (<>
{localZoneSettings.polygons.map(zone =>
)}
{ currentPolygon.length > 0 && <>
>}
{highlightNodes.map((node, i) =>
)}
>);
}
export default function PolygonZoneSelector({ display }) {
const defaultDuration = 10;
const { settings } = useAdmin();
const [localZoneSettings, setLocalZoneSettings, applyLocalZoneSettings] = useLocalVariable(settings.zones, (e) => emitSettings({...settings, zone: e}));
const [localOutOfZoneDelay, setLocalOutOfZoneDelay, applyLocalOutOfZoneDelay] = useLocalVariable(settings.outOfZoneDelay, (e) => emitSettings({...settings, outOfZoneDelay: e}));
useEffect(() => {
if (!localZoneSettings || localZoneSettings.type != ZoneTypes.POLYGON) {
setLocalZoneSettings(defaultZoneSettings.polygon);
}
}, [localZoneSettings, setLocalZoneSettings]);
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();
}
return (
{localZoneSettings && localZoneSettings.type == ZoneTypes.POLYGON && <>
Ordre de réduction
{ (zone) =>
Zone {zone.id}
updateDuration(zone.id, value)}/>
}
>}
);
}