mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
* basic login button implementation * refactoring and add dynamic auth button to the other pages * connect wallet without authenticating an user * login and logout user in blockscout account * refactoring and RPC overrides * authenticate user with connected wallet * add ENS to profile button * override evm chains and fix contract write with waas wallet * disconnect wallet and fix docker build * add dynamic imports to components that use web3 wallet packages * refactor user profile directory * clean up * tweaks * update pw ci script * fix "connect wallet" button behavior * handle linking email to profile with dynamic provider * fix test * async loading web3 hooks and remove dynamic loading from components that use them * fix pw tests * add account navigation in horizontal layout when auth provider is dynamic * fix address verification modal * review fixes * spelling fixes
60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { useAppKit, useAppKitState } from '@reown/appkit/react';
|
|
import React from 'react';
|
|
import { useAccountEffect, useAccount, useDisconnect } from 'wagmi';
|
|
|
|
import type { Params, Result } from './types';
|
|
|
|
import * as mixpanel from 'lib/mixpanel/index';
|
|
|
|
export default function useWalletReown({ source, onConnect }: Params): Result {
|
|
const { open: openModal } = useAppKit();
|
|
const { open: isOpen } = useAppKitState();
|
|
const { disconnect } = useDisconnect();
|
|
const [ isOpening, setIsOpening ] = React.useState(false);
|
|
const [ isClientLoaded, setIsClientLoaded ] = React.useState(false);
|
|
const isConnectionStarted = React.useRef(false);
|
|
|
|
React.useEffect(() => {
|
|
setIsClientLoaded(true);
|
|
}, []);
|
|
|
|
const handleConnect = React.useCallback(async() => {
|
|
setIsOpening(true);
|
|
await openModal();
|
|
setIsOpening(false);
|
|
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: source, Status: 'Started' });
|
|
isConnectionStarted.current = true;
|
|
}, [ openModal, source ]);
|
|
|
|
const handleAccountConnected = React.useCallback(({ isReconnected }: { isReconnected: boolean }) => {
|
|
if (!isReconnected && isConnectionStarted.current) {
|
|
mixpanel.logEvent(mixpanel.EventTypes.WALLET_CONNECT, { Source: source, Status: 'Connected' });
|
|
mixpanel.userProfile.setOnce({
|
|
'With Connected Wallet': true,
|
|
});
|
|
onConnect?.();
|
|
}
|
|
isConnectionStarted.current = false;
|
|
}, [ source, onConnect ]);
|
|
|
|
const handleDisconnect = React.useCallback(() => {
|
|
disconnect();
|
|
}, [ disconnect ]);
|
|
|
|
useAccountEffect({ onConnect: handleAccountConnected });
|
|
|
|
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, isOpening, isOpen, isConnected, account.isReconnecting, address, openModal ]);
|
|
}
|