import { Polygon } from "@/types/canvas"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogTitle, } from "@/components/ui/dialog"; import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"; import { Drawer, DrawerContent, DrawerTrigger } from "../ui/drawer"; import { useMemo, useState } from "react"; import { Input } from "../ui/input"; import { GeneralFilterContent } from "../filter/ReviewFilterGroup"; import { FaObjectGroup } from "react-icons/fa"; import { Button } from "../ui/button"; import { ATTRIBUTES, FrigateConfig } from "@/types/frigateConfig"; import useSWR from "swr"; import { isMobile } from "react-device-detect"; type ZoneObjectSelectorProps = { camera: string; zoneName: string; allLabels: string[]; updateLabelFilter: (labels: string[] | undefined) => void; }; export function ZoneObjectSelector({ camera, zoneName, allLabels, updateLabelFilter, }: ZoneObjectSelectorProps) { const { data: config } = useSWR("config"); const [open, setOpen] = useState(false); const cameraConfig = useMemo(() => { if (config && camera) { return config.cameras[camera]; } }, [config, camera]); const zoneLabels = useMemo(() => { if (!cameraConfig || !zoneName) { return []; } console.log(zoneName); const labels = new Set(); // console.log("zone name", zoneName); // console.log(cameraConfig.zones[zoneName].objects); cameraConfig.objects.track.forEach((label) => { if (!ATTRIBUTES.includes(label)) { labels.add(label); } }); cameraConfig.zones[zoneName].objects.forEach((label) => { labels.add(label); }); return [...labels].sort() || []; }, [cameraConfig, zoneName]); const [currentLabels, setCurrentLabels] = useState( zoneLabels, ); const trigger = ( ); const content = ( setOpen(false)} /> ); if (isMobile) { return ( { if (!open) { setCurrentLabels(zoneLabels); } setOpen(open); }} > {trigger} {content} ); } return ( { if (!open) { setCurrentLabels(zoneLabels); } setOpen(open); }} > {trigger} {content} ); } type ZoneControlsProps = { camera: string; polygons: Polygon[]; setPolygons: React.Dispatch>; activePolygonIndex: number | null; setActivePolygonIndex: React.Dispatch>; }; export function ZoneControls({ camera, polygons, setPolygons, activePolygonIndex, setActivePolygonIndex, }: ZoneControlsProps) { 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", camera: camera, color: updatedPolygons[activePolygonIndex].color ?? [220, 0, 0], }; setPolygons(updatedPolygons); } }; const handleNewPolygon = (zoneName: string) => { setPolygons([ ...(polygons || []), { points: [], isFinished: false, name: zoneName, camera: camera, color: [220, 0, 0], }, ]); setActivePolygonIndex(polygons.length); }; return (
{ setDialogOpen(open); if (!open) { setZoneName(""); } }} > New Zone Enter a label for your zone. Do not include spaces, and don't use the name of a camera. <> { setInvalidName( Object.keys(config.cameras).includes(e.target.value) || e.target.value.includes(" "), ); setZoneName(e.target.value); }} /> {invalidName && (
Zone name is not valid.
)}
); } export default ZoneControls;