mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-10-03 02:32:00 +08:00
Persist slice of redux state in local storage
This commit is contained in:
24
ui/src/localStorage.ts
Normal file
24
ui/src/localStorage.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { AppState } from "./store";
|
||||
|
||||
const LOCAL_STORAGE_KEY = "asynqmon:state";
|
||||
|
||||
export function loadState(): AppState | undefined {
|
||||
try {
|
||||
const serializedState = localStorage.getItem(LOCAL_STORAGE_KEY);
|
||||
if (serializedState === null) {
|
||||
return undefined;
|
||||
}
|
||||
return JSON.parse(serializedState);
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
export function saveState(state: AppState) {
|
||||
try {
|
||||
const serializedState = JSON.stringify({ settings: state.settings });
|
||||
localStorage.setItem(LOCAL_STORAGE_KEY, serializedState);
|
||||
} catch (err) {
|
||||
console.error("saveState: could not save state: ", err);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user