-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkElement.tsx
More file actions
153 lines (141 loc) · 5.5 KB
/
NetworkElement.tsx
File metadata and controls
153 lines (141 loc) · 5.5 KB
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
import React, {useEffect} from 'react'
import s from './NetworkElement.module.scss'
import Select, {PropsValue, StylesConfig} from 'react-select'
import {useDispatch, useSelector} from 'react-redux'
import {BridgeInitialStateType, changeNetworkThunk} from '../../../redux/bridgeReducer'
import {chainIDs, chainsNames, isLightTheme} from '../../../common/common'
import {
backgroundColor,
backgroundColorLight,
menuColor,
menuColorLight,
swapperAndSwapButtonColor,
swapperAndSwapButtonColorLight,
textColor,
textColorDisabled,
textColorLight
} from '../../../common/styles/variables'
import cn from 'classnames'
import {AppStoreType} from '../../../redux/store'
import {AppStateType, RequestStatusType} from '../../../redux/appReducer'
export const NetworkElement = (props: PropsType) => {
const isTestNets = useSelector<AppStoreType, boolean>(state => state.app.isTestNets)
const { isSelectorsDisabled } = useSelector<AppStoreType, AppStateType>(state => state.app)
const options =
isTestNets
? [
{value: chainIDs.mumbaiTest, label: chainsNames.mumbaiTest},
{value: chainIDs.rinkebyTest, label: chainsNames.rinkebyTest},
{value: chainIDs.coinExTest, label: chainsNames.coinExTest},
{value: chainIDs.moonbeamAlphaTestnet, label: chainsNames.moonbeamAlphaTestnet},
]
: [
{value: chainIDs.eth, label: chainsNames.eth},
{value: chainIDs.bsc, label: chainsNames.bsc},
{value: chainIDs.polygon, label: chainsNames.polygon},
{value: chainIDs.moonriverMainnet, label: chainsNames.moonriverMainnet},
{value: chainIDs.coinex, label: chainsNames.coinex},
]
const selectByArrowColor = isLightTheme ? menuColorLight : menuColor
const backgroundColorFinal = isLightTheme ? backgroundColorLight : backgroundColor
const {
chainID
} = useSelector<AppStoreType, BridgeInitialStateType>(state => state.bridge)
const appStatus = useSelector<AppStoreType, RequestStatusType>(state => state.app.status)
const selectStyles: StylesConfig = {
control: base => ({
...base,
backgroundColor: backgroundColorFinal,
border: 0,
boxShadow: 'none',
color: 'red'
}),
singleValue: base => ({
...base,
color: appStatus === 'loading' || isSelectorsDisabled
? textColorDisabled
: isLightTheme ? textColorLight : textColor
}),
menuList: base => ({
...base,
backgroundColor: backgroundColorFinal,
}),
option: (base, {isSelected, isFocused}) => ({
...base,
backgroundColor: isSelected
? selectByArrowColor
: isFocused
? selectByArrowColor
: backgroundColorFinal,
color: isLightTheme ? textColorLight : textColor,
':hover': {
...base[':hover'],
backgroundColor: isLightTheme ? swapperAndSwapButtonColorLight : swapperAndSwapButtonColor,
color: 'white'
},
}),
dropdownIndicator: base => ({
...base,
color: (chainID === chainIDs.notSelected || appStatus === 'loading' || isSelectorsDisabled)
? textColorDisabled
: isLightTheme ? textColorLight : textColor
}),
placeholder: base => ({
...base,
color: (chainID === chainIDs.notSelected || appStatus === 'loading' || isSelectorsDisabled)
? textColorDisabled
: isLightTheme ? textColorLight : textColor
})
}
const dispatch = useDispatch()
useEffect(() => {
//change network in Metamask
if (props.isMain && props.state !== chainIDs.notSelected) dispatch(changeNetworkThunk(props.state))
}, [props.state])
const onChange = (option: PropsValue<Option | Option[]>) => {
props.setState((option as Option).value)
}
const getValue = () => {
if (options) {
return props.state ? options.find((option) => option.value === props.state) : null
} else {
return 0 as any // todo: fix any
}
}
// const selectValue = props.state ? options.find((option) => option.value === props.state) : null
return <div className={s.networkElement}>
<span>{props.text}</span>
<div className={isLightTheme ? `${s.item} ${s.lightTheme}` : `${s.item}`}>
<div className={isLightTheme ? cn(s.circle, s.lightTheme) : cn(s.circle)}>
{
props.chainPicture !== '' &&
<img src={props.chainPicture} alt="chainSymbol" className={s.chainSymbol}/>
}
</div>
<Select
options={options}
styles={selectStyles}
name={'select'}
onChange={onChange}
placeholder={'Select Network'}
value={getValue()}
isDisabled={props.isDisabled || isSelectorsDisabled}
components={{
IndicatorSeparator: () => null
}}
/>
</div>
</div>
}
type Option = {
label: string;
value: number;
}
type PropsType = {
text: string
isMain?: boolean
state: number
setState: (value: number) => void
isDisabled: boolean
chainPicture: string
}