forked from blakeblackshear/frigate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist live view muted/unmuted for session only (blakeblackshear#12727)
* Persist live view muted/unmuted for session only * consistent naming
- Loading branch information
1 parent
bb65f4e
commit 05561d9
Showing
2 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters