diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 09a5bb6..9da3827 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -25,7 +25,7 @@ import { useNotesService } from './apps/notes/hooks/useNotesService'; import { usePhotoService } from './apps/camera/hooks/usePhotoService'; import Nui from './os/nui-events/utils/Nui'; import { usePhone } from './os/phone/hooks/usePhone'; -import useLocalStorage from './os/phone/hooks/useLocalStorage'; +import { getStorageItem } from './os/phone/hooks/getLocalStorage'; // Inject mock data when in development env. if (process.env.NODE_ENV === 'development') { @@ -246,7 +246,6 @@ function Phone() { useNuiService(); usePhoneService(); const { visibility } = usePhone(); - const [, , getStorageItem] = useLocalStorage(); const { settings, currentTheme } = useSettings(); const { allApps } = useApps(); useSimcardService(); diff --git a/phone/src/apps/settings/hooks/useSettings.ts b/phone/src/apps/settings/hooks/useSettings.ts index 8ef1312..c472605 100644 --- a/phone/src/apps/settings/hooks/useSettings.ts +++ b/phone/src/apps/settings/hooks/useSettings.ts @@ -3,6 +3,7 @@ import { useConfig } from '../../../config/hooks/useConfig'; import { atom } from 'recoil'; import { createMuiTheme } from '@material-ui/core'; import useLocalStorage from '../../../os/phone/hooks/useLocalStorage'; +import { getStorageItem } from '../../../os/phone/hooks/getLocalStorage'; const settingsState = atom({ key: 'settings', @@ -10,10 +11,9 @@ const settingsState = atom({ }); export const useSettings = (): any => { - const [, , getStorageItem] = useLocalStorage('theme', 'taso-dark'); + const theme = getStorageItem('theme'); const [settings, setSettings] = useLocalStorage(); const [config] = useConfig(); - const currentTheme = () => - createMuiTheme(config.themes[getStorageItem('theme')]); + const currentTheme = () => createMuiTheme(config.themes[theme]); return { settings, setSettings, currentTheme }; }; diff --git a/phone/src/config/hooks/useConfig.ts b/phone/src/config/hooks/useConfig.ts index ab2b06e..02f15e7 100644 --- a/phone/src/config/hooks/useConfig.ts +++ b/phone/src/config/hooks/useConfig.ts @@ -8,12 +8,15 @@ const configState = atom({ }); export const useConfig = (state: any = configState): any => { - const [, setVal] = useLocalStorage(); - const [config] = useRecoilState(state); + const [, setVal] = useLocalStorage(''); + const [config, _setConfig] = useRecoilState(state); const setConfig = (key, value) => { - console.log(`setConfig: ${key} ${value}`); - setVal(key, value); + _setConfig((oldConfig) => ({ + ...oldConfig, + [key]: value, + })); + //setVal(key, value); }; return [config, setConfig]; diff --git a/phone/src/os/phone/hooks/getLocalStorage.ts b/phone/src/os/phone/hooks/getLocalStorage.ts new file mode 100644 index 0000000..84e2838 --- /dev/null +++ b/phone/src/os/phone/hooks/getLocalStorage.ts @@ -0,0 +1,12 @@ +export const getStorageItem = (key: string) => { + const UNIQ_PREFIX = 'npwd_phone_'; + try { + const data = window.localStorage.getItem(UNIQ_PREFIX + key); + // return stored or initValue if none stored + return JSON.parse(data); + } catch (e) { + // TODO: Integrate debug error state here + console.log(e); + // We still return initVal if error is thrown + } +}; diff --git a/phone/src/os/phone/hooks/settingState.ts b/phone/src/os/phone/hooks/settingState.ts new file mode 100644 index 0000000..a8edf73 --- /dev/null +++ b/phone/src/os/phone/hooks/settingState.ts @@ -0,0 +1,12 @@ +import { atom } from 'recoil'; + +export const phoneSettings = { + theme: atom({ + key: 'settingTheme', + default: 'default-dark', + }), + zoom: atom({ + key: 'settingZoom', + default: '100%', + }), +}; diff --git a/phone/src/os/phone/hooks/useLocalStorage.ts b/phone/src/os/phone/hooks/useLocalStorage.ts index fab9019..042acf3 100644 --- a/phone/src/os/phone/hooks/useLocalStorage.ts +++ b/phone/src/os/phone/hooks/useLocalStorage.ts @@ -1,5 +1,4 @@ import { useState } from 'react'; -import { useForceUpdate } from '../../../os/phone/hooks/forceUpdate'; // TODO: Integrate debug log to fetch when that branch is merged @@ -11,7 +10,6 @@ import { useForceUpdate } from '../../../os/phone/hooks/forceUpdate'; */ function useLocalStorage(key?: string, initialValue?: T) { - const { update, setUpdate } = useForceUpdate(); // Serves as a nice identifying prefix for the key const UNIQ_PREFIX = 'npwd_phone_'; const [localVal, setLocalVal] = useState(() => { @@ -27,31 +25,16 @@ function useLocalStorage(key?: string, initialValue?: T) { } }); - const getStorageItem = (localKey: string) => { - try { - const data = window.localStorage.getItem(UNIQ_PREFIX + localKey); - // return stored or initValue if none stored - return data ? JSON.parse(data) : initialValue; - } catch (e) { - // TODO: Integrate debug error state here - console.log(e); - // We still return initVal if error is thrown - return initialValue; - } - }; - // Trying to rep useState setter pretty close just with localStorage - const setVal = (localKey, value: T | ((val: T) => T)) => { - console.log(localKey, value); + const setVal = (value: T | ((val: T) => T)) => { try { - setUpdate(update + 1); // Same API as useState meaning we can accept a function for value const valForStore = value instanceof Function ? value(localVal) : value; // Save that state so we repli useState setLocalVal(valForStore); // Set that localStorage yo window.localStorage.setItem( - UNIQ_PREFIX + localKey, + UNIQ_PREFIX + key, JSON.stringify(valForStore) ); } catch (e) { @@ -60,7 +43,7 @@ function useLocalStorage(key?: string, initialValue?: T) { } }; - return [localVal, setVal, getStorageItem] as const; + return [localVal, setVal] as const; } export default useLocalStorage;