/
DebugStateBar.js
263 lines (234 loc) · 8.25 KB
/
DebugStateBar.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
260
261
262
263
import React, { PureComponent } from 'react'
import { colors } from '../../constants'
import { promiseWithTimeout } from 'react-relay-network-modern/es/middlewares/retry'
import { Flex, Text } from './index'
import { View, Platform } from 'react-native'
import RelayContext from '../../relay/RelayContext'
import Icon from './Icon'
import NavigationService from '../../helpers/NavigationService'
const daemonStateValues = {
'down': 0,
'connecting': 1,
'connected': 2,
}
class DebugStateBar extends PureComponent {
constructor (props) {
super(props)
this.state = {
watchTime: 10000,
listenAddrs: [],
listenInterfaceAddrs: [],
timeouted: false,
requestTimeout: 3000,
listenAddrTimer: null,
InterfaceAddrTimer: null,
bertyColor: colors.lightGrey,
bleColor: colors.lightGrey,
bgBertyColor: colors.inputGrey,
bgBleColor: colors.inputGrey,
bleText: 'off',
bleState: false,
bertyText: 'no daemon',
daemonState: daemonStateValues.down,
peers: [],
compact: true,
collapsed: true,
}
}
componentDidMount () {
this.fetchListenAddrs()
this.fetchListenInterfaceAddrs()
this.fetchPeers()
this.subscriber = this.props.context.subscriptions.monitorPeers.subscribe(
{
iterator: undefined,
updater: (store, data) => {
const peer = data.MonitorPeers
this.addPeer(peer)
},
}
)
}
componentWillUnmount () {
const { listenAddrTimer, InterfaceAddrTimer } = this.state
if (listenAddrTimer !== null) {
clearTimeout(listenAddrTimer)
}
if (InterfaceAddrTimer !== null) {
clearTimeout(InterfaceAddrTimer)
}
this.subscriber.unsubscribe()
}
timeoutPromise = () => {
return new Promise((resolve, reject) => {
this.setState({ timeouted: true }, this.setColor)
reject(new Error('Request timed out'))
})
}
addPeer = (peer) => {
this.setState(prevState => ({
peers: [...prevState.peers.filter(p => p.id !== peer.id), peer],
}))
}
fetchPeers = () => {
this.props.context.queries.Peers.fetch().then(data =>
this.setState({ peers: data.list })
)
}
fetchListenAddrs = () => {
const { context } = this.props
const { watchTime, requestTimeout, timeouted } = this.state
promiseWithTimeout(context.queries.GetListenAddrs.fetch(), requestTimeout, this.timeoutPromise).then(e => {
const timer = setTimeout(this.fetchListenAddrs, watchTime)
// if we previously timeouted we need to refetch peers
if (timeouted === true) {
this.fetchPeers()
}
this.setState({
listenAddrs: e.addrs,
timeouted: false,
listenAddrTimer: timer,
}, this.setColor)
}).catch(err => {
const timer = setTimeout(this.fetchListenAddrs, watchTime)
this.setState({
listenAddrTimer: timer,
peers: [],
timeouted: true,
listenAddrs: [],
})
console.log('err listen address', err)
})
}
fetchListenInterfaceAddrs = () => {
const { context } = this.props
const { watchTime, requestTimeout, timeouted } = this.state
promiseWithTimeout(context.queries.GetListenInterfaceAddrs.fetch(), requestTimeout, this.timeoutPromise).then(e => {
const timer = setTimeout(this.fetchListenInterfaceAddrs, watchTime)
// if we previously timeouted we need to refetch peers
if (timeouted === true) {
this.fetchPeers()
}
this.setState({
listenInterfaceAddrs: e.addrs,
timeouted: false,
InterfaceAddrTimer: timer,
}, this.setColor)
}).catch(err => {
const timer = setTimeout(this.fetchListenInterfaceAddrs, watchTime)
this.setState({
InterfaceAddrTimer: timer,
peers: [],
timeouted: true,
listenInterfaceAddrs: [],
}, this.setColor)
console.log('err Listen address', err)
})
}
setColor = () => {
const { listenAddrs, listenInterfaceAddrs, timeouted } = this.state
let bertyColor = colors.orange
let bgBertyColor = colors.orange25
let bertyText = 'connecting'
let daemonState = daemonStateValues.connecting
let bleColor = colors.lightGrey
let bgBleColor = colors.inputGrey
let bleText = 'off'
let bleState = false
if (listenAddrs.length > 0 && listenInterfaceAddrs.length > 0) {
listenInterfaceAddrs.forEach((v, i, arr) => {
try {
const splited = v.split('/')
if (splited[1] === 'ip4' && splited[2] !== '127.0.0.1') {
bertyColor = colors.green
bgBertyColor = colors.green25
bertyText = 'connected'
daemonState = daemonStateValues.connected
}
if (splited[1] === 'ble' && splited[2] !== '00000000-0000-0000-0000-000000000000') {
bleColor = colors.green
bgBleColor = colors.green25
bleText = 'on'
bleState = true
}
} catch (e) {
// Silence error since /p2p-circuit isn't valid
// console.log(e)
}
})
}
if (timeouted) {
bertyColor = colors.pink
bgBertyColor = colors.pink25
bertyText = 'no daemon'
daemonState = daemonStateValues.down
}
this.setState({
bertyColor,
bgBertyColor,
bertyText,
bleText,
bleState,
bleColor,
bgBleColor,
daemonState,
})
}
getPeersColor = (peers) => {
if (peers.length > 0) {
return {
bgPeerColor: colors.blue25,
peerColor: colors.blue,
}
}
return {
bgPeerColor: colors.inputGrey,
peerColor: colors.lightGrey,
}
}
render () {
const { bertyColor, bleColor, bleText, bertyText, peers, bgBertyColor, bgBleColor } = this.state
const { bgPeerColor, peerColor } = this.getPeersColor(peers)
const count = peers.reduce((acc, cur) => cur.connection === 1 ? acc + 1 : acc, 0)
return (
<Flex.Cols size={1} style={[{ backgroundColor: '#EAF0FCee', padding: 8, borderRadius: 8 }]}>
{!this.state.collapsed && <View style={{ marginRight: 2 }}>
<Text icon='berty-berty_picto' size={5} padding={5} rounded small background={bgBertyColor} color={bertyColor}>{
this.state.compact ? <Icon name={
this.state.daemonState === daemonStateValues.connected
? 'check'
: this.state.daemonState === daemonStateValues.down
? 'x-circle' : 'more-horizontal'
} color={bertyColor} /> : bertyText.toLocaleUpperCase()
}</Text>
</View>}
{!this.state.collapsed && <View style={{ marginRight: 2 }}>
<Text icon='berty-chart-network-solid' size={5} padding={5} rounded small background={bgPeerColor} color={peerColor} onPress={() => NavigationService.navigate('network/peers')}>{count.toString()}</Text>
</View>}
{!this.state.collapsed && <View style={{ marginRight: 2 }}>
<Text icon='bluetooth' size={5} padding={5} rounded small background={bgBleColor} color={bleColor} onPress={() => NavigationService.navigate('network/config')}>{bleText.toLocaleUpperCase()}</Text>
</View>}
{!this.state.collapsed && <View style={{ marginRight: 2 }}>
<Text icon='settings' size={5} padding={5} rounded small background={colors.darkGrey} color={colors.inputGrey} onPress={() => NavigationService.navigate('settings/devtools')} />
</View>}
<View style={{ marginRight: 2 }}>
<Text icon={this.state.collapsed ? 'chevrons-left' : 'chevrons-right'} size={5} padding={5} rounded small background={this.state.collapsed ? bgBertyColor : colors.inputGrey} color={this.state.collapsed ? bertyColor : colors.darkGrey} onPress={() => this.setState({ collapsed: !this.state.collapsed })} />
</View>
{Platform.OS !== 'android' &&
<View style={{ marginRight: 2 }} {...this.props.panHandlers}>
<Text icon={'menu'} large padding />
</View>
}
</Flex.Cols>
)
}
}
const WrappedDebugStateBar = ({ context, ...props }) => {
if (context !== null) {
return <DebugStateBar context={context} {...props} />
}
return <RelayContext.Consumer>
{context => <DebugStateBar context={context} {...props} />}
</RelayContext.Consumer>
}
export default WrappedDebugStateBar