feat(phone/settings): Schema validation & Reset Settings

You can now reset your settings to default using the action in the Settings App

Setting schema validation has been added that will temporarily reset to default settings if
the JSON string contains unexpected values. A alert will pop up informing the user
that they should reset to default settings using the action in the settings app,
as a permanent solution.
This commit is contained in:
Taso committed 2021-03-20 12:57:22 -04:00
1 parent 97c308f550
commit 2a4457b323
6 files changed
+157 -29

No files matched your search

+90 -20
View File
@@ -1,29 +1,64 @@
import { atom, DefaultValue, SetterOrUpdater, useRecoilState } from 'recoil';
import { atom, DefaultValue, useRecoilState, useResetRecoilState } from 'recoil';
import { SETTINGS_ALL_TWEETS, SETTING_MENTIONS } from '../../../../../typings/twitter';
import config from '../../../config/default.json';
import { SettingOption } from '../../../ui/hooks/useContextMenu';
import { Schema, Validator } from 'jsonschema';
const localStorageEffect = (key) => ({ setSelf, onSet }) => {
const savedVal = localStorage.getItem(key);
if (savedVal != null) {
setSelf(JSON.parse(savedVal));
}
const NPWD_STORAGE_KEY = 'npwd_settings';
onSet((newValue) => {
if (newValue instanceof DefaultValue) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, JSON.stringify(newValue));
}
});
const v = new Validator();
const settingOptionSchema: Schema = {
id: '/SettingOption',
type: 'object',
properties: {
label: { type: 'string' },
val: { type: 'string' },
},
required: true,
};
export const settingsState = atom({
key: 'settings',
default: config.defaultSettings,
effects_UNSTABLE: [localStorageEffect('npwd_settings')],
});
const settingsSchema: Schema = {
type: 'object',
properties: {
language: { $ref: '/SettingOption' },
wallpaper: { $ref: '/SettingOption' },
frame: { $ref: '/SettingOption' },
theme: { $ref: '/SettingOption' },
zoom: { $ref: '/SettingOption' },
streamerMode: { type: 'boolean' },
ringtone: { $ref: '/SettingOption' },
ringtoneVol: { type: 'number' },
notiSound: { $ref: '/SettingOption' },
notiSoundVol: { type: 'number' },
TWITTER_notiFilter: { $ref: '/SettingOption' },
TWITTER_notiSound: { $ref: '/SettingOption' },
TWITTER_notiSoundVol: { type: 'number' },
TWITTER_notifyNewFeedTweet: { type: 'boolean' },
},
required: true,
};
v.addSchema(settingOptionSchema, '/SettingOption');
function isSchemaValid(schema: string): boolean {
const storedSettings = JSON.parse(schema);
const resp = v.validate(storedSettings, settingsSchema);
return resp.valid;
}
export function isSettingsSchemaValid(): boolean {
const localStore = localStorage.getItem(NPWD_STORAGE_KEY);
if (localStore) {
try {
const parsedSettings = JSON.parse(localStore);
return v.validate(parsedSettings, settingsSchema).valid;
} catch (e) {
console.error('Unable to parse settings JSON', e);
}
}
return true;
}
export interface IPhoneSettings {
language: SettingOption;
@@ -42,6 +77,41 @@ export interface IPhoneSettings {
TWITTER_notifyNewFeedTweet: boolean;
}
export const useSettings = (): [IPhoneSettings, SetterOrUpdater<any>] => {
const localStorageEffect = (key) => ({ setSelf, onSet }) => {
const savedVal = localStorage.getItem(key);
if (savedVal) {
try {
const validString = !isSchemaValid(savedVal);
if (validString) {
console.error('Settings Schema was invalid, applying default settings');
setSelf(config.defaultSettings);
}
} catch (e) {
// If we are unable to parse the json string, we set default settings
console.error('Unable to parse JSON');
setSelf(config.defaultSettings);
}
}
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_STORAGE_KEY)],
});
export const useSettings = () => {
return useRecoilState(settingsState);
};
export const useResetSettings = () => {
return useResetRecoilState(settingsState);
};