mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
fix(localStorage): solution to localStorage issue
Still need to find a good way to re-render
This commit is contained in:
1 parent
07dc73f0cb
commit
5fdb9ba21c
7 files changed
+51
-53
No files matched your search
+4
-6
@@ -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 { getStorageItem } from './os/phone/hooks/getLocalStorage';
|
import { getStorage } from './os/phone/hooks/useLocalStorage';
|
||||||
|
|
||||||
// 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') {
|
||||||
@@ -273,21 +273,19 @@ function Phone() {
|
|||||||
return (
|
return (
|
||||||
<ThemeProvider theme={currentTheme()}>
|
<ThemeProvider theme={currentTheme()}>
|
||||||
<div className='PhoneWrapper'>
|
<div className='PhoneWrapper'>
|
||||||
<div style={{ zoom: getStorageItem('zoom') }}>
|
<div style={{ zoom: getStorage('zoom') }}>
|
||||||
<div className='Phone'>
|
<div className='Phone'>
|
||||||
<div
|
<div
|
||||||
className='PhoneFrame'
|
className='PhoneFrame'
|
||||||
style={{
|
style={{
|
||||||
backgroundImage: `url(./media/frames/${getStorageItem(
|
backgroundImage: `url(./media/frames/${getStorage('frame')})`,
|
||||||
'frame'
|
|
||||||
)})`,
|
|
||||||
}}
|
}}
|
||||||
></div>
|
></div>
|
||||||
<div
|
<div
|
||||||
id='phone'
|
id='phone'
|
||||||
className='PhoneScreen'
|
className='PhoneScreen'
|
||||||
style={{
|
style={{
|
||||||
backgroundImage: `url(./media/backgrounds/${getStorageItem(
|
backgroundImage: `url(./media/backgrounds/${getStorage(
|
||||||
'wallpaper'
|
'wallpaper'
|
||||||
)})`,
|
)})`,
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -24,7 +24,10 @@ import {
|
|||||||
} from '@material-ui/icons';
|
} from '@material-ui/icons';
|
||||||
|
|
||||||
import { ListSubheader } from '@material-ui/core';
|
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 }) => (
|
const SubHeaderComp = (props: { text: string }) => (
|
||||||
<ListSubheader component='div' disableSticky>
|
<ListSubheader component='div' disableSticky>
|
||||||
@@ -35,33 +38,32 @@ const SubHeaderComp = (props: { text: string }) => (
|
|||||||
export const SettingsApp = () => {
|
export const SettingsApp = () => {
|
||||||
const settingsApp = useApp('SETTINGS');
|
const settingsApp = useApp('SETTINGS');
|
||||||
const [config] = useConfig();
|
const [config] = useConfig();
|
||||||
const [localVal, setVal, getStorageItem] = useLocalStorage();
|
|
||||||
const simcard = useSimcard();
|
const simcard = useSimcard();
|
||||||
|
|
||||||
const wallpapers = config.wallpapers.map(
|
const wallpapers = config.wallpapers.map(
|
||||||
MapStringOptions(getStorageItem('wallpaper'), (val: string) =>
|
MapStringOptions(getStorage('wallpaper'), (val: string) =>
|
||||||
setVal('wallpaper', val)
|
writeStorage('wallpaper', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
const frames = config.frames.map(
|
const frames = config.frames.map(
|
||||||
MapStringOptions(getStorageItem('frame'), (val: string) =>
|
MapStringOptions(getStorage('frame'), (val: string) =>
|
||||||
setVal('frame', val)
|
writeStorage('frame', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
const themes = Object.keys(config.themes).map(
|
const themes = Object.keys(config.themes).map(
|
||||||
MapStringOptions(getStorageItem('theme'), (val: string) =>
|
MapStringOptions(getStorage('theme'), (val: string) =>
|
||||||
setVal('theme', val)
|
writeStorage('theme', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
const zoomOptions = config.zoomOptions.map(
|
const zoomOptions = config.zoomOptions.map(
|
||||||
MapStringOptions(getStorageItem('zoom'), (val: string) =>
|
MapStringOptions(getStorage('zoom'), (val: string) =>
|
||||||
setVal('zoom', val)
|
writeStorage('zoom', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
// Doesn't actually do anything for the time being
|
// Doesn't actually do anything for the time being
|
||||||
const ringtones = config.ringtones.map(
|
const ringtones = config.ringtones.map(
|
||||||
MapStringOptions(getStorageItem('ringtone'), (val: string) =>
|
MapStringOptions(getStorage('ringtone'), (val: string) =>
|
||||||
setVal('ringtone', val)
|
writeStorage('ringtone', val)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
// * Probably gonna make this a slider component in the future
|
// * Probably gonna make this a slider component in the future
|
||||||
@@ -71,9 +73,7 @@ export const SettingsApp = () => {
|
|||||||
|
|
||||||
// TODO: These new settings all work
|
// TODO: These new settings all work
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {}, [getStorage]);
|
||||||
// this will be used to trigger a alert that gives feedback when the a setting is changed
|
|
||||||
}, [setVal]);
|
|
||||||
|
|
||||||
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu();
|
const [openMenu, closeMenu, ContextMenu, isMenuOpen] = useContextMenu();
|
||||||
return (
|
return (
|
||||||
@@ -88,7 +88,7 @@ export const SettingsApp = () => {
|
|||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Ringtone'
|
label='Ringtone'
|
||||||
value={getStorageItem('ringtone')}
|
value={getStorage('ringtone')}
|
||||||
options={ringtones}
|
options={ringtones}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<LibraryMusic />}
|
icon={<LibraryMusic />}
|
||||||
@@ -108,28 +108,28 @@ export const SettingsApp = () => {
|
|||||||
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
|
<List disablePadding subheader={<SubHeaderComp text='Appearance' />}>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Theme'
|
label='Theme'
|
||||||
value={getStorageItem('theme')}
|
value={getStorage('theme')}
|
||||||
options={themes}
|
options={themes}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<Brush />}
|
icon={<Brush />}
|
||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Wallpaper'
|
label='Wallpaper'
|
||||||
value={getStorageItem('wallpaper')}
|
value={getStorage('wallpaper')}
|
||||||
options={wallpapers}
|
options={wallpapers}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<Wallpaper />}
|
icon={<Wallpaper />}
|
||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Frame'
|
label='Frame'
|
||||||
value={getStorageItem('frame')}
|
value={getStorage('frame')}
|
||||||
options={frames}
|
options={frames}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<Smartphone />}
|
icon={<Smartphone />}
|
||||||
/>
|
/>
|
||||||
<SettingItem
|
<SettingItem
|
||||||
label='Zoom'
|
label='Zoom'
|
||||||
value={getStorageItem('zoom')}
|
value={getStorage('zoom')}
|
||||||
options={zoomOptions}
|
options={zoomOptions}
|
||||||
onClick={openMenu}
|
onClick={openMenu}
|
||||||
icon={<ZoomIn />}
|
icon={<ZoomIn />}
|
||||||
|
|||||||
@@ -2,8 +2,9 @@ import Default from '../default.json';
|
|||||||
import { useConfig } from '../../../config/hooks/useConfig';
|
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, {
|
||||||
import { getStorageItem } from '../../../os/phone/hooks/getLocalStorage';
|
getStorage,
|
||||||
|
} from '../../../os/phone/hooks/useLocalStorage';
|
||||||
|
|
||||||
const settingsState = atom({
|
const settingsState = atom({
|
||||||
key: 'settings',
|
key: 'settings',
|
||||||
@@ -11,9 +12,8 @@ const settingsState = atom({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const useSettings = (): any => {
|
export const useSettings = (): any => {
|
||||||
const theme = getStorageItem('theme');
|
const theme = getStorage('theme');
|
||||||
const [settings, setSettings] = useLocalStorage();
|
|
||||||
const [config] = useConfig();
|
const [config] = useConfig();
|
||||||
const currentTheme = () => createMuiTheme(config.themes[theme]);
|
const currentTheme = () => createMuiTheme(config.themes[theme]);
|
||||||
return { settings, setSettings, currentTheme };
|
return { currentTheme };
|
||||||
};
|
};
|
||||||
@@ -8,7 +8,6 @@ const configState = atom({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const useConfig = (state: any = configState): any => {
|
export const useConfig = (state: any = configState): any => {
|
||||||
const [, setVal] = useLocalStorage('');
|
|
||||||
const [config, _setConfig] = useRecoilState(state);
|
const [config, _setConfig] = useRecoilState(state);
|
||||||
|
|
||||||
const setConfig = (key, value) => {
|
const setConfig = (key, value) => {
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -9,7 +9,7 @@ import { useState } from 'react';
|
|||||||
* if there is no savedState
|
* 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
|
// 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>(() => {
|
||||||
@@ -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;
|
export default useLocalStorage;
|
||||||
Reference in new issue
Block a user