-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathConnectWallet.tsx
64 lines (57 loc) · 2.03 KB
/
ConnectWallet.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
import { useChain, useManager } from '@cosmos-kit/react-lite'
import { useTranslation } from 'react-i18next'
import { useRecoilValue } from 'recoil'
import { walletChainIdAtom } from '@dao-dao/state'
import {
ConnectWallet as StatelessConnectWallet,
Tooltip,
useChainContextIfAvailable,
} from '@dao-dao/stateless'
import { StatefulConnectWalletProps } from '@dao-dao/types'
import { getSupportedChains, maybeGetChainForChainId } from '@dao-dao/utils'
export const ConnectWallet = (props: StatefulConnectWalletProps) => {
const { t } = useTranslation()
const { chain: { chainName: currentChainName } = { chainName: undefined } } =
useChainContextIfAvailable() ?? {}
const chainName = currentChainName || getSupportedChains()[0].chain.chainName
const { getWalletRepo } = useManager()
// Chain of main wallet connection.
const mainWalletChainId = useRecoilValue(walletChainIdAtom)
// Get main wallet connection.
const mainWallet = getWalletRepo(
maybeGetChainForChainId(mainWalletChainId)?.chainName || chainName
)?.current
const {
walletRepo,
disconnect,
isWalletConnecting,
chain: connectingToChain,
} = useChain(chainName, false)
return (
<Tooltip
title={isWalletConnecting ? t('button.stopConnecting') : undefined}
>
<StatelessConnectWallet
allowClickWhileLoading
loading={isWalletConnecting}
onConnect={() =>
isWalletConnecting
? disconnect()
: // If connecting to a chain other than the main wallet connection,
// auto-select the main wallet if it exists. Otherwise, allow
// wallet modal to show up by passing undefined to
// walletRepo.connect.
walletRepo.connect(
connectingToChain.chain_id !== mainWalletChainId &&
mainWallet?.isWalletConnected
? mainWallet.walletName
: undefined,
false
)
}
variant="brand"
{...props}
/>
</Tooltip>
)
}