Skip to content

Commit

Permalink
fix(setting): Ensure default settings are loaded in case of invalid v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
tcrammond authored and shantanuraj committed Mar 22, 2019
1 parent 9173262 commit 77dbb05
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/scripts/lib/bugsnag.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getBugsnagClient () {
collectUserIp: false,
beforeSend: async function (report) {
const db = browser.extension.getBackgroundPage().db;
const sendErrorReports = await db.get('sendErrorReports', true);
const sendErrorReports = await db.get('sendErrorReports');
if (!sendErrorReports) {
report.ignore();
return false;
Expand Down
55 changes: 29 additions & 26 deletions src/scripts/lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@ const DEFAULT_SETTINGS = {
dayEndTime: '17:00',
defaultProject: 0,
rememberProjectPer: 'false',
enableAutoTagging: false
};
enableAutoTagging: false,

// core settings: key, default value
const CORE_SETTINGS = {
'dont-show-permissions': false,
'show-permissions-info': 0,
'settings-active-tab': 0,
sendErrorReports: true,
sendUsageStatistics: true
};

const transformLegacyValue = (value) => {
if (typeof value !== 'undefined') {
// Ensure older version's settings still function if they get saved to sync storage.
Expand Down Expand Up @@ -153,14 +149,27 @@ export default class Db {
this.set(this.togglButton.$user.id + '-defaultProjects', {});
}

get (key, defaultValue) {
get (key) {
const defaultValue = DEFAULT_SETTINGS[key];
const hasDefaultValue = typeof defaultValue !== 'undefined';
return browser.storage.sync.get(hasDefaultValue ? { [key]: defaultValue } : key)
const options = hasDefaultValue
? { [key]: defaultValue }
: key;

return browser.storage.sync.get(options)
.then((result) => {
let value = result[key];
if (hasDefaultValue && typeof value !== typeof defaultValue) {
if (process.env.DEBUG) {
console.info(`Retrieved setting [${key}] is incorrect type`, value);
}
value = defaultValue;
}

if (process.env.DEBUG) {
console.info(`Retrieved value ${key}: `, result[key]);
console.info(`Retrieved value ${key}: `, value);
}
return transformLegacyValue(result[key]);
return transformLegacyValue(value);
});
}

Expand All @@ -173,7 +182,7 @@ export default class Db {
return browser.storage.sync.get(settings)
.then((result) => {
if (process.env.DEBUG) {
console.info(`Retrieved values ${Object.keys(settings).join(', ')}: `, Object.values(result).map(JSON.strinfiy).join(', '));
console.info(`Retrieved values ${Object.keys(settings).join(', ')}: `, Object.values(result).map(JSON.stringify).join(', '));
}
return Object.keys(result).reduce((results, key) => {
return Object.assign(results, {
Expand Down Expand Up @@ -220,14 +229,14 @@ export default class Db {
return collection;
}

async load (setting, defaultValue) {
let value = await this.get(setting);
if (typeof value === 'undefined') {
value = defaultValue;
}

this.set(setting, value);
return value;
load (setting, defaultValue) {
this.get(setting)
.then((value) => {
if (typeof value === 'undefined') {
value = defaultValue;
}
this.set(setting, value);
});
}

loadAll () {
Expand All @@ -236,12 +245,6 @@ export default class Db {
this.load(k, DEFAULT_SETTINGS[k]);
}
}

for (const k in CORE_SETTINGS) {
if (CORE_SETTINGS.hasOwnProperty(k)) {
this.load(k, CORE_SETTINGS[k]);
}
}
}

updateSetting (key, state, callback, condition) {
Expand All @@ -254,7 +257,7 @@ export default class Db {
}

resetAllSettings () {
const allSettings = { ...DEFAULT_SETTINGS, ...CORE_SETTINGS };
const allSettings = { ...DEFAULT_SETTINGS };
return this.setMultiple(allSettings)
.then(() => {
bugsnagClient.leaveBreadcrumb('Completed reset all settings');
Expand All @@ -270,7 +273,7 @@ export default class Db {
bugsnagClient.leaveBreadcrumb('Attempting settings migration to v2');

try {
const allSettings = { ...DEFAULT_SETTINGS, ...CORE_SETTINGS };
const allSettings = { ...DEFAULT_SETTINGS };
const oldSettings = Object.keys(allSettings)
.reduce((accumulator, key) => {
const defaultValue = allSettings[key];
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ document.addEventListener('DOMContentLoaded', async function (e) {
if (
!dontShowPermissions
) {
const showPermissionsInfo = await db.get('show-permissions-info', 0);
const showPermissionsInfo = await db.get('show-permissions-info');
document.querySelector('.guide-container').style.display = 'flex';
document.querySelector(
".guide > div[data-id='" + (showPermissionsInfo || 0) + "']"
Expand All @@ -644,7 +644,7 @@ document.addEventListener('DOMContentLoaded', async function (e) {
}

// Change active tab.
const settingsActiveTab = await db.get('settings-active-tab');
const settingsActiveTab = await db.get('settings-active-tab', 0);
const activeTab = Number.parseInt(settingsActiveTab, 10);
changeActiveTab(activeTab);
document.querySelector('body').style.display = 'block';
Expand Down

0 comments on commit 77dbb05

Please sign in to comment.