This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
/
activity.js
469 lines (430 loc) · 13.2 KB
/
activity.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { createSelector } from 'reselect'
import { decodePayReq, getNodeAlias } from '@zap/utils/crypto'
import { openModal, closeModal } from './modal'
import { fetchDescribeNetwork } from './network'
import { fetchTransactions, transactionsSelectors } from './transaction'
import { fetchPayments } from './payment'
import { fetchInvoices } from './invoice'
import { fetchBalance } from './balance'
import { fetchChannels } from './channels'
// ------------------------------------
// Initial State
// ------------------------------------
const initialState = {
filter: 'ALL_ACTIVITY',
filters: [
{ key: 'ALL_ACTIVITY' },
{ key: 'SENT_ACTIVITY' },
{ key: 'RECEIVED_ACTIVITY' },
{ key: 'PENDING_ACTIVITY' },
{ key: 'EXPIRED_ACTIVITY' },
{ key: 'INTERNAL_ACTIVITY' },
],
modal: {
itemType: null,
itemId: null,
},
searchText: '',
showExpiredRequests: false,
isActivityLoading: false,
activityLoadingError: null,
}
// ------------------------------------
// Helpers
// ------------------------------------
/**
* Check wether a prop exists and contains a given search string.
* @param {string} prop Prop name
* @return {boolean} Boolean indicating if the prop was found and contains the search string.
*/
const propMatches = function(prop) {
const { item, searchText } = this
return item[prop] && item[prop].includes(searchText)
}
// ------------------------------------
// Constants
// ------------------------------------
export const SHOW_ACTIVITY_MODAL = 'SHOW_ACTIVITY_MODAL'
export const HIDE_ACTIVITY_MODAL = 'HIDE_ACTIVITY_MODAL'
export const CHANGE_FILTER = 'CHANGE_FILTER'
export const TOGGLE_EXPIRED_REQUESTS = 'TOGGLE_EXPIRED_REQUESTS'
export const UPDATE_SEARCH_TEXT = 'UPDATE_SEARCH_TEXT'
export const FETCH_ACTIVITY_HISTORY = 'FETCH_ACTIVITY_HISTORY'
export const FETCH_ACTIVITY_HISTORY_SUCCESS = 'FETCH_ACTIVITY_HISTORY_SUCCESS'
export const FETCH_ACTIVITY_HISTORY_FAILURE = 'FETCH_ACTIVITY_HISTORY_FAILURE'
// ------------------------------------
// Actions
// ------------------------------------
export function showActivityModal(itemType, itemId) {
return dispatch => {
dispatch({ type: SHOW_ACTIVITY_MODAL, itemType, itemId })
dispatch(openModal('ACTIVITY_MODAL'))
}
}
export function hideActivityModal() {
return dispatch => {
dispatch({ type: HIDE_ACTIVITY_MODAL })
dispatch(closeModal('ACTIVITY_MODAL'))
}
}
export function changeFilter(filter) {
return {
type: CHANGE_FILTER,
filter,
}
}
export function updateSearchText(searchText) {
return {
type: UPDATE_SEARCH_TEXT,
searchText,
}
}
export function toggleExpiredRequests() {
return {
type: TOGGLE_EXPIRED_REQUESTS,
}
}
/**
* Fetches user activity history. Which includes:
* Balance
* Payments
* Invoices
* Transactions
*/
export const fetchActivityHistory = () => dispatch => {
dispatch({ type: FETCH_ACTIVITY_HISTORY })
try {
dispatch(fetchDescribeNetwork())
dispatch(fetchChannels())
dispatch(fetchBalance())
dispatch(fetchPayments())
dispatch(fetchInvoices())
dispatch(fetchTransactions())
dispatch({ type: FETCH_ACTIVITY_HISTORY_SUCCESS })
} catch (error) {
dispatch({ type: FETCH_ACTIVITY_HISTORY_FAILURE, error })
}
}
// ------------------------------------
// Action Handlers
// ------------------------------------
const ACTION_HANDLERS = {
[SHOW_ACTIVITY_MODAL]: (state, { itemType, itemId }) => ({
...state,
modal: { itemType, itemId },
}),
[HIDE_ACTIVITY_MODAL]: state => ({ ...state, modal: { itemType: null, itemId: null } }),
[CHANGE_FILTER]: (state, { filter }) => ({ ...state, filter }),
[TOGGLE_EXPIRED_REQUESTS]: state => ({
...state,
showExpiredRequests: !state.showExpiredRequests,
}),
[UPDATE_SEARCH_TEXT]: (state, { searchText }) => ({ ...state, searchText }),
[FETCH_ACTIVITY_HISTORY]: state => ({ ...state, isActivityLoading: true }),
[FETCH_ACTIVITY_HISTORY_SUCCESS]: state => ({ ...state, isActivityLoading: false }),
[FETCH_ACTIVITY_HISTORY_FAILURE]: (state, { error }) => ({
...state,
isActivityLoading: false,
activityLoadingError: error,
}),
}
// ------------------------------------
// Selectors
// ------------------------------------
const activitySelectors = {}
const filtersSelector = state => state.activity.filters
const filterSelector = state => state.activity.filter
const searchSelector = state => state.activity.searchText
const paymentsSelector = state => state.payment.payments
const paymentsSendingSelector = state => state.payment.paymentsSending
const invoicesSelector = state => state.invoice.invoices
const transactionsSelector = state => transactionsSelectors.transactionsSelector(state)
const transactionsSendingSelector = state => state.transaction.transactionsSending
const modalItemTypeSelector = state => state.activity.modal.itemType
const modalItemIdSelector = state => state.activity.modal.itemId
const nodesSelector = state => state.network.nodes
const invoiceExpired = invoice => {
const expiresAt = parseInt(invoice.creation_date, 10) + parseInt(invoice.expiry, 10)
return expiresAt < Math.round(new Date() / 1000)
}
/**
* Augment payments with additional data.
*/
const decoratedPaymentsSelector = createSelector(
paymentsSelector,
nodesSelector,
(payments, nodes) =>
payments.map(payment => {
const destPubKey = payment.path && payment.path[payment.path.length - 1]
payment.dest_node_pubkey = destPubKey ? destPubKey : null
payment.dest_node_alias = destPubKey ? getNodeAlias(destPubKey, nodes) : null
return payment
})
)
/**
* Map sending payments to something that looks like normal payments.
*/
const paymentsSending = createSelector(
paymentsSendingSelector,
paymentsSending => {
const payments = paymentsSending.map(payment => {
const invoice = decodePayReq(payment.paymentRequest)
return {
type: 'payment',
creation_date: payment.creation_date,
value: payment.amt,
path: [invoice.payeeNodeKey],
payment_hash: invoice.tags.find(t => t.tagName === 'payment_hash').data,
sending: true,
status: payment.status,
error: payment.error,
}
})
return payments
}
)
/**
* Map sending transactions to something that looks like normal transactions.
*/
const transactionsSending = createSelector(
transactionsSendingSelector,
transactionsSending => {
const transactions = transactionsSending.map(transaction => {
return {
type: 'transaction',
time_stamp: transaction.timestamp,
amount: transaction.amount,
sending: true,
status: transaction.status,
error: transaction.error,
}
})
return transactions
}
)
activitySelectors.activityModalItem = createSelector(
decoratedPaymentsSelector,
invoicesSelector,
transactionsSelector,
modalItemTypeSelector,
modalItemIdSelector,
(payments, invoices, transactions, itemType, itemId) => {
switch (itemType) {
case 'INVOICE':
return invoices.find(invoice => invoice.payment_request === itemId)
case 'TRANSACTION':
return transactions.find(transaction => transaction.tx_hash === itemId)
case 'PAYMENT':
return payments.find(payment => payment.payment_hash === itemId)
default:
return null
}
}
)
// helper function that returns invoice, payment or transaction timestamp
function returnTimestamp(activity) {
switch (activity.type) {
case 'transaction':
return activity.time_stamp
case 'invoice':
return activity.settled ? activity.settle_date : activity.creation_date
case 'payment':
return activity.creation_date
}
}
// getMonth() returns the month in 0 index (0 for Jan), so we create an arr of the
// string representation we want for the UI
const months = [
'Jan',
'Feb',
'Mar',
'April',
'May',
'June',
'July',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]
/**
* Sorts data by date and inserts grouping titles
* @param {*} data
*/
function groupAll(data) {
// according too https://stackoverflow.com/a/11252167/3509860
// this provides an accurate measurement including handling of DST
const daysBetween = (t1, t2) => Math.round((t2 - t1) / 86400)
const createTitle = entry => {
const d = new Date(returnTimestamp(entry) * 1000)
const date = d.getDate()
return `${months[d.getMonth()]} ${date}, ${d.getFullYear()}`
}
return data
.sort((a, b) => returnTimestamp(b) - returnTimestamp(a))
.reduce((acc, next) => {
const prev = acc[acc.length - 1]
//check if need insert a group title
if (prev) {
const days = daysBetween(returnTimestamp(next), returnTimestamp(prev))
if (days >= 1) {
acc.push({ title: createTitle(next) })
}
} else {
//This is a very first row. Insert title here too
acc.push({ title: createTitle(next) })
}
acc.push(next)
return acc
}, [])
}
/**
* Filter activity list by checking various properties against a given search string.
* @param {Array} data Activity item list
* @param {string} searchText Search text
* @return {Array} Filtered activity list
*/
const applySearch = (data, searchText) => {
if (!searchText) {
return data
}
return data.filter(item => {
// Check basic props for a match.
const hasPropMatch = [
'tx_hash',
'payment_hash',
'payment_preimage',
'payment_request',
'dest_node_pubkey',
'dest_node_alias',
].some(propMatches, { item, searchText })
// Check every destination address.
const hasAddressMatch =
item.dest_addresses && item.dest_addresses.find(addr => addr.includes(searchText))
// Include the item if at least one search critera matches.
return hasPropMatch || hasAddressMatch
})
}
const prepareData = (data, searchText) => {
return groupAll(applySearch(data, searchText))
}
const allActivity = createSelector(
searchSelector,
paymentsSending,
transactionsSending,
decoratedPaymentsSelector,
transactionsSelector,
invoicesSelector,
(searchText, paymentsSending, transactionsSending, payments, transactions, invoices) => {
const allData = [
...paymentsSending,
...transactionsSending,
...payments,
...transactions.filter(
transaction => !transaction.isFunding && !transaction.isClosing && !transaction.isPending
),
...invoices.filter(invoice => invoice.settled || !invoiceExpired(invoice)),
]
return prepareData(allData, searchText)
}
)
const sentActivity = createSelector(
searchSelector,
paymentsSending,
transactionsSending,
decoratedPaymentsSelector,
transactionsSelector,
(searchText, paymentsSending, transactionsSending, payments, transactions) => {
const allData = [
...paymentsSending,
...transactionsSending,
...payments,
...transactions.filter(
transaction =>
!transaction.received &&
!transaction.isFunding &&
!transaction.isClosing &&
!transaction.isPending
),
]
return prepareData(allData, searchText)
}
)
const receivedActivity = createSelector(
searchSelector,
invoicesSelector,
transactionsSelector,
(searchText, invoices, transactions) => {
const allData = [
...invoices.filter(invoice => invoice.settled),
...transactions.filter(
transaction =>
transaction.received &&
!transaction.isFunding &&
!transaction.isClosing &&
!transaction.isPending
),
]
return prepareData(allData, searchText)
}
)
const pendingActivity = createSelector(
searchSelector,
paymentsSending,
transactionsSending,
transactionsSelector,
invoicesSelector,
(searchText, paymentsSending, transactionsSending, transactions, invoices) => {
const allData = [
...paymentsSending,
...transactionsSending,
...transactions.filter(transaction => transaction.isPending),
...invoices.filter(invoice => !invoice.settled && !invoiceExpired(invoice)),
]
return prepareData(allData, searchText)
}
)
const expiredActivity = createSelector(
searchSelector,
invoicesSelector,
(searchText, invoices) => {
const allData = invoices.filter(invoice => !invoice.settled && invoiceExpired(invoice))
return prepareData(allData, searchText)
}
)
const internalActivity = createSelector(
searchSelector,
transactionsSelector,
(searchText, transactions) => {
const allData = transactions.filter(
transaction => transaction.isFunding || (transaction.isClosing && !transaction.isPending)
)
return prepareData(allData, searchText)
}
)
const FILTERS = {
ALL_ACTIVITY: allActivity,
SENT_ACTIVITY: sentActivity,
RECEIVED_ACTIVITY: receivedActivity,
PENDING_ACTIVITY: pendingActivity,
EXPIRED_ACTIVITY: expiredActivity,
INTERNAL_ACTIVITY: internalActivity,
}
activitySelectors.currentActivity = createSelector(
filterSelector,
filter => FILTERS[filter]
)
activitySelectors.nonActiveFilters = createSelector(
filtersSelector,
filterSelector,
(filters, filter) => filters.filter(f => f.key !== filter)
)
export { activitySelectors }
// ------------------------------------
// Reducer
// ------------------------------------
export default function activityReducer(state = initialState, action) {
const handler = ACTION_HANDLERS[action.type]
return handler ? handler(state, action) : state
}