-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a new settings "Disable fetching public keys ..."
- Loading branch information
1 parent
14dc504
commit 0f282fc
Showing
6 changed files
with
110 additions
and
47 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ValueRef<any>, SettingsTexts>() | ||
export function createNewSettings<T extends browser.storage.StorageValue>( | ||
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<typeof createNewSettings>) { | ||
const currentValue = useValueRef(settingsRef) | ||
const text = texts.get(settingsRef)! | ||
function ui() { | ||
switch (typeof currentValue) { | ||
case 'boolean': | ||
return ( | ||
<ListItem button onClick={() => (settingsRef.value = !settingsRef.value)}> | ||
<ListItemText id={text.primary} primary={text.primary} secondary={text.secondary} /> | ||
<ListItemSecondaryAction> | ||
<Switch | ||
inputProps={{ 'aria-labelledby': text.primary }} | ||
edge="end" | ||
checked={currentValue} | ||
/> | ||
</ListItemSecondaryAction> | ||
</ListItem> | ||
) | ||
default: | ||
throw new Error('Invalid settings') | ||
} | ||
} | ||
return ui() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,20 @@ | ||
import { createNewSettings } from './createSettings' | ||
export const debugModeSetting = createNewSettings<boolean>('debugMode', false) | ||
/** | ||
* Does the debug mode on | ||
*/ | ||
export const debugModeSetting = createNewSettings<boolean>('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<boolean>( | ||
'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", | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters