OSDN Git Service

Merge branch 'dashboardmaster' into dev
[bytom/bytom-electron.git] / src / features / core / reducers.js
1 import { combineReducers } from 'redux'
2 import { testnetUrl } from 'utility/environment'
3 import moment from 'moment'
4 import { DeltaSampler } from 'utility/time'
5
6 const LONG_TIME_FORMAT = 'YYYY-MM-DD, h:mm:ss a'
7
8 const coreConfigReducer = (key, state, defaultState, action) => {
9   if (action.type == 'UPDATE_CORE_INFO') {
10     return action.param.data[key] || defaultState
11   }
12
13   return state || defaultState
14 }
15
16 const buildConfigReducer = (key, state, defaultState, action) => {
17   // if (action.type == 'UPDATE_CORE_INFO') {
18         // return action.param.buildConfig[key] || defaultState
19   // }
20
21   return state || defaultState
22 }
23
24 const configKnown = (state = false, action) => {
25   if (action.type == 'UPDATE_CORE_INFO') {
26     return true
27   }
28   return state
29 }
30
31 // export const configured = (state, action) =>
32 //   coreConfigReducer('isConfigured', state, false, action)
33
34
35 export const configuredAt = (state, action) => {
36   let value = coreConfigReducer('configuredAt', state, '', action)
37   if (action.type == 'UPDATE_CORE_INFO' && value != '') {
38     value = moment(value).format(LONG_TIME_FORMAT)
39   }
40   return value
41 }
42
43 // export const mockhsm = (state, action) =>
44 //   buildConfigReducer('isMockhsm', state, false, action)
45 // export const localhostAuth = (state, action) =>
46 //   buildConfigReducer('isLocalhostAuth', state, false, action)
47 // export const reset = (state, action) =>
48 //   buildConfigReducer('isReset', state, false, action)
49 // export const httpOk = (state, action) =>
50 //   buildConfigReducer('isHttpOk', state, false, action)
51 export const blockHeight = (state, action) =>
52   coreConfigReducer('highestBlock', state, 0, action)
53 export const currentBlockHeight = (state, action) =>
54   coreConfigReducer('currentBlock', state, 0, action)
55 export const peerCount = (state, action) =>
56   coreConfigReducer('peerCount', state, 0, action)
57 export const generatorBlockHeight = (state, action) => {
58   if (action.type == 'UPDATE_CORE_INFO') {
59     if (action.param.generatorBlockHeight == 0) return '???'
60   }
61
62   return coreConfigReducer('generatorBlockHeight', state, 0, action)
63 }
64 export const signer = (state, action) =>
65   coreConfigReducer('isSigner', state, false, action)
66 export const generator = (state, action) =>
67   coreConfigReducer('isGenerator', state, false, action)
68 export const generatorUrl = (state, action) =>
69   coreConfigReducer('generatorUrl', state, false, action)
70 export const generatorAccessToken = (state, action) =>
71   coreConfigReducer('generatorAccessToken', state, false, action)
72 export const blockchainId = (state, action) =>
73   coreConfigReducer('blockchainId', state, 0, action)
74 // export const crosscoreRpcVersion = (state, action) =>
75 //   coreConfigReducer('crosscoreRpcVersion', state, 0, action)
76 //
77 // export const coreType = (state = '', action) => {
78 //   if (action.type == 'UPDATE_CORE_INFO') {
79 //     if (action.param.isGenerator) return 'Generator'
80 //     if (action.param.isSigner) return 'Signer'
81 //     return 'Participant'
82 //   }
83 //   return state
84 // }
85 //
86 // export const replicationLag = (state = null, action) => {
87 //   if (action.type == 'UPDATE_CORE_INFO') {
88 //     if (action.param.generatorBlockHeight == 0) {
89 //       return null
90 //     }
91 //     return action.param.generatorBlockHeight - action.param.blockHeight
92 //   }
93 //
94 //   return state
95 // }
96
97 // let syncSamplers = null
98 // const resetSyncSamplers = () => {
99 //   syncSamplers = {
100 //     snapshot: new DeltaSampler({sampleTtl: 10 * 1000}),
101 //     replicationLag: new DeltaSampler({sampleTtl: 10 * 1000}),
102 //   }
103 // }
104 //
105 // export const syncEstimates = (state = {}, action) => {
106 //   switch (action.type) {
107 //     case 'UPDATE_CORE_INFO': {
108 //       if (!syncSamplers) {
109 //         resetSyncSamplers()
110 //       }
111 //
112 //       const {
113 //         snapshot,
114 //         generatorBlockHeight,
115 //         blockHeight,
116 //       } = action.param
117 //
118 //       const estimates = {}
119 //
120 //       if (snapshot && snapshot.inProgress) {
121 //         const speed = syncSamplers.snapshot.sample(snapshot.downloaded)
122 //
123 //         if (speed != 0) {
124 //           estimates.snapshot = (snapshot.size - snapshot.downloaded) / speed
125 //         }
126 //       } else if (generatorBlockHeight > 0) {
127 //         const replicationLag = generatorBlockHeight - blockHeight
128 //         const speed = syncSamplers.replicationLag.sample(replicationLag)
129 //         if (speed != 0) {
130 //           const duration = -1 * replicationLag / speed
131 //           if (duration > 0) {
132 //             estimates.replicationLag = duration
133 //           }
134 //         }
135 //       }
136 //
137 //       return estimates
138 //     }
139 //
140 //     case 'CORE_DISCONNECT':
141 //       resetSyncSamplers()
142 //       return {}
143 //
144 //     default:
145 //       return state
146 //   }
147 // }
148 //
149 // export const replicationLagClass = (state = null, action) => {
150 //   if (action.type == 'UPDATE_CORE_INFO') {
151 //     if (action.param.generatorBlockHeight == 0) {
152 //       return 'red'
153 //     } else {
154 //       let lag = action.param.generatorBlockHeight - action.param.blockHeight
155 //       if (lag < 5) {
156 //         return 'green'
157 //       } else if (lag < 10) {
158 //         return 'yellow'
159 //       } else {
160 //         return 'red'
161 //       }
162 //     }
163 //   }
164 //
165 //   return state
166 // }
167
168 // export const onTestnet = (state = false, action) => {
169 //   if (action.type == 'UPDATE_CORE_INFO') {
170 //     return (action.param.generatorUrl || '').indexOf(testnetUrl) >= 0
171 //   }
172 //
173 //   return state
174 // }
175
176 export const requireClientToken = (state = false, action) => {
177   if (action.type == 'ERROR' && action.payload.status == 401) return true
178
179   return state
180 }
181
182 export const clientToken = (state = '', action) => {
183   if      (action.type == 'SET_CLIENT_TOKEN') return action.token
184   else if (action.type == 'ERROR' &&
185            action.payload.status == 401)      return ''
186
187   return state
188 }
189
190 export const validToken = (state = false, action) => {
191   if      (action.type == 'SET_CLIENT_TOKEN') return false
192   else if (action.type == 'USER_LOG_IN')      return true
193   else if (action.type == 'ERROR' &&
194            action.payload.status == 401)      return false
195
196   return state
197 }
198
199 export const connected = (state = true, action) => {
200   if      (action.type == 'UPDATE_CORE_INFO') return true
201   else if (action.type == 'CORE_DISCONNECT')  return false
202
203   return state
204 }
205
206 export const btmAmountUnit = (state = 'BTM' , action) => {
207   if (action.type == 'UPDATE_BTM_AMOUNT_UNIT') {
208     return action.param
209   }
210   return state
211 }
212
213 let configuredState = false
214 if(window.remote){
215   configuredState = window.remote.getGlobal('fileExist')
216 }
217 export const configured = (state = configuredState, action) => {
218   if (action.type == 'SET_CONFIGURED') {
219     return true
220   }
221   return state
222 }
223
224 const mingStatus = (state = false, action) => {
225   if (action.type == 'UPDATE_CORE_INFO') {
226     if(window.remote && window.remote.getGlobal('mining').isMining!= action.param.data.mining){
227       window.remote.getGlobal('mining').isMining = action.param.data.mining
228       window.ipcRenderer.send('refresh-menu')
229     }
230
231     return action.param.data.mining
232   }
233   return state
234 }
235
236 const coreData = (state = null, action) => {
237   if (action.type == 'UPDATE_CORE_INFO') {
238     return action.param.data || null
239   }
240   return state
241 }
242
243 const accountInit = (state = false, action) => {
244   if (action.type == 'CREATE_REGISTER_ACCOUNT') {
245     return true
246   }
247   return state
248 }
249
250 const snapshot = (state = null, action) => {
251   if (action.type == 'UPDATE_CORE_INFO') {
252     return action.param.snapshot || null // snapshot may be undefined, which Redux doesn't like.
253   }
254   return state
255 }
256
257 const version = (state = 'N/A', action) => {
258   if (action.type == 'UPDATE_CORE_INFO') {
259     return action.param.data.versionInfo.version
260   }
261   return state
262 }
263
264 const newVersionCode = (state = 'N/A', action) => {
265   if (action.type == 'UPDATE_CORE_INFO') {
266     return action.param.data.versionInfo.newVersion
267   }
268   return state
269 }
270
271 const update = (state = false, action) => {
272   if (action.type == 'UPDATE_CORE_INFO') {
273     return action.param.data.versionInfo.update > 0
274   }
275   return state
276 }
277
278
279 export default combineReducers({
280   accountInit,
281   blockchainId,
282   blockHeight,
283   connected,
284   clientToken,
285   configKnown,
286   configured,
287   configuredAt,
288   // coreType,
289   coreData,
290   currentBlockHeight,
291   generator,
292   generatorAccessToken,
293   generatorBlockHeight,
294   generatorUrl,
295   // localhostAuth,
296   // newVersionCode,
297   // mockhsm,
298   mingStatus,
299   // crosscoreRpcVersion,
300   // onTestnet,
301   // httpOk,
302   peerCount,
303   // replicationLag,
304   // replicationLagClass,
305   requireClientToken,
306   // reset,
307   signer,
308   snapshot,
309   // syncEstimates,
310   update,
311   validToken,
312   version,
313   btmAmountUnit
314 })