Skip to content

Commit db08d05

Browse files
committed
perf: Use chainid hook
1 parent b4d52dc commit db08d05

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { isEvm } from '@pancakeswap/chains'
21
import { useAtomValue } from 'jotai'
32
import { useEffect, useRef } from 'react'
43

54
import { accountActiveChainAtom } from 'wallet/atoms/accountStateAtoms'
65

76
export const useActiveChainId = (checkChainId?: number) => {
8-
const { isNotMatched, isWrongNetwork, chainId } = useAccountActiveChain()
7+
const result = useAtomValue(accountActiveChainAtom)
8+
9+
if (!checkChainId) return result
10+
11+
const { chainId, isWrongNetwork } = result
912
return {
10-
chainId,
11-
isNotMatched,
12-
isWrongNetwork: checkChainId ? isWrongNetwork && checkChainId !== chainId : isWrongNetwork,
13+
...result,
14+
isWrongNetwork: isWrongNetwork && checkChainId !== chainId,
1315
}
1416
}
1517

@@ -24,10 +26,7 @@ export const useActiveChainIdRef = () => {
2426
}
2527

2628
export const useAccountActiveChain = () => {
27-
const result = useAtomValue(accountActiveChainAtom)
28-
const { chainId, account, solanaAccount } = result
29-
const unifiedAccount = isEvm(chainId) ? account : solanaAccount
30-
return { ...result, unifiedAccount }
29+
return useAtomValue(accountActiveChainAtom)
3130
}
3231

3332
export default useAccountActiveChain

apps/web/src/wallet/atoms/accountStateAtoms.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChainId } from '@pancakeswap/chains'
1+
import { ChainId, isEvm } from '@pancakeswap/chains'
22
import { atom } from 'jotai'
33
import { getHashKey } from 'utils/hash'
44
import { Connector } from 'wagmi'
@@ -7,6 +7,7 @@ import { getQueryChainId } from 'wallet/util/getQueryChainId'
77
export interface AccountChainState {
88
account?: `0x${string}`
99
solanaAccount?: string | null
10+
unifiedAccount?: string | null
1011
chainId: number
1112
isWrongNetwork: boolean
1213
isNotMatched: boolean
@@ -20,14 +21,18 @@ const _accountActiveChainAtom = atom<AccountChainState>({
2021
isWrongNetwork: false,
2122
status: null,
2223
solanaAccount: null,
24+
unifiedAccount: null,
2325
isNotMatched: false,
2426
})
2527

2628
type Updater = AccountChainState | ((prev: AccountChainState) => AccountChainState)
2729

2830
export const accountActiveChainAtom = atom(
2931
(get) => {
30-
return get(_accountActiveChainAtom)
32+
const state = get(_accountActiveChainAtom)
33+
const { chainId, account, solanaAccount } = state
34+
const unifiedAccount = isEvm(chainId) ? account : solanaAccount
35+
return { ...state, unifiedAccount }
3136
},
3237
(_get, set, updater: Updater) => {
3338
const prev = _get(_accountActiveChainAtom)

0 commit comments

Comments
 (0)