Skip to content

Commit

Permalink
fix: add option to show error on user rejection in modal connection flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaymaeJhabli committed Dec 22, 2024
1 parent dea126a commit 27808e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion demos/react-dapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ const App: React.FC = () => {
let session: SessionTypes.Struct
setIsLoading(true)
if (extensionId) session = await dAppConnector.connectExtension(extensionId)
else session = await dAppConnector.openModal()
// Open modal with showErrorOnReject set to true
else session = await dAppConnector.openModal(undefined, true)

setNewSession(session)
} finally {
Expand Down
20 changes: 13 additions & 7 deletions src/lib/dapp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,20 +202,26 @@ export class DAppConnector {
/**
* Initiates the WalletConnect connection flow using a QR code.
* @param pairingTopic - The pairing topic for the connection (optional).
* @param showErrorOnReject - Whether to show an error when the user rejects the pairing (default: false).
* @returns {Promise<SessionTypes.Struct>} - A Promise that resolves when the connection process is complete.
*/
public async openModal(pairingTopic?: string): Promise<SessionTypes.Struct> {
public async openModal(
pairingTopic?: string,
showErrorOnReject: boolean = false,
): Promise<SessionTypes.Struct> {
try {
const { uri, approval } = await this.connectURI(pairingTopic)
this.walletConnectModal.openModal({ uri })

const session = await new Promise<SessionTypes.Struct>(async (resolve, reject) => {
this.walletConnectModal.subscribeModal((state: { open: boolean }) => {
// the modal was closed so reject the promise
if (!state.open) {
reject(new Error('User rejected pairing'))
}
})
if (showErrorOnReject) {
this.walletConnectModal.subscribeModal((state: { open: boolean }) => {
// the modal was closed so reject the promise
if (!state.open) {
reject(new Error('User rejected pairing'))
}
})
}

try {
const approvedSession = await approval()
Expand Down

0 comments on commit 27808e5

Please sign in to comment.