forked from 1Hive/gardens-ui
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAccountModule.js
259 lines (233 loc) · 7.01 KB
/
AccountModule.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useWallet } from 'use-wallet'
import {
Button,
GU,
IconConnect,
springs,
shortenAddress,
} from '@tecommons/ui'
import { Transition, animated } from 'react-spring/renderprops'
import ScreenError from './ScreenError'
import AccountButton from './AccountButton'
import ScreenProviders from './ScreenProviders'
import ScreenConnected from './ScreenConnected'
import ScreenConnecting from './ScreenConnecting'
import HeaderPopover from '../Header/HeaderPopover'
import { useAppState } from '../../providers/AppState'
import { getUseWalletProviders } from '../../lib/web3-utils'
const AnimatedDiv = animated.div
const SCREENS = [
{
id: 'providers',
title: 'Ethereum providers',
height:
4 * GU + // header
(12 + 1.5) * GU * Math.ceil(getUseWalletProviders().length / 2) + // buttons
7 * GU, // footer
},
{
id: 'connecting',
title: 'Ethereum providers',
height: 38 * GU,
},
{
id: 'connected',
title: 'Active wallet',
height: 22 * GU,
},
{
id: 'error',
title: 'Ethereum providers',
height: 50 * GU,
},
]
function AccountModule({ compact }) {
const buttonRef = useRef()
const wallet = useWallet()
const [opened, setOpened] = useState(false)
const [animate, setAnimate] = useState(false)
const [activatingDelayed, setActivatingDelayed] = useState(false)
const [activationError, setActivationError] = useState(null)
const popoverFocusElement = useRef()
const { account, activating } = wallet
const { isLoading } = useAppState()
const clearError = useCallback(() => setActivationError(null), [])
const toggle = useCallback(() => setOpened(opened => !opened), [])
const handleCancelConnection = useCallback(() => {
wallet.deactivate()
}, [wallet])
const activate = useCallback(
async providerId => {
try {
await wallet.activate(providerId)
} catch (error) {
setActivationError(error)
}
},
[wallet]
)
// Don’t animate the slider until the popover has opened
useEffect(() => {
if (!opened) {
return
}
setAnimate(false)
const timer = setTimeout(() => {
setAnimate(true)
}, 0)
return () => clearTimeout(timer)
}, [opened])
// Always show the “connecting…” screen, even if there are no delay
useEffect(() => {
if (activationError) {
setActivatingDelayed(null)
}
if (activating) {
setActivatingDelayed(activating)
return
}
const timer = setTimeout(() => {
setActivatingDelayed(null)
}, 500)
return () => {
clearTimeout(timer)
}
}, [activating, activationError])
const previousScreenIndex = useRef(-1)
const { screenIndex, direction } = useMemo(() => {
const screenId = (() => {
if (activationError) return 'error'
if (activatingDelayed) return 'connecting'
if (account) return 'connected'
return 'providers'
})()
const screenIndex = SCREENS.findIndex(screen => screen.id === screenId)
const direction = previousScreenIndex.current > screenIndex ? -1 : 1
previousScreenIndex.current = screenIndex
return { direction, screenIndex }
}, [account, activationError, activatingDelayed])
const screen = SCREENS[screenIndex]
const screenId = screen.id
const handlePopoverClose = useCallback(
reject => {
if (screenId === 'connecting' || screenId === 'error') {
// reject closing the popover
return false
}
setOpened(false)
setActivationError(null)
},
[screenId]
)
// Prevents to lose the focus on the popover when a screen leaves while an
// element inside is focused (e.g. when clicking on the “disconnect” button).
useEffect(() => {
if (popoverFocusElement.current) {
popoverFocusElement.current.focus()
}
}, [screenId])
return (
<div
ref={buttonRef}
tabIndex="0"
css={`
display: flex;
align-items: center;
justify-content: space-around;
outline: 0;
`}
>
{screen.id === 'connected' ? (
<AccountButton
label={shortenAddress(wallet.account)}
onClick={toggle}
/>
) : (
<Button
icon={<IconConnect />}
label="Enable account"
onClick={toggle}
display={compact ? 'icon' : 'all'}
disabled={isLoading}
css={`
right: 56px;
`}
/>
)}
<HeaderPopover
animateHeight={animate}
heading={screen.title}
height={screen.height}
width={51 * GU}
onClose={handlePopoverClose}
opener={buttonRef.current}
visible={opened}
>
<div ref={popoverFocusElement} tabIndex="0" css="outline: 0">
<Transition
native
immediate={!animate}
config={springs.smooth}
items={{
screen,
// This is needed because use-wallet throws an error when the
// activation fails before React updates the state of `activating`.
// A future version of use-wallet might return an
// `activationError` object instead, making this unnecessary.
activating: screen.id === 'error' ? null : activatingDelayed,
wallet,
}}
keys={({ screen }) => screen.id + activatingDelayed}
from={{
opacity: 0,
transform: `translate3d(${3 * GU * direction}px, 0, 0)`,
}}
enter={{ opacity: 1, transform: `translate3d(0, 0, 0)` }}
leave={{
opacity: 0,
transform: `translate3d(${3 * GU * -direction}px, 0, 0)`,
}}
>
{({ screen, activating, wallet }) => ({ opacity, transform }) => (
<AnimatedDiv
style={{ opacity, transform }}
css={`
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
`}
>
{(() => {
if (screen.id === 'connecting') {
return (
<ScreenConnecting
providerId={activating}
onCancel={handleCancelConnection}
/>
)
}
if (screen.id === 'connected') {
return <ScreenConnected wallet={wallet} />
}
if (screen.id === 'error') {
return (
<ScreenError
error={activationError}
onBack={clearError}
/>
)
}
return <ScreenProviders onActivate={activate} />
})()}
</AnimatedDiv>
)}
</Transition>
</div>
</HeaderPopover>
</div>
)
}
export default AccountModule