-
Notifications
You must be signed in to change notification settings - Fork 865
/
menu.js
179 lines (155 loc) · 3.82 KB
/
menu.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
import React, {Component} from 'react'
import {ipcRenderer} from 'electron'
import {DragDropContext} from 'react-dnd'
import HTML5Backend from 'react-dnd-html5-backend'
import Pane from '../components/view/pane'
import Loader from '../components/view/loader'
import MenuOption from '../components/view/menu-option'
import Files from '../panes/files'
import Peers from '../panes/peers'
import NodeInfo from '../panes/node-info'
import Settings from '../panes/settings'
const UNINITIALIZED = 'uninitialized'
const RUNNING = 'running'
const STARTING = 'starting'
const STOPPING = 'stopping'
const panes = {
nodeInfo: {
option: {
name: 'Node',
icon: 'info'
},
render (state) {
return (
<NodeInfo
{...state.stats.node}
running={state.status === RUNNING}
bandwidth={state.stats.bw}
repo={state.stats.repo} />
)
}
},
files: {
option: {
name: 'Files',
icon: 'files'
},
render (state) {
return <Files files={state.files} />
}
},
settings: {
option: {
name: 'Settings',
icon: 'settings'
},
render (state) {
return <Settings settings={state.settings} />
}
},
peers: {
option: {
name: 'Peers',
icon: 'pulse'
},
render (state) {
let location = 'Unknown'
if (state.stats.node) {
location = state.stats.node.location
}
return (
<Peers
peers={state.stats.peers}
location={location} />
)
}
}
}
const panesOrder = [
'nodeInfo',
'files',
'peers',
'settings'
]
class Menu extends Component {
state = {
status: UNINITIALIZED,
route: 'files',
stats: {},
settings: {}
}
listeners = {}
_onSomething = (key) => {
if (this.listeners[key]) {
return this.listeners[key]
}
this.listeners[key] = (event, value) => {
const obj = {}
obj[key] = value
this.setState(obj)
}
return this.listeners[key]
}
_changeRoute = (route) => {
this.setState({route: route})
}
componentDidMount () {
// -- Listen to control events
ipcRenderer.on('node-status', this._onSomething('status'))
ipcRenderer.on('stats', this._onSomething('stats'))
ipcRenderer.on('files', this._onSomething('files'))
ipcRenderer.on('settings', this._onSomething('settings'))
ipcRenderer.send('request-state')
ipcRenderer.send('request-files')
ipcRenderer.send('request-settings')
}
componentWillUnmount () {
// -- Remove control events
ipcRenderer.removeListener('node-status', this._onSomething('status'))
ipcRenderer.removeListener('stats', this._onSomething('stats'))
ipcRenderer.removeListeneron('files', this._onSomething('files'))
ipcRenderer.removeListener('settings', this._onSomething('settings'))
}
_getRouteScreen () {
if (this.state.status === STARTING || this.state.status === STOPPING) {
return (
<Pane>
<Loader key='loader-screen' />
</Pane>
)
}
if (panes.hasOwnProperty(this.state.route)) {
return panes[this.state.route].render(this.state)
}
return (
<Pane class='left-pane'>
<p className='notice'>
Hmmm... Something strange happened and you should not be here.
</p>
</Pane>
)
}
_getMenu () {
const menu = []
panesOrder.forEach((paneName) => {
const pane = panes[paneName]
menu.push((
<MenuOption
name={pane.option.name}
icon={pane.option.icon}
active={this.state.route === paneName}
onClick={() => this._changeRoute(paneName)} />
))
})
return (
<div className='menu'>{menu}</div>
)
}
render () {
return [
this._getMenu(),
this._getRouteScreen()
]
}
}
export default DragDropContext(HTML5Backend)(Menu)