Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect wallet event sent twice #1515

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading