feat(app): add persistence of settings across sessions

This commit is contained in:
rektdeckard
2021-11-26 22:03:54 -05:00
parent cdcf38466e
commit 14d8c9d0e7
8 changed files with 140 additions and 10 deletions

View File

@@ -34,4 +34,29 @@ export default () => {
if (normalizedColor.isValid()) setColor(normalizedColor.toHexString());
}
}, [color, setColor]);
useEffect(() => {
if (!weight && !size && !color) {
const persistedState = JSON.parse(
window.localStorage.getItem("__phosphor_settings__") || "null"
);
if (!!persistedState) {
const { weight, size, color } = persistedState;
if (weight) {
if (weight.toUpperCase() in IconStyle) setWeight(weight as IconStyle);
}
if (size) {
const normalizedSize = parseInt(size);
if (typeof normalizedSize === "number" && isFinite(normalizedSize))
setSize(Math.min(Math.max(normalizedSize, 16), 96));
}
if (color) {
const normalizedColor = TinyColor(color);
if (normalizedColor.isValid())
setColor(normalizedColor.toHexString());
}
}
}
}, []);
};

View File

@@ -0,0 +1,18 @@
import { useRecoilValue } from "recoil";
import useDebounce from "./useDebounce";
import { iconWeightAtom, iconSizeAtom, iconColorAtom } from "../state/atoms";
export default function usePersistSettings() {
const weight = useRecoilValue(iconWeightAtom);
const size = useRecoilValue(iconSizeAtom);
const color = useRecoilValue(iconColorAtom);
useDebounce(
() => {
const serializedState = JSON.stringify({ weight, size, color });
window.localStorage.setItem("__phosphor_settings__", serializedState);
},
2000,
[weight, size, color]
);
}