mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
refactor: LocalStorage state integration
This commit is contained in:
1 parent
5af6e4936d
commit
8912650f0e
10 files changed
+103
-154
No files matched your search
@@ -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];
|
||||
};
|
||||
Reference in new issue
Block a user