fix(localStorage): solution to localStorage issue

Still need to find a good way to re-render
This commit is contained in:
Christopher committed 2021-01-08 21:48:24 +01:00
1 parent 07dc73f0cb
commit 5fdb9ba21c
7 files changed
+51 -53

No files matched your search

+4 -6
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 { getStorageItem } from './os/phone/hooks/getLocalStorage';
import { getStorage } from './os/phone/hooks/useLocalStorage';
// Inject mock data when in development env.
if (process.env.NODE_ENV === 'development') {
@@ -273,21 +273,19 @@ function Phone() {
return (
<ThemeProvider theme={currentTheme()}>
<div className='PhoneWrapper'>
<div style={{ zoom: getStorageItem('zoom') }}>
<div style={{ zoom: getStorage('zoom') }}>
<div className='Phone'>
<div
className='PhoneFrame'
style={{
backgroundImage: `url(./media/frames/${getStorageItem(
'frame'
)})`,
backgroundImage: `url(./media/frames/${getStorage('frame')})`,
}}
></div>
<div
id='phone'
className='PhoneScreen'
style={{
backgroundImage: `url(./media/backgrounds/${getStorageItem(
backgroundImage: `url(./media/backgrounds/${getStorage(
'wallpaper'
)})`,
}}
@@ -24,7 +24,10 @@ import {
} from '@material-ui/icons';
import { ListSubheader } from '@material-ui/core';
import useLocalStorage from '../../../os/phone/hooks/useLocalStorage';
import useLocalStorage, {
writeStorage,
getStorage,
} from '../../../os/phone/hooks/useLocalStorage';
const SubHeaderComp = (props: { text: string }) => (
<ListSubheader component='div' disableSticky>
@@ -35,33 +38,32 @@ const SubHeaderComp = (props: { text: string }) => (
export const SettingsApp = () => {
const settingsApp = useApp('SETTINGS');
const [config] = useConfig();
const [localVal, setVal, getStorageItem] = useLocalStorage();
const simcard = useSimcard();
const wallpapers = config.wallpapers.map(
MapStringOptions(getStorageItem('wallpaper'), (val: string) =>
setVal('wallpaper', val)
MapStringOptions(getStorage('wallpaper'), (val: string) =>
writeStorage('wallpaper', val)
)
);
const frames = config.frames.map(
MapStringOptions(getStorageItem('frame'), (val: string) =>
setVal('frame', val)
MapStringOptions(getStorage('frame'), (val: string) =>
writeStorage('frame', val)
)
);
const themes = Object.keys(config.themes).map(
MapStringOptions(getStorageItem('theme'), (val: string) =>
setVal('theme', val)
MapStringOptions(getStorage('theme'), (val: string) =>
writeStorage('theme', val)
)
);
const zoomOptions = config.zoomOptions.map(
MapStringOptions(getStorageItem('zoom'), (val: string) =>
setVal('zoom', val)
MapStringOptions(getStorage('zoom'), (val: string) =>
writeStorage('zoom', val)
)
);
// Doesn't actually do anything for the time being
const ringtones = config.ringtones.map(
MapStringOptions(getStorageItem('ringtone'), (val: string) =>
setVal('ringtone', val)
MapStringOptions(getStorage('ringtone'), (val: string) =>
writeStorage('ringtone', val)
)
);
// * Probably gonna make this a slider component in the future
@@ -71,9 +73,7 @@ export const SettingsApp = () => {
// TODO: These new settings all work
useEffect(() => {
// this will be used to trigger a alert that gives feedback when the a setting is changed
}, [setVal]);
useEffect(() => {}, [getStorage]);
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu();
return (
@@ -88,7 +88,7 @@ export const SettingsApp = () => {
/>
<SettingItem
label='Ringtone'
value={getStorageItem('ringtone')}
value={getStorage('ringtone')}
options={ringtones}
onClick={openMenu}
icon={<LibraryMusic />}
@@ -108,28 +108,28 @@ export const SettingsApp = () => {
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
<SettingItem
label='Theme'
value={getStorageItem('theme')}
value={getStorage('theme')}
options={themes}
onClick={openMenu}
icon={<Brush />}
/>
<SettingItem
label='Wallpaper'
value={getStorageItem('wallpaper')}
value={getStorage('wallpaper')}
options={wallpapers}
onClick={openMenu}
icon={<Wallpaper />}
/>
<SettingItem
label='Frame'
value={getStorageItem('frame')}
value={getStorage('frame')}
options={frames}
onClick={openMenu}
icon={<Smartphone />}
/>
<SettingItem
label='Zoom'
value={getStorageItem('zoom')}
value={getStorage('zoom')}
options={zoomOptions}
onClick={openMenu}
icon={<ZoomIn />}
+5 -5
View File
@@ -2,8 +2,9 @@ import Default from '../default.json';
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';
import useLocalStorage, {
getStorage,
} from '../../../os/phone/hooks/useLocalStorage';
const settingsState = atom({
key: 'settings',
@@ -11,9 +12,8 @@ const settingsState = atom({
});
export const useSettings = (): any => {
const theme = getStorageItem('theme');
const [settings, setSettings] = useLocalStorage();
const theme = getStorage('theme');
const [config] = useConfig();
const currentTheme = () => createMuiTheme(config.themes[theme]);
return { settings, setSettings, currentTheme };
return { currentTheme };
};
-1
View File
@@ -8,7 +8,6 @@ const configState = atom({
});
export const useConfig = (state: any = configState): any => {
const [, setVal] = useLocalStorage('');
const [config, _setConfig] = useRecoilState(state);
const setConfig = (key, value) => {
-7
View File
@@ -1,7 +0,0 @@
import { useRecoilState } from 'recoil';
import { phoneState } from './state';
export const useForceUpdate = () => {
const [update, setUpdate] = useRecoilState(phoneState.forceUpdateState);
return { update, setUpdate };
};
@@ -1,12 +0,0 @@
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
}
};
+22 -2
View File
@@ -9,7 +9,7 @@ import { useState } from 'react';
* if there is no savedState
*/
function useLocalStorage<T>(key?: string, initialValue?: T) {
function useLocalStorage<T>(key: string, initialValue: T) {
// Serves as a nice identifying prefix for the key
const UNIQ_PREFIX = 'npwd_phone_';
const [localVal, setLocalVal] = useState<T>(() => {
@@ -43,7 +43,27 @@ function useLocalStorage<T>(key?: string, initialValue?: T) {
}
};
return [localVal, setVal] as const;
return [localVal, setVal];
}
export const writeStorage = <T>(key: string, value: T) => {
const UNIQ_PREFIX = 'npwd_phone_';
try {
window.localStorage.setItem(UNIQ_PREFIX + key, JSON.stringify(value));
} catch (error) {
console.log(error);
}
};
export const getStorage = (key: string) => {
const UNIQ_PREFIX = 'npwd_phone_';
try {
const data = window.localStorage.getItem(UNIQ_PREFIX + key);
return JSON.parse(data);
} catch (error) {
console.log(error);
}
};
export default useLocalStorage;