Skip to content

Commit

Permalink
fix: deconstruct undefined (#11158)
Browse files Browse the repository at this point in the history
* chore: bump version to 2.23.12

* chore: context accessors

* refactor: remove dup hooks

* fix: type errors
  • Loading branch information
guanbinrui authored Nov 22, 2023
1 parent 997ce51 commit 17b7ef8
Show file tree
Hide file tree
Showing 53 changed files with 181 additions and 193 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"yarn": ">=999.0.0",
"npm": ">=999.0.0"
},
"version": "2.23.11",
"version": "2.23.12",
"private": true,
"license": "AGPL-3.0-or-later",
"scripts": {
Expand Down
8 changes: 4 additions & 4 deletions packages/mask/src/extension/popups/pages/Swap/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useMemo } from 'react'
import { SiteAdaptorContextRef } from '@masknet/plugin-infra/dom'
import { AllProviderTradeContext } from '@masknet/plugin-trader'
import { Typography } from '@mui/material'
import { Appearance } from '@masknet/public-api'
import { SharedContextProvider, SwapPageModals } from '@masknet/shared'
import { openWindow } from '@masknet/shared-base-ui'
import { SharedContextProvider, SwapPageModals } from '@masknet/shared'
import { applyMaskColorVars, makeStyles } from '@masknet/theme'
import { ChainContextProvider, DefaultWeb3ContextProvider } from '@masknet/web3-hooks-base'
import { Typography } from '@mui/material'
import { useEffect, useMemo } from 'react'
import { TwitterAdaptor } from '../../../../../shared/site-adaptors/implementations/twitter.com.js'
import { useI18N } from '../../../../utils/index.js'
import { NetworkSelector } from '../../components/NetworkSelector/index.js'
Expand Down Expand Up @@ -69,7 +69,7 @@ export default function SwapPage() {

useEffect(() => {
SiteAdaptorContextRef.value = {
...SiteAdaptorContextRef.value,
...SiteAdaptorContextRef.value!,
share(text) {
const url = TwitterAdaptor.getShareLinkURL!(text)
const width = 700
Expand Down
2 changes: 1 addition & 1 deletion packages/mask/src/site-adaptor-infra/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export async function activateSiteAdaptorUIInner(ui_deferred: SiteAdaptorUI.Defe
Services.Settings.setPluginMinimalModeEnabled(id, enabled)
},
...createPartialSharedUIContext(id, def, signal),
...SiteAdaptorContextRef.value,
...SiteAdaptorContextRef.value!,
}
},
Services.Settings.getPluginMinimalModeEnabled,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-infra/src/dom/useSiteAdaptorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useValueRef } from '@masknet/shared-base-ui'
import type { Plugin } from '../types.js'

export const SiteAdaptorContextRef = new ValueRefWithReady<
Omit<Plugin.SiteAdaptor.SiteAdaptorContext, 'createKVStorage' | 'setWeb3State' | 'setMinimalMode'>
Omit<Plugin.SiteAdaptor.SiteAdaptorContext, 'createKVStorage' | 'setWeb3State' | 'setMinimalMode'> | undefined
>()

export function useSiteAdaptorContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { allPersonas } from '../dom/context.js'

export function useCurrentPersonaInformation() {
const { currentPersona } = useSiteAdaptorContext()
const current = useSubscription(currentPersona ?? UNDEFINED)
const context = useSiteAdaptorContext()
const current = useSubscription(context?.currentPersona ?? UNDEFINED)
const personas = useSubscription(allPersonas)
return personas?.find((x) => x.identifier.rawPublicKey === current?.rawPublicKey)
}
6 changes: 3 additions & 3 deletions packages/plugin-infra/src/site-adaptor/usePostInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { usePostInfoDetails } from './PostContext.js'
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'

export function usePostLink() {
const { getPostURL } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const id = usePostInfoDetails.postID()
const identifier = usePostInfoDetails.identifier()
return useMemo(() => {
if (!id || !identifier) return ''
return getPostURL?.(identifier) ?? ''
}, [id, identifier, getPostURL])
return context?.getPostURL?.(identifier) ?? ''
}, [id, identifier, context?.getPostURL])
}
7 changes: 3 additions & 4 deletions packages/plugin-infra/src/site-adaptor/useSocialIdentity.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { useEffect } from 'react'
import { MaskMessages, type SocialIdentity } from '@masknet/shared-base'
import { useQuery } from '@tanstack/react-query'
import type { IdentityResolved } from '../types.js'
import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { useQuery } from '@tanstack/react-query'
import { currentNextIDPlatform } from './context.js'

/**
* Get the social identity of the given identity
*/
export function useSocialIdentity(identity: IdentityResolved | null | undefined) {
const { getSocialIdentity } = useSiteAdaptorContext()

const context = useSiteAdaptorContext()
const result = useQuery<SocialIdentity | undefined, Error>({
queryKey: ['next-id', identity, currentNextIDPlatform],
enabled: Boolean(identity && currentNextIDPlatform),
queryFn: async () => {
if (!currentNextIDPlatform || !identity) return
return getSocialIdentity(currentNextIDPlatform, identity)
return context?.getSocialIdentity(currentNextIDPlatform, identity)
},
refetchOnWindowFocus: false,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { useSiteAdaptorContext } from '../dom/useSiteAdaptorContext.js'
import { useSocialIdentity } from './index.js'

export function useSocialIdentityByUserId(userId?: string) {
const { getUserIdentity } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const { value: identity } = useAsync(async () => {
if (!userId) return
return getUserIdentity?.(userId)
}, [userId, getUserIdentity])
return context?.getUserIdentity?.(userId)
}, [userId, context?.getUserIdentity])
return useSocialIdentity(identity)
}
4 changes: 2 additions & 2 deletions packages/plugins/ArtBlocks/src/SiteAdaptor/PurchaseDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function PurchaseDialog(props: ActionBarProps) {
const t = useI18N()
const { classes } = useStyles()
const { project, open, onClose, chainId } = props
const { share } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()

const {
token: { data: token },
Expand Down Expand Up @@ -94,7 +94,7 @@ export function PurchaseDialog(props: ActionBarProps) {
await openShareTxDialog({
hash,
onShare() {
share?.(shareText)
context?.share?.(shareText)
},
})
onClose()
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/Avatar/src/Application/NFTListDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function NFTListDialog() {
const [disabled, setDisabled] = useState(false)
const [pendingTokenCount, setPendingTokenCount] = useState(0)
const [tokens, setTokens] = useState<AllChainsNonFungibleToken[]>([])
const { openPopupWindow } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const targetWallet = wallets.find((x) => isSameAddress(targetAccount, x.address))

useEffect(() => setSelectedToken(undefined), [chainId])
Expand Down Expand Up @@ -265,7 +265,7 @@ export function NFTListDialog() {

<PluginVerifiedWalletStatusBar
openPopupWindow={() =>
openPopupWindow(PopupRoutes.Personas, {
context?.openPopupWindow(PopupRoutes.Personas, {
tab: PopupHomeTabType.ConnectedWallets,
})
}
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/Avatar/src/Application/PersonaPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useCallback, useMemo, useState } from 'react'
import { useAsyncRetry } from 'react-use'
import { useNavigate } from 'react-router-dom'
import { useSubscription } from 'use-subscription'
import { type BindingProof, EMPTY_LIST, NextIDPlatform, type PersonaInformation } from '@masknet/shared-base'
import { type BindingProof, EMPTY_LIST, NextIDPlatform, type PersonaInformation, UNDEFINED } from '@masknet/shared-base'
import { LoadingBase } from '@masknet/theme'
import { DialogActions, DialogContent, Stack } from '@mui/material'
import { useI18N } from '../locales/index.js'
Expand All @@ -28,9 +28,9 @@ export function PersonaPage() {
const network = socialIdentity?.identifier?.network.replace('.com', '')
const userId = socialIdentity?.identifier?.userId

const { currentPersona: currentPersona_ } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const myPersonas = useAllPersonas()
const _persona = useSubscription(currentPersona_)
const _persona = useSubscription(context?.currentPersona ?? UNDEFINED)
const currentPersona = myPersonas?.find(
(x: PersonaInformation) => x.identifier.rawPublicKey.toLowerCase() === _persona?.rawPublicKey.toLowerCase(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const AvatarManagementContext = createContext<AvatarManagementContextOpti
interface Props extends PropsWithChildren<{ socialIdentity?: SocialIdentity }> {}

export const AvatarManagementProvider = memo(({ children }: Props) => {
const { queryPersonaByProfile } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const identity = useLastRecognizedIdentity()

const [proof, setProof] = useState<BindingProof>()
Expand All @@ -82,7 +82,7 @@ export const AvatarManagementProvider = memo(({ children }: Props) => {
currentNextIDPlatform,
identity.identifier.userId.toLowerCase(),
)
const linkedPersona = await queryPersonaByProfile(identity.identifier)
const linkedPersona = await context?.queryPersonaByProfile(identity.identifier)
const personaBindings = bindings.filter(
(x) => x.persona === linkedPersona?.identifier.publicKeyAsHex.toLowerCase(),
)
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/Avatar/src/hooks/usePersonaVerified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import { currentNextIDPlatform, lastRecognizedProfile } from '@masknet/plugin-in
import { NextIDPlatform } from '@masknet/shared-base'

export function usePersonaVerify() {
const { queryPersonaByProfile } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const visitingPersonaIdentifier = useSubscription(lastRecognizedProfile)
return useAsyncRetry(async () => {
if (!visitingPersonaIdentifier?.identifier) return
const persona = await queryPersonaByProfile(visitingPersonaIdentifier.identifier)
const persona = await context?.queryPersonaByProfile(visitingPersonaIdentifier.identifier)
if (!persona?.identifier.publicKeyAsHex) return
const isVerified = await NextIDProof.queryIsBound(
persona.identifier.publicKeyAsHex,
currentNextIDPlatform ?? NextIDPlatform.Twitter,
visitingPersonaIdentifier.identifier.userId,
)
return { isVerified }
}, [visitingPersonaIdentifier, queryPersonaByProfile])
}, [visitingPersonaIdentifier, context?.queryPersonaByProfile])
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,14 @@ export function ClaimSuccessDialog({ open, onClose, amount, tokenAddress }: Prop
const t = useI18N()
const { classes } = useStyles()

const { share } = useSiteAdaptorContext()

const context = useSiteAdaptorContext()
const { data: tokenDetail } = useFungibleToken(NetworkPluginID.PLUGIN_EVM, tokenAddress)

const onShare = useCallback(() => {
if (!amount || !tokenDetail) return

share?.(t.share_text({ amount, symbol: tokenDetail.symbol }))
}, [share, amount, tokenDetail?.symbol])
context?.share?.(t.share_text({ amount, symbol: tokenDetail.symbol }))
}, [context?.share, amount, tokenDetail?.symbol])

return usePortalShadowRoot((container) => (
<Dialog container={container} open={open} onClose={onClose} classes={{ paper: classes.paper }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function CardDialogContent(props: CardDialogContentProps) {
} = Context.useContainer()
const currentVisitingIdentity = useCurrentVisitingIdentity()
const lastRecognized = useLastRecognizedIdentity()
const { openDashboard } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const currentIdentifier = useSubscription(currentPersonaIdentifier)
const personas = useAllPersonas()
const onBeforeAction = useCallback(() => {
Expand Down Expand Up @@ -149,7 +149,7 @@ export function CardDialogContent(props: CardDialogContentProps) {
personas={personas}
identity={lastRecognized}
currentPersonaIdentifier={currentIdentifier}
openDashboard={openDashboard}
openDashboard={context?.openDashboard}
handlerPosition="top-right"
customHint
directTo={PluginID.Avatar}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/External/src/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { useSiteAdaptorContext } from '@masknet/plugin-infra/dom'

// TODO: support suspense
export function useExternalPluginManifest(url: string) {
const { fetchManifest } = useSiteAdaptorContext()
return useAsyncRetry(async () => fetchManifest?.(url), [url, fetchManifest])
const context = useSiteAdaptorContext()
return useAsyncRetry(async () => context?.fetchManifest?.(url), [url, context?.fetchManifest])
}

export function useExternalPluginTemplate(url: string, manifest: Manifest | undefined, metaKey: string) {
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/Game/src/SiteAdaptor/GameShareDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ interface PetSetDialogProps {
export default function GameShareDialog({ onClose, gameInfo }: PetSetDialogProps) {
const t = useI18N()
const { classes } = useStyles()
const { share } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()

const onShareClick = useCallback(() => {
share?.(
context?.share?.(
t.game_share_text({
name: gameInfo?.name ?? '',
snsId: gameInfo?.snsId ?? '',
share_Twitter: Share_Twitter,
}),
)
onClose()
}, [gameInfo?.name, gameInfo?.snsId, onClose, share])
}, [gameInfo?.name, gameInfo?.snsId, onClose, context?.share])

return (
<Box className={classes.root}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const DonateDialog = memo(({ grant, ...rest }: DonateDialogProps) => {
const t = useI18N()
const { classes, theme } = useStyles()
const { title, admin_address: address, tenants } = grant
const { share } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()

const availableChains = useMemo(() => getSupportedChainIds(tenants), [tenants])
const { account, chainId, setChainId } = useChainContext<NetworkPluginID.PLUGIN_EVM>()
Expand Down Expand Up @@ -147,7 +147,7 @@ export const DonateDialog = memo(({ grant, ...rest }: DonateDialogProps) => {
token,
uiAmount,
onShare() {
share?.(shareText)
context?.share?.(shareText)
},
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export interface DrawResultDialogProps {
export function DrawResultDialog(props: DrawResultDialogProps) {
const { open, onClose, boxInfo, contractDetailed } = props
const { classes } = useStyles()
const { share } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()
const { lastPurchasedTokenIds } = useContainer(Context)

const postLink = usePostLink()
const shareText = `I just claimed a #MaskBox with @realMaskNetwork. Install mask.io and create your own NFT mystery box! \n ${postLink}`

const onShare = () => {
onClose()
share?.(shareText)
context?.share?.(shareText)
}

if (!contractDetailed) return null
Expand Down
12 changes: 6 additions & 6 deletions packages/plugins/NextID/src/components/NextIdPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export const NextIdPage = memo(function NextIdPage() {
const visitingPersonaIdentifier = useCurrentVisitingIdentity()
const allPersonas = useAllPersonas()
const currentIdentifier = useValueRef(currentPersonaIdentifier)
const { openDashboard, queryPersonaByProfile, openPopupWindow } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()

const { value: personaConnectStatus, loading: statusLoading } = useCurrentPersonaConnectStatus(
allPersonas,
currentIdentifier,
openDashboard,
context?.openDashboard,
currentProfileIdentifier,
)

Expand All @@ -46,16 +46,16 @@ export const NextIdPage = memo(function NextIdPage() {

const { value: currentPersona, loading: loadingPersona } = useAsyncRetry(async () => {
if (!visitingPersonaIdentifier?.identifier) return
return queryPersonaByProfile?.(visitingPersonaIdentifier.identifier)
}, [visitingPersonaIdentifier?.identifier, personaConnectStatus.hasPersona, queryPersonaByProfile])
return context?.queryPersonaByProfile?.(visitingPersonaIdentifier.identifier)
}, [visitingPersonaIdentifier?.identifier, personaConnectStatus.hasPersona, context?.queryPersonaByProfile])
const publicKeyAsHex = currentPersona?.identifier.publicKeyAsHex
const proofs = usePersonaProofs(publicKeyAsHex)

const handleAddWallets = useCallback(() => {
openPopupWindow?.(PopupRoutes.Personas, {
context?.openPopupWindow?.(PopupRoutes.Personas, {
tab: PopupHomeTabType.ConnectedWallets,
})
}, [openPopupWindow])
}, [context?.openPopupWindow])

const ActionComponent = useMemo(() => {
if (!isOwn) return <OtherLackWalletAction />
Expand Down
6 changes: 3 additions & 3 deletions packages/plugins/NextID/src/hooks/usePersonaSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { type ECKeyIdentifier, SignType } from '@masknet/shared-base'
import { useSiteAdaptorContext } from '@masknet/plugin-infra/dom'

export function usePersonaSign(message?: string, currentIdentifier?: ECKeyIdentifier) {
const { signWithPersona } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()

return useAsyncFn(async () => {
if (!message || !currentIdentifier) return
try {
return await signWithPersona?.(SignType.Message, message, currentIdentifier)
return await context?.signWithPersona?.(SignType.Message, message, currentIdentifier)
} catch {
return
}
}, [message, currentIdentifier, signWithPersona])
}, [message, currentIdentifier, context?.signWithPersona])
}
6 changes: 3 additions & 3 deletions packages/plugins/Pets/src/SiteAdaptor/PetShareDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ interface PetSetDialogProps {
export function PetShareDialog({ onClose }: PetSetDialogProps) {
const t = useI18N()
const { classes } = useStyles()
const { share } = useSiteAdaptorContext()
const context = useSiteAdaptorContext()

const onShareClick = useCallback(() => {
share?.(t.share_twitter())
context?.share?.(t.share_twitter())
onClose()
}, [onClose, share])
}, [onClose, context?.share])

return (
<Box className={classes.root}>
Expand Down
Loading

0 comments on commit 17b7ef8

Please sign in to comment.