minor changes in localStorage - nothing to see here

This commit is contained in:
Christopher committed 2021-01-07 21:17:44 +01:00
1 parent dddddc01f8
commit 07dc73f0cb
6 files changed
+38 -29

No files matched your search

+1 -2
View File
@@ -25,7 +25,7 @@ import { useNotesService } from './apps/notes/hooks/useNotesService';
import { usePhotoService } from './apps/camera/hooks/usePhotoService'; import { usePhotoService } from './apps/camera/hooks/usePhotoService';
import Nui from './os/nui-events/utils/Nui'; import Nui from './os/nui-events/utils/Nui';
import { usePhone } from './os/phone/hooks/usePhone'; 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. // Inject mock data when in development env.
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
@@ -246,7 +246,6 @@ function Phone() {
useNuiService(); useNuiService();
usePhoneService(); usePhoneService();
const { visibility } = usePhone(); const { visibility } = usePhone();
const [, , getStorageItem] = useLocalStorage();
const { settings, currentTheme } = useSettings(); const { settings, currentTheme } = useSettings();
const { allApps } = useApps(); const { allApps } = useApps();
useSimcardService(); useSimcardService();
+3 -3
View File
@@ -3,6 +3,7 @@ import { useConfig } from '../../../config/hooks/useConfig';
import { atom } from 'recoil'; import { atom } from 'recoil';
import { createMuiTheme } from '@material-ui/core'; import { createMuiTheme } from '@material-ui/core';
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage'; import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
import { getStorageItem } from '../../../os/phone/hooks/getLocalStorage';
const settingsState = atom({ const settingsState = atom({
key: 'settings', key: 'settings',
@@ -10,10 +11,9 @@ const settingsState = atom({
}); });
export const useSettings = (): any => { export const useSettings = (): any => {
const [, , getStorageItem] = useLocalStorage('theme', 'taso-dark'); const theme = getStorageItem('theme');
const [settings, setSettings] = useLocalStorage(); const [settings, setSettings] = useLocalStorage();
const [config] = useConfig(); const [config] = useConfig();
const currentTheme = () => const currentTheme = () => createMuiTheme(config.themes[theme]);
createMuiTheme(config.themes[getStorageItem('theme')]);
return { settings, setSettings, currentTheme }; return { settings, setSettings, currentTheme };
}; };
+7 -4
View File
@@ -8,12 +8,15 @@ const configState = atom({
}); });
export const useConfig = (state: any = configState): any => { export const useConfig = (state: any = configState): any => {
const [, setVal] = useLocalStorage(); const [, setVal] = useLocalStorage('');
const [config] = useRecoilState(state); const [config, _setConfig] = useRecoilState(state);
const setConfig = (key, value) => { const setConfig = (key, value) => {
console.log(`setConfig: ${key} ${value}`); _setConfig((oldConfig) => ({
setVal(key, value); ...oldConfig,
[key]: value,
}));
//setVal(key, value);
}; };
return [config, setConfig]; return [config, setConfig];
@@ -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
}
};
+12
View File
@@ -0,0 +1,12 @@
import { atom } from 'recoil';
export const phoneSettings = {
theme: atom({
key: 'settingTheme',
default: 'default-dark',
}),
zoom: atom({
key: 'settingZoom',
default: '100%',
}),
};
+3 -20
View File
@@ -1,5 +1,4 @@
import { useState } from 'react'; import { useState } from 'react';
import { useForceUpdate } from '../../../os/phone/hooks/forceUpdate';
// TODO: Integrate debug log to fetch when that branch is merged // TODO: Integrate debug log to fetch when that branch is merged
@@ -11,7 +10,6 @@ import { useForceUpdate } from '../../../os/phone/hooks/forceUpdate';
*/ */
function useLocalStorage<T>(key?: string, initialValue?: T) { function useLocalStorage<T>(key?: string, initialValue?: T) {
const { update, setUpdate } = useForceUpdate();
// Serves as a nice identifying prefix for the key // Serves as a nice identifying prefix for the key
const UNIQ_PREFIX = 'npwd_phone_'; const UNIQ_PREFIX = 'npwd_phone_';
const [localVal, setLocalVal] = useState<T>(() => { const [localVal, setLocalVal] = useState<T>(() => {
@@ -27,31 +25,16 @@ function useLocalStorage<T>(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 // Trying to rep useState setter pretty close just with localStorage
const setVal = (localKey, value: T | ((val: T) => T)) => { const setVal = (value: T | ((val: T) => T)) => {
console.log(localKey, value);
try { try {
setUpdate(update + 1);
// Same API as useState meaning we can accept a function for value // Same API as useState meaning we can accept a function for value
const valForStore = value instanceof Function ? value(localVal) : value; const valForStore = value instanceof Function ? value(localVal) : value;
// Save that state so we repli useState // Save that state so we repli useState
setLocalVal(valForStore); setLocalVal(valForStore);
// Set that localStorage yo // Set that localStorage yo
window.localStorage.setItem( window.localStorage.setItem(
UNIQ_PREFIX + localKey, UNIQ_PREFIX + key,
JSON.stringify(valForStore) JSON.stringify(valForStore)
); );
} catch (e) { } catch (e) {
@@ -60,7 +43,7 @@ function useLocalStorage<T>(key?: string, initialValue?: T) {
} }
}; };
return [localVal, setVal, getStorageItem] as const; return [localVal, setVal] as const;
} }
export default useLocalStorage; export default useLocalStorage;