mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-06-21 03:41:55 +03:00
Compare commits
10 Commits
c16e6fe8dd
...
4a784acdcc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4a784acdcc | ||
|
|
8203e39b7f | ||
|
|
f00dd5c7af | ||
|
|
51dd890eb6 | ||
|
|
8f6e083420 | ||
|
|
bf25560067 | ||
|
|
df40d9e2b5 | ||
|
|
263554a5f6 | ||
|
|
597a9f9fb4 | ||
|
|
0d05f0feaa |
0
.devcontainer/post_create.sh
Executable file → Normal file
0
.devcontainer/post_create.sh
Executable file → Normal file
@ -21,6 +21,7 @@
|
||||
"1hour": "1 hour",
|
||||
"12hours": "12 hours",
|
||||
"24hours": "24 hours",
|
||||
"custom": "Custom...",
|
||||
"pm": "pm",
|
||||
"am": "am",
|
||||
"yr": "{{time}}yr",
|
||||
|
||||
@ -1191,8 +1191,15 @@
|
||||
"1hour": "Suspend for 1 hour",
|
||||
"12hours": "Suspend for 12 hours",
|
||||
"24hours": "Suspend for 24 hours",
|
||||
"custom": "Suspend until...",
|
||||
"untilRestart": "Suspend until restart"
|
||||
},
|
||||
"customSuspension": {
|
||||
"title": "Custom suspension time",
|
||||
"description": "Suspend notifications for this camera until the selected time.",
|
||||
"untilLabel": "Suspend until",
|
||||
"invalidTime": "Pick a time in the future."
|
||||
},
|
||||
"cancelSuspension": "Cancel Suspension",
|
||||
"toast": {
|
||||
"success": {
|
||||
|
||||
@ -23,7 +23,7 @@ import {
|
||||
useState,
|
||||
} from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { LuCheck, LuExternalLink, LuX } from "react-icons/lu";
|
||||
import { LuCheck, LuChevronDown, LuExternalLink, LuX } from "react-icons/lu";
|
||||
import { CiCircleAlert } from "react-icons/ci";
|
||||
import { Link } from "react-router-dom";
|
||||
import { toast } from "sonner";
|
||||
@ -35,12 +35,12 @@ import {
|
||||
useNotificationTest,
|
||||
} from "@/api/ws";
|
||||
import {
|
||||
Select,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
} from "@/components/ui/select";
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { formatUnixTimestampToDateTime } from "@/utils/dateUtil";
|
||||
import { use24HourTime } from "@/hooks/use-date-utils";
|
||||
import FilterSwitch from "@/components/filter/FilterSwitch";
|
||||
@ -51,6 +51,7 @@ import { useDocDomain } from "@/hooks/use-doc-domain";
|
||||
import { isPWA } from "@/utils/isPWA";
|
||||
import { isIOS } from "react-device-detect";
|
||||
import { CameraNameLabel } from "@/components/camera/FriendlyNameLabel";
|
||||
import CustomSuspensionDialog from "@/components/overlay/dialog/CustomSuspensionDialog";
|
||||
import { useIsAdmin } from "@/hooks/use-is-admin";
|
||||
import { cn } from "@/lib/utils";
|
||||
import cloneDeep from "lodash/cloneDeep";
|
||||
@ -756,6 +757,8 @@ export function CameraNotificationSwitch({
|
||||
}
|
||||
}, [notificationSuspendUntil, notificationState]);
|
||||
|
||||
const [customDialogOpen, setCustomDialogOpen] = useState(false);
|
||||
|
||||
const handleSuspend = (duration: string) => {
|
||||
setIsSuspended(true);
|
||||
if (duration == "off") {
|
||||
@ -765,6 +768,11 @@ export function CameraNotificationSwitch({
|
||||
}
|
||||
};
|
||||
|
||||
const handleCustomSuspend = (totalMinutes: number) => {
|
||||
setIsSuspended(true);
|
||||
sendNotificationSuspend(totalMinutes);
|
||||
};
|
||||
|
||||
const handleCancelSuspension = () => {
|
||||
sendNotification("ON");
|
||||
sendNotificationSuspend(0);
|
||||
@ -824,34 +832,41 @@ export function CameraNotificationSwitch({
|
||||
</div>
|
||||
|
||||
{!isSuspended ? (
|
||||
<Select onValueChange={handleSuspend}>
|
||||
<SelectTrigger className="w-auto">
|
||||
<SelectValue placeholder={t("notification.suspendTime.suspend")} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="5">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button size="sm" variant="outline" className="flex gap-2">
|
||||
{t("notification.suspendTime.suspend")}
|
||||
<LuChevronDown className="h-4 w-4 opacity-50" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("5")}>
|
||||
{t("notification.suspendTime.5minutes")}
|
||||
</SelectItem>
|
||||
<SelectItem value="10">
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("10")}>
|
||||
{t("notification.suspendTime.10minutes")}
|
||||
</SelectItem>
|
||||
<SelectItem value="30">
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("30")}>
|
||||
{t("notification.suspendTime.30minutes")}
|
||||
</SelectItem>
|
||||
<SelectItem value="60">
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("60")}>
|
||||
{t("notification.suspendTime.1hour")}
|
||||
</SelectItem>
|
||||
<SelectItem value="840">
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("840")}>
|
||||
{t("notification.suspendTime.12hours")}
|
||||
</SelectItem>
|
||||
<SelectItem value="1440">
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("1440")}>
|
||||
{t("notification.suspendTime.24hours")}
|
||||
</SelectItem>
|
||||
<SelectItem value="off">
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => handleSuspend("off")}>
|
||||
{t("notification.suspendTime.untilRestart")}
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem onClick={() => setCustomDialogOpen(true)}>
|
||||
{t("notification.suspendTime.custom")}
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
) : (
|
||||
<Button
|
||||
variant="destructive"
|
||||
@ -861,6 +876,12 @@ export function CameraNotificationSwitch({
|
||||
{t("notification.cancelSuspension")}
|
||||
</Button>
|
||||
)}
|
||||
|
||||
<CustomSuspensionDialog
|
||||
open={customDialogOpen}
|
||||
onOpenChange={setCustomDialogOpen}
|
||||
onConfirm={handleCustomSuspend}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@ -50,6 +50,7 @@ import { use24HourTime } from "@/hooks/use-date-utils";
|
||||
import { useIsAdmin } from "@/hooks/use-is-admin";
|
||||
import { CameraNameLabel } from "../camera/FriendlyNameLabel";
|
||||
import { LiveStreamMetadata } from "@/types/live";
|
||||
import CustomSuspensionDialog from "@/components/overlay/dialog/CustomSuspensionDialog";
|
||||
|
||||
type LiveContextMenuProps = {
|
||||
className?: string;
|
||||
@ -238,6 +239,8 @@ export default function LiveContextMenu({
|
||||
}
|
||||
}, [notificationSuspendUntil, notificationState]);
|
||||
|
||||
const [customDialogOpen, setCustomDialogOpen] = useState(false);
|
||||
|
||||
const handleSuspend = (duration: string) => {
|
||||
if (duration === "off") {
|
||||
sendNotification("OFF");
|
||||
@ -534,6 +537,16 @@ export default function LiveContextMenu({
|
||||
>
|
||||
{t("time.24hours", { ns: "common" })}
|
||||
</ContextMenuItem>
|
||||
<ContextMenuItem
|
||||
disabled={!isEnabled}
|
||||
onClick={
|
||||
isEnabled
|
||||
? () => setCustomDialogOpen(true)
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
{t("time.custom", { ns: "common" })}
|
||||
</ContextMenuItem>
|
||||
<ContextMenuItem
|
||||
disabled={!isEnabled}
|
||||
onClick={
|
||||
@ -566,6 +579,12 @@ export default function LiveContextMenu({
|
||||
streamMetadata={streamMetadata}
|
||||
/>
|
||||
</Dialog>
|
||||
|
||||
<CustomSuspensionDialog
|
||||
open={customDialogOpen}
|
||||
onOpenChange={setCustomDialogOpen}
|
||||
onConfirm={(minutes) => sendNotificationSuspend(minutes)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,6 +1,12 @@
|
||||
import { RecordingsSummary, ReviewSummary } from "@/types/review";
|
||||
import { Calendar } from "../ui/calendar";
|
||||
import { ButtonHTMLAttributes, useEffect, useMemo, useRef } from "react";
|
||||
import {
|
||||
ButtonHTMLAttributes,
|
||||
ComponentProps,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
} from "react";
|
||||
import { FaCircle } from "react-icons/fa";
|
||||
import { getUTCOffset } from "@/utils/dateUtil";
|
||||
import { type DayButtonProps } from "react-day-picker";
|
||||
@ -156,12 +162,14 @@ type TimezoneAwareCalendarProps = {
|
||||
timezone?: string;
|
||||
selectedDay?: Date;
|
||||
onSelect: (day?: Date) => void;
|
||||
disabled?: ComponentProps<typeof Calendar>["disabled"];
|
||||
recordingsSummary?: RecordingsSummary;
|
||||
};
|
||||
export function TimezoneAwareCalendar({
|
||||
timezone,
|
||||
selectedDay,
|
||||
onSelect,
|
||||
disabled,
|
||||
recordingsSummary,
|
||||
}: TimezoneAwareCalendarProps) {
|
||||
const [weekStartsOn] = useUserPersistence("weekStartsOn", 0);
|
||||
@ -187,7 +195,7 @@ export function TimezoneAwareCalendar({
|
||||
timezone ? Math.round(getUTCOffset(new Date(), timezone)) : undefined,
|
||||
[timezone],
|
||||
);
|
||||
const disabledDates = useMemo(() => {
|
||||
const defaultDisabledDates = useMemo(() => {
|
||||
const tomorrow = new Date();
|
||||
|
||||
if (timezoneOffset) {
|
||||
@ -205,6 +213,7 @@ export function TimezoneAwareCalendar({
|
||||
future.setFullYear(tomorrow.getFullYear() + 10);
|
||||
return { from: tomorrow, to: future };
|
||||
}, [timezoneOffset]);
|
||||
const disabledDates = disabled ?? defaultDisabledDates;
|
||||
|
||||
const today = useMemo(() => {
|
||||
if (!timezoneOffset) {
|
||||
|
||||
167
web/src/components/overlay/dialog/CustomSuspensionDialog.tsx
Normal file
167
web/src/components/overlay/dialog/CustomSuspensionDialog.tsx
Normal file
@ -0,0 +1,167 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { isDesktop } from "react-device-detect";
|
||||
import { FaCalendarAlt } from "react-icons/fa";
|
||||
import useSWR from "swr";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { TimezoneAwareCalendar } from "@/components/overlay/ReviewActivityCalendar";
|
||||
import { FrigateConfig } from "@/types/frigateConfig";
|
||||
import { useFormattedTimestamp } from "@/hooks/use-date-utils";
|
||||
|
||||
type CustomSuspensionDialogProps = {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
onConfirm: (minutes: number) => void;
|
||||
};
|
||||
|
||||
const ONE_HOUR_MS = 60 * 60 * 1000;
|
||||
|
||||
function pad(n: number): string {
|
||||
return n.toString().padStart(2, "0");
|
||||
}
|
||||
|
||||
function isValidDate(d: Date): boolean {
|
||||
return !Number.isNaN(d.getTime());
|
||||
}
|
||||
|
||||
export default function CustomSuspensionDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
onConfirm,
|
||||
}: CustomSuspensionDialogProps) {
|
||||
const { t } = useTranslation(["views/settings"]);
|
||||
const { data: config } = useSWR<FrigateConfig>("config");
|
||||
|
||||
const [until, setUntil] = useState<Date>(
|
||||
() => new Date(Date.now() + ONE_HOUR_MS),
|
||||
);
|
||||
const [calendarOpen, setCalendarOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) setUntil(new Date(Date.now() + ONE_HOUR_MS));
|
||||
}, [open]);
|
||||
|
||||
const formattedDate = useFormattedTimestamp(
|
||||
isValidDate(until) ? Math.floor(until.getTime() / 1000) : 0,
|
||||
t("time.formattedTimestampMonthDayYear.24hour", { ns: "common" }),
|
||||
config?.ui.timezone,
|
||||
);
|
||||
|
||||
const isFuture = isValidDate(until) && until.getTime() > Date.now();
|
||||
|
||||
const handleApply = () => {
|
||||
if (!isFuture) return;
|
||||
onConfirm(Math.ceil((until.getTime() - Date.now()) / 60_000));
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>{t("notification.customSuspension.title")}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{t("notification.customSuspension.description")}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex flex-col gap-2">
|
||||
<Label>{t("notification.customSuspension.untilLabel")}</Label>
|
||||
<div className="flex items-center gap-2 rounded-lg bg-secondary p-2 text-secondary-foreground">
|
||||
<FaCalendarAlt />
|
||||
<Popover open={calendarOpen} onOpenChange={setCalendarOpen}>
|
||||
<PopoverTrigger asChild>
|
||||
<Button
|
||||
className={`text-primary ${isDesktop ? "" : "text-xs"}`}
|
||||
variant={calendarOpen ? "select" : "default"}
|
||||
size="sm"
|
||||
>
|
||||
{isValidDate(until) ? formattedDate : "—"}
|
||||
</Button>
|
||||
</PopoverTrigger>
|
||||
<PopoverContent
|
||||
className="flex flex-col items-center"
|
||||
disablePortal
|
||||
>
|
||||
<TimezoneAwareCalendar
|
||||
timezone={config?.ui.timezone}
|
||||
selectedDay={isValidDate(until) ? until : undefined}
|
||||
disabled={{
|
||||
before: new Date(new Date().setHours(0, 0, 0, 0)),
|
||||
}}
|
||||
onSelect={(day) => {
|
||||
if (!day) return;
|
||||
const next = new Date(day);
|
||||
const carry = isValidDate(until) ? until : new Date();
|
||||
next.setHours(
|
||||
carry.getHours(),
|
||||
carry.getMinutes(),
|
||||
carry.getSeconds(),
|
||||
0,
|
||||
);
|
||||
setUntil(next);
|
||||
setCalendarOpen(false);
|
||||
}}
|
||||
/>
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
<input
|
||||
className="text-md border border-input bg-background p-1 text-secondary-foreground hover:bg-accent hover:text-accent-foreground dark:[color-scheme:dark]"
|
||||
aria-label={t("notification.customSuspension.untilLabel")}
|
||||
type="time"
|
||||
value={
|
||||
isValidDate(until)
|
||||
? `${pad(until.getHours())}:${pad(until.getMinutes())}`
|
||||
: ""
|
||||
}
|
||||
step="60"
|
||||
onChange={(e) => {
|
||||
const [h, m] = e.target.value.split(":");
|
||||
const hh = Number.parseInt(h ?? "", 10);
|
||||
const mm = Number.parseInt(m ?? "", 10);
|
||||
if (Number.isNaN(hh) || Number.isNaN(mm)) return;
|
||||
const base = isValidDate(until) ? until : new Date();
|
||||
const next = new Date(base);
|
||||
next.setHours(hh, mm, 0, 0);
|
||||
setUntil(next);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{!isFuture && (
|
||||
<p className="text-sm text-danger">
|
||||
{t("notification.customSuspension.invalidTime")}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button type="button" onClick={() => onOpenChange(false)}>
|
||||
{t("button.cancel", { ns: "common" })}
|
||||
</Button>
|
||||
<Button
|
||||
variant="select"
|
||||
type="button"
|
||||
disabled={!isFuture}
|
||||
onClick={handleApply}
|
||||
>
|
||||
{t("button.apply", { ns: "common" })}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@ -34,6 +34,8 @@ import { isMobile } from "react-device-detect";
|
||||
import { FaVideo } from "react-icons/fa";
|
||||
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig";
|
||||
import type { ConfigSectionData, JsonObject } from "@/types/configForm";
|
||||
import isEqual from "lodash/isEqual";
|
||||
import { maskCredentials } from "@/utils/credentialMask";
|
||||
import useSWR from "swr";
|
||||
import FilterSwitch from "@/components/filter/FilterSwitch";
|
||||
import { ZoneMaskFilterButton } from "@/components/filter/ZoneMaskFilter";
|
||||
@ -660,6 +662,11 @@ export default function Settings() {
|
||||
|
||||
const isAdmin = useIsAdmin();
|
||||
|
||||
// for unmasked go2rtc stream sources
|
||||
const { data: rawPaths } = useSWR<{
|
||||
go2rtc: { streams: Record<string, string | string[]> };
|
||||
}>(isAdmin ? "config/raw_paths" : null);
|
||||
|
||||
const visibleSettingsViews = !isAdmin
|
||||
? ALLOWED_VIEWS_FOR_VIEWER
|
||||
: allSettingsViews;
|
||||
@ -788,6 +795,40 @@ export default function Settings() {
|
||||
},
|
||||
);
|
||||
|
||||
// go2rtc streams aren't schema-backed, so build their preview items directly
|
||||
if ("go2rtc_streams" in pendingDataBySection) {
|
||||
const live =
|
||||
(pendingDataBySection["go2rtc_streams"] as Record<string, string[]>) ??
|
||||
{};
|
||||
const saved: Record<string, string[]> = {};
|
||||
for (const [name, urls] of Object.entries(
|
||||
rawPaths?.go2rtc?.streams ?? {},
|
||||
)) {
|
||||
saved[name] = Array.isArray(urls) ? urls : [urls];
|
||||
}
|
||||
|
||||
// Added or changed streams
|
||||
for (const [name, urls] of Object.entries(live)) {
|
||||
if (name in saved && isEqual(urls, saved[name])) continue;
|
||||
const masked = urls.map((url) => maskCredentials(url));
|
||||
items.push({
|
||||
scope: "global",
|
||||
fieldPath: `go2rtc.streams.${name}`,
|
||||
value: masked.length === 1 ? masked[0] : masked,
|
||||
});
|
||||
}
|
||||
|
||||
// Deleted streams (present in saved config, absent from pending)
|
||||
for (const name of Object.keys(saved)) {
|
||||
if (name in live) continue;
|
||||
items.push({
|
||||
scope: "global",
|
||||
fieldPath: `go2rtc.streams.${name}`,
|
||||
value: "",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return items.sort((left, right) => {
|
||||
const scopeCompare = left.scope.localeCompare(right.scope);
|
||||
if (scopeCompare !== 0) return scopeCompare;
|
||||
@ -797,7 +838,13 @@ export default function Settings() {
|
||||
if (cameraCompare !== 0) return cameraCompare;
|
||||
return left.fieldPath.localeCompare(right.fieldPath);
|
||||
});
|
||||
}, [config, fullSchema, pendingDataBySection, profileFriendlyNames]);
|
||||
}, [
|
||||
config,
|
||||
fullSchema,
|
||||
pendingDataBySection,
|
||||
profileFriendlyNames,
|
||||
rawPaths,
|
||||
]);
|
||||
|
||||
// Map a pendingDataKey to SettingsType menu key for clearing section status
|
||||
const pendingKeyToMenuKey = useCallback(
|
||||
@ -869,10 +916,7 @@ export default function Settings() {
|
||||
// after `mutate("config")` resolves
|
||||
const keysToClear: string[] = [];
|
||||
|
||||
// `detectors` and `model` are owned by DetectorsAndModelSettingsView,
|
||||
// which saves them atomically (single combined PUT with a pre-clear when
|
||||
// detector keys change or the Plus/Custom tab flips). Doing the same here
|
||||
// keeps Save All consistent with the page's own Save button
|
||||
// `detectors` and `model` are owned by DetectorsAndModelSettingsView
|
||||
const hasPendingDetectors = "detectors" in pendingDataBySection;
|
||||
const hasPendingModel = "model" in pendingDataBySection;
|
||||
if (hasPendingDetectors || hasPendingModel) {
|
||||
@ -975,8 +1019,58 @@ export default function Settings() {
|
||||
}
|
||||
}
|
||||
|
||||
// go2rtc streams are owned by Go2RtcStreamsSettingsView
|
||||
if ("go2rtc_streams" in pendingDataBySection) {
|
||||
try {
|
||||
const liveStreams =
|
||||
(pendingDataBySection["go2rtc_streams"] as Record<
|
||||
string,
|
||||
string[]
|
||||
>) ?? {};
|
||||
const streamsPayload: Record<string, string[] | string> = {
|
||||
...liveStreams,
|
||||
};
|
||||
const deletedStreamNames = Object.keys(
|
||||
config.go2rtc?.streams ?? {},
|
||||
).filter((name) => !(name in liveStreams));
|
||||
for (const deleted of deletedStreamNames) {
|
||||
streamsPayload[deleted] = "";
|
||||
}
|
||||
|
||||
await axios.put("config/set", {
|
||||
requires_restart: 0,
|
||||
config_data: { go2rtc: { streams: streamsPayload } },
|
||||
});
|
||||
|
||||
// Update the running go2rtc instance to match
|
||||
const go2rtcUpdates: Promise<unknown>[] = [];
|
||||
for (const [streamName, urls] of Object.entries(liveStreams)) {
|
||||
if (urls[0]) {
|
||||
go2rtcUpdates.push(
|
||||
axios.put(
|
||||
`go2rtc/streams/${streamName}?src=${encodeURIComponent(urls[0])}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
for (const deleted of deletedStreamNames) {
|
||||
go2rtcUpdates.push(axios.delete(`go2rtc/streams/${deleted}`));
|
||||
}
|
||||
await Promise.allSettled(go2rtcUpdates);
|
||||
|
||||
keysToClear.push("go2rtc_streams");
|
||||
savedKeys.push("go2rtc_streams");
|
||||
successCount++;
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error("Save All – error saving go2rtc streams", error);
|
||||
failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
const pendingKeys = Object.keys(pendingDataBySection).filter(
|
||||
(key) => key !== "detectors" && key !== "model",
|
||||
(key) =>
|
||||
key !== "detectors" && key !== "model" && key !== "go2rtc_streams",
|
||||
);
|
||||
|
||||
for (const key of pendingKeys) {
|
||||
|
||||
@ -58,8 +58,13 @@ import {
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
||||
import SaveAllPreviewPopover, {
|
||||
type SaveAllPreviewItem,
|
||||
} from "@/components/overlay/detail/SaveAllPreviewPopover";
|
||||
import { useDocDomain } from "@/hooks/use-doc-domain";
|
||||
import { FrigateConfig } from "@/types/frigateConfig";
|
||||
import type { SettingsPageProps } from "@/views/settings/SingleSectionPage";
|
||||
import type { ConfigSectionData } from "@/types/configForm";
|
||||
import { cn } from "@/lib/utils";
|
||||
import {
|
||||
isMaskedPath,
|
||||
@ -85,18 +90,8 @@ type RawPathsResponse = {
|
||||
go2rtc: { streams: Record<string, string | string[]> };
|
||||
};
|
||||
|
||||
type Go2RtcStreamsSettingsViewProps = {
|
||||
setUnsavedChanges: React.Dispatch<React.SetStateAction<boolean>>;
|
||||
onSectionStatusChange?: (
|
||||
sectionKey: string,
|
||||
level: "global" | "camera",
|
||||
status: {
|
||||
hasChanges: boolean;
|
||||
isOverridden: boolean;
|
||||
hasValidationErrors: boolean;
|
||||
},
|
||||
) => void;
|
||||
};
|
||||
const SECTION_KEY = "go2rtc_streams";
|
||||
const EMPTY_PENDING: Record<string, ConfigSectionData> = {};
|
||||
|
||||
const STREAM_NAME_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
||||
|
||||
@ -114,7 +109,11 @@ function normalizeStreams(
|
||||
export default function Go2RtcStreamsSettingsView({
|
||||
setUnsavedChanges,
|
||||
onSectionStatusChange,
|
||||
}: Go2RtcStreamsSettingsViewProps) {
|
||||
pendingDataBySection,
|
||||
onPendingDataChange,
|
||||
isSavingAll,
|
||||
onSectionSavingChange,
|
||||
}: SettingsPageProps) {
|
||||
const { t } = useTranslation(["views/settings", "common"]);
|
||||
const { getLocaleDocUrl } = useDocDomain();
|
||||
const { data: config, mutate: updateConfig } =
|
||||
@ -122,13 +121,6 @@ export default function Go2RtcStreamsSettingsView({
|
||||
const { data: rawPaths, mutate: updateRawPaths } =
|
||||
useSWR<RawPathsResponse>("config/raw_paths");
|
||||
|
||||
const [editedStreams, setEditedStreams] = useState<Record<string, string[]>>(
|
||||
{},
|
||||
);
|
||||
const [serverStreams, setServerStreams] = useState<Record<string, string[]>>(
|
||||
{},
|
||||
);
|
||||
const [initialized, setInitialized] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [credentialVisibility, setCredentialVisibility] = useState<
|
||||
Record<string, boolean>
|
||||
@ -138,34 +130,51 @@ export default function Go2RtcStreamsSettingsView({
|
||||
const [addStreamDialogOpen, setAddStreamDialogOpen] = useState(false);
|
||||
const [newlyAdded, setNewlyAdded] = useState<Set<string>>(new Set());
|
||||
|
||||
// Initialize from config — wait for both config and rawPaths to avoid
|
||||
// a mismatch when rawPaths arrives after config with different data
|
||||
useEffect(() => {
|
||||
if (!config || !rawPaths) return;
|
||||
const childPending = pendingDataBySection ?? EMPTY_PENDING;
|
||||
|
||||
// Always use rawPaths for go2rtc streams — the /config endpoint masks
|
||||
// credentials, so using config.go2rtc.streams would save masked values
|
||||
const normalized = normalizeStreams(rawPaths.go2rtc?.streams);
|
||||
// Saved/server state. Always read from rawPaths
|
||||
const serverStreams = useMemo<Record<string, string[]>>(
|
||||
() => normalizeStreams(rawPaths?.go2rtc?.streams),
|
||||
[rawPaths],
|
||||
);
|
||||
|
||||
setServerStreams(normalized);
|
||||
if (!initialized) {
|
||||
setEditedStreams(normalized);
|
||||
setInitialized(true);
|
||||
}
|
||||
}, [config, rawPaths, initialized]);
|
||||
// Pending edits live in the parent's store so they survive navigation; fall back to saved state
|
||||
const liveStreams = useMemo<Record<string, string[]>>(
|
||||
() =>
|
||||
(childPending[SECTION_KEY] as Record<string, string[]> | undefined) ??
|
||||
serverStreams,
|
||||
[childPending, serverStreams],
|
||||
);
|
||||
|
||||
// Persist edits to the parent store, clearing the entry when an edit returns
|
||||
// the section to its saved state so Save All and the sidebar dot reset cleanly.
|
||||
const commitStreams = useCallback(
|
||||
(next: Record<string, string[]>) => {
|
||||
if (isEqual(next, serverStreams)) {
|
||||
onPendingDataChange?.(SECTION_KEY, undefined, null);
|
||||
} else {
|
||||
onPendingDataChange?.(
|
||||
SECTION_KEY,
|
||||
undefined,
|
||||
next as ConfigSectionData,
|
||||
);
|
||||
}
|
||||
},
|
||||
[serverStreams, onPendingDataChange],
|
||||
);
|
||||
|
||||
// Track unsaved changes
|
||||
const hasChanges = useMemo(
|
||||
() => initialized && !isEqual(editedStreams, serverStreams),
|
||||
[editedStreams, serverStreams, initialized],
|
||||
() => !isEqual(liveStreams, serverStreams),
|
||||
[liveStreams, serverStreams],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setUnsavedChanges(hasChanges);
|
||||
setUnsavedChanges?.(hasChanges);
|
||||
}, [hasChanges, setUnsavedChanges]);
|
||||
|
||||
const hasValidationErrors = useMemo(() => {
|
||||
const names = Object.keys(editedStreams);
|
||||
const names = Object.keys(liveStreams);
|
||||
const seenNames = new Set<string>();
|
||||
|
||||
for (const name of names) {
|
||||
@ -173,13 +182,43 @@ export default function Go2RtcStreamsSettingsView({
|
||||
if (seenNames.has(name)) return true;
|
||||
seenNames.add(name);
|
||||
|
||||
const urls = editedStreams[name];
|
||||
const urls = liveStreams[name];
|
||||
if (!urls || urls.length === 0 || urls.every((u) => !u.trim()))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}, [editedStreams]);
|
||||
}, [liveStreams]);
|
||||
|
||||
// Pending changes for this section's Save All preview popover. Diff the
|
||||
// pending streams against the saved state and mask credentials for display.
|
||||
const sectionPreviewItems = useMemo<SaveAllPreviewItem[]>(() => {
|
||||
if (!hasChanges) return [];
|
||||
const items: SaveAllPreviewItem[] = [];
|
||||
|
||||
// Added or changed streams
|
||||
for (const [name, urls] of Object.entries(liveStreams)) {
|
||||
if (name in serverStreams && isEqual(urls, serverStreams[name])) continue;
|
||||
const masked = urls.map((url) => maskCredentials(url));
|
||||
items.push({
|
||||
scope: "global",
|
||||
fieldPath: `go2rtc.streams.${name}`,
|
||||
value: masked.length === 1 ? masked[0] : masked,
|
||||
});
|
||||
}
|
||||
|
||||
// Deleted streams (present in saved config, absent from pending)
|
||||
for (const name of Object.keys(serverStreams)) {
|
||||
if (name in liveStreams) continue;
|
||||
items.push({
|
||||
scope: "global",
|
||||
fieldPath: `go2rtc.streams.${name}`,
|
||||
value: "",
|
||||
});
|
||||
}
|
||||
|
||||
return items;
|
||||
}, [hasChanges, liveStreams, serverStreams]);
|
||||
|
||||
// Report status to parent for sidebar red dot
|
||||
useEffect(() => {
|
||||
@ -193,13 +232,14 @@ export default function Go2RtcStreamsSettingsView({
|
||||
// Save handler
|
||||
const saveToConfig = useCallback(async () => {
|
||||
setIsLoading(true);
|
||||
onSectionSavingChange?.(true);
|
||||
|
||||
try {
|
||||
const streamsPayload: Record<string, string[] | string> = {
|
||||
...editedStreams,
|
||||
...liveStreams,
|
||||
};
|
||||
const deletedStreamNames = Object.keys(serverStreams).filter(
|
||||
(name) => !(name in editedStreams),
|
||||
(name) => !(name in liveStreams),
|
||||
);
|
||||
for (const deleted of deletedStreamNames) {
|
||||
streamsPayload[deleted] = "";
|
||||
@ -212,7 +252,7 @@ export default function Go2RtcStreamsSettingsView({
|
||||
|
||||
// Update running go2rtc instance
|
||||
const go2rtcUpdates: Promise<unknown>[] = [];
|
||||
for (const [streamName, urls] of Object.entries(editedStreams)) {
|
||||
for (const [streamName, urls] of Object.entries(liveStreams)) {
|
||||
if (urls[0]) {
|
||||
go2rtcUpdates.push(
|
||||
axios.put(
|
||||
@ -233,9 +273,9 @@ export default function Go2RtcStreamsSettingsView({
|
||||
}),
|
||||
);
|
||||
|
||||
setServerStreams(editedStreams);
|
||||
updateConfig();
|
||||
updateRawPaths();
|
||||
await updateConfig();
|
||||
await updateRawPaths();
|
||||
onPendingDataChange?.(SECTION_KEY, undefined, null);
|
||||
} catch {
|
||||
toast.error(
|
||||
t("toast.error", {
|
||||
@ -245,74 +285,86 @@ export default function Go2RtcStreamsSettingsView({
|
||||
);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
onSectionSavingChange?.(false);
|
||||
}
|
||||
}, [editedStreams, serverStreams, t, updateConfig, updateRawPaths]);
|
||||
}, [
|
||||
liveStreams,
|
||||
serverStreams,
|
||||
t,
|
||||
updateConfig,
|
||||
updateRawPaths,
|
||||
onPendingDataChange,
|
||||
onSectionSavingChange,
|
||||
]);
|
||||
|
||||
// Reset handler
|
||||
const onReset = useCallback(() => {
|
||||
setEditedStreams(serverStreams);
|
||||
onPendingDataChange?.(SECTION_KEY, undefined, null);
|
||||
setCredentialVisibility({});
|
||||
}, [serverStreams]);
|
||||
}, [onPendingDataChange]);
|
||||
|
||||
// Stream CRUD operations
|
||||
const addStream = useCallback((name: string) => {
|
||||
setEditedStreams((prev) => ({ ...prev, [name]: [""] }));
|
||||
setNewlyAdded((prev) => new Set(prev).add(name));
|
||||
setAddStreamDialogOpen(false);
|
||||
}, []);
|
||||
const addStream = useCallback(
|
||||
(name: string) => {
|
||||
commitStreams({ ...liveStreams, [name]: [""] });
|
||||
setNewlyAdded((prev) => new Set(prev).add(name));
|
||||
setAddStreamDialogOpen(false);
|
||||
},
|
||||
[liveStreams, commitStreams],
|
||||
);
|
||||
|
||||
const deleteStream = useCallback((streamName: string) => {
|
||||
setEditedStreams((prev) => {
|
||||
const { [streamName]: _, ...rest } = prev;
|
||||
return rest;
|
||||
});
|
||||
setDeleteDialog(null);
|
||||
}, []);
|
||||
const deleteStream = useCallback(
|
||||
(streamName: string) => {
|
||||
const { [streamName]: _removed, ...rest } = liveStreams;
|
||||
commitStreams(rest);
|
||||
setDeleteDialog(null);
|
||||
},
|
||||
[liveStreams, commitStreams],
|
||||
);
|
||||
|
||||
const renameStream = useCallback((oldName: string, newName: string) => {
|
||||
if (oldName === newName || !newName.trim()) return;
|
||||
const renameStream = useCallback(
|
||||
(oldName: string, newName: string) => {
|
||||
if (oldName === newName || !newName.trim()) return;
|
||||
if (!(oldName in liveStreams)) return;
|
||||
|
||||
setEditedStreams((prev) => {
|
||||
const urls = prev[oldName];
|
||||
if (!urls) return prev;
|
||||
|
||||
const entries = Object.entries(prev);
|
||||
const result: Record<string, string[]> = {};
|
||||
for (const [key, value] of entries) {
|
||||
if (key === oldName) {
|
||||
result[newName] = value;
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
for (const [key, value] of Object.entries(liveStreams)) {
|
||||
result[key === oldName ? newName : key] = value;
|
||||
}
|
||||
return result;
|
||||
});
|
||||
}, []);
|
||||
commitStreams(result);
|
||||
},
|
||||
[liveStreams, commitStreams],
|
||||
);
|
||||
|
||||
const updateUrl = useCallback(
|
||||
(streamName: string, urlIndex: number, newUrl: string) => {
|
||||
setEditedStreams((prev) => {
|
||||
const urls = [...(prev[streamName] || [])];
|
||||
urls[urlIndex] = newUrl;
|
||||
return { ...prev, [streamName]: urls };
|
||||
});
|
||||
const urls = [...(liveStreams[streamName] || [])];
|
||||
urls[urlIndex] = newUrl;
|
||||
commitStreams({ ...liveStreams, [streamName]: urls });
|
||||
},
|
||||
[],
|
||||
[liveStreams, commitStreams],
|
||||
);
|
||||
|
||||
const addUrl = useCallback((streamName: string) => {
|
||||
setEditedStreams((prev) => {
|
||||
const urls = [...(prev[streamName] || []), ""];
|
||||
return { ...prev, [streamName]: urls };
|
||||
});
|
||||
}, []);
|
||||
const addUrl = useCallback(
|
||||
(streamName: string) => {
|
||||
const urls = [...(liveStreams[streamName] || []), ""];
|
||||
commitStreams({ ...liveStreams, [streamName]: urls });
|
||||
},
|
||||
[liveStreams, commitStreams],
|
||||
);
|
||||
|
||||
const removeUrl = useCallback((streamName: string, urlIndex: number) => {
|
||||
setEditedStreams((prev) => {
|
||||
const urls = (prev[streamName] || []).filter((_, i) => i !== urlIndex);
|
||||
return { ...prev, [streamName]: urls.length > 0 ? urls : [""] };
|
||||
});
|
||||
}, []);
|
||||
const removeUrl = useCallback(
|
||||
(streamName: string, urlIndex: number) => {
|
||||
const urls = (liveStreams[streamName] || []).filter(
|
||||
(_, i) => i !== urlIndex,
|
||||
);
|
||||
commitStreams({
|
||||
...liveStreams,
|
||||
[streamName]: urls.length > 0 ? urls : [""],
|
||||
});
|
||||
},
|
||||
[liveStreams, commitStreams],
|
||||
);
|
||||
|
||||
const toggleCredentialVisibility = useCallback((key: string) => {
|
||||
setCredentialVisibility((prev) => ({ ...prev, [key]: !prev[key] }));
|
||||
@ -320,7 +372,7 @@ export default function Go2RtcStreamsSettingsView({
|
||||
|
||||
if (!config) return null;
|
||||
|
||||
const streamEntries = Object.entries(editedStreams);
|
||||
const streamEntries = Object.entries(liveStreams);
|
||||
|
||||
return (
|
||||
<div className="flex size-full flex-col lg:pr-2">
|
||||
@ -391,6 +443,12 @@ export default function Go2RtcStreamsSettingsView({
|
||||
<span className="text-sm text-unsaved">
|
||||
{t("unsavedChanges")}
|
||||
</span>
|
||||
<SaveAllPreviewPopover
|
||||
items={sectionPreviewItems}
|
||||
className="h-7 w-7"
|
||||
align="start"
|
||||
side="top"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex w-full flex-col gap-2 sm:flex-row sm:items-center md:w-auto">
|
||||
@ -398,7 +456,7 @@ export default function Go2RtcStreamsSettingsView({
|
||||
<Button
|
||||
onClick={onReset}
|
||||
variant="outline"
|
||||
disabled={isLoading}
|
||||
disabled={isLoading || isSavingAll}
|
||||
className="flex min-w-36 flex-1 gap-2"
|
||||
>
|
||||
{t("button.undo", { ns: "common" })}
|
||||
@ -407,7 +465,9 @@ export default function Go2RtcStreamsSettingsView({
|
||||
<Button
|
||||
onClick={saveToConfig}
|
||||
variant="select"
|
||||
disabled={!hasChanges || isLoading || hasValidationErrors}
|
||||
disabled={
|
||||
!hasChanges || isLoading || isSavingAll || hasValidationErrors
|
||||
}
|
||||
className="flex min-w-36 flex-1 gap-2"
|
||||
>
|
||||
{isLoading ? (
|
||||
@ -459,7 +519,7 @@ export default function Go2RtcStreamsSettingsView({
|
||||
<RenameStreamDialog
|
||||
open={renameDialog !== null}
|
||||
streamName={renameDialog ?? ""}
|
||||
allStreamNames={Object.keys(editedStreams)}
|
||||
allStreamNames={Object.keys(liveStreams)}
|
||||
onRename={(oldName, newName) => {
|
||||
renameStream(oldName, newName);
|
||||
setRenameDialog(null);
|
||||
@ -469,7 +529,7 @@ export default function Go2RtcStreamsSettingsView({
|
||||
|
||||
<AddStreamDialog
|
||||
open={addStreamDialogOpen}
|
||||
allStreamNames={Object.keys(editedStreams)}
|
||||
allStreamNames={Object.keys(liveStreams)}
|
||||
onAdd={addStream}
|
||||
onClose={() => setAddStreamDialogOpen(false)}
|
||||
/>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user