Skip to content

Commit

Permalink
feat(pilot-app): disconnect wallet (#564)
Browse files Browse the repository at this point in the history
* restructure context for wallets

* make tests more robust
  • Loading branch information
frontendphil authored Jan 16, 2025
1 parent 33a1d0c commit 3de469b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 58 deletions.
51 changes: 14 additions & 37 deletions deployables/app/app/components/wallet/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import { invariant } from '@epic-web/invariant'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { ZERO_ADDRESS } from '@zodiac/chains'
import { type HexAddress, ProviderType } from '@zodiac/schema'
import { getDefaultConfig } from 'connectkit'
import { type ChainId } from 'ser-kit'
import { createConfig, WagmiProvider } from 'wagmi'
import { injected, metaMask, walletConnect } from 'wagmi/connectors'
import { useDisconnect } from 'wagmi'
import { Connect } from './Connect'
import { Wallet } from './Wallet'

const queryClient = new QueryClient()

const WALLETCONNECT_PROJECT_ID = '0f8a5e2cf60430a26274b421418e8a27'

const wagmiConfig = createConfig(
getDefaultConfig({
appName: 'Zodiac Pilot',
walletConnectProjectId: WALLETCONNECT_PROJECT_ID,
connectors: [
injected(),
metaMask(),
walletConnect({ projectId: WALLETCONNECT_PROJECT_ID }),
],
}),
)

interface Props {
pilotAddress: HexAddress | null
chainId?: ChainId
Expand All @@ -44,14 +25,10 @@ export const ConnectWallet = ({
onConnect,
onDisconnect,
}: Props) => {
const { disconnect } = useDisconnect()

if (pilotAddress == null || pilotAddress === ZERO_ADDRESS) {
return (
<QueryClientProvider client={queryClient}>
<WagmiProvider config={wagmiConfig}>
<Connect onConnect={onConnect} />
</WagmiProvider>
</QueryClientProvider>
)
return <Connect onConnect={onConnect} />
}

invariant(
Expand All @@ -60,15 +37,15 @@ export const ConnectWallet = ({
)

return (
<QueryClientProvider client={queryClient}>
<WagmiProvider config={wagmiConfig}>
<Wallet
chainId={chainId}
providerType={providerType}
pilotAddress={pilotAddress}
onDisconnect={onDisconnect}
/>
</WagmiProvider>
</QueryClientProvider>
<Wallet
chainId={chainId}
providerType={providerType}
pilotAddress={pilotAddress}
onDisconnect={() => {
disconnect()

onDisconnect()
}}
/>
)
}
27 changes: 27 additions & 0 deletions deployables/app/app/components/wallet/WalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { getDefaultConfig } from 'connectkit'
import type { PropsWithChildren } from 'react'
import { createConfig, injected, WagmiProvider } from 'wagmi'
import { metaMask, walletConnect } from 'wagmi/connectors'

const queryClient = new QueryClient()

const WALLETCONNECT_PROJECT_ID = '0f8a5e2cf60430a26274b421418e8a27'

const wagmiConfig = createConfig(
getDefaultConfig({
appName: 'Zodiac Pilot',
walletConnectProjectId: WALLETCONNECT_PROJECT_ID,
connectors: [
injected(),
metaMask(),
walletConnect({ projectId: WALLETCONNECT_PROJECT_ID }),
],
}),
)

export const WalletProvider = ({ children }: PropsWithChildren) => (
<QueryClientProvider client={queryClient}>
<WagmiProvider config={wagmiConfig}>{children}</WagmiProvider>
</QueryClientProvider>
)
1 change: 1 addition & 0 deletions deployables/app/app/components/wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { ConnectWallet } from './ConnectWallet'
export { WalletProvider } from './WalletProvider'
6 changes: 3 additions & 3 deletions deployables/app/app/routes/edit-route.$data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ describe('Edit route', () => {

await render(`/edit-route/${btoa(JSON.stringify(route))}`)

expect(screen.getByText('Roles v1')).toBeInTheDocument()
expect(await screen.findByText('Roles v1')).toBeInTheDocument()
})

it('is possible to select the v1 roles mod', async () => {
Expand Down Expand Up @@ -451,7 +451,7 @@ describe('Edit route', () => {

await render(`/edit-route/${btoa(JSON.stringify(route))}`)

expect(screen.getByText('Roles v2')).toBeInTheDocument()
expect(await screen.findByText('Roles v2')).toBeInTheDocument()
})

it('is possible to select the v2 roles mod', async () => {
Expand Down Expand Up @@ -529,7 +529,7 @@ describe('Edit route', () => {
await render(`/edit-route/${btoa(JSON.stringify(route))}`)

await userEvent.type(
screen.getByRole('textbox', { name: 'Role Key' }),
await screen.findByRole('textbox', { name: 'Role Key' }),
'MANAGER',
)

Expand Down
49 changes: 31 additions & 18 deletions deployables/app/app/routes/edit-route.$data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
ChainSelect,
ConnectWallet,
DebugRouteData,
WalletProvider,
ZodiacMod,
} from '@/components'
import { editRoute, jsonRpcProvider, parseRouteData } from '@/utils'
import { invariantResponse } from '@epic-web/invariant'
import { verifyChainId } from '@zodiac/chains'
import { verifyChainId, ZERO_ADDRESS } from '@zodiac/chains'
import {
formData,
getHexString,
Expand Down Expand Up @@ -130,6 +131,11 @@ export const clientAction = async ({
),
)
}
case Intent.DisconnectWallet: {
const route = parseRouteData(params.data)

return editRoute(request.url, updatePilotAddress(route, ZERO_ADDRESS))
}
default:
return serverAction()
}
Expand Down Expand Up @@ -157,23 +163,29 @@ const EditRoute = ({
}}
/>

<ConnectWallet
chainId={chainId}
pilotAddress={getPilotAddress(waypoints)}
providerType={providerType}
onConnect={({ account, chainId, providerType }) => {
submit(
formData({
intent: Intent.ConnectWallet,
account,
chainId,
providerType,
}),
{ method: 'POST' },
)
}}
onDisconnect={() => {}}
/>
<WalletProvider>
<ConnectWallet
chainId={chainId}
pilotAddress={getPilotAddress(waypoints)}
providerType={providerType}
onConnect={({ account, chainId, providerType }) => {
submit(
formData({
intent: Intent.ConnectWallet,
account,
chainId,
providerType,
}),
{ method: 'POST' },
)
}}
onDisconnect={() => {
submit(formData({ intent: Intent.DisconnectWallet }), {
method: 'POST',
})
}}
/>
</WalletProvider>

<AvatarInput
value={avatar}
Expand Down Expand Up @@ -221,6 +233,7 @@ enum Intent {
UpdateAvatar = 'UpdateAvatar',
RemoveAvatar = 'RemoveAvatar',
ConnectWallet = 'ConnectWallet',
DisconnectWallet = 'DisconnectWallet',
}

const getPilotAddress = (waypoints?: Waypoints) => {
Expand Down

0 comments on commit 3de469b

Please sign in to comment.