From dbad22c74aff06520f39e3e78f34850a76fe637c Mon Sep 17 00:00:00 2001 From: tom Date: Wed, 6 Nov 2024 15:43:29 +0100 Subject: [PATCH] handle reconnecting state of web3wallet --- lib/web3/useWallet.ts | 12 +++++---- .../user/profile/UserProfileContentWallet.tsx | 27 ++++++++++--------- ui/snippets/user/wallet/UserWalletDesktop.tsx | 1 + .../user/wallet/UserWalletMenuContent.tsx | 27 ++++++++++--------- ui/snippets/user/wallet/UserWalletMobile.tsx | 1 + 5 files changed, 38 insertions(+), 30 deletions(-) diff --git a/lib/web3/useWallet.ts b/lib/web3/useWallet.ts index 6d95671fcd..951ccdf555 100644 --- a/lib/web3/useWallet.ts +++ b/lib/web3/useWallet.ts @@ -1,8 +1,9 @@ import { useWeb3Modal, useWeb3ModalState } from '@web3modal/wagmi/react'; import React from 'react'; -import { useAccount, useDisconnect, useAccountEffect } from 'wagmi'; +import { useDisconnect, useAccountEffect } from 'wagmi'; import * as mixpanel from 'lib/mixpanel/index'; +import useAccount from 'lib/web3/useAccount'; interface Params { source: mixpanel.EventPayload['Source']; @@ -44,16 +45,17 @@ export default function useWeb3Wallet({ source }: Params) { useAccountEffect({ onConnect: handleAccountConnected }); - const { address, isDisconnected } = useAccount(); - - const isConnected = isClientLoaded && !isDisconnected && address !== undefined; + const account = useAccount(); + const address = account.address; + const isConnected = isClientLoaded && !account.isDisconnected && account.address !== undefined; return React.useMemo(() => ({ connect: handleConnect, disconnect: handleDisconnect, isOpen: isOpening || isOpen, isConnected, + isReconnecting: account.isReconnecting, address, openModal, - }), [ handleConnect, handleDisconnect, isOpen, isOpening, isConnected, address, openModal ]); + }), [ handleConnect, handleDisconnect, isOpening, isOpen, isConnected, account.isReconnecting, address, openModal ]); } diff --git a/ui/snippets/user/profile/UserProfileContentWallet.tsx b/ui/snippets/user/profile/UserProfileContentWallet.tsx index 3899c38046..5177a978f1 100644 --- a/ui/snippets/user/profile/UserProfileContentWallet.tsx +++ b/ui/snippets/user/profile/UserProfileContentWallet.tsx @@ -1,4 +1,4 @@ -import { chakra, Box, Button, Flex, IconButton, useColorModeValue } from '@chakra-ui/react'; +import { chakra, Box, Button, Flex, IconButton, useColorModeValue, Spinner } from '@chakra-ui/react'; import React from 'react'; import delay from 'lib/delay'; @@ -34,7 +34,7 @@ const UserProfileContentWallet = ({ onClose, className }: Props) => { const content = (() => { if (web3Wallet.isConnected && web3AccountWithDomain.address) { return ( - + { fontWeight={ 500 } noAltHash /> - } - variant="simple" - color="icon_info" - boxSize={ 5 } - onClick={ handleOpenWalletClick } - isLoading={ web3Wallet.isOpen } - flexShrink={ 0 } - ml="auto" - /> + { web3Wallet.isReconnecting ? : ( + } + variant="simple" + color="icon_info" + boxSize={ 5 } + onClick={ handleOpenWalletClick } + isLoading={ web3Wallet.isOpen } + flexShrink={ 0 } + /> + ) } ); } diff --git a/ui/snippets/user/wallet/UserWalletDesktop.tsx b/ui/snippets/user/wallet/UserWalletDesktop.tsx index 642bfe7c1f..78e2e9018c 100644 --- a/ui/snippets/user/wallet/UserWalletDesktop.tsx +++ b/ui/snippets/user/wallet/UserWalletDesktop.tsx @@ -55,6 +55,7 @@ const UserWalletDesktop = ({ buttonSize, buttonVariant = 'header' }: Props) => { address={ web3AccountWithDomain.address } domain={ web3AccountWithDomain.domain } isAutoConnectDisabled={ isAutoConnectDisabled } + isReconnecting={ web3Wallet.isReconnecting } onOpenWallet={ handleOpenWalletClick } onDisconnect={ handleDisconnectClick } /> diff --git a/ui/snippets/user/wallet/UserWalletMenuContent.tsx b/ui/snippets/user/wallet/UserWalletMenuContent.tsx index f6b50f65ca..7de21a2ed8 100644 --- a/ui/snippets/user/wallet/UserWalletMenuContent.tsx +++ b/ui/snippets/user/wallet/UserWalletMenuContent.tsx @@ -1,4 +1,4 @@ -import { Box, Button, Flex, IconButton, Text } from '@chakra-ui/react'; +import { Box, Button, Flex, IconButton, Spinner, Text } from '@chakra-ui/react'; import React from 'react'; import delay from 'lib/delay'; @@ -11,11 +11,12 @@ interface Props { address: string; domain?: string; isAutoConnectDisabled?: boolean; + isReconnecting?: boolean; onDisconnect: () => void; onOpenWallet: () => void; } -const UserWalletMenuContent = ({ isAutoConnectDisabled, address, domain, onDisconnect, onOpenWallet }: Props) => { +const UserWalletMenuContent = ({ isAutoConnectDisabled, address, domain, isReconnecting, onDisconnect, onOpenWallet }: Props) => { const handleOpenWalletClick = React.useCallback(async() => { await delay(100); @@ -29,7 +30,7 @@ const UserWalletMenuContent = ({ isAutoConnectDisabled, address, domain, onDisco Your wallet is used to interact with apps and contracts in the explorer. - + - } - variant="simple" - color="icon_info" - boxSize={ 5 } - onClick={ handleOpenWalletClick } - flexShrink={ 0 } - /> + { isReconnecting ? : ( + } + variant="simple" + color="icon_info" + boxSize={ 5 } + onClick={ handleOpenWalletClick } + flexShrink={ 0 } + /> + ) }