From 0f282fcbca4ba76906acbfd4bd3203b036ceb1ec Mon Sep 17 00:00:00 2001 From: Jack Works Date: Thu, 19 Sep 2019 18:06:15 +0800 Subject: [PATCH] feat: add a new settings "Disable fetching public keys ..." --- .../shared-settings/createSettings.ts | 25 -------- .../shared-settings/createSettings.tsx | 64 +++++++++++++++++++ src/components/shared-settings/settings.ts | 20 +++++- src/extension/content-script/tasks.ts | 13 +++- src/extension/options-page/Developer.tsx | 16 ++++- src/extension/popup-page/index.tsx | 19 +----- 6 files changed, 110 insertions(+), 47 deletions(-) delete mode 100644 src/components/shared-settings/createSettings.ts create mode 100644 src/components/shared-settings/createSettings.tsx diff --git a/src/components/shared-settings/createSettings.ts b/src/components/shared-settings/createSettings.ts deleted file mode 100644 index bdfd2b05354..00000000000 --- a/src/components/shared-settings/createSettings.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { ValueRef } from '@holoflows/kit/es/util/ValueRef' -import { MessageCenter } from '../../utils/messages' - -export function createNewSettings(key: string, initialValue: T) { - const settings = new ValueRef(initialValue) - - update() - settings.addListener(newVal => { - MessageCenter.emit('settingsUpdated', undefined) - browser.storage.local.set({ - settings: { [key]: newVal }, - }) - }) - MessageCenter.on('settingsUpdated', update) - async function update() { - if (typeof browser === 'object') { - const value = await browser.storage.local.get() - const stored = value.settings - if (typeof stored === 'object' && stored !== null && key in (stored as any)) { - settings.value = Reflect.get(stored, key) - } - } - } - return settings -} diff --git a/src/components/shared-settings/createSettings.tsx b/src/components/shared-settings/createSettings.tsx new file mode 100644 index 00000000000..04cba7abc41 --- /dev/null +++ b/src/components/shared-settings/createSettings.tsx @@ -0,0 +1,64 @@ +import React from 'react' +import { ValueRef } from '@holoflows/kit/es/util/ValueRef' +import { MessageCenter } from '../../utils/messages' +import { ListItem, ListItemText, ListItemSecondaryAction, Switch } from '@material-ui/core' +import { useValueRef } from '../../utils/hooks/useValueRef' + +interface SettingsTexts { + primary: string + secondary?: string +} +const texts = new WeakMap, SettingsTexts>() +export function createNewSettings( + key: string, + initialValue: T, + UITexts: SettingsTexts, +) { + const settings = new ValueRef(initialValue) + texts.set(settings, UITexts) + + update() + settings.addListener(async newVal => { + const stored = ((await browser.storage.local.get()).settings as object) || {} + browser.storage.local.set({ + settings: { ...stored, [key]: newVal }, + }) + MessageCenter.emit('settingsUpdated', undefined) + }) + MessageCenter.on('settingsUpdated', update) + async function update() { + if (typeof browser === 'object') { + const value = await browser.storage.local.get() + const stored = value.settings + if (typeof stored === 'object' && stored !== null && key in (stored as any)) { + settings.value = Reflect.get(stored, key) + } + } + } + return settings +} + +export function useSettingsUI(settingsRef: ReturnType) { + const currentValue = useValueRef(settingsRef) + const text = texts.get(settingsRef)! + function ui() { + switch (typeof currentValue) { + case 'boolean': + return ( + (settingsRef.value = !settingsRef.value)}> + + + + + + ) + default: + throw new Error('Invalid settings') + } + } + return ui() +} diff --git a/src/components/shared-settings/settings.ts b/src/components/shared-settings/settings.ts index 21fef45c613..8ef3ddb88d6 100644 --- a/src/components/shared-settings/settings.ts +++ b/src/components/shared-settings/settings.ts @@ -1,2 +1,20 @@ import { createNewSettings } from './createSettings' -export const debugModeSetting = createNewSettings('debugMode', false) +/** + * Does the debug mode on + */ +export const debugModeSetting = createNewSettings('debugMode', false, { + primary: 'Enable debug mode', + secondary: 'Enable this will display additional information on the Maskbook UI to help debugging', +}) +/** + * Never open a new tab in the background + */ +export const disableOpenNewTabInBackgroundSettings = createNewSettings( + 'disable automated tab task open new tab', + false, + { + primary: 'Disable open hidden tabs in the background', + secondary: + "Many of Maskbook features relies on this behavior. Disable this behavior will limit Maskbook's functionality", + }, +) diff --git a/src/extension/content-script/tasks.ts b/src/extension/content-script/tasks.ts index 3da68df6e0e..91ee12adddb 100644 --- a/src/extension/content-script/tasks.ts +++ b/src/extension/content-script/tasks.ts @@ -1,8 +1,9 @@ import { AutomatedTabTask } from '@holoflows/kit' import { getActivatedUI, SocialNetworkUI } from '../../social-network/ui' import { PersonIdentifier, PostIdentifier } from '../../database/type' +import { disableOpenNewTabInBackgroundSettings } from '../../components/shared-settings/settings' -export default AutomatedTabTask( +const tasks = AutomatedTabTask( { /** * Access post url @@ -35,3 +36,13 @@ export default AutomatedTabTask( }, { memorable: true }, )! +export default function tasksMocked(...args: Parameters) { + const [, options] = args + if (disableOpenNewTabInBackgroundSettings.value) { + if (!options || !options.active) + throw new Error( + `You have disabled "Disable fetching public keys in the background" in the settings so Maskbook can not perform this action`, + ) + } + return tasks(...args) +} diff --git a/src/extension/options-page/Developer.tsx b/src/extension/options-page/Developer.tsx index 7e308062b89..9a0c6c9df76 100644 --- a/src/extension/options-page/Developer.tsx +++ b/src/extension/options-page/Developer.tsx @@ -1,14 +1,24 @@ -import { ListSubheader, Grid, Paper, makeStyles } from '@material-ui/core' +import { + ListSubheader, + Grid, + makeStyles, + List, + ListItem, + ListItemText, + ListItemSecondaryAction, + Switch, +} from '@material-ui/core' import { PersonIdentifier, Identifier } from '../../database/type' import { deconstructPayload } from '../../utils/type-transform/Payload' import Services from '../service' -import { useCurrentIdentity } from '../../components/DataSource/useActivatedUI' import { Person } from '../../database' import React from 'react' import { AddProve } from './DeveloperComponents/AddProve' import { DecryptPostDeveloperMode } from './DeveloperComponents/DecryptPost' import { SeeMyProvePost } from './DeveloperComponents/SeeMyProvePost' import { FriendsDeveloperMode } from './DeveloperComponents/Friends' +import { debugModeSetting, disableOpenNewTabInBackgroundSettings } from '../../components/shared-settings/settings' +import { useSettingsUI } from '../../components/shared-settings/createSettings' async function swallowGoo(me: Person | null) { const boxElem = document.querySelector('#raw-box') as HTMLTextAreaElement @@ -50,12 +60,12 @@ async function assimilateGoo(content: string, me: Person | null): Promise ({ root: { padding: theme.spacing(0, 2) } })) const DevPage = () => { - // const me = useCurrentIdentity(false) const classes = useStyles() return ( <> Developer Settings
+ {useSettingsUI(disableOpenNewTabInBackgroundSettings)} diff --git a/src/extension/popup-page/index.tsx b/src/extension/popup-page/index.tsx index 0c3151ac5b5..2c490f9ac9e 100644 --- a/src/extension/popup-page/index.tsx +++ b/src/extension/popup-page/index.tsx @@ -8,6 +8,7 @@ import '../../setup.ui' import { SSRRenderer } from '../../utils/SSRRenderer' import { useValueRef } from '../../utils/hooks/useValueRef' import { debugModeSetting } from '../../components/shared-settings/settings' +import { useSettingsUI } from '../../components/shared-settings/createSettings' const useStyles = makeStyles(theme => ({ button: { @@ -50,23 +51,7 @@ export function Popup() { onClick={e => browser.runtime.openOptionsPage()}> Options - - - - - (debugModeSetting.value = newValue)} - /> - - - + {useSettingsUI(debugModeSetting)} )