-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
214 lines (188 loc) · 6.1 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import type { BaseError, Platform } from '@reown/appkit-core'
import {
ConnectionController,
ConstantsUtil,
CoreHelperUtil,
EventsController,
ModalController,
OptionsController,
RouterController,
SnackController,
StorageUtil
} from '@reown/appkit-core'
import { customElement } from '@reown/appkit-ui'
import { LitElement, html } from 'lit'
import { state } from 'lit/decorators.js'
@customElement('w3m-connecting-wc-view')
export class W3mConnectingWcView extends LitElement {
// -- Members ------------------------------------------- //
private interval?: ReturnType<typeof setInterval> = undefined
private lastRetry = Date.now()
private wallet = RouterController.state.data?.wallet
// -- State & Properties -------------------------------- //
@state() private platform?: Platform = undefined
@state() private platforms: Platform[] = []
@state() private isSiweEnabled = OptionsController.state.isSiweEnabled
public constructor() {
super()
this.determinePlatforms()
this.initializeConnection()
this.interval = setInterval(
this.initializeConnection.bind(this),
ConstantsUtil.TEN_SEC_MS
) as unknown as NodeJS.Timeout
}
public override disconnectedCallback() {
clearTimeout(this.interval)
}
// -- Render -------------------------------------------- //
public override render() {
return html`
${this.headerTemplate()}
<div>${this.platformTemplate()}</div>
`
}
// -- Private ------------------------------------------- //
private async initializeConnection(retry = false) {
if (this.platform === 'browser') {
/*
* If the platform is browser it means the user is using a browser wallet,
* in this case the connection is handled in w3m-connecting-wc-browser component.
*/
return
}
try {
const { wcPairingExpiry, status } = ConnectionController.state
if (retry || CoreHelperUtil.isPairingExpired(wcPairingExpiry) || status === 'connecting') {
await ConnectionController.connectWalletConnect()
this.finalizeConnection()
if (!this.isSiweEnabled) {
ModalController.close()
}
}
} catch (error) {
EventsController.sendEvent({
type: 'track',
event: 'CONNECT_ERROR',
properties: { message: (error as BaseError)?.message ?? 'Unknown' }
})
ConnectionController.setWcError(true)
if (CoreHelperUtil.isAllowedRetry(this.lastRetry)) {
SnackController.showError((error as BaseError).message ?? 'Declined')
this.lastRetry = Date.now()
this.initializeConnection(true)
} else {
SnackController.showError((error as BaseError).message ?? 'Connection error')
}
}
}
private finalizeConnection() {
const { wcLinking, recentWallet } = ConnectionController.state
if (wcLinking) {
StorageUtil.setWalletConnectDeepLink(wcLinking)
}
if (recentWallet) {
StorageUtil.setAppKitRecent(recentWallet)
}
EventsController.sendEvent({
type: 'track',
event: 'CONNECT_SUCCESS',
properties: {
method: wcLinking ? 'mobile' : 'qrcode',
name: this.wallet?.name || 'Unknown'
}
})
}
private determinePlatforms() {
if (!this.wallet) {
this.platforms.push('qrcode')
this.platform = 'qrcode'
return
}
if (this.platform) {
return
}
const { mobile_link, desktop_link, webapp_link, injected, rdns, name } = this.wallet
const injectedIds = injected?.map(({ injected_id }) => injected_id).filter(Boolean) as string[]
const browserIds = [...(rdns ? [rdns] : injectedIds ?? []), name]
const isBrowser = OptionsController.state.isUniversalProvider ? false : browserIds.length
const isMobileWc = mobile_link
const isWebWc = webapp_link
const isBrowserInstalled = ConnectionController.checkInstalled(browserIds)
const isBrowserWc = isBrowser && isBrowserInstalled
const isDesktopWc = desktop_link && !CoreHelperUtil.isMobile()
// Populate all preferences
if (isBrowserWc) {
this.platforms.push('browser')
}
if (isMobileWc) {
this.platforms.push(CoreHelperUtil.isMobile() ? 'mobile' : 'qrcode')
}
if (isWebWc) {
this.platforms.push('web')
}
if (isDesktopWc) {
this.platforms.push('desktop')
}
if (!isBrowserWc && isBrowser) {
this.platforms.push('unsupported')
}
this.platform = this.platforms[0]
}
private platformTemplate() {
switch (this.platform) {
case 'browser':
return html`<w3m-connecting-wc-browser></w3m-connecting-wc-browser>`
case 'web':
return html`<w3m-connecting-wc-web></w3m-connecting-wc-web>`
case 'desktop':
return html`
<w3m-connecting-wc-desktop .onRetry=${() => this.initializeConnection(true)}>
</w3m-connecting-wc-desktop>
`
case 'mobile':
return html`
<w3m-connecting-wc-mobile isMobile .onRetry=${() => this.initializeConnection(true)}>
</w3m-connecting-wc-mobile>
`
case 'qrcode':
return html`<w3m-connecting-wc-qrcode></w3m-connecting-wc-qrcode>`
default:
return html`<w3m-connecting-wc-unsupported></w3m-connecting-wc-unsupported>`
}
}
private headerTemplate() {
const multiPlatform = this.platforms.length > 1
if (!multiPlatform) {
return null
}
return html`
<w3m-connecting-header
.platforms=${this.platforms}
.onSelectPlatfrom=${this.onSelectPlatform.bind(this)}
>
</w3m-connecting-header>
`
}
private async onSelectPlatform(platform: Platform) {
const container = this.shadowRoot?.querySelector('div')
if (container) {
await container.animate([{ opacity: 1 }, { opacity: 0 }], {
duration: 200,
fill: 'forwards',
easing: 'ease'
}).finished
this.platform = platform
container.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 200,
fill: 'forwards',
easing: 'ease'
})
}
}
}
declare global {
interface HTMLElementTagNameMap {
'w3m-connecting-wc-view': W3mConnectingWcView
}
}