Skip to content

Commit

Permalink
feat #78: perform migration if old keys exist
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijithvijayan committed Mar 9, 2020
1 parent 9f9ab82 commit 8a6d5ff
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';

import PopupBody, { ProcessedRequestProperties } from './PopupBody';
import { Kutt, UserSettingsResponseProperties } from '../Background';
import { getExtensionSettings } from '../util/settings';
import { getExtensionSettings, migrateSettings, getPreviousSettings } from '../util/settings';
import BodyWrapper from '../components/BodyWrapper';
import Loader from '../components/Loader';
import PopupForm from './PopupForm';
Expand Down Expand Up @@ -51,6 +51,53 @@ const Popup: React.FC = () => {
// re-renders on `pageReloadFlag` change
useEffect((): void => {
async function getUserSettings(): Promise<void> {
// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//
// ----- // ToDo: remove in next major release // ----- //
// ----- Ref: https://github.com/abhijithvijayan/kutt-extension/issues/78 ----- //
// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//

const {
// old keys from extension v3.x.x
key = '',
host = '',
userOptions = { autoCopy: false, devMode: false, keepHistory: false, pwdForUrls: false },
} = await getPreviousSettings();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const migrationSettings: any = {};
let performMigration = false;

if (key.trim().length > 0) {
// map it to `settings.apikey`
migrationSettings.apikey = key;
performMigration = true;
}
if (host.trim().length > 0 && userOptions.devMode) {
// map `host` to `settings.customhost`
migrationSettings.customhost = host;
// set `advanced` to true
migrationSettings.advanced = true;
performMigration = true;
}
if (userOptions.keepHistory) {
// set `settings.history` to true
migrationSettings.history = true;
performMigration = true;
}
if (performMigration) {
// perform migration
await migrateSettings(migrationSettings);
}

// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//
// -----------------------------------------------------------------------------//

// ToDo: set types: refer https://kutt.it/jITyIU
const { settings = {} } = await getExtensionSettings();

Expand Down Expand Up @@ -129,7 +176,6 @@ const Popup: React.FC = () => {
setUserConfig({ apikey: settings.apikey, domainOptions: defaultOptions, host: defaultHost });
}

// ToDo: handle init operations(if any)
setLoading(false);
}

Expand Down
16 changes: 16 additions & 0 deletions src/util/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ export async function updateExtensionSettings(newFields?: { [s: string]: any }):

return saveExtensionSettings({ ...settings, ...newFields });
}

// ToDo: Remove in the next major release
export function migrateSettings(settings: Storage.StorageAreaSetItemsType): Promise<void> {
// clear all keys
browser.storage.local.clear();

return browser.storage.local.set({
settings,
});
}

// ToDo: Remove in the next major release
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getPreviousSettings(): Promise<{ [s: string]: any }> {
return browser.storage.local.get(null);
}

0 comments on commit 8a6d5ff

Please sign in to comment.