Skip to content

Commit

Permalink
fixes for wallet connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jfschwarz committed Jan 30, 2025
1 parent 1c4ccc5 commit 72d2a5a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 45 deletions.
9 changes: 5 additions & 4 deletions deployables/app/app/components/wallet/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { type HexAddress, ProviderType } from '@zodiac/schema'
import { useEffect, useRef } from 'react'
import { type ChainId } from 'ser-kit'
import { useAccount, useAccountEffect, useDisconnect } from 'wagmi'
import { Connect, type OnConnectArgs } from './Connect'
import { EmptyAccount } from './EmptyAccount'
import type { OnConnectArgs } from './LaunchConnectKit'
import { Wallet } from './Wallet'

export type ConnectWalletProps = {
Expand All @@ -24,7 +25,7 @@ export const ConnectWallet = ({

useAutoReconnect({ currentConnectedAddress: pilotAddress, onConnect })

const accountNotConnected =
const pilotAccountIsEmpty =
pilotAddress == null || pilotAddress === ZERO_ADDRESS

const disconnectRequestRef = useRef(false)
Expand All @@ -41,8 +42,8 @@ export const ConnectWallet = ({
},
})

if (accountNotConnected) {
return <Connect onConnect={onConnect} />
if (pilotAccountIsEmpty) {
return <EmptyAccount onConnect={onConnect} />
}

return (
Expand Down
18 changes: 18 additions & 0 deletions deployables/app/app/components/wallet/EmptyAccount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Labeled, PrimaryButton } from '@zodiac/ui'
import { LaunchConnectKit, type OnConnectArgs } from './LaunchConnectKit'

type Props = {
onConnect?: (args: OnConnectArgs) => void
}

export const EmptyAccount = ({ onConnect }: Props) => {
return (
<LaunchConnectKit onConnect={onConnect}>
{({ show }) => (
<Labeled label="Pilot Account">
<PrimaryButton onClick={show}>Connect wallet</PrimaryButton>
</Labeled>
)}
</LaunchConnectKit>
)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { ChainId } from '@zodiac/chains'
import { ProviderType, type HexAddress } from '@zodiac/schema'
import { Labeled, PrimaryButton } from '@zodiac/ui'
import {
ConnectKitButton,
ConnectKitButton as ConnectKitButtonBase,
ConnectKitProvider,
Types as ConnectKitTypes,
} from 'connectkit'
import { useAccountEffect } from 'wagmi'

Expand All @@ -13,16 +12,16 @@ export type OnConnectArgs = {
}

type ConnectProps = {
children: (props: { show?: () => void }) => React.ReactNode
onConnect?: (args: OnConnectArgs) => void
initialChainId?: ChainId
}

const connectKitOptions: ConnectKitTypes.ConnectKitOptions = {
initialChainId: 0,
hideNoWalletCTA: true,
hideQuestionMarkCTA: true,
}

export const Connect = ({ onConnect }: ConnectProps) => {
export const LaunchConnectKit = ({
initialChainId,
onConnect,
children,
}: ConnectProps) => {
useAccountEffect({
onConnect({ address, connector, isReconnected }) {
if (isReconnected) {
Expand All @@ -44,14 +43,16 @@ export const Connect = ({ onConnect }: ConnectProps) => {
})

return (
<ConnectKitProvider options={connectKitOptions}>
<ConnectKitButton.Custom>
{({ show }) => (
<Labeled label="Pilot Account">
<PrimaryButton onClick={show}>Connect wallet</PrimaryButton>
</Labeled>
)}
</ConnectKitButton.Custom>
<ConnectKitProvider
options={{
initialChainId: initialChainId ?? 0,
hideNoWalletCTA: true,
hideQuestionMarkCTA: true,
}}
>
<ConnectKitButtonBase.Custom>
{({ show }) => children({ show })}
</ConnectKitButtonBase.Custom>
</ConnectKitProvider>
)
}
8 changes: 2 additions & 6 deletions deployables/app/app/components/wallet/Wallet.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ChainId } from 'ser-kit'
import { useAccount, useReconnect, useSwitchChain } from 'wagmi'
import { useAccount, useSwitchChain } from 'wagmi'
import { Account } from './Account'
import { Connected } from './Connected'
import { SwitchChain } from './SwitchChain'
Expand All @@ -20,7 +20,6 @@ export const Wallet = ({
}: WalletProps) => {
const { address, chainId: accountChainId, addresses = [] } = useAccount()
const { switchChain } = useSwitchChain()
const { reconnect } = useReconnect()

const isServer = typeof document === 'undefined'

Expand All @@ -31,10 +30,7 @@ export const Wallet = ({
// Wallet disconnected
if (address == null || addresses.length === 0) {
return (
<WalletDisconnected
onDisconnect={onDisconnect}
onReconnect={() => reconnect()}
>
<WalletDisconnected>
<Account>{pilotAddress}</Account>
</WalletDisconnected>
)
Expand Down
26 changes: 10 additions & 16 deletions deployables/app/app/components/wallet/WalletDisconnected.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { SecondaryButton, Warning } from '@zodiac/ui'
import type { PropsWithChildren } from 'react'
import { LaunchConnectKit } from './LaunchConnectKit'
import { Section } from './Section'

type WalletDisconnectedProps = PropsWithChildren<{
onReconnect: () => void
onDisconnect: () => void
}>
type WalletDisconnectedProps = PropsWithChildren

export const WalletDisconnected = ({
children,
onReconnect,
onDisconnect,
}: WalletDisconnectedProps) => (
export const WalletDisconnected = ({ children }: WalletDisconnectedProps) => (
<Section>
{children}

Expand All @@ -21,13 +15,13 @@ export const WalletDisconnected = ({
</Warning>

<Section.Actions>
<SecondaryButton fluid onClick={onReconnect}>
Connect
</SecondaryButton>

<SecondaryButton fluid onClick={onDisconnect}>
Disconnect
</SecondaryButton>
<LaunchConnectKit>
{({ show }) => (
<SecondaryButton fluid onClick={show}>
Connect wallet
</SecondaryButton>
)}
</LaunchConnectKit>
</Section.Actions>
</Section>
)
2 changes: 1 addition & 1 deletion deployables/app/app/components/wallet/WrongAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const WrongAccount = ({ children, onDisconnect }: WrongAccountProps) => (
<Section>
{children}

<Warning title="Account is not connected">
<Warning title="Wallet is set to a different account">
Switch your wallet to this account in order to use Pilot.
</Warning>

Expand Down

0 comments on commit 72d2a5a

Please sign in to comment.