import Heading from "../ui/heading"; import { Separator } from "../ui/separator"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { useCallback, useEffect, useMemo, useState } from "react"; import { FrigateConfig } from "@/types/frigateConfig"; import useSWR from "swr"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { ZoneFormValuesType, Polygon } from "@/types/canvas"; import { reviewQueries } from "@/utils/zoneEdutUtil"; import { Switch } from "../ui/switch"; import { Label } from "../ui/label"; import PolygonEditControls from "./PolygonEditControls"; import { FaCheckCircle } from "react-icons/fa"; import axios from "axios"; import { Toaster } from "@/components/ui/sonner"; import { toast } from "sonner"; import { flattenPoints, interpolatePoints } from "@/utils/canvasUtil"; import ActivityIndicator from "../indicators/activity-indicator"; import { getAttributeLabels } from "@/utils/iconUtil"; import { Trans, useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import { LuExternalLink } from "react-icons/lu"; import { useDocDomain } from "@/hooks/use-doc-domain"; import { getTranslatedLabel } from "@/utils/i18n"; type ZoneEditPaneProps = { polygons?: Polygon[]; setPolygons: React.Dispatch>; activePolygonIndex?: number; scaledWidth?: number; scaledHeight?: number; isLoading: boolean; setIsLoading: React.Dispatch>; onSave?: () => void; onCancel?: () => void; setActiveLine: React.Dispatch>; snapPoints: boolean; setSnapPoints: React.Dispatch>; }; export default function ZoneEditPane({ polygons, setPolygons, activePolygonIndex, scaledWidth, scaledHeight, isLoading, setIsLoading, onSave, onCancel, setActiveLine, snapPoints, setSnapPoints, }: ZoneEditPaneProps) { const { t } = useTranslation(["views/settings"]); const { getLocaleDocUrl } = useDocDomain(); const { data: config, mutate: updateConfig } = useSWR("config"); const cameras = useMemo(() => { if (!config) { return []; } return Object.values(config.cameras) .filter((conf) => conf.ui.dashboard && conf.enabled_in_config) .sort((aConf, bConf) => aConf.ui.order - bConf.ui.order); }, [config]); const polygon = useMemo(() => { if (polygons && activePolygonIndex !== undefined) { return polygons[activePolygonIndex]; } else { return null; } }, [polygons, activePolygonIndex]); const cameraConfig = useMemo(() => { if (polygon?.camera && config) { return config.cameras[polygon.camera]; } }, [polygon, config]); const [lineA, lineB, lineC, lineD] = useMemo(() => { const distances = polygon?.camera && polygon?.name && config?.cameras[polygon.camera]?.zones[polygon.name]?.distances; return Array.isArray(distances) ? distances.map((value) => parseFloat(value) || 0) : [undefined, undefined, undefined, undefined]; }, [polygon, config]); const formSchema = z .object({ name: z .string() .min(2, { message: t( "masksAndZones.form.zoneName.error.mustBeAtLeastTwoCharacters", ), }) .transform((val: string) => val.trim().replace(/\s+/g, "_")) .refine( (value: string) => { return !cameras.map((cam) => cam.name).includes(value); }, { message: t( "masksAndZones.form.zoneName.error.mustNotBeSameWithCamera", ), }, ) .refine( (value: string) => { const otherPolygonNames = polygons ?.filter((_, index) => index !== activePolygonIndex) .map((polygon) => polygon.name) || []; return !otherPolygonNames.includes(value); }, { message: t("masksAndZones.form.zoneName.error.alreadyExists"), }, ) .refine( (value: string) => { return !value.includes("."); }, { message: t( "masksAndZones.form.zoneName.error.mustNotContainPeriod", ), }, ) .refine((value: string) => /^[a-zA-Z0-9_-]+$/.test(value), { message: t("masksAndZones.form.zoneName.error.hasIllegalCharacter"), }), inertia: z.coerce .number() .min(1, { message: t("masksAndZones.form.inertia.error.mustBeAboveZero"), }) .or(z.literal("")), loitering_time: z.coerce .number() .min(0, { message: t( "masksAndZones.form.loiteringTime.error.mustBeGreaterOrEqualZero", ), }) .optional() .or(z.literal("")), isFinished: z.boolean().refine(() => polygon?.isFinished === true, { message: t("masksAndZones.form.polygonDrawing.error.mustBeFinished"), }), objects: z.array(z.string()).optional(), review_alerts: z.boolean().default(false).optional(), review_detections: z.boolean().default(false).optional(), speedEstimation: z.boolean().default(false), lineA: z.coerce .number() .min(0.1, { message: t("masksAndZones.form.distance.error.text"), }) .optional() .or(z.literal("")), lineB: z.coerce .number() .min(0.1, { message: t("masksAndZones.form.distance.error.text"), }) .optional() .or(z.literal("")), lineC: z.coerce .number() .min(0.1, { message: t("masksAndZones.form.distance.error.text"), }) .optional() .or(z.literal("")), lineD: z.coerce .number() .min(0.1, { message: t("masksAndZones.form.distance.error.text"), }) .optional() .or(z.literal("")), speed_threshold: z.coerce .number() .min(0.1, { message: t("masksAndZones.form.speed.error.mustBeGreaterOrEqualTo"), }) .optional() .or(z.literal("")), }) .refine( (data) => { if (data.speedEstimation) { return !!data.lineA && !!data.lineB && !!data.lineC && !!data.lineD; } return true; }, { message: t("masksAndZones.form.distance.error.mustBeFilled"), path: ["speedEstimation"], }, ) .refine( (data) => { // Prevent speed estimation when loitering_time is greater than 0 return !( data.speedEstimation && data.loitering_time && data.loitering_time > 0 ); }, { message: t( "masksAndZones.zones.speedThreshold.toast.error.loiteringTimeError", ), path: ["loitering_time"], }, ); const form = useForm>({ resolver: zodResolver(formSchema), mode: "onBlur", defaultValues: { name: polygon?.name ?? "", inertia: polygon?.camera && polygon?.name && config?.cameras[polygon.camera]?.zones[polygon.name]?.inertia, loitering_time: polygon?.camera && polygon?.name && config?.cameras[polygon.camera]?.zones[polygon.name]?.loitering_time, isFinished: polygon?.isFinished ?? false, objects: polygon?.objects ?? [], speedEstimation: !!(lineA || lineB || lineC || lineD), lineA, lineB, lineC, lineD, speed_threshold: polygon?.camera && polygon?.name && config?.cameras[polygon.camera]?.zones[polygon.name]?.speed_threshold, }, }); useEffect(() => { if ( form.watch("speedEstimation") && polygon && polygon.points.length !== 4 ) { toast.error( t("masksAndZones.zones.speedThreshold.toast.error.pointLengthError"), ); form.setValue("speedEstimation", false); } }, [polygon, form, t]); const saveToConfig = useCallback( async ( { name: zoneName, inertia, loitering_time, objects: form_objects, speedEstimation, lineA, lineB, lineC, lineD, speed_threshold, }: ZoneFormValuesType, // values submitted via the form objects: string[], ) => { if (!scaledWidth || !scaledHeight || !polygon) { return; } let mutatedConfig = config; let alertQueries = ""; let detectionQueries = ""; const renamingZone = zoneName != polygon.name && polygon.name != ""; if (renamingZone) { // rename - delete old zone and replace with new const zoneInAlerts = cameraConfig?.review.alerts.required_zones.includes(polygon.name) ?? false; const zoneInDetections = cameraConfig?.review.detections.required_zones.includes( polygon.name, ) ?? false; const { alertQueries: renameAlertQueries, detectionQueries: renameDetectionQueries, } = reviewQueries( polygon.name, false, false, polygon.camera, cameraConfig?.review.alerts.required_zones || [], cameraConfig?.review.detections.required_zones || [], ); try { await axios.put( `config/set?cameras.${polygon.camera}.zones.${polygon.name}${renameAlertQueries}${renameDetectionQueries}`, { requires_restart: 0, }, ); // Wait for the config to be updated mutatedConfig = await updateConfig(); } catch (error) { toast.error(t("toast.save.error.noMessage", { ns: "common" }), { position: "top-center", }); return; } // make sure new zone name is readded to review ({ alertQueries, detectionQueries } = reviewQueries( zoneName, zoneInAlerts, zoneInDetections, polygon.camera, mutatedConfig?.cameras[polygon.camera]?.review.alerts .required_zones || [], mutatedConfig?.cameras[polygon.camera]?.review.detections .required_zones || [], )); } const coordinates = flattenPoints( interpolatePoints(polygon.points, scaledWidth, scaledHeight, 1, 1), ).join(","); let objectQueries = objects .map( (object) => `&cameras.${polygon?.camera}.zones.${zoneName}.objects=${object}`, ) .join(""); const same_objects = form_objects.length == objects.length && form_objects.every(function (element, index) { return element === objects[index]; }); // deleting objects if (!objectQueries && !same_objects && !renamingZone) { objectQueries = `&cameras.${polygon?.camera}.zones.${zoneName}.objects`; } let inertiaQuery = ""; if (inertia) { inertiaQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.inertia=${inertia}`; } let loiteringTimeQuery = ""; if (loitering_time >= 0) { loiteringTimeQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.loitering_time=${loitering_time}`; } let distancesQuery = ""; const distances = [lineA, lineB, lineC, lineD].filter(Boolean).join(","); if (speedEstimation) { distancesQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.distances=${distances}`; } else { if (distances != "") { distancesQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.distances`; } } let speedThresholdQuery = ""; if (speed_threshold >= 0 && speedEstimation) { speedThresholdQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.speed_threshold=${speed_threshold}`; } else { if ( polygon?.camera && polygon?.name && config?.cameras[polygon.camera]?.zones[polygon.name]?.speed_threshold ) { speedThresholdQuery = `&cameras.${polygon?.camera}.zones.${zoneName}.speed_threshold`; } } axios .put( `config/set?cameras.${polygon?.camera}.zones.${zoneName}.coordinates=${coordinates}${inertiaQuery}${loiteringTimeQuery}${speedThresholdQuery}${distancesQuery}${objectQueries}${alertQueries}${detectionQueries}`, { requires_restart: 0 }, ) .then((res) => { if (res.status === 200) { toast.success( t("masksAndZones.zones.toast.success", { zoneName, }), { position: "top-center", }, ); updateConfig(); } else { toast.error( t("toast.save.error.title", { errorMessage: res.statusText, ns: "common", }), { position: "top-center", }, ); } }) .catch((error) => { const errorMessage = error.response?.data?.message || error.response?.data?.detail || "Unknown error"; toast.error( t("toast.save.error.title", { errorMessage, ns: "common", }), { position: "top-center", }, ); }) .finally(() => { setIsLoading(false); }); }, [ config, updateConfig, polygon, scaledWidth, scaledHeight, setIsLoading, cameraConfig, t, ], ); function onSubmit(values: z.infer) { if (activePolygonIndex === undefined || !values || !polygons) { return; } setIsLoading(true); saveToConfig( values as ZoneFormValuesType, polygons[activePolygonIndex].objects, ); if (onSave) { onSave(); } } useEffect(() => { document.title = t("masksAndZones.zones.documentTitle"); }, [t]); if (!polygon) { return; } return ( <> {polygon.name.length ? t("masksAndZones.zones.edit") : t("masksAndZones.zones.add")}

{t("masksAndZones.zones.desc.title")}

{polygons && activePolygonIndex !== undefined && (
{t("masksAndZones.zones.point", { count: polygons[activePolygonIndex].points.length, })} {polygons[activePolygonIndex].isFinished && ( )}
)}
{t("masksAndZones.zones.clickDrawPolygon")}
( {t("masksAndZones.zones.name.title")} {t("masksAndZones.zones.name.tips")} )} /> ( {t("masksAndZones.zones.inertia.title")} masksAndZones.zones.inertia.desc )} /> ( {t("masksAndZones.zones.loiteringTime.title")} masksAndZones.zones.loiteringTime.desc )} /> {t("masksAndZones.zones.objects.title")} {t("masksAndZones.zones.objects.desc")} { if (activePolygonIndex === undefined || !polygons) { return; } const updatedPolygons = [...polygons]; const activePolygon = updatedPolygons[activePolygonIndex]; updatedPolygons[activePolygonIndex] = { ...activePolygon, objects: objects ?? [], }; setPolygons(updatedPolygons); }} /> (
{t("masksAndZones.zones.speedEstimation.title")} { if ( checked && polygons && activePolygonIndex && polygons[activePolygonIndex].points.length !== 4 ) { toast.error( t( "masksAndZones.zones.speedThreshold.toast.error.pointLengthError", ), ); return; } const loiteringTime = form.getValues("loitering_time"); if (checked && loiteringTime && loiteringTime > 0) { toast.error( t( "masksAndZones.zones.speedThreshold.toast.error.loiteringTimeError", ), ); } field.onChange(checked); }} />
{t("masksAndZones.zones.speedEstimation.desc")}
{t("masksAndZones.zones.speedEstimation.docs")}
)} /> {form.watch("speedEstimation") && polygons && activePolygonIndex !== undefined && polygons[activePolygonIndex].points.length === 4 && ( <> ( {t( "masksAndZones.zones.speedEstimation.lineADistance", { unit: config?.ui.unit_system == "imperial" ? t("unit.length.feet", { ns: "common" }) : t("unit.length.meters", { ns: "common" }), }, )} setActiveLine(1)} onBlur={() => setActiveLine(undefined)} /> )} /> ( {t( "masksAndZones.zones.speedEstimation.lineBDistance", { unit: config?.ui.unit_system == "imperial" ? t("unit.length.feet", { ns: "common" }) : t("unit.length.meters", { ns: "common" }), }, )} setActiveLine(2)} onBlur={() => setActiveLine(undefined)} /> )} /> ( {t( "masksAndZones.zones.speedEstimation.lineCDistance", { unit: config?.ui.unit_system == "imperial" ? t("unit.length.feet", { ns: "common" }) : t("unit.length.meters", { ns: "common" }), }, )} setActiveLine(3)} onBlur={() => setActiveLine(undefined)} /> )} /> ( {t( "masksAndZones.zones.speedEstimation.lineDDistance", { unit: config?.ui.unit_system == "imperial" ? t("unit.length.feet", { ns: "common" }) : t("unit.length.meters", { ns: "common" }), }, )} setActiveLine(4)} onBlur={() => setActiveLine(undefined)} /> )} /> ( {t("masksAndZones.zones.speedThreshold.title", { ns: "views/settings", unit: config?.ui.unit_system == "imperial" ? t("unit.speed.mph", { ns: "common" }) : t("unit.speed.kph", { ns: "common" }), })} {t("masksAndZones.zones.speedThreshold.desc")} )} /> )} ( )} />
); } type ZoneObjectSelectorProps = { camera: string; zoneName: string; selectedLabels: string[]; updateLabelFilter: (labels: string[] | undefined) => void; }; export function ZoneObjectSelector({ camera, zoneName, selectedLabels, updateLabelFilter, }: ZoneObjectSelectorProps) { const { t } = useTranslation(["views/settings"]); const { data: config } = useSWR("config"); const attributeLabels = useMemo(() => { if (!config) { return []; } return getAttributeLabels(config); }, [config]); const cameraConfig = useMemo(() => { if (config && camera) { return config.cameras[camera]; } }, [config, camera]); const allLabels = useMemo(() => { if (!cameraConfig || !config) { return []; } const labels = new Set(); cameraConfig.objects.track.forEach((label) => { if (!attributeLabels.includes(label)) { labels.add(label); } }); if (zoneName) { if (cameraConfig.zones[zoneName]) { cameraConfig.zones[zoneName].objects.forEach((label) => { if (!attributeLabels.includes(label)) { labels.add(label); } }); } } return [...labels].sort() || []; }, [config, cameraConfig, attributeLabels, zoneName]); const [currentLabels, setCurrentLabels] = useState( selectedLabels, ); useEffect(() => { updateLabelFilter(currentLabels); // we know that these deps are correct // eslint-disable-next-line react-hooks/exhaustive-deps }, [currentLabels]); return ( <>
{ if (isChecked) { setCurrentLabels([]); } }} />
{allLabels.map((item) => (
{ if (isChecked) { const updatedLabels = currentLabels ? [...currentLabels] : []; updatedLabels.push(item); setCurrentLabels(updatedLabels); } else { const updatedLabels = currentLabels ? [...currentLabels] : []; // can not deselect the last item if (updatedLabels.length > 1) { updatedLabels.splice(updatedLabels.indexOf(item), 1); setCurrentLabels(updatedLabels); } } }} />
))}
); }