2024-04-19 13:34:07 +02:00
|
|
|
import {
|
|
|
|
AlertDialog,
|
|
|
|
AlertDialogAction,
|
|
|
|
AlertDialogCancel,
|
|
|
|
AlertDialogContent,
|
|
|
|
AlertDialogDescription,
|
|
|
|
AlertDialogFooter,
|
|
|
|
AlertDialogHeader,
|
|
|
|
AlertDialogTitle,
|
|
|
|
} from "../ui/alert-dialog";
|
|
|
|
import {
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
} from "@/components/ui/dropdown-menu";
|
|
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip";
|
|
|
|
import { LuCopy, LuPencil } from "react-icons/lu";
|
|
|
|
import { FaDrawPolygon, FaObjectGroup } from "react-icons/fa";
|
|
|
|
import { BsPersonBoundingBox } from "react-icons/bs";
|
|
|
|
import { HiOutlineDotsVertical, HiTrash } from "react-icons/hi";
|
2024-06-16 20:58:28 +02:00
|
|
|
import { isDesktop, isMobile } from "react-device-detect";
|
2024-04-19 13:34:07 +02:00
|
|
|
import {
|
|
|
|
flattenPoints,
|
|
|
|
parseCoordinates,
|
|
|
|
toRGBColorString,
|
|
|
|
} from "@/utils/canvasUtil";
|
|
|
|
import { Polygon, PolygonType } from "@/types/canvas";
|
2024-05-18 20:55:17 +02:00
|
|
|
import { useCallback, useContext, useMemo, useState } from "react";
|
2024-04-19 13:34:07 +02:00
|
|
|
import axios from "axios";
|
|
|
|
import { Toaster } from "@/components/ui/sonner";
|
|
|
|
import { toast } from "sonner";
|
|
|
|
import useSWR from "swr";
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
import { reviewQueries } from "@/utils/zoneEdutUtil";
|
|
|
|
import IconWrapper from "../ui/icon-wrapper";
|
2024-05-18 20:55:17 +02:00
|
|
|
import { StatusBarMessagesContext } from "@/context/statusbar-provider";
|
2024-10-17 13:30:52 +02:00
|
|
|
import { buttonVariants } from "../ui/button";
|
2024-04-19 13:34:07 +02:00
|
|
|
|
|
|
|
type PolygonItemProps = {
|
|
|
|
polygon: Polygon;
|
|
|
|
index: number;
|
|
|
|
hoveredPolygonIndex: number | null;
|
|
|
|
setHoveredPolygonIndex: (index: number | null) => void;
|
|
|
|
setActivePolygonIndex: (index: number | undefined) => void;
|
|
|
|
setEditPane: (type: PolygonType) => void;
|
|
|
|
handleCopyCoordinates: (index: number) => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function PolygonItem({
|
|
|
|
polygon,
|
|
|
|
index,
|
|
|
|
hoveredPolygonIndex,
|
|
|
|
setHoveredPolygonIndex,
|
|
|
|
setActivePolygonIndex,
|
|
|
|
setEditPane,
|
|
|
|
handleCopyCoordinates,
|
|
|
|
}: PolygonItemProps) {
|
|
|
|
const { data: config, mutate: updateConfig } =
|
|
|
|
useSWR<FrigateConfig>("config");
|
|
|
|
const [deleteDialogOpen, setDeleteDialogOpen] = useState(false);
|
2024-05-18 20:55:17 +02:00
|
|
|
const { addMessage } = useContext(StatusBarMessagesContext)!;
|
2024-04-19 13:34:07 +02:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
const cameraConfig = useMemo(() => {
|
|
|
|
if (polygon?.camera && config) {
|
|
|
|
return config.cameras[polygon.camera];
|
|
|
|
}
|
|
|
|
}, [polygon, config]);
|
|
|
|
|
|
|
|
const polygonTypeIcons = {
|
|
|
|
zone: FaDrawPolygon,
|
|
|
|
motion_mask: FaObjectGroup,
|
|
|
|
object_mask: BsPersonBoundingBox,
|
|
|
|
};
|
|
|
|
|
|
|
|
const PolygonItemIcon = polygon ? polygonTypeIcons[polygon.type] : undefined;
|
|
|
|
|
|
|
|
const saveToConfig = useCallback(
|
|
|
|
async (polygon: Polygon) => {
|
|
|
|
if (!polygon || !cameraConfig) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let url = "";
|
|
|
|
if (polygon.type == "zone") {
|
|
|
|
const { alertQueries, detectionQueries } = reviewQueries(
|
|
|
|
polygon.name,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
polygon.camera,
|
|
|
|
cameraConfig?.review.alerts.required_zones || [],
|
|
|
|
cameraConfig?.review.detections.required_zones || [],
|
|
|
|
);
|
|
|
|
url = `cameras.${polygon.camera}.zones.${polygon.name}${alertQueries}${detectionQueries}`;
|
|
|
|
}
|
|
|
|
if (polygon.type == "motion_mask") {
|
|
|
|
const filteredMask = (
|
|
|
|
Array.isArray(cameraConfig.motion.mask)
|
|
|
|
? cameraConfig.motion.mask
|
|
|
|
: [cameraConfig.motion.mask]
|
|
|
|
).filter((_, currentIndex) => currentIndex !== polygon.typeIndex);
|
|
|
|
|
|
|
|
url = filteredMask
|
|
|
|
.map((pointsArray) => {
|
|
|
|
const coordinates = flattenPoints(
|
|
|
|
parseCoordinates(pointsArray),
|
|
|
|
).join(",");
|
|
|
|
return `cameras.${polygon?.camera}.motion.mask=${coordinates}&`;
|
|
|
|
})
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
// deleting last mask
|
|
|
|
url = `cameras.${polygon?.camera}.motion.mask&`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (polygon.type == "object_mask") {
|
|
|
|
let configObject;
|
|
|
|
let globalMask = false;
|
|
|
|
|
|
|
|
// global mask on camera for all objects
|
|
|
|
if (!polygon.objects.length) {
|
|
|
|
configObject = cameraConfig.objects.mask;
|
|
|
|
globalMask = true;
|
|
|
|
} else {
|
|
|
|
configObject = cameraConfig.objects.filters[polygon.objects[0]].mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!configObject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const globalObjectMasksArray = Array.isArray(cameraConfig.objects.mask)
|
|
|
|
? cameraConfig.objects.mask
|
|
|
|
: cameraConfig.objects.mask
|
|
|
|
? [cameraConfig.objects.mask]
|
|
|
|
: [];
|
|
|
|
|
|
|
|
let filteredMask;
|
|
|
|
if (globalMask) {
|
|
|
|
filteredMask = (
|
|
|
|
Array.isArray(configObject) ? configObject : [configObject]
|
|
|
|
).filter((_, currentIndex) => currentIndex !== polygon.typeIndex);
|
|
|
|
} else {
|
|
|
|
filteredMask = (
|
|
|
|
Array.isArray(configObject) ? configObject : [configObject]
|
|
|
|
)
|
|
|
|
.filter((mask) => !globalObjectMasksArray.includes(mask))
|
|
|
|
.filter((_, currentIndex) => currentIndex !== polygon.typeIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
url = filteredMask
|
|
|
|
.map((pointsArray) => {
|
|
|
|
const coordinates = flattenPoints(
|
|
|
|
parseCoordinates(pointsArray),
|
|
|
|
).join(",");
|
|
|
|
return globalMask
|
|
|
|
? `cameras.${polygon?.camera}.objects.mask=${coordinates}&`
|
|
|
|
: `cameras.${polygon?.camera}.objects.filters.${polygon.objects[0]}.mask=${coordinates}&`;
|
|
|
|
})
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
// deleting last mask
|
|
|
|
url = globalMask
|
|
|
|
? `cameras.${polygon?.camera}.objects.mask&`
|
|
|
|
: `cameras.${polygon?.camera}.objects.filters.${polygon.objects[0]}.mask`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
|
|
await axios
|
|
|
|
.put(`config/set?${url}`, { requires_restart: 0 })
|
|
|
|
.then((res) => {
|
|
|
|
if (res.status === 200) {
|
|
|
|
toast.success(`${polygon?.name} has been deleted.`, {
|
|
|
|
position: "top-center",
|
|
|
|
});
|
|
|
|
updateConfig();
|
|
|
|
} else {
|
|
|
|
toast.error(`Failed to save config changes: ${res.statusText}`, {
|
|
|
|
position: "top-center",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
toast.error(
|
|
|
|
`Failed to save config changes: ${error.response.data.message}`,
|
|
|
|
{ position: "top-center" },
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[updateConfig, cameraConfig],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleDelete = () => {
|
|
|
|
setActivePolygonIndex(undefined);
|
|
|
|
saveToConfig(polygon);
|
2024-05-18 20:55:17 +02:00
|
|
|
addMessage(
|
|
|
|
"masks_zones",
|
|
|
|
"Restart required (masks/zones changed)",
|
|
|
|
undefined,
|
|
|
|
"masks_zones",
|
|
|
|
);
|
2024-04-19 13:34:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-05-04 21:54:50 +02:00
|
|
|
<Toaster position="top-center" closeButton={true} />
|
2024-04-19 13:34:07 +02:00
|
|
|
|
|
|
|
<div
|
|
|
|
key={index}
|
2024-05-14 17:06:44 +02:00
|
|
|
className="transition-background my-1.5 flex flex-row items-center justify-between rounded-lg p-1 duration-100"
|
2024-04-19 13:34:07 +02:00
|
|
|
data-index={index}
|
|
|
|
onMouseEnter={() => setHoveredPolygonIndex(index)}
|
|
|
|
onMouseLeave={() => setHoveredPolygonIndex(null)}
|
|
|
|
style={{
|
|
|
|
backgroundColor:
|
|
|
|
hoveredPolygonIndex === index
|
|
|
|
? toRGBColorString(polygon.color, false)
|
|
|
|
: "",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={`flex items-center ${
|
|
|
|
hoveredPolygonIndex === index
|
|
|
|
? "text-primary"
|
|
|
|
: "text-primary-variant"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{PolygonItemIcon && (
|
|
|
|
<PolygonItemIcon
|
2024-05-14 17:06:44 +02:00
|
|
|
className="mr-2 size-5"
|
2024-04-19 13:34:07 +02:00
|
|
|
style={{
|
|
|
|
fill: toRGBColorString(polygon.color, true),
|
|
|
|
color: toRGBColorString(polygon.color, true),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<p className="cursor-default">{polygon.name}</p>
|
|
|
|
</div>
|
|
|
|
<AlertDialog
|
|
|
|
open={deleteDialogOpen}
|
|
|
|
onOpenChange={() => setDeleteDialogOpen(!deleteDialogOpen)}
|
|
|
|
>
|
|
|
|
<AlertDialogContent>
|
|
|
|
<AlertDialogHeader>
|
|
|
|
<AlertDialogTitle>Confirm Delete</AlertDialogTitle>
|
|
|
|
</AlertDialogHeader>
|
|
|
|
<AlertDialogDescription>
|
|
|
|
Are you sure you want to delete the{" "}
|
|
|
|
{polygon.type.replace("_", " ")} <em>{polygon.name}</em>?
|
|
|
|
</AlertDialogDescription>
|
|
|
|
<AlertDialogFooter>
|
|
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
2024-10-17 13:30:52 +02:00
|
|
|
<AlertDialogAction
|
|
|
|
className={buttonVariants({ variant: "destructive" })}
|
|
|
|
onClick={handleDelete}
|
|
|
|
>
|
2024-04-19 13:34:07 +02:00
|
|
|
Delete
|
|
|
|
</AlertDialogAction>
|
|
|
|
</AlertDialogFooter>
|
|
|
|
</AlertDialogContent>
|
|
|
|
</AlertDialog>
|
|
|
|
|
|
|
|
{isMobile && (
|
|
|
|
<>
|
2024-06-16 20:58:28 +02:00
|
|
|
<DropdownMenu modal={!isDesktop}>
|
2024-04-19 13:34:07 +02:00
|
|
|
<DropdownMenuTrigger>
|
|
|
|
<HiOutlineDotsVertical className="size-5" />
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent>
|
|
|
|
<DropdownMenuItem
|
2024-10-23 00:07:42 +02:00
|
|
|
aria-label="Edit"
|
2024-04-19 13:34:07 +02:00
|
|
|
onClick={() => {
|
|
|
|
setActivePolygonIndex(index);
|
|
|
|
setEditPane(polygon.type);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Edit
|
|
|
|
</DropdownMenuItem>
|
2024-10-23 00:07:42 +02:00
|
|
|
<DropdownMenuItem
|
|
|
|
aria-label="Copy"
|
|
|
|
onClick={() => handleCopyCoordinates(index)}
|
|
|
|
>
|
2024-04-19 13:34:07 +02:00
|
|
|
Copy
|
|
|
|
</DropdownMenuItem>
|
|
|
|
<DropdownMenuItem
|
2024-10-23 00:07:42 +02:00
|
|
|
aria-label="Delete"
|
2024-04-19 13:34:07 +02:00
|
|
|
disabled={isLoading}
|
|
|
|
onClick={() => setDeleteDialogOpen(true)}
|
|
|
|
>
|
|
|
|
Delete
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{!isMobile && hoveredPolygonIndex === index && (
|
2024-05-14 17:06:44 +02:00
|
|
|
<div className="flex flex-row items-center gap-2">
|
2024-04-19 13:34:07 +02:00
|
|
|
<Tooltip>
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
<IconWrapper
|
|
|
|
icon={LuPencil}
|
|
|
|
className={`size-[15px] cursor-pointer ${hoveredPolygonIndex === index && "text-primary-variant"}`}
|
|
|
|
onClick={() => {
|
|
|
|
setActivePolygonIndex(index);
|
|
|
|
setEditPane(polygon.type);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</TooltipTrigger>
|
|
|
|
<TooltipContent>Edit</TooltipContent>
|
|
|
|
</Tooltip>
|
|
|
|
|
|
|
|
<Tooltip>
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
<IconWrapper
|
|
|
|
icon={LuCopy}
|
|
|
|
className={`size-[15px] cursor-pointer ${
|
|
|
|
hoveredPolygonIndex === index && "text-primary-variant"
|
|
|
|
}`}
|
|
|
|
onClick={() => handleCopyCoordinates(index)}
|
|
|
|
/>
|
|
|
|
</TooltipTrigger>
|
|
|
|
<TooltipContent>Copy coordinates</TooltipContent>
|
|
|
|
</Tooltip>
|
|
|
|
|
|
|
|
<Tooltip>
|
|
|
|
<TooltipTrigger asChild>
|
|
|
|
<IconWrapper
|
|
|
|
icon={HiTrash}
|
|
|
|
className={`size-[15px] cursor-pointer ${
|
|
|
|
hoveredPolygonIndex === index &&
|
2024-05-14 17:06:44 +02:00
|
|
|
"fill-primary-variant text-primary-variant"
|
2024-04-19 13:34:07 +02:00
|
|
|
}`}
|
|
|
|
onClick={() => !isLoading && setDeleteDialogOpen(true)}
|
|
|
|
/>
|
|
|
|
</TooltipTrigger>
|
|
|
|
<TooltipContent>Delete</TooltipContent>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|