Skip to content

Commit

Permalink
Fix connecting in hook, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
tolgahan-arikan committed Jan 16, 2025
1 parent e81ff0a commit dc75502
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
47 changes: 13 additions & 34 deletions examples/react/src/components/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
validateEthProof,
getModalPositionCss,
useOpenConnectModal,
ContractVerificationStatus
ContractVerificationStatus,
useKitWallets
} from '@0xsequence/kit'
import { useCheckoutModal, useAddFundsModal, useERC1155SaleContractPaymentModal, useSwapModal } from '@0xsequence/kit-checkout'
import type { SwapModalSettings } from '@0xsequence/kit-checkout'
Expand All @@ -21,8 +22,6 @@ import { formatUnits, parseUnits } from 'viem'
import {
useAccount,
useChainId,
useConnections,
useConnect,
useDisconnect,
usePublicClient,
useSendTransaction,
Expand Down Expand Up @@ -57,11 +56,8 @@ export const Connected = () => {
const [checkoutTokenContractAddress, setCheckoutTokenContractAddress] = React.useState('')
const [checkoutTokenId, setCheckoutTokenId] = React.useState('')

const connections = useConnections()
console.log('connections:', connections)

const waasConnection = connections.find(c => c.connector.id.includes('waas'))
const isWaasConnectionActive = waasConnection !== undefined && address === waasConnection.accounts[0]
const { wallets, setActiveWallet } = useKitWallets()
const isWaasConnectionActive = wallets.some(w => w.isEmbedded && w.isActive)

const {
data: txnData,
Expand Down Expand Up @@ -98,17 +94,8 @@ export const Connected = () => {

const [selectedFeeOptionTokenName, setSelectedFeeOptionTokenName] = React.useState<string | undefined>()

const { connectAsync } = useConnect()
const { disconnect } = useDisconnect()

const handleConnectorChange = async (connectorId: string) => {
const newConnection = connections.find(c => c.connector.id === connectorId)
if (!newConnection) return

// Connect with the selected connector
await connectAsync({ connector: newConnection.connector })
}

useEffect(() => {
if (pendingFeeOptionConfirmation) {
setSelectedFeeOptionTokenName(pendingFeeOptionConfirmation.options[0].token.name)
Expand Down Expand Up @@ -524,12 +511,12 @@ export const Connected = () => {
<Text fontWeight="semibold" variant="small" color="text50">
Connected Wallets
</Text>
{connections.map(connection => (
{wallets.map(wallet => (
<Box
key={(connection.connector._wallet as any)?.id ?? connection.connector.id}
key={wallet.id}
padding="2"
borderRadius="md"
background={connection.accounts[0] === address ? 'backgroundRaised' : 'backgroundMuted'}
background={wallet.isActive ? 'backgroundRaised' : 'backgroundMuted'}
display="flex"
flexDirection="row"
alignItems="center"
Expand All @@ -540,29 +527,21 @@ export const Connected = () => {
flexDirection="row"
alignItems="center"
gap="2"
onClick={() => handleConnectorChange(connection.connector.id)}
onClick={() => setActiveWallet(wallet.address)}
style={{ cursor: 'pointer' }}
>
<Box
borderColor="text50"
background={connection.connector.id === connections[0]?.connector.id ? 'text100' : 'transparent'}
/>
<Box borderColor="text50" background={wallet.isActive ? 'text100' : 'transparent'} />
<Box flexDirection="column" gap="1">
<Text variant="normal" color="text100">
{connection.connector.id.includes('waas') ? 'Embedded - ' : ''}
{(connection.connector._wallet as any)?.name ?? connection.connector.name}
{wallet.isEmbedded ? 'Embedded - ' : ''}
{wallet.name}
</Text>
<Text variant="normal" fontWeight="bold" color="text100">
{truncateAtMiddle(connection.accounts[0], 10)}
{truncateAtMiddle(wallet.address, 10)}
</Text>
</Box>
</Box>
<Button
variant="text"
size="sm"
label="Disconnect"
onClick={() => disconnect({ connector: connection.connector })}
/>
<Button variant="text" size="sm" label="Disconnect" onClick={() => disconnect()} />
</Box>
))}
</Box>
Expand Down
5 changes: 3 additions & 2 deletions packages/kit/src/hooks/useKitWallets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type UseConnectionsReturnType, useAccount, useConnections } from 'wagmi'
import { type UseConnectionsReturnType, useAccount, useConnect, useConnections } from 'wagmi'

export interface KitWallet {
id: string
Expand All @@ -11,6 +11,7 @@ export interface KitWallet {
export const useKitWallets = () => {
const { address } = useAccount()
const connections = useConnections()
const { connectAsync } = useConnect()

const wallets: KitWallet[] = connections.map((connection: UseConnectionsReturnType[number]) => ({
id: connection.connector.id,
Expand All @@ -25,7 +26,7 @@ export const useKitWallets = () => {
if (!connection) return

try {
await connection.connector.connect()
await connectAsync({ connector: connection.connector })
} catch (error) {
console.error('Failed to set active wallet:', error)
}
Expand Down

0 comments on commit dc75502

Please sign in to comment.