From 6419245d935ff869ce6d67759a5263fca96eac0c Mon Sep 17 00:00:00 2001 From: Taso Date: Fri, 5 Mar 2021 12:38:22 -0500 Subject: [PATCH] Revert "feat(phone/settings): Add localStorage settings schema validation (#139)" This reverts commit cb5a00e57e82c37cd0f693b6ab50b91bba5b9dc0. Reverted due a non-descript bug in CEF. Will be reapplied after fix. --- phone/src/Phone.tsx | 2 +- .../apps/settings/components/SettingsApp.tsx | 2 +- .../hooks/__tests__/useSettings.test.ts | 54 ------------ phone/src/apps/settings/hooks/useSettings.ts | 86 +++++-------------- .../providers/NotificationsProvider.tsx | 15 ++-- phone/src/os/phone/hooks/usePhoneTheme.ts | 2 +- .../src/os/phone/hooks/usePhoneVisibility.ts | 4 +- phone/src/os/sound/hooks/useRingtoneSound.ts | 2 +- phone/src/os/sound/utils/getSoundSettings.ts | 4 +- phone/src/ui/hooks/useContextMenu.tsx | 8 +- 10 files changed, 43 insertions(+), 136 deletions(-) delete mode 100644 phone/src/apps/settings/hooks/__tests__/useSettings.test.ts diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index e18ba9b..256f612 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -32,7 +32,7 @@ import { useCallModal } from './os/call/hooks/useCallModal'; function Phone() { const { apps } = useApps(); - const { settings } = useSettings(); + const [settings] = useSettings(); useNuiService(); useKeyboardService(); diff --git a/phone/src/apps/settings/components/SettingsApp.tsx b/phone/src/apps/settings/components/SettingsApp.tsx index 9cc6a80..ff5b48d 100644 --- a/phone/src/apps/settings/components/SettingsApp.tsx +++ b/phone/src/apps/settings/components/SettingsApp.tsx @@ -35,7 +35,7 @@ export const SettingsApp = () => { const settingsApp = useApp('SETTINGS'); const [config] = useConfig(); const simcard = useSimcard(); - const { settings, setSettings } = useSettings(); + const [settings, setSettings] = useSettings(); const { t } = useTranslation(); // TODO: Uncomment when #135 is merged // const { addAlert } = useSnackbar(); diff --git a/phone/src/apps/settings/hooks/__tests__/useSettings.test.ts b/phone/src/apps/settings/hooks/__tests__/useSettings.test.ts deleted file mode 100644 index ba0b136..0000000 --- a/phone/src/apps/settings/hooks/__tests__/useSettings.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import config from '../../../../config/default.json'; -import { PhoneSettings } from '../useSettings'; - -describe('useSettings', () => { - it('Creates settings and returns every needed key', () => { - const invalid = {}; - const validated = new PhoneSettings(invalid, config.defaultSettings); - for (const key of Object.keys(config.defaultSettings)) { - expect(validated[key]).toBe(config.defaultSettings[key]); - } - }); - - it('Creates settings and adds missing key', () => { - const invalid = {}; - const validated = new PhoneSettings(invalid, { ...config.defaultSettings, ringtoneVol: 2 }); - expect(validated.ringtoneVol).toBe(2); - }); - - it('Creates settings and leave valid value as is', () => { - const valid = { ringtoneVol: 0 }; - const validated = new PhoneSettings(valid, { ...config.defaultSettings, ringtoneVol: 50 }); - expect(validated.ringtoneVol).toBe(0); - }); - - it('Creates settings and adds missing key for SettingOption', () => { - const invalid = {}; - const validated = new PhoneSettings(invalid, { - ...config.defaultSettings, - ringtone: { label: 'Bye', value: 'bye' }, - }); - expect(validated.ringtone.label).toBe('Bye'); - expect(validated.ringtone.value).toBe('bye'); - }); - - it('Creates settings and leave valid value as is for SettingOption', () => { - const valid = { ringtone: { label: 'Test', value: 'test' } }; - const validated = new PhoneSettings(valid, { - ...config.defaultSettings, - ringtone: { label: 'Bye', value: 'bye' }, - }); - expect(validated.ringtone.label).toBe('Test'); - expect(validated.ringtone.value).toBe('test'); - }); - - it('Creates settings and add default value when value typeof is not correct', () => { - const invalid = { ringtone: 'test' }; - const validated = new PhoneSettings(invalid as any, { - ...config.defaultSettings, - ringtone: { label: 'Bye', value: 'bye' }, - }); - expect(validated.ringtone.label).toBe('Bye'); - expect(validated.ringtone.value).toBe('bye'); - }); -}); diff --git a/phone/src/apps/settings/hooks/useSettings.ts b/phone/src/apps/settings/hooks/useSettings.ts index d148d26..4ddc8d7 100644 --- a/phone/src/apps/settings/hooks/useSettings.ts +++ b/phone/src/apps/settings/hooks/useSettings.ts @@ -2,6 +2,27 @@ import { atom, DefaultValue, useRecoilState } from 'recoil'; import config from '../../../config/default.json'; import { SettingOption } from '../../../ui/hooks/useContextMenu'; +const localStorageEffect = (key) => ({ setSelf, onSet }) => { + const savedVal = localStorage.getItem(key); + if (savedVal != null) { + setSelf(JSON.parse(savedVal)); + } + + onSet((newValue) => { + if (newValue instanceof DefaultValue) { + localStorage.removeItem(key); + } else { + localStorage.setItem(key, JSON.stringify(newValue)); + } + }); +}; + +export const settingsState = atom({ + key: 'settings', + default: config.defaultSettings, + effects_UNSTABLE: [localStorageEffect('npwd_settings')], +}); + export interface IPhoneSettings { wallpaper: SettingOption; frame: SettingOption; @@ -16,69 +37,6 @@ export interface IPhoneSettings { TWITTER_notifyNewFeedTweet: boolean; } -export class PhoneSettings implements IPhoneSettings { - wallpaper: SettingOption; - frame: SettingOption; - theme: SettingOption; - zoom: SettingOption; - ringtone: SettingOption; - ringtoneVol: number; - notiSound: SettingOption; - notiSoundVol: number; - TWITTER_notiSound: SettingOption; - TWITTER_notiSoundVol: number; - TWITTER_notifyNewFeedTweet: boolean; - - constructor(value: Partial, defaultSettings: IPhoneSettings) { - return this.validate(value, defaultSettings); - } - - validate(value, defaultSettings) { - Object.keys(this).map((key) => { - if (value[key] === undefined || typeof value[key] !== typeof defaultSettings[key]) { - this[key] = defaultSettings[key]; - return key; - } - this[key] = value[key]; - return key; - }); - return this; - } -} - -export const localStorageEffect = (key) => ({ setSelf, onSet }) => { - const storedSettings = localStorage.getItem(key); - if (storedSettings) { - try { - const validated = new PhoneSettings(JSON.parse(storedSettings), config.defaultSettings); - setSelf(validated); - } catch (e) { - console.error('Local storage parse error, restoring to defaults', e.message); - setSelf(config.defaultSettings); - } - } - - onSet((settingValue) => { - if (settingValue instanceof DefaultValue) { - localStorage.removeItem(key); - } else { - localStorage.setItem(key, JSON.stringify(settingValue)); - } - }); -}; - -export const settingsState = atom({ - key: 'settings', - default: config.defaultSettings, - effects_UNSTABLE: [localStorageEffect('npwd_settings')], -}); - export const useSettings = () => { - const [settings, setSettings] = useRecoilState(settingsState); - - const resetSettings = () => { - setSettings(config.defaultSettings); - }; - - return { settings, setSettings, resetSettings }; + return useRecoilState(settingsState); }; diff --git a/phone/src/os/notifications/providers/NotificationsProvider.tsx b/phone/src/os/notifications/providers/NotificationsProvider.tsx index 7b9de57..e6791f3 100644 --- a/phone/src/os/notifications/providers/NotificationsProvider.tsx +++ b/phone/src/os/notifications/providers/NotificationsProvider.tsx @@ -52,7 +52,7 @@ export const NotificationsContext = createContext<{ export function NotificationsProvider({ children }) { const isPhoneOpen = useRecoilValue(phoneState.visibility); - const { settings } = useSettings(); + const [settings] = useSettings(); const [barUncollapsed, setBarUncollapsed] = useState(false); @@ -110,7 +110,7 @@ export function NotificationsProvider({ children }) { (id: string): INotification | null => { return notifications.find((n) => n.id === id) || null; }, - [notifications], + [notifications] ); const _showAlert = useCallback( @@ -149,13 +149,18 @@ export function NotificationsProvider({ children }) { }, DEFAULT_ALERT_HIDE_TIME + 300); }); }, - [play], + [play] ); - const addNotificationAlert = (n: INotification, cb: (n: INotification) => void) => { + const addNotificationAlert = ( + n: INotification, + cb: (n: INotification) => void + ) => { if (n.sound) { const { sound, volume } = getSoundSettings('notiSound', settings, n.app); - mount(sound, volume, false).then(({ url }) => setAlerts((curr) => [...curr, [n, cb, url]])); + mount(sound, volume, false).then(({ url }) => + setAlerts((curr) => [...curr, [n, cb, url]]) + ); return; } setAlerts((curr) => [...curr, [n, cb, undefined]]); diff --git a/phone/src/os/phone/hooks/usePhoneTheme.ts b/phone/src/os/phone/hooks/usePhoneTheme.ts index 55bae97..a14d775 100644 --- a/phone/src/os/phone/hooks/usePhoneTheme.ts +++ b/phone/src/os/phone/hooks/usePhoneTheme.ts @@ -4,6 +4,6 @@ import { useSettings } from '../../../apps/settings/hooks/useSettings'; import themes from '../../../config/themes.json'; export const usePhoneTheme = () => { - const { settings } = useSettings(); + const [settings] = useSettings(); return useMemo(() => createMuiTheme(themes[settings.theme.value]), [settings.theme]); }; diff --git a/phone/src/os/phone/hooks/usePhoneVisibility.ts b/phone/src/os/phone/hooks/usePhoneVisibility.ts index cb59401..d35b71e 100644 --- a/phone/src/os/phone/hooks/usePhoneVisibility.ts +++ b/phone/src/os/phone/hooks/usePhoneVisibility.ts @@ -8,9 +8,7 @@ import { phoneState } from './state'; export const usePhoneVisibility = () => { const [visibility, setVisibility] = useRecoilState(phoneState.visibility); const { currentAlert } = useNotifications(); - const { - settings: { zoom }, - } = useSettings(); + const [{ zoom }] = useSettings(); const [notifVisibility, setNotifVisibility] = useState(false); diff --git a/phone/src/os/sound/hooks/useRingtoneSound.ts b/phone/src/os/sound/hooks/useRingtoneSound.ts index 63baa2f..2524a01 100644 --- a/phone/src/os/sound/hooks/useRingtoneSound.ts +++ b/phone/src/os/sound/hooks/useRingtoneSound.ts @@ -4,7 +4,7 @@ import { getSoundSettings } from '../utils/getSoundSettings'; import { useSoundProvider } from './useSoundProvider'; export const useRingtoneSound = () => { - const { settings } = useSettings(); + const [settings] = useSettings(); const sound = useSoundProvider(); diff --git a/phone/src/os/sound/utils/getSoundSettings.ts b/phone/src/os/sound/utils/getSoundSettings.ts index fed85c7..824016d 100644 --- a/phone/src/os/sound/utils/getSoundSettings.ts +++ b/phone/src/os/sound/utils/getSoundSettings.ts @@ -17,11 +17,11 @@ export const getSoundSettings = ( try { return app ? { - sound: getPath[type](settings[`${app}_${type}`]?.value || settings[type]?.value), + sound: getPath[type](settings[`${app}_${type}`].value || settings[type].value), volume: (settings[`${app}_${type}Vol`] || settings[`${type}Vol`]) / 100, } : { - sound: getPath[type](settings[type as string]?.value), + sound: getPath[type](settings[type as string].value), volume: settings[`${type}Vol`] / 100, }; } catch (e) { diff --git a/phone/src/ui/hooks/useContextMenu.tsx b/phone/src/ui/hooks/useContextMenu.tsx index 567e82c..2a20649 100644 --- a/phone/src/ui/hooks/useContextMenu.tsx +++ b/phone/src/ui/hooks/useContextMenu.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useState } from 'react'; +import React, { useState } from 'react'; import { ContextMenu, IContextMenuOption } from '../components/ContextMenu'; type UseContextMenu = [any, any, () => JSX.Element, boolean]; @@ -30,12 +30,12 @@ export const useContextMenu = (_options?: IContextMenuOption[]): UseContextMenu const [open, setOpen] = useState(false); const [options, setOptions] = useState(_options || []); - const onClose = useCallback(() => setOpen(false), []); + const onClose = () => setOpen(false); - const onOpen = useCallback((opts) => { + const onOpen = (opts) => { setOptions(opts); setOpen(true); - }, []); + }; return [ onOpen,