Skip to content

Commit

Permalink
fix(storage): change webextension polyfill not to use
Browse files Browse the repository at this point in the history
- fix storage get logic
- add prettier config
- add prettier eslint integration
- bump up version to 1.2.0
  • Loading branch information
Jaewoook committed Jun 23, 2024
1 parent d88758c commit 99cee4e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
"eslint:recommended",
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
"prettier",
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
globals: {
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"trailingComma": "all",
"semi": true,
"printWidth": 120
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kor-address-extension",
"version": "1.1.2",
"version": "1.2.0",
"main": "dist/index.js",
"type": "module",
"repository": "git@github.com:Jaewoook/kor-address-extension.git",
Expand All @@ -23,17 +23,18 @@
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/styled-components": "^5.1.34",
"@types/webextension-polyfill": "^0.10.7",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"@vitejs/plugin-react": "^4.3.1",
"archiver": "^5.3.1",
"chalk": "^4.1.0",
"eslint": "^8",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"fs-extra": "^10.1.0",
"prettier": "3.3.2",
"typescript": "^5.4.5",
"vite": "^5.3.1"
},
Expand All @@ -46,7 +47,6 @@
"react-dom": "^18.3.1",
"react-icons": "^5.2.1",
"recoil": "^0.7.7",
"styled-components": "^6.1.11",
"webextension-polyfill": "^0.12.0"
"styled-components": "^6.1.11"
}
}
29 changes: 14 additions & 15 deletions src/shared/storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import type { Browser } from "webextension-polyfill";
import type { AddressData, SearchKey } from "./models/address";
import { isExtension } from "./utils";

let browser: Browser | null = null;
if (isExtension()) {
import("webextension-polyfill").then((module) => {
browser = module.default;
});
}
import { isExtension, getExtensionAPI } from "./utils";

type SearchResultOptions = {
showEng: boolean;
Expand Down Expand Up @@ -37,12 +29,14 @@ export const DEFAULT_SETTINGS: Settings = {
};

const get = (key?: string) => {
if (isExtension() && browser) {
return browser.storage.local.get(key);
const extAPI = getExtensionAPI();
if (isExtension() && extAPI) {
return extAPI.storage.local.get(key);
}

if (key) {
return { key: localStorage.getItem(key) };
const raw = localStorage.getItem(key);
return { [key]: raw ? JSON.parse(raw) : null };
}

return Array.from({ length: localStorage.length })
Expand All @@ -56,8 +50,9 @@ const get = (key?: string) => {
};

const set = (items: Record<string, any>) => {
if (isExtension() && browser) {
return browser.storage.local.set(items);
const extAPI = getExtensionAPI();
if (isExtension() && extAPI) {
return extAPI.storage.local.set(items);
}

return Object.entries(items).forEach(([key, value]) => {
Expand All @@ -77,6 +72,7 @@ export const getSearchResultOptions = async (): Promise<SearchResultOptions | nu

export const getRecentAddressList = async (): Promise<AddressData[] | null> => {
const recentAddressList = await get("addressData");
console.log("recent address list", recentAddressList);
return recentAddressList?.addressData ?? null;
};

Expand All @@ -88,7 +84,10 @@ export const setRecentAddressList = async (addressList: AddressData[]) => {
set({ addressData: addressList });
};

export const validateSettingsData = (settingsData: unknown, expected: Record<string, any> = DEFAULT_SETTINGS): boolean => {
export const validateSettingsData = (
settingsData: unknown,
expected: Record<string, any> = DEFAULT_SETTINGS,
): boolean => {
if (typeof settingsData !== "object" || settingsData === null) {
return false;
}
Expand Down
13 changes: 13 additions & 0 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ export const getRuntime = (): Runtime => {
};

export const isExtension = () => getRuntime() === "extension";

export const getExtensionAPI = () => {
if (!isExtension()) {
return null;
}
if (typeof chrome === "object") {
return chrome;
}
if (typeof browser === "object") {
return browser;
}
return null;
};
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -935,11 +935,6 @@
resolved "https://registry.yarnpkg.com/@types/stylis/-/stylis-4.2.5.tgz#1daa6456f40959d06157698a653a9ab0a70281df"
integrity sha512-1Xve+NMN7FWjY14vLoY5tL3BVEQ/n42YLwaqJIPYhotZ9uBHt87VceMwWQpzmdEt2TNXIorIFG+YeCUUW7RInw==

"@types/webextension-polyfill@^0.10.7":
version "0.10.7"
resolved "https://registry.yarnpkg.com/@types/webextension-polyfill/-/webextension-polyfill-0.10.7.tgz#de059250599733a60ed26c8a0c81e21e11183b90"
integrity sha512-10ql7A0qzBmFB+F+qAke/nP1PIonS0TXZAOMVOxEUsm+lGSW6uwVcISFNa0I4Oyj0884TZVWGGMIWeXOVSNFHw==

"@typescript-eslint/eslint-plugin@^7.13.0":
version "7.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.13.0.tgz#3cdeb5d44d051b21a9567535dd90702b2a42c6ff"
Expand Down Expand Up @@ -1790,6 +1785,11 @@ escape-string-regexp@^4.0.0:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==

eslint-config-prettier@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz#31af3d94578645966c082fcb71a5846d3c94867f"
integrity sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw==

eslint-import-resolver-node@^0.3.9:
version "0.3.9"
resolved "https://registry.yarnpkg.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.9.tgz#d4eaac52b8a2e7c3cd1903eb00f7e053356118ac"
Expand Down Expand Up @@ -2884,6 +2884,11 @@ prelude-ls@^1.2.1:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==

prettier@3.3.2:
version "3.3.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.3.2.tgz#03ff86dc7c835f2d2559ee76876a3914cec4a90a"
integrity sha512-rAVeHYMcv8ATV5d508CFdn+8/pHPpXeIid1DdrPwXnaAdH7cqjVbpJaT5eq4yRAFU/lsbwYwSF/n5iNrdJHPQA==

printj@~1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/printj/-/printj-1.1.2.tgz#d90deb2975a8b9f600fb3a1c94e3f4c53c78a222"
Expand Down Expand Up @@ -3874,11 +3879,6 @@ vite@^5.3.1:
optionalDependencies:
fsevents "~2.3.3"

webextension-polyfill@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.12.0.tgz#f62c57d2cd42524e9fbdcee494c034cae34a3d69"
integrity sha512-97TBmpoWJEE+3nFBQ4VocyCdLKfw54rFaJ6EVQYLBCXqCIpLSZkwGgASpv4oPt9gdKCJ80RJlcmNzNn008Ag6Q==

which-boxed-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"
Expand Down

0 comments on commit 99cee4e

Please sign in to comment.