-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
163 lines (145 loc) · 4.9 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
import { customElement } from '@reown/appkit-ui'
import { LitElement, html } from 'lit'
import styles from './styles.js'
import {
AccountController,
ChainController,
CoreHelperUtil,
RouterController,
SendController
} from '@reown/appkit-core'
import { state } from 'lit/decorators.js'
import type { Balance } from '@reown/appkit-common'
@customElement('w3m-wallet-send-select-token-view')
export class W3mSendSelectTokenView extends LitElement {
public static override styles = styles
// -- Members ------------------------------------------- //
private unsubscribe: (() => void)[] = []
// -- State & Properties -------------------------------- //
@state() private tokenBalance = AccountController.state.tokenBalance
@state() private tokens?: Balance[]
@state() private filteredTokens?: Balance[]
@state() private search = ''
// -- Lifecycle ----------------------------------------- //
public constructor() {
super()
this.unsubscribe.push(
...[
AccountController.subscribe(val => {
this.tokenBalance = val.tokenBalance
})
]
)
}
public override disconnectedCallback() {
this.unsubscribe.forEach(unsubscribe => unsubscribe())
}
// -- Render -------------------------------------------- //
public override render() {
return html`
<wui-flex flexDirection="column">
${this.templateSearchInput()} <wui-separator></wui-separator> ${this.templateTokens()}
</wui-flex>
`
}
// -- Private ------------------------------------------- //
private templateSearchInput() {
return html`
<wui-flex gap="xs" padding="s">
<wui-input-text
@inputChange=${this.onInputChange.bind(this)}
class="network-search-input"
size="sm"
placeholder="Search token"
icon="search"
></wui-input-text>
</wui-flex>
`
}
private templateTokens() {
this.tokens = this.tokenBalance?.filter(
token => token.chainId === ChainController.state.activeCaipNetwork?.caipNetworkId
)
if (this.search) {
this.filteredTokens = this.tokenBalance?.filter(token =>
token.name.toLowerCase().includes(this.search.toLowerCase())
)
} else {
this.filteredTokens = this.tokens
}
return html`
<wui-flex
class="contentContainer"
flexDirection="column"
.padding=${['0', 's', '0', 's'] as const}
>
<wui-flex justifyContent="flex-start" .padding=${['m', 's', 's', 's'] as const}>
<wui-text variant="paragraph-500" color="fg-200">Your tokens</wui-text>
</wui-flex>
<wui-flex flexDirection="column" gap="xs">
${this.filteredTokens && this.filteredTokens.length > 0
? this.filteredTokens.map(
token =>
html`<wui-list-token
@click=${this.handleTokenClick.bind(this, token)}
?clickable=${true}
tokenName=${token.name}
tokenImageUrl=${token.iconUrl}
tokenAmount=${token.quantity.numeric}
tokenValue=${token.value}
tokenCurrency=${token.symbol}
></wui-list-token>`
)
: html`<wui-flex
.padding=${['4xl', '0', '0', '0'] as const}
alignItems="center"
flexDirection="column"
gap="l"
>
<wui-icon-box
icon="coinPlaceholder"
size="inherit"
iconColor="fg-200"
backgroundColor="fg-200"
iconSize="lg"
></wui-icon-box>
<wui-flex
class="textContent"
gap="xs"
flexDirection="column"
justifyContent="center"
flexDirection="column"
>
<wui-text variant="paragraph-500" align="center" color="fg-100"
>No tokens found</wui-text
>
<wui-text variant="small-400" align="center" color="fg-200"
>Your tokens will appear here</wui-text
>
</wui-flex>
<wui-link @click=${this.onBuyClick.bind(this)}>Buy</wui-link>
</wui-flex>`}
</wui-flex>
</wui-flex>
`
}
private onBuyClick() {
RouterController.push('OnRampProviders')
}
private onInputChange(event: CustomEvent<string>) {
this.onDebouncedSearch(event.detail)
}
private onDebouncedSearch = CoreHelperUtil.debounce((value: string) => {
this.search = value
})
private handleTokenClick(token: Balance) {
SendController.setToken(token)
SendController.setTokenAmount(undefined)
RouterController.goBack()
}
}
declare global {
interface HTMLElementTagNameMap {
'w3m-wallet-send-select-token-view': W3mSendSelectTokenView
}
}