1
1
import PropTypes from 'prop-types'
2
- import { useState , useEffect , useMemo } from 'react'
2
+ import { useState , useEffect , useMemo , useCallback } from 'react'
3
3
import { useAsync } from 'react-use'
4
4
import { useSWRConfig } from 'swr'
5
5
import { isUndefined } from '@fluent-wallet/checks'
@@ -17,7 +17,12 @@ import {
17
17
CheckCircleFilled ,
18
18
QuestionCircleOutlined ,
19
19
} from '@fluent-wallet/component-icons'
20
- import { DEFAULT_CFX_HDPATH , DEFAULT_ETH_HDPATH } from '@fluent-wallet/consts'
20
+ import {
21
+ DEFAULT_CFX_HDPATH ,
22
+ DEFAULT_ETH_HDPATH ,
23
+ LEDGER_LIVE_PATH ,
24
+ MEW_PATH ,
25
+ } from '@fluent-wallet/consts'
21
26
import { encode } from '@fluent-wallet/base32-address'
22
27
import { TitleNav , CompWithLabel , Avatar , DisplayBalance } from '../../components'
23
28
import {
@@ -34,12 +39,35 @@ import {useLedgerBindingApi} from '../../hooks'
34
39
import useLoading from '../../hooks/useLoading'
35
40
import useImportHWParams from './useImportHWParams'
36
41
import { request } from '../../utils'
42
+ import Dropdown from '@fluent-wallet/component-dropdown'
43
+ import Menu from '../../../../ui/components/Menu'
44
+ import MenuItem from '../../../../ui/components/Menu/MenuItem'
37
45
38
46
const {
39
47
WALLET_IMPORT_HARDWARE_WALLET_ACCOUNT_GROUP_OR_ACCOUNT ,
40
48
WALLETDB_REFETCH_BALANCE ,
41
49
} = RPC_METHODS
42
50
51
+ const confluxLedgerPath = {
52
+ name : 'BIP 44 Standard' ,
53
+ value : DEFAULT_CFX_HDPATH ,
54
+ }
55
+
56
+ const ethereumLedgerPath = [
57
+ {
58
+ name : 'BIP 44 Standard' ,
59
+ value : DEFAULT_ETH_HDPATH ,
60
+ } ,
61
+ {
62
+ name : 'Ledger Live' ,
63
+ value : LEDGER_LIVE_PATH ,
64
+ } ,
65
+ {
66
+ name : 'Legacy' ,
67
+ value : MEW_PATH ,
68
+ } ,
69
+ ]
70
+
43
71
function ImportingResults ( { importStatus} ) {
44
72
const { t} = useTranslation ( )
45
73
const { mutate} = useSWRConfig ( )
@@ -114,14 +142,18 @@ function ImportHwAccount() {
114
142
const { data : importedAddressData } = useQueryImportedAddress ( networkId )
115
143
const ledgerBindingApi = useLedgerBindingApi ( )
116
144
145
+ const [ selectedPath , setSelectedPath ] = useState ( )
146
+
117
147
const { value : addressList , loading} = useAsync ( async ( ) => {
118
148
let addresses = [ ]
149
+ if ( selectedPath === undefined ) return addresses
119
150
try {
120
151
if ( ledgerBindingApi ) {
121
152
addresses = await ledgerBindingApi . getAddressList (
122
153
new Array ( HARDWARE_ACCOUNT_PAGE_LIMIT )
123
154
. fill ( '' )
124
155
. map ( ( _item , index ) => index + offset ) ,
156
+ selectedPath ?. value ,
125
157
)
126
158
}
127
159
} catch ( e ) {
@@ -138,7 +170,13 @@ function ImportHwAccount() {
138
170
return { address : address ?. toLowerCase ?. ( ) , hdPath}
139
171
} )
140
172
: addresses
141
- } , [ offset , ledgerBindingApi ] )
173
+ } , [ selectedPath , offset , ledgerBindingApi ] )
174
+
175
+ const handleSelectChange = useCallback ( path => {
176
+ // if user change the hd path, reset the offset
177
+ setSelectedPath ( path )
178
+ setOffset ( 0 )
179
+ } , [ ] )
142
180
143
181
const { value : deviceInfo } = useAsync ( async ( ) => {
144
182
if ( ledgerBindingApi ) {
@@ -183,6 +221,16 @@ function ImportHwAccount() {
183
221
)
184
222
} , [ checkboxStatusObj ] )
185
223
224
+ useEffect ( ( ) => {
225
+ if ( selectedPath === undefined ) {
226
+ if ( type === NETWORK_TYPE . CFX ) {
227
+ setSelectedPath ( confluxLedgerPath )
228
+ } else if ( type === NETWORK_TYPE . ETH ) {
229
+ setSelectedPath ( ethereumLedgerPath [ 0 ] )
230
+ }
231
+ }
232
+ } , [ type , selectedPath ] )
233
+
186
234
const onSelectAllAccount = ( ) => {
187
235
const ret = { }
188
236
Object . keys ( checkboxStatusObj ) . forEach (
@@ -278,17 +326,38 @@ function ImportHwAccount() {
278
326
className = "mt-5"
279
327
label = { < p className = "text-sm text-gray-40" > { t ( 'hdPath' ) } </ p > }
280
328
>
281
- < Input
282
- value = { `BIP 44 Standard(${
283
- type === NETWORK_TYPE . CFX
284
- ? DEFAULT_CFX_HDPATH
285
- : DEFAULT_ETH_HDPATH
286
- } )`}
287
- width = "w-full box-border"
288
- readOnly
289
- className = "pointer-events-none"
290
- id = "hd-path"
291
- />
329
+ { type === NETWORK_TYPE . CFX ? (
330
+ < Input
331
+ value = { `${ confluxLedgerPath ?. name } (${ confluxLedgerPath ?. value } )` }
332
+ width = "w-full box-border"
333
+ readOnly
334
+ className = "pointer-events-none"
335
+ id = "hd-path"
336
+ />
337
+ ) : (
338
+ < Dropdown
339
+ overlay = {
340
+ < Menu >
341
+ { ethereumLedgerPath . map ( ( { name, value} ) => (
342
+ < MenuItem
343
+ onClick = { ( ) => handleSelectChange ( { name, value} ) }
344
+ selected = { selectedPath ?. value === value }
345
+ containerClassName = "w-full"
346
+ key = { value }
347
+ itemKey = { value }
348
+ > { `${ name } (${ value } )` } </ MenuItem >
349
+ ) ) }
350
+ </ Menu >
351
+ }
352
+ >
353
+ < Input
354
+ value = { `${ selectedPath ?. name } (${ selectedPath ?. value } )` }
355
+ width = "w-full box-border"
356
+ readOnly
357
+ id = "hd-path"
358
+ />
359
+ </ Dropdown >
360
+ ) }
292
361
</ CompWithLabel >
293
362
< CompWithLabel
294
363
label = {
0 commit comments