Skip to content

Commit

Permalink
Merge pull request #9 from jqphu/8-create-a-simple-way-to-disable-the…
Browse files Browse the repository at this point in the history
…-extension

Add a settings button to disable the extension
  • Loading branch information
jqphu authored Jul 29, 2022
2 parents 7d7a30f + 0d498a9 commit b3f0a34
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 51 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"format": "prettier --write '**/*.{js,jsx,ts,tsx,json,css,scss,md}'"
},
"dependencies": {
"@headlessui/react": "^1.6.6",
"@hot-loader/react-dom": "^17.0.2",
"@metamask/detect-provider": "1.2.0",
"@reduxjs/toolkit": "^1.8.3",
Expand All @@ -17,7 +18,7 @@
"ethers": "^5.6.9",
"mixpanel-browser": "^2.45.0",
"pino": "8.1.0",
"pocket-universe-js": "^0.0.1",
"pocket-universe-js": "^0.0.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hot-loader": "^4.13.0",
Expand Down
Binary file added src/assets/img/icon-32-gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions src/containers/Settings/Settings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { MdClose } from 'react-icons/md';
import { Switch } from '@headlessui/react';
import { useState, useEffect } from 'react';
import { setSettings, getSettings } from '../../lib/storage';
import React from 'react';

const Settings = ({ closeSettings }: { closeSettings: () => void }) => {
const [enabled, setEnabled] = useState<boolean>(true);

useEffect(() => {
getSettings().then((settings) => setEnabled(!settings.disable));
}, []);

const switchedCallback = async (enabled: boolean) => {
await setSettings({ disable: !enabled });
setEnabled(enabled);
};

return (
<div className="flex flex-col">
<div className="flex flex-col grow">
<div className="flex flex-row border-t border-gray-600 py-4">
<div className="text-xl font-bold text-gray-100 px-4">Settings</div>
<button
onClick={closeSettings}
className="text-2xl font-bold text-gray-400 ml-auto my-auto text-right mr-3 p-1 hover:bg-gray-600 hover:rounded-full"
>
<MdClose />
</button>
</div>
<div className="flex flex-col gap-4 px-4 pt-4 w-full">
<div className="flex flex-row w-full">
<div className="text-lg text-gray-100 my-auto">Run Simulations</div>
<Switch
checked={enabled}
onChange={switchedCallback}
className={`${enabled ? 'bg-blue-400' : 'bg-teal-700'}
my-auto ml-auto relative inline-flex h-6 w-12 shrink-0 cursor-pointer rounded-full items-center border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75`}
>
<span className="sr-only">Use setting</span>
<span
aria-hidden="true"
className={`${enabled ? 'translate-x-6' : 'translate-x-0'}
pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out`}
/>
</Switch>
</div>
</div>
</div>
<div>
<img className="mt-auto w-screen" src="waves_bottom.png" alt="" />
</div>
</div>
);
};

export default Settings;
25 changes: 25 additions & 0 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export let settings = {
disable: false,
};

const DISPATCH_SETTINGS = 'POCKET_UNIVERSE_DISPATCH_SETTINGS';

/**
* Dispatch from ContentScript to InjectedScript the new settings.
*/
export const dispatchSettings = (settings: any) => {
document.dispatchEvent(
new CustomEvent(DISPATCH_SETTINGS, {
detail: settings,
})
);
};

/**
* Listen to updates in settings.
*/
export const listenForSettingsUpdates = () => {
document.addEventListener(DISPATCH_SETTINGS, (event: any) => {
settings = event.detail;
});
};
51 changes: 51 additions & 0 deletions src/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,57 @@ export const clearOldSimulations = async () => {
return chrome.storage.sync.set({ simulations });
};

export const SETTINGS_KEY = 'settings';

export interface Settings {
/**
* Whether or not we should disable the extension.
*/
disable: boolean;
}

const updateIcon = (settings: Settings) => {
if (settings.disable) {
chrome.action.setIcon({ path: 'icon-32-gray.png' });
} else {
chrome.action.setIcon({ path: 'icon-32.png' });
}
};

/**
* Set the settings to the given args.
*/
export const setSettings = async (args: Settings) => {
// Default is enabled.
let { settings = { disable: false } } = await chrome.storage.sync.get(
SETTINGS_KEY
);
log.info({ settings: settings, msg: 'Updating settings' });

settings.disable = args.disable;

updateIcon(settings);

return chrome.storage.sync.set({ settings });
};

/**
* Get the settings.
*/
export const getSettings = async (): Promise<Settings> => {
const { settings = { disable: false } } = await chrome.storage.sync.get(
SETTINGS_KEY
);
log.info({ settings: settings, msg: 'Getting settings.' });

return settings as Settings;
};

/**
* Get the initial set of settings for the icon.
*/
getSettings().then(updateIcon);

export const simulationNeedsAction = (
state: StoredSimulationState
): boolean => {
Expand Down
10 changes: 9 additions & 1 deletion src/pages/Content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
SIMULATE_REQUEST_COMMAND,
SimulateResponse,
} from '../../lib/simulate_request_reply';
import { dispatchSettings } from '../../lib/settings';
import type { StoredSimulation } from '../../lib/storage';
import { removeSimulation, StoredSimulationState } from '../../lib/storage';

const log = logger.child({ component: 'Content-Script' });
console.log('Content Script Loaded');

log.debug({ msg: 'Content Script Loaded' });

// There is a bit of a memory leak here. If the user navigates away from this page before the request is sent in, the request will never be removed from storage.
// There shouldn't be too many requests though so this is okay.
Expand All @@ -27,6 +29,12 @@ const maybeRemoveId = (id: string) => {
}
};

chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'sync' && changes.settings?.newValue) {
dispatchSettings(changes.settings.newValue);
}
});

listenToSimulateRequest((simulateRequest: SimulateRequestArgs) => {
log.info({ simulateRequest }, 'SimulateRequest');
ids.push(simulateRequest.id);
Expand Down
9 changes: 9 additions & 0 deletions src/pages/Injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RequestManager,
SimulateResponse,
} from '../../lib/simulate_request_reply';
import { settings, listenForSettingsUpdates } from '../../lib/settings';

declare global {
interface Window {
Expand All @@ -11,9 +12,11 @@ declare global {
}

const log = logger.child({ component: 'Injected' });
log.debug({ msg: 'Injected script loaded.' });

/// Handling all the request communication.
const REQUEST_MANAGER = new RequestManager();
listenForSettingsUpdates();

let cachedProxy: any;

Expand All @@ -26,6 +29,12 @@ let providerChanged = true;
const pocketUniverseProxyHandler = {
get(target: any, prop: any, receiver: any) {
log.debug({ prop, msg: 'Props' });
/*
* User has disabled PU, just reflect.
*/
if (settings.disable) {
return Reflect.get(target, prop, receiver);
}

if (prop === 'providers') {
return null;
Expand Down
29 changes: 23 additions & 6 deletions src/pages/Popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import mixpanel from 'mixpanel-browser';
import React, { useEffect } from 'react';
import { AiFillSetting } from 'react-icons/ai';
import React, { useEffect, useState } from 'react';

import Transaction from '../../containers/Transaction/Transaction';
import Settings from '../../containers/Settings/Settings';

mixpanel.init('00d3b8bc7c620587ecb1439557401a87');

const Popup = () => {
const [settingsOpen, setSettingsOpen] = useState(false);

useEffect(() => {
document.title = 'Pocket Universe';
chrome.storage.sync.get('first_open', (result) => {
Expand All @@ -17,15 +21,28 @@ const Popup = () => {
}, []);

return (
<div className="flex flex-col text-white bg-gray-900 overflow-hidden min-w-[360px] min-h-screen items-center">
<div className="flex flex-row p-5 text-center">
<h3 className="flex flex-row gap-4 text-xl leading-6 font-medium text-purple-300">
<div className="flex flex-col text-white bg-gray-900 overflow-hidden min-w-[360px] min-h-screen items-center ">
<div className="flex flex-row p-4 text-center w-full">
<button
onClick={() => setSettingsOpen(false)}
className="flex flex-row gap-4 text-xl leading-6 font-medium text-purple-300 hover:bg-gray-600 rounded-lg"
>
<img src="icon-128.png" className="h-10 my-auto" alt="logo" />
<div className="font-light text-xl my-auto">Pocket Universe</div>
</h3>
</button>
<button
className="flex ml-auto my-auto hover:bg-gray-600 hover:rounded-full text-gray-200 text-xl w-7 h-7 justify-center items-center"
onClick={() => setSettingsOpen(true)}
>
<AiFillSetting />
</button>
</div>
<div className="flex grow">
<Transaction />
{settingsOpen ? (
<Settings closeSettings={() => setSettingsOpen(false)} />
) : (
<Transaction />
)}
</div>
</div>
);
Expand Down
10 changes: 10 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ var options = {
},
],
}),
new CopyWebpackPlugin({
patterns: [
{
from: 'src/assets/img/icon-32-gray.png',
to: path.join(__dirname, 'build'),
force: true,
},
],
}),

new HtmlWebpackPlugin({
template: path.join(__dirname, 'src', 'pages', 'Popup', 'index.html'),
filename: 'popup.html',
Expand Down
54 changes: 11 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,11 @@
"@ethersproject/properties" "^5.6.0"
"@ethersproject/strings" "^5.6.1"

"@headlessui/react@^1.6.6":
version "1.6.6"
resolved "https://registry.yarnpkg.com/@headlessui/react/-/react-1.6.6.tgz#3073c066b85535c9d28783da0a4d9288b5354d0c"
integrity sha512-MFJtmj9Xh/hhBMhLccGbBoSk+sk61BlP6sJe4uQcVMtXZhCgGqd2GyIQzzmsdPdTEWGSF434CBi8mnhR6um46Q==

"@hot-loader/react-dom@^17.0.2":
version "17.0.2"
resolved "https://registry.yarnpkg.com/@hot-loader/react-dom/-/react-dom-17.0.2.tgz#0b24e484093e8f97eb5c72bebdda44fc20bc8400"
Expand Down Expand Up @@ -1716,14 +1721,6 @@
resolved "https://registry.yarnpkg.com/@types/mixpanel-browser/-/mixpanel-browser-2.38.0.tgz#b3e28e1ba06c10a9f88510b88f1ac9d1b2adfc42"
integrity sha512-TR8rvsILnqXA7oiiGOxuMGXwvDeCoQDonXJB5UR+TYvEAFpiK8ReFj5LhZT+Xhm3NpI9aPoju30jB2ssorSUww==

"@types/node-fetch@^2.6.2":
version "2.6.2"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.2.tgz#d1a9c5fd049d9415dce61571557104dec3ec81da"
integrity sha512-DHqhlq5jeESLy19TYhLakJ07kNumXWjcDdxXsLUMJZ6ue8VZJj4kLPQVE/2mdHh3xZziNF1xppu5lwmS53HR+A==
dependencies:
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*":
version "18.6.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.6.1.tgz#828e4785ccca13f44e2fb6852ae0ef11e3e20ba5"
Expand Down Expand Up @@ -2364,14 +2361,6 @@ axe-core@^4.4.3:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.3.tgz#11c74d23d5013c0fa5d183796729bc3482bd2f6f"
integrity sha512-32+ub6kkdhhWick/UjvEwRchgoetXqTK14INLqbGm5U2TzBkBNF3nQtLYm8ovxSkQWArjEQvftCKryjZaATu3w==

axios@^0.27.2:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
dependencies:
follow-redirects "^1.14.9"
form-data "^4.0.0"

axobject-query@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
Expand Down Expand Up @@ -2755,7 +2744,7 @@ colorette@^2.0.10, colorette@^2.0.14:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.19.tgz#cdf044f47ad41a0f4b56b3a0d5b4e6e1a2d5a798"
integrity sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ==

combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -3815,7 +3804,7 @@ flatted@^3.1.0:
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.6.tgz#022e9218c637f9f3fc9c35ab9c9193f05add60b2"
integrity sha512-0sQoMh9s0BYsm+12Huy/rkKxVu4R1+r96YX5cG44rHV0pQ6iC3Q+mkoMFaGWObMFYQxCVT+ssG1ksneA2MI9KQ==

follow-redirects@^1.0.0, follow-redirects@^1.14.9:
follow-redirects@^1.0.0:
version "1.15.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.1.tgz#0ca6a452306c9b276e4d3127483e29575e207ad5"
integrity sha512-yLAMQs+k0b2m7cVxpS1VKJVvoz7SS9Td1zss3XRwXj+ZDH00RJgnuLx7E44wx02kQLrdM3aOOy+FpzS7+8OizA==
Expand All @@ -3825,24 +3814,6 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha512-j0KLYPhm6zeac4lz3oJ3o65qvgQCcPubiyotZrXqEaG4hNagNYO8qdlUrX5vwqv9ohqeT/Z3j6+yW067yWWdUw==

form-data@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.1.tgz#ebd53791b78356a99af9a300d4282c4d5eb9755f"
integrity sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down Expand Up @@ -5579,13 +5550,10 @@ pkg-dir@^4.1.0, pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

pocket-universe-js@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/pocket-universe-js/-/pocket-universe-js-0.0.1.tgz#0a7fed5d1a8c48c450815f3432e20384ed4b7430"
integrity sha512-8wc2wk4mPBYE3xfQST0n35hQrbB448TDR52m06O6w/vew7BexKuTvOZafeQG5WAHHCnxQs2vhUpuh5SPMazu1g==
dependencies:
"@types/node-fetch" "^2.6.2"
axios "^0.27.2"
pocket-universe-js@^0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/pocket-universe-js/-/pocket-universe-js-0.0.3.tgz#1c104d62a650662ba6be9271328659fca847fba0"
integrity sha512-+0RiMGvyq3LDxqWyF+QYbonw/StgS6yCMaWsa18XfJik0qyBw8icLqQ7ANeBuctY2g5/Gah+AaBZMjRKaemEew==

postcss-import@^14.1.0:
version "14.1.0"
Expand Down

0 comments on commit b3f0a34

Please sign in to comment.