Skip to content

Commit

Permalink
fix: dsearch switcher (#10825)
Browse files Browse the repository at this point in the history
* fix: i18n add .

* refactor: use the built-in plugin switcher

* chore: remove customized switcher
  • Loading branch information
guanbinrui committed Sep 22, 2023
1 parent 799be0b commit 2cd09da
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
setCurrentPluginMinimalMode,
type PersonaIdentifier,
type ValueRefWithReady,
decentralizedSearchSettings,
appearanceSettings,
BooleanPreference,
InjectSwitchSettings,
Expand Down Expand Up @@ -68,6 +67,4 @@ export async function setInjectSwitchSetting(network: string, value: boolean) {
InjectSwitchSettings[network].value = value
}

export const [getDecentralizedSearchSettings, setDecentralizedSearchSettings] = create(decentralizedSearchSettings)

export { __deprecated__getStorage as getLegacySettingsInitialValue } from '../../utils/deprecated-storage.js'
2 changes: 1 addition & 1 deletion packages/mask/shared-ui/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@
"plugin_web3_profile_recommend_feature_description": "Optimize your social profile by customizing the display of Web3 features.",
"plugin_nft_avatar_recommend_feature_description": "Set your NFT as profile picture with exclusive aura.",
"application_hint": "Socialize and show off your NFTs. People can bid, buy and view your valuable NFTs without leaving Twitter.",
"plugin_goPlusSecurity_description": "Provide you with fast, reliable and convenient security services",
"plugin_goPlusSecurity_description": "Provide you with fast, reliable and convenient security services.",
"plugin_red_packet_create": "Create your Lucky Drop.",
"plugin_red_packet_claimed": "Claimed",
"plugin_red_packet_claim": "Claim",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,19 @@ import {
getSearchResultContentForProfileTab,
getSearchResultTabContent,
getSearchResultTabs,
useActivatedPlugin,
useActivatedPluginsSiteAdaptor,
usePluginI18NField,
} from '@masknet/plugin-infra/content-script'
import {
EMPTY_LIST,
PluginID,
type SocialIdentity,
ProfileTabs,
decentralizedSearchSettings,
} from '@masknet/shared-base'
import { useValueRef } from '@masknet/shared-base-ui'
import { EMPTY_LIST, PluginID, type SocialIdentity, type ProfileTabs } from '@masknet/shared-base'
import { makeStyles, MaskTabList, useTabs } from '@masknet/theme'
import { DSearch } from '@masknet/web3-providers'
import type { Web3Helper } from '@masknet/web3-helpers'
import { ScopedDomainsContainer } from '@masknet/web3-hooks-base'
import { DSearch } from '@masknet/web3-providers'
import { type SearchResult, SearchResultType } from '@masknet/web3-shared-base'
import { useSearchedKeyword } from '../DataSource/useSearchedKeyword.js'
import { Telemetry } from '@masknet/web3-telemetry'
import { EventID, EventType } from '@masknet/web3-telemetry/types'
import { useSearchedKeyword } from '../DataSource/useSearchedKeyword.js'

const useStyles = makeStyles<{ isProfilePage?: boolean; searchType?: SearchResultType }>()(
(theme, { isProfilePage, searchType }) => ({
Expand Down Expand Up @@ -60,7 +54,8 @@ export interface SearchResultInspectorProps {
export function SearchResultInspector(props: SearchResultInspectorProps) {
const translate = usePluginI18NField()

const dSearchEnabled = useValueRef(decentralizedSearchSettings)
const dSearchEnabled = useActivatedPlugin(PluginID.Handle, false)

const { profileTabType } = props
const keyword_ = useSearchedKeyword()
const keyword = props.keyword || keyword_
Expand Down Expand Up @@ -123,7 +118,7 @@ export function SearchResultInspector(props: SearchResultInspectorProps) {
return <Component result={currentResult} />
}, [currentTab, resultList.value])

if (!dSearchEnabled && profileTabType === ProfileTabs.WEB3) return null
if (!dSearchEnabled) return null
if (!keyword && !currentResult) return null
if (!contentComponent) return null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export function useOpenApplicationBoardDialog(quickMode?: boolean, focusPluginID
currentSite: activatedSiteAdaptorUI!.networkIdentifier,
queryOwnedPersonaInformation: Services.Identity.queryOwnedPersonaInformation,
setPluginMinimalModeEnabled: Services.Settings.setPluginMinimalModeEnabled,
getDecentralizedSearchSettings: Services.Settings.getDecentralizedSearchSettings,
setDecentralizedSearchSettings: Services.Settings.setDecentralizedSearchSettings,
personaPerSiteConnectStatusLoading,
applicationCurrentStatus,
quickMode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { memo } from 'react'
import { Trans } from 'react-i18next'
import { Icons } from '@masknet/icons'
import { EMPTY_LIST } from '@masknet/shared-base'
import { makeStyles } from '@masknet/theme'
import { Box, Button, Typography, alpha } from '@mui/material'
import { useToggle } from '@react-hookz/web'
import { memo } from 'react'
import { useI18N } from '../../../../utils/i18n-next-ui.js'
import { Trans } from 'react-i18next'

const useStyles = makeStyles()((theme) => ({
root: {
Expand Down
2 changes: 0 additions & 2 deletions packages/mask/src/site-adaptor-infra/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ export async function activateSiteAdaptorUIInner(ui_deferred: SiteAdaptorUI.Defe
postMessage: ui.automation?.nativeCompositionDialog?.appendText,
setCurrentPersonaIdentifier: Services.Settings.setCurrentPersonaIdentifier,
setPluginMinimalModeEnabled: Services.Settings.setPluginMinimalModeEnabled,
setDecentralizedSearchSettings: Services.Settings.setDecentralizedSearchSettings,
getDecentralizedSearchSettings: Services.Settings.getDecentralizedSearchSettings,
getSearchedKeyword: ui.collecting.getSearchedKeyword,
hasHostPermission: Services.Helper.hasHostPermission,
requestHostPermission: Services.Helper.requestHostPermission,
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-infra/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ export namespace Plugin.SiteAdaptor {
getSearchedKeyword?: () => string
hasHostPermission?: (origins: readonly string[]) => Promise<boolean>
requestHostPermission?: (origins: readonly string[]) => Promise<boolean>
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>
}

export interface Definition extends GeneralUI.Definition, Shared.DefinitionDeferred<SiteAdaptorContext> {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/ScamSniffer/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PluginID } from '@masknet/shared-base'

export const PLUGIN_ID = PluginID.ScamSniffer
export const PLUGIN_DESCRIPTION = 'Help people fight against crypto scammers'
export const PLUGIN_DESCRIPTION = 'Help people fight against crypto scammers.'
export const PLUGIN_NAME = 'Scam Sniffer'
15 changes: 5 additions & 10 deletions packages/shared/src/UI/components/SearchResultInspector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ import {
getSearchResultContentForProfileTab,
getSearchResultTabContent,
getSearchResultTabs,
useActivatedPlugin,
useActivatedPluginsSiteAdaptor,
usePluginI18NField,
} from '@masknet/plugin-infra/content-script'
import {
EMPTY_LIST,
PluginID,
type SocialIdentity,
ProfileTabs,
decentralizedSearchSettings,
} from '@masknet/shared-base'
import { useValueRef } from '@masknet/shared-base-ui'
import { EMPTY_LIST, PluginID, type SocialIdentity, type ProfileTabs } from '@masknet/shared-base'
import { makeStyles, MaskTabList, useTabs } from '@masknet/theme'
import type { Web3Helper } from '@masknet/web3-helpers'
import { ScopedDomainsContainer } from '@masknet/web3-hooks-base'
Expand Down Expand Up @@ -60,7 +54,8 @@ export interface SearchResultInspectorProps {
export function SearchResultInspector(props: SearchResultInspectorProps) {
const translate = usePluginI18NField()

const dSearchEnabled = useValueRef(decentralizedSearchSettings)
const dSearchEnabled = useActivatedPlugin(PluginID.Handle, false)

const { profileTabType, empty = null } = props
const keyword_ = useSearchedKeyword()
const keyword = props.keyword || keyword_
Expand Down Expand Up @@ -111,7 +106,7 @@ export function SearchResultInspector(props: SearchResultInspectorProps) {
return <Component result={currentResult} />
}, [currentTab, resultList.value])

if (!dSearchEnabled && profileTabType === ProfileTabs.WEB3) return empty
if (!dSearchEnabled) return empty
if (!keyword && !currentResult) return empty
if (!contentComponent) return empty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ interface ApplicationBoardProps {
applicationCurrentStatus?: PersonaPerSiteConnectStatus
personaPerSiteConnectStatusLoading: boolean
setPluginMinimalModeEnabled?: (id: string, checked: boolean) => Promise<void>
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>

focusPluginID?: PluginID
tab?: ApplicationSettingTabs
Expand All @@ -67,8 +65,6 @@ export function ApplicationBoard({
applicationCurrentStatus,
personaPerSiteConnectStatusLoading,
setPluginMinimalModeEnabled,
getDecentralizedSearchSettings,
setDecentralizedSearchSettings,
quickMode = false,
tab = ApplicationSettingTabs.pluginSwitch,
focusPluginID,
Expand Down Expand Up @@ -136,8 +132,6 @@ export function ApplicationBoard({
<ApplicationSettingPluginSwitch
focusPluginID={focusPluginID}
setPluginMinimalModeEnabled={setPluginMinimalModeEnabled}
getDecentralizedSearchSettings={getDecentralizedSearchSettings}
setDecentralizedSearchSettings={setDecentralizedSearchSettings}
/>
</TabPanel>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export interface ApplicationBoardFormProps {
applicationCurrentStatus?: PersonaPerSiteConnectStatus
personaPerSiteConnectStatusLoading: boolean
setPluginMinimalModeEnabled?: (id: string, checked: boolean) => Promise<void>
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>

focusPluginID?: PluginID
tab?: ApplicationSettingTabs
Expand Down Expand Up @@ -93,8 +91,6 @@ export function ApplicationBoardForm(props: ApplicationBoardFormProps) {
<ApplicationSettingPluginSwitch
focusPluginID={props.focusPluginID}
setPluginMinimalModeEnabled={props.setPluginMinimalModeEnabled}
getDecentralizedSearchSettings={props.getDecentralizedSearchSettings}
setDecentralizedSearchSettings={props.setDecentralizedSearchSettings}
/>
</TabPanel>
</TabContext>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { CrossIsolationMessages, PluginID } from '@masknet/shared-base'
import { openWindow } from '@masknet/shared-base-ui'
import { makeStyles, MaskColorVar } from '@masknet/theme'
import { Avatar, Box, List, ListItem, ListItemAvatar, Stack, Switch, Typography } from '@mui/material'
import { useAsyncRetry } from 'react-use'
import { useSharedI18N } from '../../../index.js'

const useStyles = makeStyles()((theme) => ({
Expand Down Expand Up @@ -79,15 +78,11 @@ const DSearch_KEY = 'decentralized_search'
interface Props {
focusPluginID?: PluginID | typeof DSearch_KEY
setPluginMinimalModeEnabled?: (id: string, checked: boolean) => Promise<void>
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>
}

export const ApplicationSettingPluginSwitch = memo(function ApplicationSettingPluginSwitch({
focusPluginID,
setPluginMinimalModeEnabled,
getDecentralizedSearchSettings,
setDecentralizedSearchSettings,
}: Props) {
const { classes } = useStyles()
const plugins = useActivatedPluginsSiteAdaptor('any')
Expand Down Expand Up @@ -118,9 +113,9 @@ export const ApplicationSettingPluginSwitch = memo(function ApplicationSettingPl

return (
<List>
<DSearchSetting
setDecentralizedSearchSettings={setDecentralizedSearchSettings}
getDecentralizedSearchSettings={getDecentralizedSearchSettings}
<DSearchSettings
checked={!pluginsInMinimalMode.map((x) => x.ID).includes(PluginID.Handle)}
onSwitch={(event) => onSwitch(PluginID.Handle, event?.target.checked)}
setRef={(element: HTMLLIElement | null) => {
if (DSearch_KEY === focusPluginID) {
targetPluginRef.current = element
Expand Down Expand Up @@ -193,32 +188,17 @@ export const ApplicationSettingPluginSwitch = memo(function ApplicationSettingPl
)
})

interface DSearchSettingProps {
interface DSearchSettingsProps {
checked: boolean
onSwitch: (event: React.ChangeEvent<HTMLInputElement>) => void
focusPluginID?: string
setRef(element: HTMLLIElement | null): void
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>
}

function DSearchSetting({
setRef,
focusPluginID,
getDecentralizedSearchSettings,
setDecentralizedSearchSettings,
}: DSearchSettingProps) {
function DSearchSettings({ checked, onSwitch, setRef, focusPluginID }: DSearchSettingsProps) {
const t = useSharedI18N()
const { classes } = useStyles()

const { value: settings = true, retry } = useAsyncRetry(async () => {
const settings = await getDecentralizedSearchSettings?.()
return settings
})

const handleSwitch = async (checked: boolean) => {
await setDecentralizedSearchSettings?.(checked)
retry()
}

return (
<ListItem key={DSearch_KEY} ref={(ele) => setRef(ele)} className={classes.listItem}>
<Stack width="100%">
Expand All @@ -237,7 +217,7 @@ function DSearchSetting({
</Stack>
</section>
<Stack justifyContent="center">
<Switch checked={settings} onChange={(event) => handleSwitch(event.target.checked)} />
<Switch checked={checked} onChange={onSwitch} />
</Stack>
</Stack>
<Stack direction="row" mt={1.25}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ interface ApplicationBoardSettingsProps {
focusPluginID?: PluginID
tab?: ApplicationSettingTabs
setPluginMinimalModeEnabled?: (id: string, checked: boolean) => Promise<void>
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>
}

const useStyles = makeStyles()((theme) => {
Expand All @@ -33,15 +31,7 @@ const useStyles = makeStyles()((theme) => {
})

export const ApplicationBoardSettingsDialog = memo<ApplicationBoardSettingsProps>(
({
focusPluginID,
setPluginMinimalModeEnabled,
getDecentralizedSearchSettings,
setDecentralizedSearchSettings,
open,
onClose,
tab = ApplicationSettingTabs.pluginSwitch,
}) => {
({ focusPluginID, setPluginMinimalModeEnabled, open, onClose, tab = ApplicationSettingTabs.pluginSwitch }) => {
const t = useSharedI18N()
const { classes } = useStyles()
const [currentTab, onChange, tabs, setTab] = useTabs(
Expand Down Expand Up @@ -71,8 +61,6 @@ export const ApplicationBoardSettingsDialog = memo<ApplicationBoardSettingsProps
<ApplicationSettingPluginSwitch
focusPluginID={focusPluginID}
setPluginMinimalModeEnabled={setPluginMinimalModeEnabled}
getDecentralizedSearchSettings={getDecentralizedSearchSettings}
setDecentralizedSearchSettings={setDecentralizedSearchSettings}
/>
</TabPanel>
</DialogContent>
Expand Down
18 changes: 0 additions & 18 deletions packages/shared/src/UI/modals/ApplicationBoardModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export interface ApplicationBoardModalOpenProps {
applicationCurrentStatus?: PersonaPerSiteConnectStatus
personaPerSiteConnectStatusLoading: boolean
setPluginMinimalModeEnabled: (id: string, checked: boolean) => Promise<void>
getDecentralizedSearchSettings: () => Promise<boolean>
setDecentralizedSearchSettings: (checked: boolean) => Promise<void>

quickMode?: boolean
tab?: ApplicationSettingTabs
Expand All @@ -45,9 +43,6 @@ export const ApplicationBoardModal = forwardRef<
const [personaPerSiteConnectStatusLoading, setPersonaPerSiteConnectStatusLoading] = useState(false)
const [setPluginMinimalModeEnabled, setSetPluginMinimalModeEnabled] =
useState<(id: string, checked: boolean) => Promise<void>>()
const [getDecentralizedSearchSettings, setGetDecentralizedSearchSettings] = useState<() => Promise<boolean>>()
const [setDecentralizedSearchSettings, setSetDecentralizedSearchSettings] =
useState<(checked: boolean) => Promise<void>>()
const [quickMode, setQuickMode] = useState(false)
const [tab, setTab] = useState<ApplicationSettingTabs>()
const [focusPluginID, setFocusPluginID] = useState<PluginID>()
Expand All @@ -62,8 +57,6 @@ export const ApplicationBoardModal = forwardRef<
setApplicationCurrentStatus(props.applicationCurrentStatus)
setPersonaPerSiteConnectStatusLoading(props.personaPerSiteConnectStatusLoading)
setSetPluginMinimalModeEnabled(() => props.setPluginMinimalModeEnabled)
setGetDecentralizedSearchSettings(() => props.getDecentralizedSearchSettings)
setSetDecentralizedSearchSettings(() => props.setDecentralizedSearchSettings)
setQuickMode(props.quickMode ?? false)
setTab(props.tab ?? ApplicationSettingTabs.pluginSwitch)
setFocusPluginID(props.focusPluginID)
Expand All @@ -82,8 +75,6 @@ export const ApplicationBoardModal = forwardRef<
personaPerSiteConnectStatusLoading={personaPerSiteConnectStatusLoading}
openDashboard={openDashboard}
setPluginMinimalModeEnabled={setPluginMinimalModeEnabled}
getDecentralizedSearchSettings={getDecentralizedSearchSettings}
setDecentralizedSearchSettings={setDecentralizedSearchSettings}
onClose={() => dispatch?.close()}
quickMode={quickMode}
focusPluginID={focusPluginID}
Expand All @@ -95,8 +86,6 @@ export const ApplicationBoardModal = forwardRef<
export interface ApplicationBoardSettingsModalOpenProps {
focusPluginID?: PluginID
setPluginMinimalModeEnabled?: (id: string, checked: boolean) => Promise<void>
getDecentralizedSearchSettings?: () => Promise<boolean>
setDecentralizedSearchSettings?: (checked: boolean) => Promise<void>
tab?: ApplicationSettingTabs
}

Expand All @@ -108,16 +97,11 @@ export const ApplicationBoardSettingsModal = forwardRef<
>((props, ref) => {
const [setPluginMinimalModeEnabled, setSetPluginMinimalModeEnabled] =
useState<(id: string, checked: boolean) => Promise<void>>()
const [getDecentralizedSearchSettings, setGetDecentralizedSearchSettings] = useState<() => Promise<boolean>>()
const [setDecentralizedSearchSettings, setSetDecentralizedSearchSettings] =
useState<(checked: boolean) => Promise<void>>()
const [focusPluginID, setFocusPluginID] = useState<PluginID>()
const [tab, setTab] = useState<ApplicationSettingTabs | undefined>(ApplicationSettingTabs.pluginSwitch)
const [open, dispatch] = useSingletonModal(ref, {
onOpen(props) {
setSetPluginMinimalModeEnabled(() => props.setPluginMinimalModeEnabled)
setGetDecentralizedSearchSettings(() => props.getDecentralizedSearchSettings)
setSetDecentralizedSearchSettings(() => props.setDecentralizedSearchSettings)
setFocusPluginID(props.focusPluginID)
setTab(props.tab)
},
Expand All @@ -128,8 +112,6 @@ export const ApplicationBoardSettingsModal = forwardRef<
<ApplicationBoardSettingsDialog
open
setPluginMinimalModeEnabled={setPluginMinimalModeEnabled}
getDecentralizedSearchSettings={getDecentralizedSearchSettings}
setDecentralizedSearchSettings={setDecentralizedSearchSettings}
onClose={() => dispatch?.close()}
focusPluginID={focusPluginID}
tab={tab}
Expand Down

0 comments on commit 2cd09da

Please sign in to comment.