forked from Github/frigate
Persist live view muted/unmuted for session only (#12727)
* Persist live view muted/unmuted for session only * consistent naming
This commit is contained in:
39
web/src/hooks/use-session-persistence.ts
Normal file
39
web/src/hooks/use-session-persistence.ts
Normal file
@@ -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];
|
||||
}
|
||||
Reference in New Issue
Block a user