Skip to content

Commit a7df42f

Browse files
authored
Fix WalletConnectModal close detection (#1994)
1 parent 3f066f9 commit a7df42f

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

.changeset/lovely-bikes-applaud.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@onflow/fcl-wc": patch
3+
---
4+
5+
Fix WalletConnectModal close detection

packages/fcl-wc/src/service.ts

+25-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {isMobile, openDeeplink} from "./utils"
44
import {FLOW_METHODS, REQUEST_TYPES} from "./constants"
55
import {SignClient} from "@walletconnect/sign-client/dist/types/client"
66
import {createSessionProposal, makeSessionData, request} from "./session"
7+
import {ModalCtrlState} from "@walletconnect/modal-core/dist/_types/src/types/controllerTypes"
78

8-
type WalletConnectModalType =
9-
typeof import("@walletconnect/modal").WalletConnectModal
9+
type WalletConnectModalType = import("@walletconnect/modal").WalletConnectModal
10+
11+
type Constructor<T> = new (...args: any[]) => T
1012

1113
export const SERVICE_PLUGIN_NAME = "fcl-plugin-service-walletconnect"
1214
export const WC_SERVICE_METHOD = "WC/RPC"
@@ -44,7 +46,7 @@ export const makeServicePlugin = (
4446
const makeExec = (
4547
clientPromise: Promise<SignClient | null>,
4648
{wcRequestHook, pairingModalOverride}: any,
47-
WalletConnectModal: Promise<WalletConnectModalType>
49+
WalletConnectModal: Promise<Constructor<WalletConnectModalType>>
4850
) => {
4951
return async ({
5052
service,
@@ -136,7 +138,9 @@ const makeExec = (
136138
}
137139

138140
// Connect to WalletConnect directly from the browser via deep link or WalletConnectModal
139-
function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
141+
function connectWc(
142+
WalletConnectModal: Promise<Constructor<WalletConnectModalType>>
143+
) {
140144
return async ({
141145
service,
142146
onClose,
@@ -165,7 +169,7 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
165169
)
166170

167171
let _uri: string | null = null,
168-
walletConnectModal: any
172+
walletConnectModal: WalletConnectModalType | null = null
169173

170174
try {
171175
const {uri, approval} = await createSessionProposal({
@@ -194,7 +198,22 @@ function connectWc(WalletConnectModal: Promise<WalletConnectModalType>) {
194198
walletConnectModal = new (await WalletConnectModal)({
195199
projectId,
196200
})
197-
walletConnectModal.openModal({uri, onClose})
201+
202+
// Open WalletConnectModal
203+
walletConnectModal.openModal({
204+
uri,
205+
onClose,
206+
})
207+
208+
// Subscribe to modal state changes
209+
const unsubscribeModal = walletConnectModal.subscribeModal(
210+
(state: ModalCtrlState) => {
211+
if (state.open === false) {
212+
onClose?.()
213+
unsubscribeModal()
214+
}
215+
}
216+
)
198217
} else {
199218
pairingModalOverride(uri, onClose)
200219
}

0 commit comments

Comments
 (0)