mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-06-21 03:41:55 +03:00
add go2rtc settings section to the save all flow (#23501)
Some checks failed
CI / AMD64 Build (push) Has been cancelled
CI / ARM Build (push) Has been cancelled
CI / Jetson Jetpack 6 (push) Has been cancelled
CI / AMD64 Extra Build (push) Has been cancelled
CI / ARM Extra Build (push) Has been cancelled
CI / Synaptics Build (push) Has been cancelled
CI / Assemble and push default build (push) Has been cancelled
Some checks failed
CI / AMD64 Build (push) Has been cancelled
CI / ARM Build (push) Has been cancelled
CI / Jetson Jetpack 6 (push) Has been cancelled
CI / AMD64 Extra Build (push) Has been cancelled
CI / ARM Extra Build (push) Has been cancelled
CI / Synaptics Build (push) Has been cancelled
CI / Assemble and push default build (push) Has been cancelled
This commit is contained in:
parent
282e70d4bf
commit
8203e39b7f
@ -34,6 +34,8 @@ import { isMobile } from "react-device-detect";
|
|||||||
import { FaVideo } from "react-icons/fa";
|
import { FaVideo } from "react-icons/fa";
|
||||||
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig";
|
import { CameraConfig, FrigateConfig } from "@/types/frigateConfig";
|
||||||
import type { ConfigSectionData, JsonObject } from "@/types/configForm";
|
import type { ConfigSectionData, JsonObject } from "@/types/configForm";
|
||||||
|
import isEqual from "lodash/isEqual";
|
||||||
|
import { maskCredentials } from "@/utils/credentialMask";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import FilterSwitch from "@/components/filter/FilterSwitch";
|
import FilterSwitch from "@/components/filter/FilterSwitch";
|
||||||
import { ZoneMaskFilterButton } from "@/components/filter/ZoneMaskFilter";
|
import { ZoneMaskFilterButton } from "@/components/filter/ZoneMaskFilter";
|
||||||
@ -660,6 +662,11 @@ export default function Settings() {
|
|||||||
|
|
||||||
const isAdmin = useIsAdmin();
|
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
|
const visibleSettingsViews = !isAdmin
|
||||||
? ALLOWED_VIEWS_FOR_VIEWER
|
? ALLOWED_VIEWS_FOR_VIEWER
|
||||||
: allSettingsViews;
|
: 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) => {
|
return items.sort((left, right) => {
|
||||||
const scopeCompare = left.scope.localeCompare(right.scope);
|
const scopeCompare = left.scope.localeCompare(right.scope);
|
||||||
if (scopeCompare !== 0) return scopeCompare;
|
if (scopeCompare !== 0) return scopeCompare;
|
||||||
@ -797,7 +838,13 @@ export default function Settings() {
|
|||||||
if (cameraCompare !== 0) return cameraCompare;
|
if (cameraCompare !== 0) return cameraCompare;
|
||||||
return left.fieldPath.localeCompare(right.fieldPath);
|
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
|
// Map a pendingDataKey to SettingsType menu key for clearing section status
|
||||||
const pendingKeyToMenuKey = useCallback(
|
const pendingKeyToMenuKey = useCallback(
|
||||||
@ -869,10 +916,7 @@ export default function Settings() {
|
|||||||
// after `mutate("config")` resolves
|
// after `mutate("config")` resolves
|
||||||
const keysToClear: string[] = [];
|
const keysToClear: string[] = [];
|
||||||
|
|
||||||
// `detectors` and `model` are owned by DetectorsAndModelSettingsView,
|
// `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
|
|
||||||
const hasPendingDetectors = "detectors" in pendingDataBySection;
|
const hasPendingDetectors = "detectors" in pendingDataBySection;
|
||||||
const hasPendingModel = "model" in pendingDataBySection;
|
const hasPendingModel = "model" in pendingDataBySection;
|
||||||
if (hasPendingDetectors || hasPendingModel) {
|
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(
|
const pendingKeys = Object.keys(pendingDataBySection).filter(
|
||||||
(key) => key !== "detectors" && key !== "model",
|
(key) =>
|
||||||
|
key !== "detectors" && key !== "model" && key !== "go2rtc_streams",
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const key of pendingKeys) {
|
for (const key of pendingKeys) {
|
||||||
|
|||||||
@ -58,8 +58,13 @@ import {
|
|||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
import ActivityIndicator from "@/components/indicators/activity-indicator";
|
||||||
|
import SaveAllPreviewPopover, {
|
||||||
|
type SaveAllPreviewItem,
|
||||||
|
} from "@/components/overlay/detail/SaveAllPreviewPopover";
|
||||||
import { useDocDomain } from "@/hooks/use-doc-domain";
|
import { useDocDomain } from "@/hooks/use-doc-domain";
|
||||||
import { FrigateConfig } from "@/types/frigateConfig";
|
import { FrigateConfig } from "@/types/frigateConfig";
|
||||||
|
import type { SettingsPageProps } from "@/views/settings/SingleSectionPage";
|
||||||
|
import type { ConfigSectionData } from "@/types/configForm";
|
||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import {
|
import {
|
||||||
isMaskedPath,
|
isMaskedPath,
|
||||||
@ -85,18 +90,8 @@ type RawPathsResponse = {
|
|||||||
go2rtc: { streams: Record<string, string | string[]> };
|
go2rtc: { streams: Record<string, string | string[]> };
|
||||||
};
|
};
|
||||||
|
|
||||||
type Go2RtcStreamsSettingsViewProps = {
|
const SECTION_KEY = "go2rtc_streams";
|
||||||
setUnsavedChanges: React.Dispatch<React.SetStateAction<boolean>>;
|
const EMPTY_PENDING: Record<string, ConfigSectionData> = {};
|
||||||
onSectionStatusChange?: (
|
|
||||||
sectionKey: string,
|
|
||||||
level: "global" | "camera",
|
|
||||||
status: {
|
|
||||||
hasChanges: boolean;
|
|
||||||
isOverridden: boolean;
|
|
||||||
hasValidationErrors: boolean;
|
|
||||||
},
|
|
||||||
) => void;
|
|
||||||
};
|
|
||||||
|
|
||||||
const STREAM_NAME_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
const STREAM_NAME_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
||||||
|
|
||||||
@ -114,7 +109,11 @@ function normalizeStreams(
|
|||||||
export default function Go2RtcStreamsSettingsView({
|
export default function Go2RtcStreamsSettingsView({
|
||||||
setUnsavedChanges,
|
setUnsavedChanges,
|
||||||
onSectionStatusChange,
|
onSectionStatusChange,
|
||||||
}: Go2RtcStreamsSettingsViewProps) {
|
pendingDataBySection,
|
||||||
|
onPendingDataChange,
|
||||||
|
isSavingAll,
|
||||||
|
onSectionSavingChange,
|
||||||
|
}: SettingsPageProps) {
|
||||||
const { t } = useTranslation(["views/settings", "common"]);
|
const { t } = useTranslation(["views/settings", "common"]);
|
||||||
const { getLocaleDocUrl } = useDocDomain();
|
const { getLocaleDocUrl } = useDocDomain();
|
||||||
const { data: config, mutate: updateConfig } =
|
const { data: config, mutate: updateConfig } =
|
||||||
@ -122,13 +121,6 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
const { data: rawPaths, mutate: updateRawPaths } =
|
const { data: rawPaths, mutate: updateRawPaths } =
|
||||||
useSWR<RawPathsResponse>("config/raw_paths");
|
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 [isLoading, setIsLoading] = useState(false);
|
||||||
const [credentialVisibility, setCredentialVisibility] = useState<
|
const [credentialVisibility, setCredentialVisibility] = useState<
|
||||||
Record<string, boolean>
|
Record<string, boolean>
|
||||||
@ -138,34 +130,51 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
const [addStreamDialogOpen, setAddStreamDialogOpen] = useState(false);
|
const [addStreamDialogOpen, setAddStreamDialogOpen] = useState(false);
|
||||||
const [newlyAdded, setNewlyAdded] = useState<Set<string>>(new Set());
|
const [newlyAdded, setNewlyAdded] = useState<Set<string>>(new Set());
|
||||||
|
|
||||||
// Initialize from config — wait for both config and rawPaths to avoid
|
const childPending = pendingDataBySection ?? EMPTY_PENDING;
|
||||||
// a mismatch when rawPaths arrives after config with different data
|
|
||||||
useEffect(() => {
|
|
||||||
if (!config || !rawPaths) return;
|
|
||||||
|
|
||||||
// Always use rawPaths for go2rtc streams — the /config endpoint masks
|
// Saved/server state. Always read from rawPaths
|
||||||
// credentials, so using config.go2rtc.streams would save masked values
|
const serverStreams = useMemo<Record<string, string[]>>(
|
||||||
const normalized = normalizeStreams(rawPaths.go2rtc?.streams);
|
() => normalizeStreams(rawPaths?.go2rtc?.streams),
|
||||||
|
[rawPaths],
|
||||||
|
);
|
||||||
|
|
||||||
setServerStreams(normalized);
|
// Pending edits live in the parent's store so they survive navigation; fall back to saved state
|
||||||
if (!initialized) {
|
const liveStreams = useMemo<Record<string, string[]>>(
|
||||||
setEditedStreams(normalized);
|
() =>
|
||||||
setInitialized(true);
|
(childPending[SECTION_KEY] as Record<string, string[]> | undefined) ??
|
||||||
}
|
serverStreams,
|
||||||
}, [config, rawPaths, initialized]);
|
[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
|
// Track unsaved changes
|
||||||
const hasChanges = useMemo(
|
const hasChanges = useMemo(
|
||||||
() => initialized && !isEqual(editedStreams, serverStreams),
|
() => !isEqual(liveStreams, serverStreams),
|
||||||
[editedStreams, serverStreams, initialized],
|
[liveStreams, serverStreams],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setUnsavedChanges(hasChanges);
|
setUnsavedChanges?.(hasChanges);
|
||||||
}, [hasChanges, setUnsavedChanges]);
|
}, [hasChanges, setUnsavedChanges]);
|
||||||
|
|
||||||
const hasValidationErrors = useMemo(() => {
|
const hasValidationErrors = useMemo(() => {
|
||||||
const names = Object.keys(editedStreams);
|
const names = Object.keys(liveStreams);
|
||||||
const seenNames = new Set<string>();
|
const seenNames = new Set<string>();
|
||||||
|
|
||||||
for (const name of names) {
|
for (const name of names) {
|
||||||
@ -173,13 +182,43 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
if (seenNames.has(name)) return true;
|
if (seenNames.has(name)) return true;
|
||||||
seenNames.add(name);
|
seenNames.add(name);
|
||||||
|
|
||||||
const urls = editedStreams[name];
|
const urls = liveStreams[name];
|
||||||
if (!urls || urls.length === 0 || urls.every((u) => !u.trim()))
|
if (!urls || urls.length === 0 || urls.every((u) => !u.trim()))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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
|
// Report status to parent for sidebar red dot
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -193,13 +232,14 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
// Save handler
|
// Save handler
|
||||||
const saveToConfig = useCallback(async () => {
|
const saveToConfig = useCallback(async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
|
onSectionSavingChange?.(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const streamsPayload: Record<string, string[] | string> = {
|
const streamsPayload: Record<string, string[] | string> = {
|
||||||
...editedStreams,
|
...liveStreams,
|
||||||
};
|
};
|
||||||
const deletedStreamNames = Object.keys(serverStreams).filter(
|
const deletedStreamNames = Object.keys(serverStreams).filter(
|
||||||
(name) => !(name in editedStreams),
|
(name) => !(name in liveStreams),
|
||||||
);
|
);
|
||||||
for (const deleted of deletedStreamNames) {
|
for (const deleted of deletedStreamNames) {
|
||||||
streamsPayload[deleted] = "";
|
streamsPayload[deleted] = "";
|
||||||
@ -212,7 +252,7 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
|
|
||||||
// Update running go2rtc instance
|
// Update running go2rtc instance
|
||||||
const go2rtcUpdates: Promise<unknown>[] = [];
|
const go2rtcUpdates: Promise<unknown>[] = [];
|
||||||
for (const [streamName, urls] of Object.entries(editedStreams)) {
|
for (const [streamName, urls] of Object.entries(liveStreams)) {
|
||||||
if (urls[0]) {
|
if (urls[0]) {
|
||||||
go2rtcUpdates.push(
|
go2rtcUpdates.push(
|
||||||
axios.put(
|
axios.put(
|
||||||
@ -233,9 +273,9 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
setServerStreams(editedStreams);
|
await updateConfig();
|
||||||
updateConfig();
|
await updateRawPaths();
|
||||||
updateRawPaths();
|
onPendingDataChange?.(SECTION_KEY, undefined, null);
|
||||||
} catch {
|
} catch {
|
||||||
toast.error(
|
toast.error(
|
||||||
t("toast.error", {
|
t("toast.error", {
|
||||||
@ -245,74 +285,86 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
onSectionSavingChange?.(false);
|
||||||
}
|
}
|
||||||
}, [editedStreams, serverStreams, t, updateConfig, updateRawPaths]);
|
}, [
|
||||||
|
liveStreams,
|
||||||
|
serverStreams,
|
||||||
|
t,
|
||||||
|
updateConfig,
|
||||||
|
updateRawPaths,
|
||||||
|
onPendingDataChange,
|
||||||
|
onSectionSavingChange,
|
||||||
|
]);
|
||||||
|
|
||||||
// Reset handler
|
// Reset handler
|
||||||
const onReset = useCallback(() => {
|
const onReset = useCallback(() => {
|
||||||
setEditedStreams(serverStreams);
|
onPendingDataChange?.(SECTION_KEY, undefined, null);
|
||||||
setCredentialVisibility({});
|
setCredentialVisibility({});
|
||||||
}, [serverStreams]);
|
}, [onPendingDataChange]);
|
||||||
|
|
||||||
// Stream CRUD operations
|
// Stream CRUD operations
|
||||||
const addStream = useCallback((name: string) => {
|
const addStream = useCallback(
|
||||||
setEditedStreams((prev) => ({ ...prev, [name]: [""] }));
|
(name: string) => {
|
||||||
setNewlyAdded((prev) => new Set(prev).add(name));
|
commitStreams({ ...liveStreams, [name]: [""] });
|
||||||
setAddStreamDialogOpen(false);
|
setNewlyAdded((prev) => new Set(prev).add(name));
|
||||||
}, []);
|
setAddStreamDialogOpen(false);
|
||||||
|
},
|
||||||
|
[liveStreams, commitStreams],
|
||||||
|
);
|
||||||
|
|
||||||
const deleteStream = useCallback((streamName: string) => {
|
const deleteStream = useCallback(
|
||||||
setEditedStreams((prev) => {
|
(streamName: string) => {
|
||||||
const { [streamName]: _, ...rest } = prev;
|
const { [streamName]: _removed, ...rest } = liveStreams;
|
||||||
return rest;
|
commitStreams(rest);
|
||||||
});
|
setDeleteDialog(null);
|
||||||
setDeleteDialog(null);
|
},
|
||||||
}, []);
|
[liveStreams, commitStreams],
|
||||||
|
);
|
||||||
|
|
||||||
const renameStream = useCallback((oldName: string, newName: string) => {
|
const renameStream = useCallback(
|
||||||
if (oldName === newName || !newName.trim()) return;
|
(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[]> = {};
|
const result: Record<string, string[]> = {};
|
||||||
for (const [key, value] of entries) {
|
for (const [key, value] of Object.entries(liveStreams)) {
|
||||||
if (key === oldName) {
|
result[key === oldName ? newName : key] = value;
|
||||||
result[newName] = value;
|
|
||||||
} else {
|
|
||||||
result[key] = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
commitStreams(result);
|
||||||
});
|
},
|
||||||
}, []);
|
[liveStreams, commitStreams],
|
||||||
|
);
|
||||||
|
|
||||||
const updateUrl = useCallback(
|
const updateUrl = useCallback(
|
||||||
(streamName: string, urlIndex: number, newUrl: string) => {
|
(streamName: string, urlIndex: number, newUrl: string) => {
|
||||||
setEditedStreams((prev) => {
|
const urls = [...(liveStreams[streamName] || [])];
|
||||||
const urls = [...(prev[streamName] || [])];
|
urls[urlIndex] = newUrl;
|
||||||
urls[urlIndex] = newUrl;
|
commitStreams({ ...liveStreams, [streamName]: urls });
|
||||||
return { ...prev, [streamName]: urls };
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
[],
|
[liveStreams, commitStreams],
|
||||||
);
|
);
|
||||||
|
|
||||||
const addUrl = useCallback((streamName: string) => {
|
const addUrl = useCallback(
|
||||||
setEditedStreams((prev) => {
|
(streamName: string) => {
|
||||||
const urls = [...(prev[streamName] || []), ""];
|
const urls = [...(liveStreams[streamName] || []), ""];
|
||||||
return { ...prev, [streamName]: urls };
|
commitStreams({ ...liveStreams, [streamName]: urls });
|
||||||
});
|
},
|
||||||
}, []);
|
[liveStreams, commitStreams],
|
||||||
|
);
|
||||||
|
|
||||||
const removeUrl = useCallback((streamName: string, urlIndex: number) => {
|
const removeUrl = useCallback(
|
||||||
setEditedStreams((prev) => {
|
(streamName: string, urlIndex: number) => {
|
||||||
const urls = (prev[streamName] || []).filter((_, i) => i !== urlIndex);
|
const urls = (liveStreams[streamName] || []).filter(
|
||||||
return { ...prev, [streamName]: urls.length > 0 ? urls : [""] };
|
(_, i) => i !== urlIndex,
|
||||||
});
|
);
|
||||||
}, []);
|
commitStreams({
|
||||||
|
...liveStreams,
|
||||||
|
[streamName]: urls.length > 0 ? urls : [""],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[liveStreams, commitStreams],
|
||||||
|
);
|
||||||
|
|
||||||
const toggleCredentialVisibility = useCallback((key: string) => {
|
const toggleCredentialVisibility = useCallback((key: string) => {
|
||||||
setCredentialVisibility((prev) => ({ ...prev, [key]: !prev[key] }));
|
setCredentialVisibility((prev) => ({ ...prev, [key]: !prev[key] }));
|
||||||
@ -320,7 +372,7 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
|
|
||||||
if (!config) return null;
|
if (!config) return null;
|
||||||
|
|
||||||
const streamEntries = Object.entries(editedStreams);
|
const streamEntries = Object.entries(liveStreams);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex size-full flex-col lg:pr-2">
|
<div className="flex size-full flex-col lg:pr-2">
|
||||||
@ -391,6 +443,12 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
<span className="text-sm text-unsaved">
|
<span className="text-sm text-unsaved">
|
||||||
{t("unsavedChanges")}
|
{t("unsavedChanges")}
|
||||||
</span>
|
</span>
|
||||||
|
<SaveAllPreviewPopover
|
||||||
|
items={sectionPreviewItems}
|
||||||
|
className="h-7 w-7"
|
||||||
|
align="start"
|
||||||
|
side="top"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div className="flex w-full flex-col gap-2 sm:flex-row sm:items-center md:w-auto">
|
<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
|
<Button
|
||||||
onClick={onReset}
|
onClick={onReset}
|
||||||
variant="outline"
|
variant="outline"
|
||||||
disabled={isLoading}
|
disabled={isLoading || isSavingAll}
|
||||||
className="flex min-w-36 flex-1 gap-2"
|
className="flex min-w-36 flex-1 gap-2"
|
||||||
>
|
>
|
||||||
{t("button.undo", { ns: "common" })}
|
{t("button.undo", { ns: "common" })}
|
||||||
@ -407,7 +465,9 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
<Button
|
<Button
|
||||||
onClick={saveToConfig}
|
onClick={saveToConfig}
|
||||||
variant="select"
|
variant="select"
|
||||||
disabled={!hasChanges || isLoading || hasValidationErrors}
|
disabled={
|
||||||
|
!hasChanges || isLoading || isSavingAll || hasValidationErrors
|
||||||
|
}
|
||||||
className="flex min-w-36 flex-1 gap-2"
|
className="flex min-w-36 flex-1 gap-2"
|
||||||
>
|
>
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
@ -459,7 +519,7 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
<RenameStreamDialog
|
<RenameStreamDialog
|
||||||
open={renameDialog !== null}
|
open={renameDialog !== null}
|
||||||
streamName={renameDialog ?? ""}
|
streamName={renameDialog ?? ""}
|
||||||
allStreamNames={Object.keys(editedStreams)}
|
allStreamNames={Object.keys(liveStreams)}
|
||||||
onRename={(oldName, newName) => {
|
onRename={(oldName, newName) => {
|
||||||
renameStream(oldName, newName);
|
renameStream(oldName, newName);
|
||||||
setRenameDialog(null);
|
setRenameDialog(null);
|
||||||
@ -469,7 +529,7 @@ export default function Go2RtcStreamsSettingsView({
|
|||||||
|
|
||||||
<AddStreamDialog
|
<AddStreamDialog
|
||||||
open={addStreamDialogOpen}
|
open={addStreamDialogOpen}
|
||||||
allStreamNames={Object.keys(editedStreams)}
|
allStreamNames={Object.keys(liveStreams)}
|
||||||
onAdd={addStream}
|
onAdd={addStream}
|
||||||
onClose={() => setAddStreamDialogOpen(false)}
|
onClose={() => setAddStreamDialogOpen(false)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user