refactor: LocalStorage state integration

This commit is contained in:
Taso committed 2021-01-12 11:28:20 -05:00
1 parent 5af6e4936d
commit 8912650f0e
10 files changed
+103 -154

No files matched your search

+23 -14
View File
@@ -1,19 +1,28 @@
import Default from '../default.json';
import { useConfig } from '../../../config/hooks/useConfig';
import { atom } from 'recoil';
import { createMuiTheme } from '@material-ui/core';
import useLocalStorage, {
getStorage,
} from '../../../os/phone/hooks/useLocalStorage';
import { atom, DefaultValue, useRecoilState } from 'recoil';
import config from '../../../config/default.json';
const settingsState = atom({
const localStorageEffect = (key) => ({ setSelf, onSet }) => {
const savedVal = localStorage.getItem(key);
if (savedVal != null) {
setSelf(JSON.parse(savedVal));
}
onSet((newValue) => {
if (newValue instanceof DefaultValue) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, JSON.stringify(newValue));
}
});
};
export const settingsState = atom({
key: 'settings',
default: Default,
default: config.defaultSettings,
effects_UNSTABLE: [localStorageEffect('settings')],
});
export const useSettings = (): any => {
const theme = getStorage('theme');
const [config] = useConfig();
const currentTheme = () => createMuiTheme(config.themes[theme]);
return { currentTheme };
export const useSettings = () => {
const [settings, setSettings] = useRecoilState(settingsState);
return [settings, setSettings];
};