From f238ea850d47b0a1c41b7b3df09434ae501faaee Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 18 Jan 2024 16:44:54 +0400 Subject: [PATCH] Connect wallet event sent twice Fixes #1509 --- ui/address/contract/ContractConnectWallet.tsx | 34 ++++--------------- ui/snippets/walletMenu/WalletMenuDesktop.tsx | 2 +- ui/snippets/walletMenu/WalletMenuMobile.tsx | 2 +- ui/snippets/walletMenu/useWallet.tsx | 18 +++++++--- 4 files changed, 21 insertions(+), 35 deletions(-) diff --git a/ui/address/contract/ContractConnectWallet.tsx b/ui/address/contract/ContractConnectWallet.tsx index 1673030a76..9263495bce 100644 --- a/ui/address/contract/ContractConnectWallet.tsx +++ b/ui/address/contract/ContractConnectWallet.tsx @@ -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 ( <> Disconnected + ); })(); diff --git a/ui/snippets/walletMenu/WalletMenuDesktop.tsx b/ui/snippets/walletMenu/WalletMenuDesktop.tsx index 303d341f69..df19ef5cf9 100644 --- a/ui/snippets/walletMenu/WalletMenuDesktop.tsx +++ b/ui/snippets/walletMenu/WalletMenuDesktop.tsx @@ -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(); diff --git a/ui/snippets/walletMenu/WalletMenuMobile.tsx b/ui/snippets/walletMenu/WalletMenuMobile.tsx index dccbe41ac0..e8e6bcb48a 100644 --- a/ui/snippets/walletMenu/WalletMenuMobile.tsx +++ b/ui/snippets/walletMenu/WalletMenuMobile.tsx @@ -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(); diff --git a/ui/snippets/walletMenu/useWallet.tsx b/ui/snippets/walletMenu/useWallet.tsx index b1ab280321..cccdaa166b 100644 --- a/ui/snippets/walletMenu/useWallet.tsx +++ b/ui/snippets/walletMenu/useWallet.tsx @@ -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['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); @@ -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();