-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathNetworkSwitcher.tsx
223 lines (210 loc) · 6.99 KB
/
NetworkSwitcher.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { ChainId } from '@pancakeswap/chains'
import { useTranslation } from '@pancakeswap/localization'
import { NATIVE } from '@pancakeswap/sdk'
import {
ArrowDownIcon,
ArrowUpIcon,
Box,
Button,
Flex,
InfoIcon,
Text,
UserMenu,
UserMenuDivider,
UserMenuItem,
useTooltip,
} from '@pancakeswap/uikit'
import { ASSET_CDN } from 'config/constants/endpoints'
import { useActiveChainId, useLocalNetworkChain } from 'hooks/useActiveChainId'
import { useHover } from 'hooks/useHover'
import { useSwitchNetwork } from 'hooks/useSwitchNetwork'
import Image from 'next/image'
import { useRouter } from 'next/router'
import { useMemo } from 'react'
import { useUserShowTestnet } from 'state/user/hooks/useUserShowTestnet'
import { chainNameConverter } from 'utils/chainNameConverter'
import { chains } from 'utils/wagmi'
import { useAccount } from 'wagmi'
import { ChainLogo } from './Logo/ChainLogo'
const AptosChain = {
id: 1,
name: 'Aptos',
}
const NetworkSelect = ({ switchNetwork, chainId, isWrongNetwork }) => {
const { t } = useTranslation()
const [showTestnet] = useUserShowTestnet()
return (
<>
<Box px="16px" py="8px">
<Text color="textSubtle">{t('Select a Network')}</Text>
</Box>
<UserMenuDivider />
{chains
.filter((chain) => {
if (chain.id === chainId) return true
if ('testnet' in chain && chain.testnet) {
return showTestnet
}
return true
})
.map((chain) => (
<UserMenuItem
key={chain.id}
style={{ justifyContent: 'flex-start' }}
onClick={() => (chain.id !== chainId || isWrongNetwork) && switchNetwork(chain.id)}
>
<ChainLogo chainId={chain.id} />
<Text
color={chain.id === chainId && !isWrongNetwork ? 'secondary' : 'text'}
bold={chain.id === chainId && !isWrongNetwork}
pl="12px"
>
{chainNameConverter(chain.name)}
</Text>
</UserMenuItem>
))}
<UserMenuItem
key={`aptos-${AptosChain.id}`}
style={{ justifyContent: 'flex-start' }}
as="a"
target="_blank"
href="https://aptos.pancakeswap.finance/swap"
>
<Image
src="https://aptos.pancakeswap.finance/images/apt.png"
width={24}
height={24}
unoptimized
alt={`chain-aptos-${AptosChain.id}`}
/>{' '}
<Text color="text" pl="12px">
{AptosChain.name}
</Text>
</UserMenuItem>
</>
)
}
const WrongNetworkSelect = ({ switchNetwork, chainId }) => {
const { t } = useTranslation()
const { targetRef, tooltip, tooltipVisible } = useTooltip(
t(
'The URL you are accessing (Chain id: %chainId%) belongs to %network%; mismatching your wallet’s network. Please switch the network to continue.',
{
chainId,
network: chains.find((c) => c.id === chainId)?.name ?? 'Unknown network',
},
),
{
placement: 'auto-start',
hideTimeout: 0,
},
)
const { chain } = useAccount()
const localChainId = useLocalNetworkChain() || ChainId.BSC
const localChainName = chains.find((c) => c.id === localChainId)?.name ?? 'BSC'
const [ref1, isHover] = useHover<HTMLButtonElement>()
return (
<>
<Flex ref={targetRef} alignItems="center" px="16px" py="8px">
<InfoIcon color="textSubtle" />
<Text color="textSubtle" pl="6px">
{t('Please switch network')}
</Text>
</Flex>
{tooltipVisible && tooltip}
<UserMenuDivider />
{chain && (
<UserMenuItem ref={ref1} style={{ justifyContent: 'flex-start' }}>
<ChainLogo chainId={chain.id} />
<Text color="secondary" bold pl="12px">
{chainNameConverter(chain.name)}
</Text>
</UserMenuItem>
)}
<Box px="16px" pt="8px">
{isHover ? <ArrowUpIcon color="text" /> : <ArrowDownIcon color="text" />}
</Box>
<UserMenuItem onClick={() => switchNetwork(localChainId)} style={{ justifyContent: 'flex-start' }}>
<ChainLogo chainId={localChainId} />
<Text pl="12px">{chainNameConverter(localChainName)}</Text>
</UserMenuItem>
<Button mx="16px" my="8px" scale="sm" onClick={() => switchNetwork(localChainId)}>
{t('Switch network in wallet')}
</Button>
</>
)
}
const SHORT_SYMBOL = {
[ChainId.ETHEREUM]: 'ETH',
[ChainId.BSC]: 'BNB',
[ChainId.BSC_TESTNET]: 'tBNB',
[ChainId.GOERLI]: 'GOR',
[ChainId.ARBITRUM_ONE]: 'ARB',
[ChainId.ARBITRUM_GOERLI]: 'tARB',
[ChainId.POLYGON_ZKEVM]: 'Polygon zkEVM',
[ChainId.POLYGON_ZKEVM_TESTNET]: 'tZkEVM',
[ChainId.ZKSYNC]: 'zkSync',
[ChainId.ZKSYNC_TESTNET]: 'tZkSync',
[ChainId.LINEA]: 'Linea',
[ChainId.LINEA_TESTNET]: 'tLinea',
[ChainId.OPBNB]: 'opBNB',
[ChainId.OPBNB_TESTNET]: 'tOpBNB',
[ChainId.BASE]: 'Base',
[ChainId.BASE_TESTNET]: 'tBase',
[ChainId.SCROLL_SEPOLIA]: 'tScroll',
[ChainId.SEPOLIA]: 'sepolia',
[ChainId.BASE_SEPOLIA]: 'Base Sepolia',
[ChainId.ARBITRUM_SEPOLIA]: 'Arb Sepolia',
} as const satisfies Record<ChainId, string>
export const NetworkSwitcher = () => {
const { t } = useTranslation()
const { chainId, isWrongNetwork, isNotMatched } = useActiveChainId()
const { isLoading, canSwitch, switchNetworkAsync } = useSwitchNetwork()
const router = useRouter()
const foundChain = useMemo(() => chains.find((c) => c.id === chainId), [chainId])
const symbol =
(foundChain?.id ? SHORT_SYMBOL[foundChain.id] ?? NATIVE[foundChain.id]?.symbol : undefined) ??
foundChain?.nativeCurrency?.symbol
const { targetRef, tooltip, tooltipVisible } = useTooltip(
t('Unable to switch network. Please try it on your wallet'),
{ placement: 'bottom' },
)
const cannotChangeNetwork = !canSwitch
if (!chainId || router.pathname.includes('/info')) {
return null
}
return (
<Box ref={cannotChangeNetwork ? targetRef : null} height="100%">
{cannotChangeNetwork && tooltipVisible && tooltip}
<UserMenu
mr="8px"
placement="bottom"
variant={isLoading ? 'pending' : isWrongNetwork ? 'danger' : 'default'}
avatarSrc={`${ASSET_CDN}/web/chains/${chainId}.png`}
disabled={cannotChangeNetwork}
text={
isLoading ? (
t('Requesting')
) : isWrongNetwork ? (
t('Network')
) : foundChain ? (
<>
<Box display={['none', null, null, null, null, null, 'block']}>{chainNameConverter(foundChain.name)}</Box>
<Box display={['block', null, null, null, null, null, 'none']}>{symbol}</Box>
</>
) : (
t('Select a Network')
)
}
>
{() =>
isNotMatched ? (
<WrongNetworkSelect switchNetwork={switchNetworkAsync} chainId={chainId} />
) : (
<NetworkSelect switchNetwork={switchNetworkAsync} chainId={chainId} isWrongNetwork={isWrongNetwork} />
)
}
</UserMenu>
</Box>
)
}