Files
frigate/web/src/components/overlay/detail/AnnotationOffsetSlider.tsx
T

237 lines
7.4 KiB
TypeScript
Raw Normal View History

2026-05-02 17:35:42 -05:00
import { useCallback, useEffect, useMemo, useState } from "react";
import { flushSync } from "react-dom";
import { throttle } from "lodash";
2025-10-16 08:24:14 -05:00
import { Slider } from "@/components/ui/slider";
import { Button } from "@/components/ui/button";
2025-10-29 17:03:38 -05:00
import { Popover, PopoverContent, PopoverTrigger } from "../../ui/popover";
2025-10-16 08:24:14 -05:00
import { useDetailStream } from "@/context/detail-stream-context";
import axios from "axios";
import { useSWRConfig } from "swr";
import { toast } from "sonner";
2025-10-29 17:03:38 -05:00
import { Trans, useTranslation } from "react-i18next";
2026-03-07 07:50:00 -06:00
import { LuExternalLink, LuInfo, LuMinus, LuPlus } from "react-icons/lu";
2025-10-29 17:03:38 -05:00
import { cn } from "@/lib/utils";
2026-03-26 13:47:24 -05:00
import {
ANNOTATION_OFFSET_MAX,
ANNOTATION_OFFSET_MIN,
ANNOTATION_OFFSET_STEP,
} from "@/lib/const";
2025-10-29 17:03:38 -05:00
import { isMobile } from "react-device-detect";
2025-12-11 08:23:34 -06:00
import { useIsAdmin } from "@/hooks/use-is-admin";
2026-03-07 07:50:00 -06:00
import { useDocDomain } from "@/hooks/use-doc-domain";
import { Link } from "react-router-dom";
2026-05-02 17:35:42 -05:00
const SLIDER_DRAG_THROTTLE_MS = 80;
2025-10-16 08:24:14 -05:00
type Props = {
className?: string;
2026-05-02 17:35:42 -05:00
// Optional side-effect invoked atomically with setAnnotationOffset (inside
// flushSync) so callers like the timeline panel can re-seek the video in the
// same React commit as the offset state update — preventing a one-frame
// overlay mismatch where annotationOffset has changed but currentTime has not.
onApplyOffset?: (newOffset: number) => void;
2025-10-16 08:24:14 -05:00
};
2026-05-02 17:35:42 -05:00
export default function AnnotationOffsetSlider({
className,
onApplyOffset,
}: Props) {
2025-10-16 08:24:14 -05:00
const { annotationOffset, setAnnotationOffset, camera } = useDetailStream();
2025-12-11 08:23:34 -06:00
const isAdmin = useIsAdmin();
2026-03-07 07:50:00 -06:00
const { getLocaleDocUrl } = useDocDomain();
2025-10-16 08:24:14 -05:00
const { mutate } = useSWRConfig();
const { t } = useTranslation(["views/explore"]);
const [isSaving, setIsSaving] = useState(false);
2026-05-02 17:35:42 -05:00
const applyOffset = useCallback(
(newOffset: number) => {
flushSync(() => {
setAnnotationOffset(newOffset);
onApplyOffset?.(newOffset);
});
},
[setAnnotationOffset, onApplyOffset],
);
const throttledApplyOffset = useMemo(
() =>
throttle(applyOffset, SLIDER_DRAG_THROTTLE_MS, {
leading: true,
trailing: true,
}),
[applyOffset],
);
useEffect(() => () => throttledApplyOffset.cancel(), [throttledApplyOffset]);
2025-10-16 08:24:14 -05:00
const handleChange = useCallback(
(values: number[]) => {
if (!values || values.length === 0) return;
2026-05-02 17:35:42 -05:00
throttledApplyOffset(values[0]);
2025-10-16 08:24:14 -05:00
},
2026-05-02 17:35:42 -05:00
[throttledApplyOffset],
);
const handleCommit = useCallback(
(values: number[]) => {
if (!values || values.length === 0) return;
// Ensure the final value lands even if it would otherwise be discarded
// by the trailing edge of the throttle window.
throttledApplyOffset.cancel();
applyOffset(values[0]);
},
[throttledApplyOffset, applyOffset],
2025-10-16 08:24:14 -05:00
);
2026-03-07 07:50:00 -06:00
const stepOffset = useCallback(
(delta: number) => {
2026-05-02 17:35:42 -05:00
const next = Math.max(
ANNOTATION_OFFSET_MIN,
Math.min(ANNOTATION_OFFSET_MAX, annotationOffset + delta),
);
throttledApplyOffset.cancel();
applyOffset(next);
2026-03-07 07:50:00 -06:00
},
2026-05-02 17:35:42 -05:00
[annotationOffset, applyOffset, throttledApplyOffset],
2026-03-07 07:50:00 -06:00
);
2025-10-16 08:24:14 -05:00
const reset = useCallback(() => {
2026-05-02 17:35:42 -05:00
throttledApplyOffset.cancel();
applyOffset(0);
}, [applyOffset, throttledApplyOffset]);
2025-10-16 08:24:14 -05:00
const save = useCallback(async () => {
setIsSaving(true);
try {
// save value in milliseconds to config
await axios.put(
`config/set?cameras.${camera}.detect.annotation_offset=${annotationOffset}`,
{ requires_restart: 0 },
);
toast.success(
2025-10-26 13:12:20 -05:00
t("trackingDetails.annotationSettings.offset.toast.success", {
2025-10-16 08:24:14 -05:00
camera,
}),
{ position: "top-center" },
);
// refresh config
await mutate("config");
} catch (e: unknown) {
const err = e as {
response?: { data?: { message?: string } };
message?: string;
};
const errorMessage =
err?.response?.data?.message || err?.message || "Unknown error";
toast.error(t("toast.save.error.title", { errorMessage, ns: "common" }), {
position: "top-center",
});
} finally {
setIsSaving(false);
}
}, [annotationOffset, camera, mutate, t]);
return (
<div
2025-10-29 17:03:38 -05:00
className={cn(
2026-03-07 07:50:00 -06:00
"flex flex-col gap-1.5",
2025-10-29 17:03:38 -05:00
isMobile && "landscape:gap-3",
className,
)}
2025-10-16 08:24:14 -05:00
>
2026-03-07 07:50:00 -06:00
<div className="flex items-center gap-2 text-sm">
<span>{t("trackingDetails.annotationSettings.offset.label")}:</span>
<span className="font-mono tabular-nums text-primary-variant">
{annotationOffset > 0 ? "+" : ""}
{annotationOffset}ms
</span>
</div>
2025-10-29 17:03:38 -05:00
<div
className={cn(
"flex items-center gap-3",
isMobile &&
"landscape:flex-col landscape:items-start landscape:gap-4",
)}
>
2026-03-07 07:50:00 -06:00
<Button
type="button"
variant="outline"
size="icon"
className="size-8 shrink-0"
aria-label="-50ms"
2026-03-26 13:47:24 -05:00
onClick={() => stepOffset(-ANNOTATION_OFFSET_STEP)}
disabled={annotationOffset <= ANNOTATION_OFFSET_MIN}
2026-03-07 07:50:00 -06:00
>
<LuMinus className="size-4" />
</Button>
2025-10-29 17:03:38 -05:00
<div className="w-full flex-1 landscape:flex">
<Slider
value={[annotationOffset]}
2026-03-26 13:47:24 -05:00
min={ANNOTATION_OFFSET_MIN}
max={ANNOTATION_OFFSET_MAX}
step={ANNOTATION_OFFSET_STEP}
2025-10-29 17:03:38 -05:00
onValueChange={handleChange}
2026-05-02 17:35:42 -05:00
onValueCommit={handleCommit}
2025-10-29 17:03:38 -05:00
/>
</div>
2026-03-07 07:50:00 -06:00
<Button
type="button"
variant="outline"
size="icon"
className="size-8 shrink-0"
aria-label="+50ms"
2026-03-26 13:47:24 -05:00
onClick={() => stepOffset(ANNOTATION_OFFSET_STEP)}
disabled={annotationOffset >= ANNOTATION_OFFSET_MAX}
2026-03-07 07:50:00 -06:00
>
<LuPlus className="size-4" />
</Button>
2025-10-16 08:24:14 -05:00
</div>
2026-03-07 07:50:00 -06:00
<div className="flex items-start gap-1.5 text-xs text-muted-foreground">
2025-10-29 17:03:38 -05:00
<Trans ns="views/explore">
trackingDetails.annotationSettings.offset.millisecondsToOffset
</Trans>
<Popover>
<PopoverTrigger asChild>
<button
2026-03-07 07:50:00 -06:00
className="mt-px shrink-0 focus:outline-none"
2025-11-06 10:22:52 -06:00
aria-label={t("trackingDetails.annotationSettings.offset.tips")}
2025-10-29 17:03:38 -05:00
>
2026-03-07 07:50:00 -06:00
<LuInfo className="size-3.5" />
2025-10-29 17:03:38 -05:00
</button>
</PopoverTrigger>
<PopoverContent className="w-80 text-sm">
2025-11-06 10:22:52 -06:00
{t("trackingDetails.annotationSettings.offset.tips")}
2026-03-07 07:50:00 -06:00
<div className="mt-2 flex items-center text-primary-variant">
<Link
to={getLocaleDocUrl(
"troubleshooting/dummy-camera#annotation-offset",
)}
target="_blank"
rel="noopener noreferrer"
className="inline"
>
{t("readTheDocumentation", { ns: "common" })}
<LuExternalLink className="ml-2 inline-flex size-3" />
</Link>
</div>
2025-10-29 17:03:38 -05:00
</PopoverContent>
</Popover>
2025-10-16 08:24:14 -05:00
</div>
2026-03-07 07:50:00 -06:00
<div className="flex items-center justify-end gap-2">
<Button size="sm" variant="ghost" onClick={reset}>
{t("button.reset", { ns: "common" })}
</Button>
{isAdmin && (
<Button size="sm" onClick={save} disabled={isSaving}>
{isSaving
? t("button.saving", { ns: "common" })
: t("button.save", { ns: "common" })}
</Button>
)}
</div>
2025-10-16 08:24:14 -05:00
</div>
);
}