2024-09-04 16:46:49 +03:00
|
|
|
import Heading from "@/components/ui/heading";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { Switch } from "@/components/ui/switch";
|
|
|
|
|
import { Event } from "@/types/event";
|
|
|
|
|
import { FrigateConfig } from "@/types/frigateConfig";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import axios from "axios";
|
|
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { LuExternalLink } from "react-icons/lu";
|
|
|
|
|
import { PiWarningCircle } from "react-icons/pi";
|
|
|
|
|
import { Link } from "react-router-dom";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
import {
|
|
|
|
|
Form,
|
|
|
|
|
FormControl,
|
|
|
|
|
FormDescription,
|
|
|
|
|
FormField,
|
|
|
|
|
FormItem,
|
|
|
|
|
FormLabel,
|
|
|
|
|
FormMessage,
|
|
|
|
|
} from "@/components/ui/form";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Separator } from "@/components/ui/separator";
|
2025-03-16 18:36:20 +03:00
|
|
|
import { Trans, useTranslation } from "react-i18next";
|
2025-05-28 15:10:45 +03:00
|
|
|
import { useDocDomain } from "@/hooks/use-doc-domain";
|
2024-09-04 16:46:49 +03:00
|
|
|
|
|
|
|
|
type AnnotationSettingsPaneProps = {
|
|
|
|
|
event: Event;
|
|
|
|
|
showZones: boolean;
|
|
|
|
|
setShowZones: React.Dispatch<React.SetStateAction<boolean>>;
|
|
|
|
|
annotationOffset: number;
|
|
|
|
|
setAnnotationOffset: React.Dispatch<React.SetStateAction<number>>;
|
|
|
|
|
};
|
|
|
|
|
export function AnnotationSettingsPane({
|
|
|
|
|
event,
|
|
|
|
|
showZones,
|
|
|
|
|
setShowZones,
|
|
|
|
|
annotationOffset,
|
|
|
|
|
setAnnotationOffset,
|
|
|
|
|
}: AnnotationSettingsPaneProps) {
|
2025-03-16 18:36:20 +03:00
|
|
|
const { t } = useTranslation(["views/explore"]);
|
2025-05-28 15:10:45 +03:00
|
|
|
const { getLocaleDocUrl } = useDocDomain();
|
2025-03-16 18:36:20 +03:00
|
|
|
|
2024-09-04 16:46:49 +03:00
|
|
|
const { data: config, mutate: updateConfig } =
|
|
|
|
|
useSWR<FrigateConfig>("config");
|
|
|
|
|
|
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
|
annotationOffset: z.coerce.number().optional().or(z.literal("")),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
|
mode: "onChange",
|
|
|
|
|
defaultValues: {
|
|
|
|
|
annotationOffset: annotationOffset,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const saveToConfig = useCallback(
|
|
|
|
|
async (annotation_offset: number | string) => {
|
|
|
|
|
if (!config || !event) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
axios
|
|
|
|
|
.put(
|
|
|
|
|
`config/set?cameras.${event?.camera}.detect.annotation_offset=${annotation_offset}`,
|
|
|
|
|
{
|
|
|
|
|
requires_restart: 0,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
|
toast.success(
|
2025-10-26 21:12:20 +03:00
|
|
|
t("trackingDetails.annotationSettings.offset.toast.success", {
|
2025-05-20 00:45:02 +03:00
|
|
|
camera: event?.camera,
|
|
|
|
|
}),
|
2024-09-04 16:46:49 +03:00
|
|
|
{
|
|
|
|
|
position: "top-center",
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
updateConfig();
|
|
|
|
|
} else {
|
2025-03-16 18:36:20 +03:00
|
|
|
toast.error(
|
2025-03-17 15:26:01 +03:00
|
|
|
t("toast.save.error.title", {
|
2025-03-16 18:36:20 +03:00
|
|
|
errorMessage: res.statusText,
|
|
|
|
|
ns: "common",
|
|
|
|
|
}),
|
|
|
|
|
{
|
|
|
|
|
position: "top-center",
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-09-04 16:46:49 +03:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2025-03-08 19:01:08 +03:00
|
|
|
const errorMessage =
|
|
|
|
|
error.response?.data?.message ||
|
|
|
|
|
error.response?.data?.detail ||
|
|
|
|
|
"Unknown error";
|
2025-03-17 15:26:01 +03:00
|
|
|
toast.error(
|
|
|
|
|
t("toast.save.error.title", { errorMessage, ns: "common" }),
|
|
|
|
|
{
|
|
|
|
|
position: "top-center",
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-09-04 16:46:49 +03:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setIsLoading(false);
|
|
|
|
|
});
|
|
|
|
|
},
|
2025-03-16 18:36:20 +03:00
|
|
|
[updateConfig, config, event, t],
|
2024-09-04 16:46:49 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function onSubmit(values: z.infer<typeof formSchema>) {
|
|
|
|
|
if (!values || values.annotationOffset == null || !config) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setIsLoading(true);
|
|
|
|
|
|
|
|
|
|
saveToConfig(values.annotationOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onApply(values: z.infer<typeof formSchema>) {
|
|
|
|
|
if (
|
|
|
|
|
!values ||
|
2024-10-29 21:33:09 +03:00
|
|
|
values.annotationOffset === null ||
|
|
|
|
|
values.annotationOffset === "" ||
|
2024-09-04 16:46:49 +03:00
|
|
|
!config
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-29 21:33:09 +03:00
|
|
|
setAnnotationOffset(values.annotationOffset ?? 0);
|
2024-09-04 16:46:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2024-09-20 02:36:07 +03:00
|
|
|
<div className="mb-3 space-y-3 rounded-lg border border-secondary-foreground bg-background_alt p-2">
|
2024-09-04 16:46:49 +03:00
|
|
|
<Heading as="h4" className="my-2">
|
2025-10-26 21:12:20 +03:00
|
|
|
{t("trackingDetails.annotationSettings.title")}
|
2024-09-04 16:46:49 +03:00
|
|
|
</Heading>
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<div className="flex flex-row items-center justify-start gap-2 p-3">
|
|
|
|
|
<Switch
|
|
|
|
|
id="show-zones"
|
|
|
|
|
checked={showZones}
|
|
|
|
|
onCheckedChange={setShowZones}
|
|
|
|
|
/>
|
|
|
|
|
<Label className="cursor-pointer" htmlFor="show-zones">
|
2025-10-26 21:12:20 +03:00
|
|
|
{t("trackingDetails.annotationSettings.showAllZones.title")}
|
2024-09-04 16:46:49 +03:00
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="text-sm text-muted-foreground">
|
2025-10-26 21:12:20 +03:00
|
|
|
{t("trackingDetails.annotationSettings.showAllZones.desc")}
|
2024-09-04 16:46:49 +03:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<Separator className="my-2 flex bg-secondary" />
|
|
|
|
|
<Form {...form}>
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
|
|
|
className="flex flex-1 flex-col space-y-6"
|
|
|
|
|
>
|
|
|
|
|
<FormField
|
|
|
|
|
control={form.control}
|
|
|
|
|
name="annotationOffset"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<FormItem>
|
2025-03-16 18:36:20 +03:00
|
|
|
<FormLabel>
|
2025-10-26 21:12:20 +03:00
|
|
|
{t("trackingDetails.annotationSettings.offset.label")}
|
2025-03-16 18:36:20 +03:00
|
|
|
</FormLabel>
|
2024-09-20 02:36:07 +03:00
|
|
|
<div className="flex flex-col gap-3 md:flex-row-reverse md:gap-8">
|
2025-10-16 16:24:14 +03:00
|
|
|
<div className="flex flex-row items-center gap-3 rounded-lg bg-destructive/50 p-3 text-sm text-primary-variant md:my-5">
|
2024-09-04 16:46:49 +03:00
|
|
|
<PiWarningCircle className="size-24" />
|
|
|
|
|
<div>
|
2025-03-16 18:36:20 +03:00
|
|
|
<Trans ns="views/explore">
|
2025-10-26 21:12:20 +03:00
|
|
|
trackingDetails.annotationSettings.offset.desc
|
2025-03-16 18:36:20 +03:00
|
|
|
</Trans>
|
2024-09-04 16:46:49 +03:00
|
|
|
<div className="mt-2 flex items-center text-primary">
|
|
|
|
|
<Link
|
2025-05-28 15:10:45 +03:00
|
|
|
to={getLocaleDocUrl("configuration/reference")}
|
2024-09-04 16:46:49 +03:00
|
|
|
target="_blank"
|
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
|
className="inline"
|
|
|
|
|
>
|
2025-08-23 01:19:00 +03:00
|
|
|
{t("readTheDocumentation", { ns: "common" })}
|
2024-09-04 16:46:49 +03:00
|
|
|
<LuExternalLink className="ml-2 inline-flex size-3" />
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-col">
|
|
|
|
|
<FormControl>
|
|
|
|
|
<Input
|
2024-09-13 06:07:35 +03:00
|
|
|
className="text-md w-full border border-input bg-background p-2 hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark]"
|
2024-09-04 16:46:49 +03:00
|
|
|
placeholder="0"
|
|
|
|
|
{...field}
|
|
|
|
|
/>
|
|
|
|
|
</FormControl>
|
|
|
|
|
<FormDescription>
|
2025-04-13 21:08:47 +03:00
|
|
|
<Trans ns="views/explore">
|
2025-10-26 21:12:20 +03:00
|
|
|
trackingDetails.annotationSettings.offset.millisecondsToOffset
|
2025-04-13 21:08:47 +03:00
|
|
|
</Trans>
|
2024-09-04 16:46:49 +03:00
|
|
|
<div className="mt-2">
|
2025-10-26 21:12:20 +03:00
|
|
|
{t("trackingDetails.annotationSettings.offset.tips")}
|
2024-09-04 16:46:49 +03:00
|
|
|
</div>
|
|
|
|
|
</FormDescription>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<FormMessage />
|
|
|
|
|
</FormItem>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-1 flex-col justify-end">
|
|
|
|
|
<div className="flex flex-row gap-2 pt-5">
|
|
|
|
|
<Button
|
|
|
|
|
className="flex flex-1"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("button.apply", { ns: "common" })}
|
2024-09-04 16:46:49 +03:00
|
|
|
onClick={form.handleSubmit(onApply)}
|
|
|
|
|
>
|
2025-03-16 18:36:20 +03:00
|
|
|
{t("button.apply", { ns: "common" })}
|
2024-09-04 16:46:49 +03:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="select"
|
2025-03-16 18:36:20 +03:00
|
|
|
aria-label={t("button.save", { ns: "common" })}
|
2024-09-04 16:46:49 +03:00
|
|
|
disabled={isLoading}
|
|
|
|
|
className="flex flex-1"
|
|
|
|
|
type="submit"
|
|
|
|
|
>
|
|
|
|
|
{isLoading ? (
|
|
|
|
|
<div className="flex flex-row items-center gap-2">
|
|
|
|
|
<ActivityIndicator />
|
2025-03-16 18:36:20 +03:00
|
|
|
<span>{t("button.saving", { ns: "common" })}</span>
|
2024-09-04 16:46:49 +03:00
|
|
|
</div>
|
|
|
|
|
) : (
|
2025-03-16 18:36:20 +03:00
|
|
|
t("button.save", { ns: "common" })
|
2024-09-04 16:46:49 +03:00
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Form>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|