-
Notifications
You must be signed in to change notification settings - Fork 7
/
AddressDetailLegacy.js
415 lines (378 loc) · 13.7 KB
/
AddressDetailLegacy.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
/**
* Copyright (c) Hathor Labs and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import React from 'react';
import hathorLib from '@hathor/wallet-lib';
import ReactLoading from 'react-loading';
import { isEqual } from 'lodash';
import AddressSummaryLegacy from './AddressSummaryLegacy';
import AddressHistoryLegacy from './AddressHistoryLegacy';
import PaginationURL from '../utils/pagination';
import colors from '../index.scss';
import WebSocketHandler from '../WebSocketHandler';
import { TX_COUNT } from '../constants';
import helpers from '../utils/helpers';
import metadataApi from '../api/metadataApi';
import addressApiLegacy from '../api/addressApiLegacy';
class AddressDetailLegacy extends React.Component {
pagination = new PaginationURL({
hash: { required: false },
page: { required: false },
token: { required: true },
});
addressSummaryRef = React.createRef();
/*
* address {String} searched address (from url)
* balance {Object} Object with balance of each token of this address indexed by tokenUid {uid1: {'name', 'symbol', 'received', 'spent'}}
* selectedToken {String} UID of the selected token when address has many
* numberOfTransactions {Number} Total number of transactions of the list
* transactions {Array} List of transactions history to show
* hasAfter {boolean} If has more elements after the list page
* hasBefore {boolean} If has more elements before the list page
* queryParams {Object} Object with URL parameters data
* loadingSummary {boolean} If is waiting response of data summary request
* loadingHistory {boolean} If is waiting response of data history request
* errorMessage {String} message to be shown in case of an error
* warningRefreshPage {boolean} If should show a warning to refresh the page to see newest data for the address
* selectedTokenMetadata {Object} Metadata of the selected token
* metadataLoaded {boolean} When the selected token metadata was loaded
*/
state = {
address: null,
balance: {},
selectedToken: '',
numberOfTransactions: 0,
transactions: [],
hasAfter: false,
hasBefore: false,
queryParams: null,
loadingSummary: true,
loadingHistory: false,
errorMessage: '',
warningRefreshPage: false,
selectedTokenMetadata: null,
metadataLoaded: false,
};
componentDidMount() {
// Expects address on URL
this.updateAddress(this.props.match.params.address);
WebSocketHandler.on('network', this.handleWebsocket);
}
componentWillUnmount() {
WebSocketHandler.removeListener('network', this.handleWebsocket);
}
componentDidUpdate(prevProps) {
if (prevProps.match.params.address !== this.props.match.params.address) {
// Address on the URL changed
this.pagination.clearOptionalQueryParams();
this.updateAddress(this.props.match.params.address);
return;
}
const queryParams = this.pagination.obtainQueryParams();
// Do we have new URL params?
if (!isEqual(this.state.queryParams, queryParams)) {
if (queryParams.token !== this.state.queryParams.token && queryParams.token !== null) {
// User selected a new token, so we must go to the first page (clear queryParams)
this.pagination.clearOptionalQueryParams();
// Need to get newQueryParams because the optional ones were cleared
// Update state to set the new selected token on it
// If we don't update this state here we might execute a duplicate request
const newQueryParams = this.pagination.obtainQueryParams();
this.setState({ queryParams: newQueryParams }, () => {
this.getHistoryData(newQueryParams);
});
return;
}
// Fetch new data, unless query params were cleared and we were already in the most recent page
if (queryParams.hash || this.state.hasBefore) {
this.getHistoryData(queryParams);
}
}
}
/**
* Called when 'network' ws message arrives
* If it's a new tx message update data, in case is necessary
*
* @param {Object} wsData Data from websocket
*/
handleWebsocket = wsData => {
if (wsData.type === 'network:new_tx_accepted') {
if (this.shouldUpdate(wsData, false) && !this.state.warningRefreshPage) {
// If the search address is in one of the inputs or outputs
this.setState({ warningRefreshPage: true });
}
}
};
/**
* Update the list with a new tx arrived in a ws message
* Checks if the list is on the first page, so it can have a new element
* Also validates if this tx is compatible with the list
*
* @param {Object} tx Data of a newly arrived tx
*/
updateListWs = tx => {
// We only add new tx/blocks if it's the first page
if (!this.state.hasBefore) {
if (this.shouldUpdate(tx, true)) {
let { transactions } = this.state;
const hasAfter = this.state.hasAfter || transactions.length === TX_COUNT;
transactions = helpers.updateListWs(transactions, tx, TX_COUNT);
const newNumberOfTransactions = this.state.numberOfTransactions + 1;
// Finally we update the state again
this.setState({ transactions, hasAfter, numberOfTransactions: newNumberOfTransactions });
}
}
};
/**
* Check if address is valid and then update the state and get data from full node
* If not valid show error message
*
* @param {Object} address New searched address to update state
*/
updateAddress = address => {
this.setState({ queryParams: this.pagination.obtainQueryParams() }, () => {
const network = hathorLib.config.getNetwork();
const addressObj = new hathorLib.Address(address, { network });
if (addressObj.isValid()) {
this.setState({ address, loadingSummary: true, transactions: [], errorMessage: '' }, () => {
const queryParams = this.pagination.obtainQueryParams();
if (queryParams.token !== null) {
// User already have a token selected on the URL
this.setState({ selectedToken: queryParams.token }, () => {
this.getSummaryData();
this.getHistoryData(this.state.queryParams);
});
} else {
// Will get data and select the default token
// In this case I don't get history data because I still don't know the token
// When the token changes, I will fetch the history data
this.getSummaryData();
}
});
} else {
this.setState({ errorMessage: 'Invalid address.' });
}
});
};
/**
* Update transactions data state after requesting data from the server
*
* @param {Object} queryParams URL parameters
*/
getHistoryData = queryParams => {
addressApiLegacy
.search(this.state.address, TX_COUNT, queryParams.hash, queryParams.page, queryParams.token)
.then(response => {
if (response.success) {
this.handleFetchedData(response, queryParams);
}
// fetch metadata for selected token
this.getSelectedTokenMetadata(queryParams.token);
});
};
/**
* Update component state when new list data arrives
*
* @param {Object} data Response data from the server
* @param {Object} queryParams URL parameters
*/
handleFetchedData = (data, queryParams) => {
// Handle differently if is the first GET response we receive
// page indicates if was clicked 'previous' or 'next'
// Set first and last hash of the transactions
let hasAfter;
let hasBefore;
if (queryParams.page === 'previous') {
hasAfter = true;
hasBefore = data.has_more;
if (!hasBefore) {
// Went back to most recent page: clear URL params
this.pagination.clearOptionalQueryParams();
}
} else if (queryParams.page === 'next') {
hasBefore = true;
hasAfter = data.has_more;
} else {
// First load without parameters
hasBefore = false;
hasAfter = data.has_more;
}
this.setState({
transactions: data.transactions,
loadingHistory: false,
hasAfter,
hasBefore,
queryParams,
numberOfTransactions: data.total,
});
};
/**
* Request data from server and update state balance
*/
getSummaryData = () => {
addressApiLegacy.getBalance(this.state.address).then(response => {
if (response.success) {
let selectedToken = '';
if (this.state.selectedToken && this.state.selectedToken in response.tokens_data) {
// If user had selected a token already, should continue the same
selectedToken = this.state.selectedToken;
} else {
const hathorUID = hathorLib.constants.NATIVE_TOKEN_UID;
if (hathorUID in response.tokens_data) {
// If HTR is in the token list of this address, it's the default selection
selectedToken = hathorUID;
} else {
// Otherwise we get the first element, if there is one
const keys = Object.keys(response.tokens_data);
if (keys.length === 0) {
// In case the length is 0, we have no transactions for this address
this.setState({ loadingSummary: false });
return;
}
[selectedToken] = keys;
}
}
// Update token in the URL
this.updateTokenURL(selectedToken);
this.setState({
balance: response.tokens_data,
loadingSummary: false,
selectedToken,
});
this.getSelectedTokenMetadata(selectedToken);
} else {
this.setState({
loadingSummary: false,
errorMessage: response.message,
});
}
});
};
getSelectedTokenMetadata = selectedToken => {
metadataApi.getDagMetadata(selectedToken).then(data => {
if (data) {
this.setState({ selectedTokenMetadata: data });
}
this.setState({ metadataLoaded: true });
});
};
/**
* Callback to be executed when user changes token on select input
*
* @param {String} Value of the selected item
*/
onTokenSelectChanged = value => {
this.setState({ selectedToken: value, metadataLoaded: false, selectedTokenMetadata: null });
this.updateTokenURL(value);
};
/**
* Update URL with new selected token and trigger didUpdate
*
* @param {String} New token selected
*/
updateTokenURL = token => {
const queryParams = this.pagination.obtainQueryParams();
queryParams.token = token;
const newURL = this.pagination.setURLParameters(queryParams);
this.props.history.push(newURL);
};
/**
* Check if the searched address is on the inputs or outputs of the new tx
*
* @param {Object} tx Transaction data received in the websocket
* @param {boolean} checkToken If should also check if token is the same, or just address
*
* @return {boolean} True if should update the list, false otherwise
*/
shouldUpdate = (tx, checkToken) => {
const arr = [...tx.outputs, ...tx.inputs];
const { token } = this.pagination.obtainQueryParams();
for (const element of arr) {
if (element.decoded.address === this.state.address) {
// Address is the same
if ((checkToken && element.token === token) || !checkToken) {
// Need to check token and token is the same, or no need to check token
return true;
}
}
}
return false;
};
/**
* Redirects to transaction detail screen after clicking on a table row
*
* @param {String} hash Hash of tx clicked
*/
onRowClicked = hash => {
this.props.history.push(`/transaction/${hash}`);
};
/**
* Refresh web page
*
* @param {Event} e Click event
*/
refreshPage = e => {
e.preventDefault();
window.location.reload();
};
render() {
const renderWarningAlert = () => {
if (this.state.warningRefreshPage) {
return (
<div className="alert alert-warning refresh-alert" role="alert">
There is a new transaction for this address. Please{' '}
<a href="true" onClick={this.refreshPage}>
refresh
</a>{' '}
the page to see the newest data.
</div>
);
}
return null;
};
const isNFT = () => {
return this.state.selectedTokenMetadata && this.state.selectedTokenMetadata.nft;
};
const renderData = () => {
if (this.state.errorMessage) {
return <p className="text-danger mt-3">{this.state.errorMessage}</p>;
}
if (this.state.address === null) {
return null;
}
if (this.state.loadingSummary || this.state.loadingHistory) {
return <ReactLoading type="spin" color={colors.purpleHathor} delay={500} />;
}
return (
<div>
{renderWarningAlert()}
<AddressSummaryLegacy
address={this.state.address}
balance={this.state.balance}
selectedToken={this.state.selectedToken}
numberOfTransactions={this.state.numberOfTransactions}
tokenSelectChanged={this.onTokenSelectChanged}
isNFT={isNFT()}
metadataLoaded={this.state.metadataLoaded}
/>
<AddressHistoryLegacy
address={this.state.address}
onRowClicked={this.onRowClicked}
pagination={this.pagination}
selectedToken={this.state.selectedToken}
transactions={this.state.transactions}
hasAfter={this.state.hasAfter}
hasBefore={this.state.hasBefore}
isNFT={isNFT()}
metadataLoaded={this.state.metadataLoaded}
/>
</div>
);
};
return <div className="content-wrapper">{renderData()}</div>;
}
}
export default AddressDetailLegacy;