Skip to content

Commit

Permalink
Persist live view muted/unmuted for session only (blakeblackshear#12727)
Browse files Browse the repository at this point in the history
* Persist live view muted/unmuted for session only

* consistent naming
  • Loading branch information
hawkeye217 authored and sandnabba committed Aug 14, 2024
1 parent bb65f4e commit 05561d9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
39 changes: 39 additions & 0 deletions web/src/hooks/use-session-persistence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useCallback, useState } from "react";

type useSessionPersistenceReturn<S> = [
value: S | undefined,
setValue: (value: S | undefined) => void,
];

export function useSessionPersistence<S>(
key: string,
defaultValue: S | undefined = undefined,
): useSessionPersistenceReturn<S> {
const [storedValue, setStoredValue] = useState(() => {
try {
const value = window.sessionStorage.getItem(key);

if (value) {
return JSON.parse(value);
} else {
window.sessionStorage.setItem(key, JSON.stringify(defaultValue));
return defaultValue;
}
} catch (err) {
return defaultValue;
}
});

const setValue = useCallback(
(newValue: S | undefined) => {
try {
window.sessionStorage.setItem(key, JSON.stringify(newValue));
// eslint-disable-next-line no-empty
} catch (err) {}
setStoredValue(newValue);
},
[key],
);

return [storedValue, setValue];
}
5 changes: 3 additions & 2 deletions web/src/views/live/LiveCameraView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import { useNavigate } from "react-router-dom";
import { TransformWrapper, TransformComponent } from "react-zoom-pan-pinch";
import useSWR from "swr";
import { cn } from "@/lib/utils";
import { useSessionPersistence } from "@/hooks/use-session-persistence";

type LiveCameraViewProps = {
config?: FrigateConfig;
Expand Down Expand Up @@ -194,7 +195,7 @@ export default function LiveCameraView({

// playback state

const [audio, setAudio] = useState(false);
const [audio, setAudio] = useSessionPersistence("liveAudio", false);
const [mic, setMic] = useState(false);
const [webRTC, setWebRTC] = useState(false);
const [pip, setPip] = useState(false);
Expand Down Expand Up @@ -404,7 +405,7 @@ export default function LiveCameraView({
className="p-2 md:p-0"
variant={fullscreen ? "overlay" : "primary"}
Icon={audio ? GiSpeaker : GiSpeakerOff}
isActive={audio}
isActive={audio ?? false}
title={`${audio ? "Disable" : "Enable"} Camera Audio`}
onClick={() => setAudio(!audio)}
/>
Expand Down

0 comments on commit 05561d9

Please sign in to comment.