Skip to content

Commit 97b83d2

Browse files
authored
fix: align chain list (#12150)
* fix: enter to search * fix: align chain list
1 parent ca180a3 commit 97b83d2

File tree

7 files changed

+67
-32
lines changed

7 files changed

+67
-32
lines changed

packages/plugins/RedPacket/src/SiteAdaptor/views/SelectCollectibles.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { Trans } from '@lingui/react/macro'
2-
import { AddCollectiblesModal, CollectionList, UserAssetsProvider } from '@masknet/shared'
2+
import { AddCollectiblesModal, CollectionList, useAssetsNetworks, UserAssetsProvider } from '@masknet/shared'
33
import { NetworkPluginID } from '@masknet/shared-base'
44
import { makeStyles } from '@masknet/theme'
55
import type { Web3Helper } from '@masknet/web3-helpers'
66
import { useChainContext, useNetworkContext, useWeb3Connection, useWeb3Hub } from '@masknet/web3-hooks-base'
77
import { isSameAddress } from '@masknet/web3-shared-base'
88
import type { ChainId } from '@masknet/web3-shared-evm'
99
import { alpha, Box, Button, DialogActions } from '@mui/material'
10+
import { compact, uniqBy } from 'lodash-es'
1011
import { useCallback, useEffect, useMemo, useState } from 'react'
1112
import { useNavigate } from 'react-router-dom'
1213
import { NFT_DEFAULT_CHAINS, NFT_RED_PACKET_MAX_SHARES } from '../../constants.js'
1314
import { useRedPacket } from '../contexts/RedPacketContext.js'
1415
import { emitter } from '../emitter.js'
15-
import { compact, uniqBy } from 'lodash-es'
1616

1717
const useStyles = makeStyles()((theme) => ({
1818
container: {
@@ -48,6 +48,7 @@ export function SelectCollectibles() {
4848
const { pluginID } = useNetworkContext()
4949
const Web3 = useWeb3Connection(pluginID)
5050
const Hub = useWeb3Hub(pluginID)
51+
const networks = useAssetsNetworks(pluginID)
5152
const { selectedNfts, setSelectedNfts, setCollection } = useRedPacket()
5253
const [pendingNfts, setPendingNfts] = useState<Web3Helper.NonFungibleAssetAll[]>(selectedNfts)
5354
const handleItemClick = useCallback((nft: Web3Helper.NonFungibleAssetAll) => {
@@ -71,10 +72,14 @@ export function SelectCollectibles() {
7172
const [pendingTokenCount, setPendingTokenCount] = useState(0)
7273
const [tokens, setTokens] = useState<Web3Helper.NonFungibleAssetAll[]>([])
7374
const handleAddCollectibles = useCallback(async () => {
75+
const chainWhiteList = networks
76+
.filter((x) => NFT_DEFAULT_CHAINS.includes(x.chainId as ChainId))
77+
.map((x) => x.chainId)
7478
const results = await AddCollectiblesModal.openAndWaitForClose({
7579
pluginID,
7680
chainId: assetChainId || chainId,
7781
account,
82+
chainWhiteList,
7883
})
7984
if (!results) return
8085
const [contract, tokenIds] = results
@@ -108,7 +113,7 @@ export function SelectCollectibles() {
108113
setTokens((originalTokens) => {
109114
return uniqBy([...originalTokens, ...tokens], (x) => `${x.contract?.address}.${x.tokenId}`)
110115
})
111-
}, [pluginID, assetChainId, chainId, account])
116+
}, [pluginID, assetChainId, chainId, account, networks])
112117

113118
const handleSelect = useCallback((assets: Web3Helper.NonFungibleAssetAll[]) => {
114119
setPendingNfts(assets.length > NFT_RED_PACKET_MAX_SHARES ? assets.slice(0, NFT_RED_PACKET_MAX_SHARES) : assets)

packages/shared/src/UI/components/AssetsManagement/ChainRuntimeProvider.tsx

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { EMPTY_LIST, NetworkPluginID } from '@masknet/shared-base'
22
import type { Web3Helper } from '@masknet/web3-helpers'
3-
import { CHAIN_DESCRIPTORS, ChainId, type NetworkType, type SchemaType } from '@masknet/web3-shared-evm'
3+
import type { ReasonableNetwork } from '@masknet/web3-shared-base'
4+
import { type ChainId } from '@masknet/web3-shared-evm'
45
import { type ChainId as FlowChainId } from '@masknet/web3-shared-flow'
5-
import { noop, sortBy } from 'lodash-es'
66
import { type ChainId as SolanaChainId } from '@masknet/web3-shared-solana'
7-
import { useNetworks } from '@masknet/web3-hooks-base'
7+
import { noop } from 'lodash-es'
88
import {
99
createContext,
1010
memo,
11+
useContext,
1112
useMemo,
12-
type PropsWithChildren,
1313
useState,
14-
type SetStateAction,
1514
type Dispatch,
16-
useContext,
15+
type PropsWithChildren,
16+
type SetStateAction,
1717
} from 'react'
18-
import type { ReasonableNetwork } from '@masknet/web3-shared-base'
19-
import { SimpleHashSupportedChains } from '../../../constants.js'
18+
import { useAssetsNetworks } from './useAssetsNetworks.js'
2019

2120
interface ChainRuntimeOptions {
2221
pluginID: NetworkPluginID
@@ -46,21 +45,13 @@ export const ChainRuntimeProvider = memo<PropsWithChildren<ChainRuntimeProviderP
4645
children,
4746
}) {
4847
const [chainId, setChainId] = useState<Web3Helper.ChainIdAll>()
49-
const allNetworks = useNetworks(pluginID, true)
48+
const assetsNetworks = useAssetsNetworks(pluginID)
5049

5150
const networks = useMemo(() => {
52-
const supported = SimpleHashSupportedChains[pluginID]
53-
const networks = allNetworks.filter(
54-
(x) => (x.network === 'mainnet' || x.isCustomized) && supported.includes(x.chainId),
55-
)
56-
// hard-coded for Zora
57-
if (pluginID === NetworkPluginID.PLUGIN_EVM) {
58-
const zora = CHAIN_DESCRIPTORS.find((x) => x.chainId === ChainId.Zora)
59-
if (zora) networks.push(zora as ReasonableNetwork<ChainId, SchemaType, NetworkType>)
60-
}
61-
const list = sortBy(networks, (x) => supported.indexOf(x.chainId))
62-
return chainWhiteList?.length ? list.filter((x) => chainWhiteList.includes(x.chainId as ChainId)) : list
63-
}, [allNetworks, pluginID, chainWhiteList])
51+
return chainWhiteList?.length ?
52+
assetsNetworks.filter((x) => chainWhiteList.includes(x.chainId as ChainId))
53+
: assetsNetworks
54+
}, [chainWhiteList, assetsNetworks])
6455

6556
const currentChainId = chainId ?? defaultChainId ?? (networks.length === 1 ? networks[0].chainId : chainId)
6657

packages/shared/src/UI/components/AssetsManagement/CollectionHeader.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ export const CollectionHeader = memo(function CollectionHeader({
157157
autoFocus
158158
fullWidth
159159
value={pendingKeyword}
160-
onKeyDown={(event: KeyboardEvent<HTMLInputElement>) => {
161-
if (event.key !== 'Enter') return
162-
setSearchKeyword(event.currentTarget.value)
163-
}}
164160
onChange={(event) => setPendingKeyword(event.target.value)}
165161
InputProps={{
166162
startAdornment: <Icons.Search size={16} />,
163+
onKeyDown: (event: KeyboardEvent<HTMLInputElement>) => {
164+
if (event.key !== 'Enter') return
165+
setSearchKeyword(pendingKeyword)
166+
},
167167
}}
168168
/>
169169
<Button

packages/shared/src/UI/components/AssetsManagement/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export type * from './types.js'
22
export * from './CollectionList.js'
33
export * from './UserAssetsContext.js'
44
export * from './CollectibleCard.js'
5+
export * from './useAssetsNetworks.js'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { SimpleHashSupportedChains } from '@masknet/shared'
2+
import { NetworkPluginID } from '@masknet/shared-base'
3+
import { useNetworks } from '@masknet/web3-hooks-base'
4+
import type { ReasonableNetwork } from '@masknet/web3-shared-base'
5+
import { CHAIN_DESCRIPTORS, ChainId, type SchemaType, type NetworkType } from '@masknet/web3-shared-evm'
6+
import { sortBy } from 'lodash-es'
7+
import { useMemo } from 'react'
8+
9+
export function useAssetsNetworks(pluginID: NetworkPluginID) {
10+
const allNetworks = useNetworks(pluginID, true)
11+
const networks = useMemo(() => {
12+
const supported = SimpleHashSupportedChains[pluginID]
13+
const networks = allNetworks.filter(
14+
(x) => (x.network === 'mainnet' || x.isCustomized) && supported.includes(x.chainId),
15+
)
16+
// hard-coded for Zora
17+
if (pluginID === NetworkPluginID.PLUGIN_EVM) {
18+
const zora = CHAIN_DESCRIPTORS.find((x) => x.chainId === ChainId.Zora)
19+
if (zora) networks.push(zora as ReasonableNetwork<ChainId, SchemaType, NetworkType>)
20+
}
21+
const list = sortBy(networks, (x) => supported.indexOf(x.chainId))
22+
return list
23+
}, [allNetworks, pluginID])
24+
25+
return networks
26+
}

packages/shared/src/UI/modals/AddCollectiblesModal/AddCollectiblesDialog.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { Trans } from '@lingui/react/macro'
22
import { type NetworkPluginID } from '@masknet/shared-base'
33
import { makeStyles } from '@masknet/theme'
44
import { useNetworks } from '@masknet/web3-hooks-base'
5+
import type { ChainId } from '@masknet/web3-shared-evm'
56
import { DialogContent } from '@mui/material'
6-
import { memo, useState } from 'react'
7+
import { memo, useMemo, useState } from 'react'
78
import { AddCollectibles, SelectNetworkSidebar, type AddCollectiblesProps } from '../../components/index.js'
89
import { InjectedDialog } from '../../contexts/components/index.js'
10+
import type { Web3Helper } from '@masknet/web3-helpers'
911

1012
const useStyles = makeStyles()((theme) => ({
1113
content: {
@@ -28,19 +30,25 @@ const useStyles = makeStyles()((theme) => ({
2830

2931
interface AddCollectiblesDialogProps<T extends NetworkPluginID = NetworkPluginID> extends AddCollectiblesProps<T> {
3032
open: boolean
33+
chainWhiteList?: Web3Helper.ChainIdAll[]
3134
}
3235

3336
export const AddCollectiblesDialog = memo(function AddCollectiblesDialog({
3437
open,
3538
pluginID,
3639
chainId: defaultChainId,
3740
account,
41+
chainWhiteList,
3842
onAdd,
3943
}: AddCollectiblesDialogProps) {
4044
const { classes } = useStyles()
4145

4246
const [chainId, setChainId] = useState(defaultChainId)
4347
const allNetworks = useNetworks(pluginID, true)
48+
const networks = useMemo(() => {
49+
if (!chainWhiteList?.length) return allNetworks
50+
return allNetworks.filter((x) => chainWhiteList.includes(x.chainId as ChainId))
51+
}, [allNetworks, chainWhiteList])
4452

4553
return (
4654
<InjectedDialog
@@ -54,7 +62,7 @@ export const AddCollectiblesDialog = memo(function AddCollectiblesDialog({
5462
chainId={chainId}
5563
onChainChange={setChainId}
5664
pluginID={pluginID}
57-
networks={allNetworks}
65+
networks={networks}
5866
hideAllButton
5967
/>
6068
<AddCollectibles

packages/shared/src/UI/modals/AddCollectiblesModal/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { useState } from 'react'
2-
import { NetworkPluginID, type SingletonModalProps } from '@masknet/shared-base'
1+
import { EMPTY_LIST, NetworkPluginID, type SingletonModalProps } from '@masknet/shared-base'
32
import { useSingletonModal } from '@masknet/shared-base-ui'
43
import type { Web3Helper } from '@masknet/web3-helpers'
54
import type { NonFungibleTokenContract } from '@masknet/web3-shared-base'
5+
import { useState } from 'react'
66
import { AddCollectiblesDialog } from './AddCollectiblesDialog.js'
77

88
export interface AddCollectiblesModalOpenProps {
@@ -13,6 +13,7 @@ export interface AddCollectiblesModalOpenProps {
1313
* For example, in PFP, we can add collectibles from verified wallets if no wallet connected.
1414
*/
1515
account?: string
16+
chainWhiteList?: Web3Helper.ChainIdAll[]
1617
}
1718

1819
export type AddCollectiblesModalCloseProps =
@@ -25,12 +26,14 @@ export function AddCollectiblesModal({
2526
const [pluginID, setPluginID] = useState<NetworkPluginID>(NetworkPluginID.PLUGIN_EVM)
2627
const [chainId, setChainId] = useState<Web3Helper.ChainIdAll>()
2728
const [account, setAccount] = useState<string>()
29+
const [chainWhiteList, setChainWhiteList] = useState<Web3Helper.ChainIdAll[]>(EMPTY_LIST)
2830

2931
const [open, dispatch] = useSingletonModal(ref, {
3032
onOpen(props) {
3133
setPluginID(props.pluginID ?? NetworkPluginID.PLUGIN_EVM)
3234
setChainId(props.chainId)
3335
setAccount(props.account)
36+
setChainWhiteList(props.chainWhiteList ?? EMPTY_LIST)
3437
},
3538
})
3639

@@ -42,6 +45,7 @@ export function AddCollectiblesModal({
4245
pluginID={pluginID}
4346
account={account}
4447
chainId={chainId}
48+
chainWhiteList={chainWhiteList}
4549
/>
4650
)
4751
}

0 commit comments

Comments
 (0)