-
Notifications
You must be signed in to change notification settings - Fork 820
/
container.tsx
328 lines (289 loc) · 11.3 KB
/
container.tsx
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) 2020 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at https://mozilla.org/MPL/2.0/.
import * as React from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { Redirect, Route, Switch, useHistory, useLocation } from 'react-router-dom'
// utils
import { getWalletLocationTitle } from '../utils/string-utils'
// actions
import * as WalletPageActions from './actions/wallet_page_actions'
import * as WalletActions from '../common/actions/wallet_actions'
// selectors
import { WalletSelectors } from '../common/selectors'
import { PageSelectors } from './selectors'
// types
import {
WalletRoutes, WalletState
} from '../constants/types'
// hooks
import { useSafePageSelector, useSafeWalletSelector } from '../common/hooks/use-safe-selector'
// style
import 'emptykit.css'
import {
SimplePageWrapper
} from './screens/page-screen.styles'
// components
import { CryptoView, LockScreen, WalletPageLayout, WalletSubViewLayout } from '../components/desktop'
import { Skeleton } from '../components/shared/loading-skeleton/styles'
import { OnboardingRoutes } from './screens/onboarding/onboarding.routes'
import { BackupWalletRoutes } from './screens/backup-wallet/backup-wallet.routes'
import { FundWalletScreen } from './screens/fund-wallet/fund-wallet'
import { OnboardingSuccess } from './screens/onboarding/onboarding-success/onboarding-success'
import { DepositFundsScreen } from './screens/fund-wallet/deposit-funds'
import { RestoreWallet } from './screens/restore-wallet/restore-wallet'
import { Swap } from './screens/swap/swap'
import { SendScreen } from './screens/send/send-page/send-screen'
import { DevBitcoin } from './screens/dev-bitcoin/dev-bitcoin'
import {
WalletPageWrapper
} from '../components/desktop/wallet-page-wrapper/wallet-page-wrapper'
export const Container = () => {
// routing
let history = useHistory()
const { pathname: walletLocation } = useLocation()
// redux
const dispatch = useDispatch()
const isBitcoinEnabled = useSelector(
({ wallet }: { wallet: WalletState }) => wallet.isBitcoinEnabled
)
// wallet selectors (safe)
const isWalletCreated = useSafeWalletSelector(WalletSelectors.isWalletCreated)
const isWalletLocked = useSafeWalletSelector(WalletSelectors.isWalletLocked)
const isWalletBackedUp = useSafeWalletSelector(WalletSelectors.isWalletBackedUp)
const hasIncorrectPassword = useSafeWalletSelector(WalletSelectors.hasIncorrectPassword)
const hasInitialized = useSafeWalletSelector(WalletSelectors.hasInitialized)
const defaultEthereumWallet = useSafeWalletSelector(WalletSelectors.defaultEthereumWallet)
const defaultSolanaWallet = useSafeWalletSelector(WalletSelectors.defaultSolanaWallet)
const isMetaMaskInstalled = useSafeWalletSelector(WalletSelectors.isMetaMaskInstalled)
// page selectors (safe)
const setupStillInProgress = useSafePageSelector(PageSelectors.setupStillInProgress)
// state
const [sessionRoute, setSessionRoute] = React.useState<string | undefined>(undefined)
const [inputValue, setInputValue] = React.useState<string>('')
// methods
const onToggleShowRestore = React.useCallback(() => {
if (walletLocation === WalletRoutes.Restore) {
// If a user has not yet created a wallet and clicks Restore
// from the panel, we need to route to onboarding if they click back.
if (!isWalletCreated) {
history.push(WalletRoutes.Onboarding)
return
}
// If a user has created a wallet and clicks Restore from the panel
// while the wallet is locked, we need to route to unlock if they click back.
if (isWalletCreated && isWalletLocked) {
history.push(WalletRoutes.Unlock)
}
} else {
history.push(WalletRoutes.Restore)
}
}, [walletLocation])
const unlockWallet = React.useCallback(() => {
dispatch(WalletActions.unlockWallet({ password: inputValue }))
setInputValue('')
if (sessionRoute) {
history.push(sessionRoute)
} else {
history.push(WalletRoutes.PortfolioAssets)
}
}, [inputValue, sessionRoute])
const handlePasswordChanged = React.useCallback((value: string) => {
setInputValue(value)
if (hasIncorrectPassword) {
dispatch(WalletActions.hasIncorrectPassword(false))
}
}, [hasIncorrectPassword])
const onOpenWalletSettings = React.useCallback(() => {
dispatch(WalletPageActions.openWalletSettings())
}, [])
// computed
const walletNotYetCreated = (!isWalletCreated || setupStillInProgress)
// effects
React.useEffect(() => {
if (hasIncorrectPassword) {
// Clear incorrect password
setInputValue('')
}
}, [hasIncorrectPassword])
React.useEffect(() => {
// store the last url before wallet lock
// so that we can return to that page after unlock
if (
walletLocation.includes(WalletRoutes.Accounts) ||
walletLocation.includes(WalletRoutes.Backup) ||
walletLocation.includes(WalletRoutes.DepositFundsPageStart) ||
walletLocation.includes(WalletRoutes.FundWalletPageStart) ||
walletLocation.includes(WalletRoutes.PortfolioAssets) ||
walletLocation.includes(WalletRoutes.PortfolioNFTs) ||
walletLocation.includes(WalletRoutes.Market) ||
walletLocation.includes(WalletRoutes.Swap) ||
walletLocation.includes(WalletRoutes.SendPageStart) ||
walletLocation.includes(WalletRoutes.LocalIpfsNode ||
walletLocation.includes(WalletRoutes.InspectNfts))
) {
setSessionRoute(walletLocation)
}
}, [walletLocation, isWalletCreated])
React.useEffect(() => {
const toobarElement = document.getElementById('toolbar')
const rootElement = document.getElementById('root')
if (toobarElement && rootElement) {
if (
walletLocation === WalletRoutes.Swap ||
walletLocation === WalletRoutes.SendPageStart ||
walletLocation.includes(WalletRoutes.DepositFundsPage) ||
walletLocation.includes(WalletRoutes.FundWalletPage) ||
walletLocation.includes(WalletRoutes.LocalIpfsNode) ||
walletLocation.includes(WalletRoutes.InspectNfts) ||
walletLocation.includes(WalletRoutes.PortfolioAssets) ||
walletLocation.includes(WalletRoutes.PortfolioNFTs) ||
walletLocation.includes(WalletRoutes.Market) ||
walletLocation.includes(WalletRoutes.Activity) ||
walletLocation.includes(WalletRoutes.Accounts)
) {
toobarElement.hidden = true
rootElement.style.setProperty('min-height', '100vh')
return
}
toobarElement.hidden = false
rootElement.style.setProperty('min-height', 'calc(100vh - 56px)')
}
}, [walletLocation])
React.useEffect(() => {
document.title = getWalletLocationTitle(walletLocation)
}, [walletLocation])
React.useEffect(() => {
// clean recovery phrase if not backing up or onboarding on route change
if (
!walletLocation.includes(WalletRoutes.Backup) &&
!walletLocation.includes(WalletRoutes.Onboarding)
) {
dispatch(WalletPageActions.recoveryWordsAvailable({ mnemonic: '' }))
}
}, [walletLocation])
// render
if (!hasInitialized) {
return <Skeleton />
}
return (
<>
<Switch>
{walletNotYetCreated
?
<WalletPageLayout>
<WalletSubViewLayout>
<OnboardingRoutes />
</WalletSubViewLayout>
</WalletPageLayout>
// Post-onboarding flows
: <Switch>
<Route path={WalletRoutes.OnboardingComplete} exact>
<WalletPageLayout>
<WalletSubViewLayout>
<OnboardingSuccess />
</WalletSubViewLayout>
</WalletPageLayout>
</Route>
<Route path={WalletRoutes.Restore} exact={true}>
<WalletPageLayout>
<WalletSubViewLayout>
<SimplePageWrapper>
<RestoreWallet />
</SimplePageWrapper>
</WalletSubViewLayout>
</WalletPageLayout>
</Route>
{isWalletLocked &&
<Route path={WalletRoutes.Unlock} exact={true}>
<WalletPageLayout>
<WalletSubViewLayout>
<SimplePageWrapper>
<LockScreen
value={inputValue}
onSubmit={unlockWallet}
disabled={inputValue === ''}
onPasswordChanged={handlePasswordChanged}
hasPasswordError={hasIncorrectPassword}
onShowRestore={onToggleShowRestore}
/>
</SimplePageWrapper>
</WalletSubViewLayout>
</WalletPageLayout>
</Route>
}
{!isWalletLocked &&
<Route path={WalletRoutes.Backup}>
<WalletPageLayout>
<WalletSubViewLayout>
<SimplePageWrapper>
<BackupWalletRoutes />
</SimplePageWrapper>
</WalletSubViewLayout>
</WalletPageLayout>
</Route>
}
{!isWalletLocked &&
<Route path={WalletRoutes.FundWalletPage} exact>
<WalletPageWrapper
wrapContentInBox={true}
cardWidth={456}
>
<FundWalletScreen />
</WalletPageWrapper>
</Route>
}
{!isWalletLocked &&
<Route path={WalletRoutes.DepositFundsPage} exact>
<WalletPageWrapper
wrapContentInBox={true}
cardWidth={456}
>
<DepositFundsScreen />
</WalletPageWrapper>
</Route>
}
{!isWalletLocked &&
<Route path={WalletRoutes.Swap} exact={true}>
<WalletPageWrapper
hideHeader={true}
hideBackground={true}
>
<Swap />
</WalletPageWrapper>
</Route>
}
{isBitcoinEnabled && !isWalletLocked && (
<Route path={WalletRoutes.DevBitcoin} exact={true}>
<DevBitcoin />
</Route>
)}
{!isWalletLocked &&
<Route path={WalletRoutes.SendPage} exact>
<WalletPageWrapper hideBackground={true}>
<SendScreen />
</WalletPageWrapper>
</Route>
}
{!isWalletLocked &&
<Route path={WalletRoutes.CryptoPage}>
<CryptoView
needsBackup={!isWalletBackedUp}
defaultEthereumWallet={defaultEthereumWallet}
defaultSolanaWallet={defaultSolanaWallet}
onOpenWalletSettings={onOpenWalletSettings}
isMetaMaskInstalled={isMetaMaskInstalled}
sessionRoute={sessionRoute}
/>
</Route>
}
{isWalletLocked && <Redirect to={WalletRoutes.Unlock} />}
{!isWalletLocked && <Redirect to={WalletRoutes.PortfolioAssets} />}
</Switch>
}
</Switch>
</>
)
}
export default Container