Skip to content

Commit

Permalink
feat: useDisconnect hook (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
magiziz authored Oct 18, 2024
1 parent 0c93f2f commit f30d116
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
23 changes: 23 additions & 0 deletions .changeset/tall-zebras-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@apps/laboratory': patch
'@reown/appkit-core': patch
'@apps/demo': patch
'@apps/gallery': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-polkadot': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-common': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
---

Added `useDisconnect` hook to `@reown/appkit/react` and `@reown/appkit/vue`.
18 changes: 14 additions & 4 deletions apps/laboratory/src/components/AppKitButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAppKit } from '@reown/appkit/react'
import { useAppKit, useDisconnect, useAppKitAccount } from '@reown/appkit/react'
import {
Stack,
Card,
Expand All @@ -12,6 +12,8 @@ import {

export function AppKitButtons() {
const { open } = useAppKit()
const { caipAddress } = useAppKitAccount()
const { disconnect } = useDisconnect()

return (
<Card marginTop={10}>
Expand All @@ -37,9 +39,17 @@ export function AppKitButtons() {
<Heading size="xs" textTransform="uppercase" pb="2">
Hooks Interactions
</Heading>
<Button data-testid="w3m-open-hook-button" onClick={() => open()}>
Open
</Button>
<Box display="flex" alignItems="center" columnGap={3}>
<Button data-testid="w3m-open-hook-button" onClick={() => open()}>
Open
</Button>

{caipAddress && (
<Button data-testid="disconnect-hook-button" onClick={() => disconnect()}>
Disconnect
</Button>
)}
</Box>
</Box>
</Stack>
</CardBody>
Expand Down
6 changes: 6 additions & 0 deletions apps/laboratory/tests/shared/pages/ModalPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ export class ModalPage {
await tabWebApp.click()
}

async clickHookDisconnectButton() {
const disconnectHookButton = this.page.getByTestId('disconnect-hook-button')
await expect(disconnectHookButton).toBeVisible()
await disconnectHookButton.click()
}

async clickCopyLink() {
const copyLink = this.page.getByTestId('wui-link-copy')
await expect(copyLink).toBeVisible()
Expand Down
12 changes: 12 additions & 0 deletions apps/laboratory/tests/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,19 @@ sampleWalletTest(
await modalPage.closeModal()
}
)

sampleWalletTest('it should connect and disconnect using hook', async () => {
await walletPage.disconnectConnection()
await modalValidator.expectDisconnected()
await modalPage.qrCodeFlow(modalPage, walletPage)
await modalValidator.expectConnected()
await modalPage.clickHookDisconnectButton()
await modalValidator.expectDisconnected()
})

sampleWalletTest('it should disconnect and close modal when connecting from wallet', async () => {
await modalPage.qrCodeFlow(modalPage, walletPage)
await modalValidator.expectConnected()
await modalPage.openModal()
await walletPage.disconnectConnection()
await walletValidator.expectSessionCard({ visible: false })
Expand Down
9 changes: 9 additions & 0 deletions packages/core/exports/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AccountController } from '../src/controllers/AccountController.js'
import { CoreHelperUtil } from '../src/utils/CoreHelperUtil.js'
import { ChainController } from '../src/controllers/ChainController.js'
import type { CaipNetwork, CaipNetworkId } from '@reown/appkit-common'
import { ConnectionController } from '../src/controllers/ConnectionController.js'

// -- Hooks ------------------------------------------------------------
export function useAppKitNetwork(): {
Expand Down Expand Up @@ -30,3 +31,11 @@ export function useAppKitAccount() {
status
}
}

export function useDisconnect() {
async function disconnect() {
await ConnectionController.disconnect()
}

return { disconnect }
}
9 changes: 9 additions & 0 deletions packages/core/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ChainController } from '../src/controllers/ChainController.js'
import type { CaipNetwork, CaipNetworkId } from '@reown/appkit-common'
import { ref } from 'valtio/vanilla'
import { onUnmounted } from 'vue'
import { ConnectionController } from '../src/controllers/ConnectionController.js'

// -- Hooks ------------------------------------------------------------
export function useAppKitNetwork(): {
Expand Down Expand Up @@ -56,3 +57,11 @@ export function useAppKitAccount() {
status: state.status
}
}

export function useDisconnect() {
async function disconnect() {
await ConnectionController.disconnect()
}

return { disconnect }
}

0 comments on commit f30d116

Please sign in to comment.