Files
frigate/web/src/components/camera/DebugCameraImage.tsx
T

174 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-02-12 18:28:36 -07:00
import { Switch } from "../ui/switch";
import { Label } from "../ui/label";
import { CameraConfig } from "@/types/frigateConfig";
import { Button } from "../ui/button";
import { LuSettings } from "react-icons/lu";
import { useCallback, useMemo, useState } from "react";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { useUserPersistence } from "@/hooks/use-user-persistence";
2024-02-12 18:28:36 -07:00
import AutoUpdatingCameraImage from "./AutoUpdatingCameraImage";
import { useTranslation } from "react-i18next";
2024-02-12 18:28:36 -07:00
type Options = { [key: string]: boolean };
const emptyObject = Object.freeze({});
type DebugCameraImageProps = {
className?: string;
cameraConfig: CameraConfig;
};
export default function DebugCameraImage({
className,
cameraConfig,
}: DebugCameraImageProps) {
const { t } = useTranslation(["components/camera"]);
2024-02-12 18:28:36 -07:00
const [showSettings, setShowSettings] = useState(false);
const [options, setOptions] = useUserPersistence<Options>(
2024-02-12 18:28:36 -07:00
`${cameraConfig?.name}-feed`,
2024-02-28 15:23:56 -07:00
emptyObject,
2024-02-12 18:28:36 -07:00
);
const handleSetOption = useCallback(
(id: string, value: boolean) => {
const newOptions = { ...options, [id]: value };
setOptions(newOptions);
},
2024-02-28 15:23:56 -07:00
[options, setOptions],
2024-02-12 18:28:36 -07:00
);
const searchParams = useMemo(
() =>
new URLSearchParams(
2024-02-28 07:16:16 -07:00
Object.keys(options || {}).reduce((memo, key) => {
2024-02-28 15:23:56 -07:00
//@ts-expect-error we know this is correct
2024-02-12 18:28:36 -07:00
memo.push([key, options[key] === true ? "1" : "0"]);
return memo;
2024-02-28 15:23:56 -07:00
}, []),
2024-02-12 18:28:36 -07:00
),
2024-02-28 15:23:56 -07:00
[options],
2024-02-12 18:28:36 -07:00
);
const handleToggleSettings = useCallback(() => {
setShowSettings(!showSettings);
}, [showSettings]);
return (
<div className={className}>
<AutoUpdatingCameraImage
camera={cameraConfig.name}
searchParams={searchParams}
cameraClasses="relative w-full h-full flex justify-center"
2024-02-12 18:28:36 -07:00
/>
2024-10-22 17:07:42 -05:00
<Button
onClick={handleToggleSettings}
variant="link"
size="sm"
aria-label={t("debug.options.label")}
2024-10-22 17:07:42 -05:00
>
2024-05-14 10:06:44 -05:00
<span className="h-5 w-5">
2024-02-12 18:28:36 -07:00
<LuSettings />
</span>{" "}
<span>
{showSettings
? t("debug.options.hideOptions")
: t("debug.options.showOptions")}
</span>
2024-02-12 18:28:36 -07:00
</Button>
{showSettings ? (
<Card>
<CardHeader>
<CardTitle>{t("debug.options.title")}</CardTitle>
2024-02-12 18:28:36 -07:00
</CardHeader>
<CardContent>
<DebugSettings
handleSetOption={handleSetOption}
2024-02-28 07:16:16 -07:00
options={options || {}}
2024-02-12 18:28:36 -07:00
/>
</CardContent>
</Card>
) : null}
</div>
);
}
type DebugSettingsProps = {
handleSetOption: (id: string, value: boolean) => void;
options: Options;
};
function DebugSettings({ handleSetOption, options }: DebugSettingsProps) {
const { t } = useTranslation(["components/camera"]);
2024-02-12 18:28:36 -07:00
return (
2024-05-14 10:06:44 -05:00
<div className="grid grid-cols-2 gap-4 md:grid-cols-3 lg:grid-cols-4">
2024-02-12 18:28:36 -07:00
<div className="flex items-center space-x-2">
<Switch
id="bbox"
checked={options["bbox"]}
onCheckedChange={(isChecked) => {
handleSetOption("bbox", isChecked);
}}
/>
<Label htmlFor="bbox">{t("debug.boundingBox")}</Label>
2024-02-12 18:28:36 -07:00
</div>
<div className="flex items-center space-x-2">
<Switch
id="timestamp"
checked={options["timestamp"]}
onCheckedChange={(isChecked) => {
handleSetOption("timestamp", isChecked);
}}
/>
<Label htmlFor="timestamp">{t("debug.timestamp")}</Label>
2024-02-12 18:28:36 -07:00
</div>
<div className="flex items-center space-x-2">
<Switch
id="zones"
checked={options["zones"]}
onCheckedChange={(isChecked) => {
handleSetOption("zones", isChecked);
}}
/>
<Label htmlFor="zones">{t("debug.zones")}</Label>
2024-02-12 18:28:36 -07:00
</div>
<div className="flex items-center space-x-2">
<Switch
id="mask"
checked={options["mask"]}
onCheckedChange={(isChecked) => {
handleSetOption("mask", isChecked);
}}
/>
<Label htmlFor="mask">{t("debug.mask")}</Label>
2024-02-12 18:28:36 -07:00
</div>
<div className="flex items-center space-x-2">
<Switch
id="motion"
checked={options["motion"]}
onCheckedChange={(isChecked) => {
handleSetOption("motion", isChecked);
}}
/>
<Label htmlFor="motion">{t("debug.motion")}</Label>
2024-02-12 18:28:36 -07:00
</div>
<div className="flex items-center space-x-2">
<Switch
id="regions"
checked={options["regions"]}
onCheckedChange={(isChecked) => {
handleSetOption("regions", isChecked);
}}
/>
<Label htmlFor="regions">{t("debug.regions")}</Label>
2024-02-12 18:28:36 -07:00
</div>
<div className="flex items-center space-x-2">
<Switch
id="paths"
checked={options["paths"]}
onCheckedChange={(isChecked) => {
handleSetOption("paths", isChecked);
}}
/>
<Label htmlFor="paths">{t("debug.paths")}</Label>
</div>
2024-02-12 18:28:36 -07:00
</div>
);
}