import { useEffect , useState } from 'react' ; import { Mainnet , WalletIds } from '../constants' ; import { Connector } from '../types' ; import { useContractKitInternal } from '../use-contractkit' ; import { useWalletVersion } from '../utils/useWalletVersion' ; import { WalletConnectConnector } from './connectors' ; export function useWalletConnectConnector ( onSubmit , autoOpen , getDeeplinkUrl ? , walletId ? ) { const { network , feeCurrency , destroy , initConnector } = useContractKitInternal ( ) ; const [ uri , setUri ] = useState ( '' ) ; const version = useWalletVersion ( walletId ) ; useEffect ( ( ) => { let mounted = true ; const initialiseConnection = async ( ) => { if ( version == null ) { console . warn ( 'WalletconnectConnector initialization awaiting for registry' ) ; return ; } const isMainnet = network . name === Mainnet . name ; const connector = new WalletConnectConnector ( network , feeCurrency , { connect : { chainId : network . chainId , } , } , autoOpen && isMainnet , getDeeplinkUrl , version ) ; connector . onUri ( ( newUri ) => { if ( mounted ) { setUri ( newUri ) ; } } ) ; connector . onClose ( ( ) => void destroy ( ) ) ; await initConnector ( connector ) ; onSubmit ( connector ) ; } ; initialiseConnection ( ) . then ( ( ) => console . info ( 'WalletConnect connection initialised' ) ) . catch ( ( reason ) => console . error ( 'Failed to initialise WalletConnect connection' , reason ) ) ; return ( ) => { mounted = false ; } ; } , [ version ] ) ; return uri ; }