Skip to content

Commit

Permalink
handle reconnecting state of web3wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
tom2drum committed Nov 6, 2024
1 parent bc1b688 commit dbad22c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 30 deletions.
12 changes: 7 additions & 5 deletions lib/web3/useWallet.ts
Original file line number Diff line number Diff line change
@@ -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<mixpanel.EventTypes.WALLET_CONNECT>['Source'];
Expand Down Expand Up @@ -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 ]);
}
27 changes: 14 additions & 13 deletions ui/snippets/user/profile/UserProfileContentWallet.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -34,7 +34,7 @@ const UserProfileContentWallet = ({ onClose, className }: Props) => {
const content = (() => {
if (web3Wallet.isConnected && web3AccountWithDomain.address) {
return (
<Flex alignItems="center" columnGap={ 2 } bgColor={ walletSnippetBgColor } px={ 2 } py="10px" borderRadius="base">
<Flex alignItems="center" columnGap={ 2 } bgColor={ walletSnippetBgColor } px={ 2 } py="10px" borderRadius="base" justifyContent="space-between">
<AddressEntity
address={{ hash: web3AccountWithDomain.address, ens_domain_name: web3AccountWithDomain.domain }}
isLoading={ web3AccountWithDomain.isLoading }
Expand All @@ -44,17 +44,18 @@ const UserProfileContentWallet = ({ onClose, className }: Props) => {
fontWeight={ 500 }
noAltHash
/>
<IconButton
aria-label="Open wallet"
icon={ <IconSvg name="gear_slim" boxSize={ 5 }/> }
variant="simple"
color="icon_info"
boxSize={ 5 }
onClick={ handleOpenWalletClick }
isLoading={ web3Wallet.isOpen }
flexShrink={ 0 }
ml="auto"
/>
{ web3Wallet.isReconnecting ? <Spinner size="sm" m="2px" flexShrink={ 0 }/> : (
<IconButton
aria-label="Open wallet"
icon={ <IconSvg name="gear_slim" boxSize={ 5 }/> }
variant="simple"
color="icon_info"
boxSize={ 5 }
onClick={ handleOpenWalletClick }
isLoading={ web3Wallet.isOpen }
flexShrink={ 0 }
/>
) }
</Flex>
);
}
Expand Down
1 change: 1 addition & 0 deletions ui/snippets/user/wallet/UserWalletDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
/>
Expand Down
27 changes: 15 additions & 12 deletions ui/snippets/user/wallet/UserWalletMenuContent.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand All @@ -29,23 +30,25 @@ const UserWalletMenuContent = ({ isAutoConnectDisabled, address, domain, onDisco
<Text fontSize="sm" mb={ 5 } fontWeight={ 400 } color="text_secondary">
Your wallet is used to interact with apps and contracts in the explorer.
</Text>
<Flex alignItems="center" columnGap={ 2 }>
<Flex alignItems="center" columnGap={ 2 } justifyContent="space-between">
<AddressEntity
address={{ hash: address, ens_domain_name: domain }}
isTooltipDisabled
truncation="dynamic"
fontSize="sm"
fontWeight={ 700 }
/>
<IconButton
aria-label="Open wallet"
icon={ <IconSvg name="gear_slim" boxSize={ 5 }/> }
variant="simple"
color="icon_info"
boxSize={ 5 }
onClick={ handleOpenWalletClick }
flexShrink={ 0 }
/>
{ isReconnecting ? <Spinner size="sm" m="2px" flexShrink={ 0 }/> : (
<IconButton
aria-label="Open wallet"
icon={ <IconSvg name="gear_slim" boxSize={ 5 }/> }
variant="simple"
color="icon_info"
boxSize={ 5 }
onClick={ handleOpenWalletClick }
flexShrink={ 0 }
/>
) }
</Flex>
<Button size="sm" width="full" variant="outline" onClick={ onDisconnect } mt={ 6 }>
Disconnect
Expand Down
1 change: 1 addition & 0 deletions ui/snippets/user/wallet/UserWalletMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const UserWalletMobile = () => {
address={ web3AccountWithDomain.address }
domain={ web3AccountWithDomain.domain }
isAutoConnectDisabled={ isAutoConnectDisabled }
isReconnecting={ web3Wallet.isReconnecting }
onOpenWallet={ handleOpenWalletClick }
onDisconnect={ handleDisconnectClick }
/>
Expand Down

0 comments on commit dbad22c

Please sign in to comment.