import { Button } from "../ui/button"; import { Polygon } from "@/types/canvas"; import { Dialog, DialogContent, DialogFooter, DialogTitle, } from "@/components/ui/dialog"; import { useState } from "react"; import { Input } from "../ui/input"; import useSWR from "swr"; type PolygonCanvasProps = { camera: string; width: number; height: number; polygons: Polygon[]; setPolygons: React.Dispatch>; activePolygonIndex: number | null; setActivePolygonIndex: React.Dispatch>; }; export function PolygonControls({ polygons, setPolygons, activePolygonIndex, setActivePolygonIndex, }: PolygonCanvasProps) { const { data: config } = useSWR("config"); const [zoneName, setZoneName] = useState(); const [invalidName, setInvalidName] = useState(); const [dialogOpen, setDialogOpen] = useState(false); const undo = () => { if (activePolygonIndex !== null && polygons) { const updatedPolygons = [...polygons]; const activePolygon = updatedPolygons[activePolygonIndex]; if (activePolygon.points.length > 0) { updatedPolygons[activePolygonIndex] = { ...activePolygon, points: activePolygon.points.slice(0, -1), isFinished: false, }; setPolygons(updatedPolygons); } } }; const reset = () => { if (activePolygonIndex !== null) { const updatedPolygons = [...polygons]; updatedPolygons[activePolygonIndex] = { points: [], isFinished: false, name: "new", }; setPolygons(updatedPolygons); } }; const handleNewPolygon = (zoneName: string) => { setPolygons([ ...(polygons || []), { points: [], isFinished: false, name: zoneName, }, ]); setActivePolygonIndex(polygons.length); }; return (
{ setDialogOpen(open); if (!open) { setZoneName(""); } }} > New Zone <> { setInvalidName( Object.keys(config.cameras).includes(e.target.value), ); setZoneName(e.target.value); }} /> {invalidName && (
Zone names must not be the name of a camera.
)}
); } export default PolygonControls;