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 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();
+3 -3
View File
@@ -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 };
};
+7 -4
View File
@@ -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];
@@ -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 { 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<T>(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<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
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<T>(key?: string, initialValue?: T) {
}
};
return [localVal, setVal, getStorageItem] as const;
return [localVal, setVal] as const;
}
export default useLocalStorage;