Files
ecoplanet/src/components/main/lib/state.ts
2024-01-23 18:00:52 +01:00

37 lines
853 B
TypeScript

import { modifyPath, mergeDeepRight } from "ramda";
const STORAGE_KEY = "ecoplanet";
const defaultState = {
dateCache: undefined,
options: {
lineWidth: undefined,
defaultDate: undefined,
},
};
const stringifyState = function (state) {
return JSON.stringify(state);
};
const parseState = function (jsonString) {
const state = JSON.parse(jsonString);
return modifyPath(["options", "defaultDate"], Date.parse, state);
};
export const persistStorage = function (state) {
localStorage.setItem(STORAGE_KEY, stringifyState(state));
};
export const loadStorage = function () {
const storageStateString = localStorage.getItem(STORAGE_KEY);
if (storageStateString) {
return parseState(storageStateString);
}
};
export const loadStorageOrDefault = function (state) {
return mergeDeepRight(loadStorage(), defaultState);
};