forked from duneanalytics/evm.codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainSelector.tsx
119 lines (98 loc) · 3.02 KB
/
ChainSelector.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
import { useContext, useEffect, useMemo, useState, useCallback } from 'react'
import { useRegisterActions, Action } from 'kbar'
import { useRouter } from 'next/router'
import Select, { OnChangeValue, components } from 'react-select'
import { EthereumContext } from 'context/ethereumContext'
import { CURRENT_FORK } from 'util/constants'
import { toKeyIndex } from 'util/string'
import { Icon, Label } from 'components/ui'
const ChainOption = (props: any) => {
const { data, children } = props
const isCurrent = data.value === CURRENT_FORK
return (
<components.Option {...props}>
{children}
{isCurrent && <Label>Live</Label>}
</components.Option>
)
}
const ChainSelector = () => {
const { forks, selectedFork, onForkChange } = useContext(EthereumContext)
const [forkValue, setForkValue] = useState()
const [actions, setActions] = useState<Action[]>([])
const router = useRouter()
const forkOptions = useMemo(
() => forks.map((fork) => ({ value: fork.name, label: fork.name })),
[forks],
)
const defaultForkOption = useMemo(
() => forkOptions.find((fork) => fork.value === selectedFork?.name),
[forkOptions, selectedFork],
)
const handleForkChange = useCallback(
(option: OnChangeValue<any, any>) => {
setForkValue(option)
onForkChange(option.value)
router.query.fork = option.value
router.push(router)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[onForkChange],
)
useEffect(() => {
if (defaultForkOption) {
handleForkChange(defaultForkOption)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultForkOption])
useEffect(() => {
const forkIds: string[] = []
const forkActions = forkOptions.map(
(option: OnChangeValue<any, any>, index) => {
const keyId = toKeyIndex('fork', index)
forkIds.push(keyId)
return {
id: keyId,
name: option.label,
shortcut: [],
keywords: option.label,
section: '',
perform: () => handleForkChange(option),
parent: 'fork',
}
},
)
if (forkIds.length > 0) {
setActions([
...forkActions,
{
id: 'fork',
name: 'Select hardfork…',
shortcut: ['f'],
keywords: 'fork network evm',
section: 'Preferences',
},
])
}
}, [forkOptions, handleForkChange])
useRegisterActions(actions, [actions])
return (
<div className="flex justify-end items-center rounded">
{forks.length > 0 && (
<div className="flex items-center mr-2">
<Icon name="git-branch-line" className="text-indigo-500 mr-2" />
<Select
onChange={handleForkChange}
options={forkOptions}
value={forkValue}
isSearchable={false}
classNamePrefix="select"
menuPlacement="auto"
components={{ Option: ChainOption }}
/>
</div>
)}
</div>
)
}
export default ChainSelector