Skip to content

Commit

Permalink
Merge pull request #1515 from blockscout/tom2drum/issue-1509
Browse files Browse the repository at this point in the history
Connect wallet event sent twice
  • Loading branch information
tom2drum authored Jan 22, 2024
2 parents c9d7786 + f238ea8 commit 815aab2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
34 changes: 6 additions & 28 deletions ui/address/contract/ContractConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
import { Alert, Button, Flex } from '@chakra-ui/react';
import { useWeb3Modal, useWeb3ModalState } from '@web3modal/wagmi/react';
import React from 'react';
import { useAccount, useDisconnect } from 'wagmi';

import useIsMobile from 'lib/hooks/useIsMobile';
import * as mixpanel from 'lib/mixpanel/index';
import AddressEntity from 'ui/shared/entities/address/AddressEntity';
import useWallet from 'ui/snippets/walletMenu/useWallet';

const ContractConnectWallet = () => {
const { open } = useWeb3Modal();
const { open: isOpen } = useWeb3ModalState();
const { disconnect } = useDisconnect();
const { isModalOpening, isModalOpen, connect, disconnect, address, isWalletConnected } = useWallet({ source: 'Smart contracts' });
const isMobile = useIsMobile();
const [ isModalOpening, setIsModalOpening ] = React.useState(false);

const handleConnect = React.useCallback(async() => {
setIsModalOpening(true);
await open();
setIsModalOpening(false);
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: 'Smart contracts', Status: 'Started' });
}, [ open ]);

const handleAccountConnected = React.useCallback(({ isReconnected }: { isReconnected: boolean }) => {
!isReconnected && mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: 'Smart contracts', Status: 'Connected' });
}, []);

const handleDisconnect = React.useCallback(() => {
disconnect();
}, [ disconnect ]);

const { address, isDisconnected } = useAccount({ onConnect: handleAccountConnected });

const content = (() => {
if (isDisconnected || !address) {
if (!isWalletConnected) {
return (
<>
<span>Disconnected</span>
<Button
ml={ 3 }
onClick={ handleConnect }
onClick={ connect }
size="sm"
variant="outline"
isLoading={ isModalOpening || isOpen }
isLoading={ isModalOpening || isModalOpen }
loadingText="Connect wallet"
>
Connect wallet
Expand All @@ -61,7 +39,7 @@ const ContractConnectWallet = () => {
ml={ 2 }
/>
</Flex>
<Button onClick={ handleDisconnect } size="sm" variant="outline">Disconnect</Button>
<Button onClick={ disconnect } size="sm" variant="outline">Disconnect</Button>
</Flex>
);
})();
Expand Down
2 changes: 1 addition & 1 deletion ui/snippets/walletMenu/WalletMenuDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Props = {
};

const WalletMenuDesktop = ({ isHomePage }: Props) => {
const { isWalletConnected, address, connect, disconnect, isModalOpening, isModalOpen } = useWallet();
const { isWalletConnected, address, connect, disconnect, isModalOpening, isModalOpen } = useWallet({ source: 'Header' });
const { themedBackground, themedBorderColor, themedColor } = useMenuButtonColors();
const [ isPopoverOpen, setIsPopoverOpen ] = useBoolean(false);
const isMobile = useIsMobile();
Expand Down
2 changes: 1 addition & 1 deletion ui/snippets/walletMenu/WalletMenuMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import WalletTooltip from './WalletTooltip';

const WalletMenuMobile = () => {
const { isOpen, onOpen, onClose } = useDisclosure();
const { isWalletConnected, address, connect, disconnect, isModalOpening, isModalOpen } = useWallet();
const { isWalletConnected, address, connect, disconnect, isModalOpening, isModalOpen } = useWallet({ source: 'Header' });
const { themedBackground, themedBorderColor, themedColor } = useMenuButtonColors();
const isMobile = useIsMobile();

Expand Down
18 changes: 13 additions & 5 deletions ui/snippets/walletMenu/useWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import { useAccount, useDisconnect } from 'wagmi';

import * as mixpanel from 'lib/mixpanel/index';

export default function useWallet() {
interface Params {
source: mixpanel.EventPayload<mixpanel.EventTypes.WALLET_CONNECT>['Source'];
}

export default function useWallet({ source }: Params) {
const { open } = useWeb3Modal();
const { open: isOpen } = useWeb3ModalState();
const { disconnect } = useDisconnect();
const [ isModalOpening, setIsModalOpening ] = React.useState(false);
const [ isClientLoaded, setIsClientLoaded ] = React.useState(false);
const isConnectionStarted = React.useRef(false);

React.useEffect(() => {
setIsClientLoaded(true);
Expand All @@ -19,12 +24,15 @@ export default function useWallet() {
setIsModalOpening(true);
await open();
setIsModalOpening(false);
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: 'Header', Status: 'Started' });
}, [ open ]);
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: source, Status: 'Started' });
isConnectionStarted.current = true;
}, [ open, source ]);

const handleAccountConnected = React.useCallback(({ isReconnected }: { isReconnected: boolean }) => {
!isReconnected && mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: 'Header', Status: 'Connected' });
}, []);
!isReconnected && isConnectionStarted.current &&
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: source, Status: 'Connected' });
isConnectionStarted.current = false;
}, [ source ]);

const handleDisconnect = React.useCallback(() => {
disconnect();
Expand Down

0 comments on commit 815aab2

Please sign in to comment.