Skip to content

Commit b6c1f09

Browse files
author
Aleksandar Cakalic
committed
refactor: handle failed mobile request, fix app not opening on request
1 parent e65b851 commit b6c1f09

File tree

7 files changed

+73
-29
lines changed

7 files changed

+73
-29
lines changed

src/connectors/argent/argentMobile/modal/argentModal.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Modal from "../../../../modal/Modal.svelte"
33
import { Layout, type ModalWallet } from "../../../../types/modal"
44
import { getModalTarget } from "../../../../helpers/modal"
55
import type { StarknetkitConnector } from "../../../connector"
6+
import { isInArgentMobileAppBrowser } from "../../helpers"
67

78
const device = getDevice()
89

@@ -74,21 +75,24 @@ class ArgentModal {
7475
})
7576
}
7677

77-
private getQR(urls: Urls) {
78-
const overlay = document.createElement("div")
79-
const shadow = document.querySelector("#starknetkit-modal-container")
78+
private openMobileApp(urls: Urls) {
79+
const toMobileApp = document.createElement("button")
80+
toMobileApp.style.display = "none"
81+
toMobileApp.addEventListener("click", () => {
82+
window.location.href = urls[device]
83+
})
84+
toMobileApp.click()
85+
}
8086

87+
private getQR(urls: Urls) {
8188
if (["android", "ios"].includes(device)) {
82-
const toMobileApp = document.createElement("button")
83-
toMobileApp.style.display = "none"
84-
toMobileApp.addEventListener("click", () => {
85-
window.location.href = urls[device]
86-
})
87-
toMobileApp.click()
88-
89+
this.openMobileApp(urls)
8990
return
9091
}
9192

93+
const overlay = document.createElement("div")
94+
const shadow = document.querySelector("#starknetkit-modal-container")
95+
9296
if (shadow?.shadowRoot) {
9397
const slot = shadow.shadowRoot.querySelector(".qr-code-slot")
9498

@@ -112,33 +116,51 @@ class ArgentModal {
112116
}
113117

114118
public showApprovalModal(_: RequestArguments): void {
115-
if (device === "desktop") {
119+
const href = encodeURIComponent(window.location.href)
120+
121+
const urls = {
122+
desktop: `${this.bridgeUrl}?action=sign&device=desktop&href=${href}`,
123+
ios: `${this.mobileUrl}app/wc/request?href=${href}&device=mobile`,
124+
android: `${this.mobileUrl}app/wc/request?href=${href}&device=mobile`,
125+
}
126+
127+
if (!isInArgentMobileAppBrowser()) {
116128
this.getModal(undefined, Layout.approval)
129+
130+
if (["android", "ios"].includes(device)) {
131+
this.openMobileApp(urls)
132+
return
133+
}
117134
return
118135
}
119-
const href = encodeURIComponent(window.location.href)
120136
/*
121137
//https://docs.walletconnect.com/2.0/web3wallet/mobileLinking?platform=ios#ios-wallet-support
122138
Additionally when there is a signing request triggered by the dapp it will hit the deep link with an incomplete URI,
123139
this should be ignored and not considered valid as it's only used for automatically redirecting the users to approve or reject a signing request.
124140
*/
125-
this.showModal(
126-
{
127-
desktop: `${this.bridgeUrl}?action=sign&device=desktop&href=${href}`,
128-
ios: `${this.mobileUrl}app/wc/request?href=${href}&device=mobile`,
129-
android: `${this.mobileUrl}app/wc/request?href=${href}&device=mobile`,
130-
},
131-
undefined,
132-
)
141+
this.showModal(urls, undefined)
133142
}
134143

135-
public closeModal(success?: boolean) {
144+
public closeModal(
145+
{
146+
success,
147+
isRequest,
148+
}: Partial<{ success: boolean; isRequest: boolean }> = {
149+
success: false,
150+
isRequest: false,
151+
},
152+
) {
136153
const modal = this.standaloneConnectorModal
137154
if (success) {
138155
modal?.$set({ layout: Layout.success })
139156
setTimeout(() => modal?.$destroy(), 500)
140157
} else {
141-
modal?.$set({ layout: Layout.failure })
158+
if (!isRequest) {
159+
modal?.$set({ layout: Layout.loginFailure })
160+
} else {
161+
modal?.$set({ layout: Layout.requestFailure })
162+
setTimeout(() => modal?.$destroy(), 500)
163+
}
142164
}
143165
}
144166

@@ -160,7 +182,7 @@ class ArgentModal {
160182
this.standaloneConnectorModal?.$destroy()
161183
await connector?.connect()
162184
} catch (err) {
163-
this.standaloneConnectorModal?.$set({ layout: Layout.failure })
185+
this.standaloneConnectorModal?.$set({ layout: Layout.loginFailure })
164186
}
165187
},
166188
},

src/connectors/argent/argentMobile/modal/login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export const login = async <TAdapter extends NamespaceAdapter>(
117117
// Await session approval from the wallet.
118118
const session = await approval()
119119
adapter.updateSession(session)
120-
argentModal.closeModal(true)
120+
argentModal.closeModal({ success: true })
121121
}
122122

123123
return adapter

src/connectors/argent/argentMobile/modal/starknet/adapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ export class StarknetAdapter
186186
const chainId = this.formatChainId(this.chainId)
187187
argentModal.showApprovalModal(request)
188188
const response = await this.client.request({ topic, chainId, request })
189-
argentModal.closeModal(true)
189+
argentModal.closeModal({ success: true })
190190
return response
191191
} catch (error: any) {
192-
argentModal.closeModal()
192+
argentModal.closeModal({ isRequest: true })
193193
if (error instanceof Error || (error && error.message !== undefined)) {
194194
throw new Error(error.message)
195195
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export const connect = async ({
212212
if (
213213
[Layout.connecting, Layout.qrCode].includes(modal.getLayout())
214214
) {
215-
modal.$set({ layout: Layout.failure })
215+
modal.$set({ layout: Layout.loginFailure })
216216
} else {
217217
reject(error)
218218
}

src/modal/Modal.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import { Braavos } from "../connectors/injected/braavos"
2121
import { getModalWallet } from "../helpers/mapModalWallets"
2222
import { getStoreVersionFromBrowser } from "../helpers/getStoreVersionFromBrowser"
23+
import FailedRequest from "./layouts/FailedRequest.svelte"
2324
2425
let nodeRef: HTMLElement | undefined
2526
@@ -129,13 +130,15 @@
129130
</Connecting>
130131
{:else if layout === Layout.success}
131132
<Success />
132-
{:else if layout === Layout.failure}
133+
{:else if layout === Layout.loginFailure}
133134
<FailedLogin
134135
walletName={selectedConnector?.name}
135136
handleCallback={() => callback(selectedWallet)}
136137
showFallback={showFallback}
137138
handleFallback={() => callback(selectedWallet, true)}
138139
/>
140+
{:else if layout === Layout.requestFailure}
141+
<FailedRequest />
139142
{:else if layout === Layout.qrCode}
140143
<ArgentMobileQR handleInstallClick={() => setLayout(Layout.download)} />
141144
{:else if layout === Layout.approval}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script lang="ts">
2+
import WarningIcon from "../components/icons/WarningIcon.svelte";
3+
</script>
4+
5+
<section class="flex flex-col justify-center items-center flex-grow">
6+
7+
<div class="flex flex-col h-full justify-center items-center gap-4 w-full flex-grow">
8+
<div class="bg-button-secondary rounded-full p-5">
9+
<WarningIcon />
10+
</div>
11+
12+
<div>
13+
<h3 class="text-primary text-h4 font-bold">Couldn't perform the action</h3>
14+
<p class="text-primary text-p3 font-[400]">Please try again.</p>
15+
</div>
16+
17+
</div>
18+
</section>

src/types/modal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export enum Layout {
1717
walletList = "walletList",
1818
connecting = "connecting",
1919
success = "success",
20-
failure = "failure",
20+
loginFailure = "loginFailure",
21+
requestFailure = "requestFailure",
2122
qrCode = "qrCode",
2223
download = "download",
2324
approval = "approval",

0 commit comments

Comments
 (0)