feat(app): add persistence of settings across sessions
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
};
|
||||
|
||||
18
src/hooks/usePersistSettings.ts
Normal file
18
src/hooks/usePersistSettings.ts
Normal 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]
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user