-
Notifications
You must be signed in to change notification settings - Fork 295
/
WalletTransactionsList.tsx
381 lines (360 loc) · 12.5 KB
/
WalletTransactionsList.tsx
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
import React, { Component } from 'react';
// @ts-ignore ts-migrate(2305) FIXME: Module '"react"' has no exported member 'Node'.
import type { Node } from 'react';
import { observer } from 'mobx-react';
import classnames from 'classnames';
import { Button } from 'react-polymorph/lib/components/Button';
import { ButtonSkin } from 'react-polymorph/lib/skins/simple/ButtonSkin';
import { defineMessages, intlShape } from 'react-intl';
import moment from 'moment';
import styles from './WalletTransactionsList.scss';
import Transaction from './Transaction';
import { WalletTransaction } from '../../../domains/WalletTransaction';
import LoadingSpinner from '../../widgets/LoadingSpinner';
import { VirtualTransactionList } from './render-strategies/VirtualTransactionList';
import { SimpleTransactionList } from './render-strategies/SimpleTransactionList';
import { TransactionInfo, TransactionsGroup } from './types';
import type { Row } from './types';
import { getNonZeroAssetTokens } from '../../../utils/assets';
import { AnalyticsTracker, EventCategories } from '../../../analytics';
const messages = defineMessages({
today: {
id: 'wallet.summary.transactionsList.todayLabel',
defaultMessage: '!!!Today',
description: 'Label for the "Today" label on the wallet summary page.',
},
yesterday: {
id: 'wallet.summary.transactionsList.yesterdayLabel',
defaultMessage: '!!!Yesterday',
description: 'Label for the "Yesterday" label on the wallet summary page.',
},
showMoreTransactionsButtonLabel: {
id: 'wallet.summary.transactionsList.showMoreTransactionsButtonLabel',
defaultMessage: '!!!Show more transactions',
description:
'Label for the "Show more transactions" button on the wallet summary page.',
},
syncingTransactionsMessage: {
id: 'wallet.summary.transactionsList.syncingTransactionsMessage',
defaultMessage:
'!!!Your transaction history for this wallet is being synced with the blockchain.',
description: 'Syncing transactions message on async wallet restore.',
},
});
export type ScrollContextType = {
setIsScrolling: (...args: Array<any>) => any;
};
export const WalletTransactionsListScrollContext = React.createContext<
ScrollContextType
>({
setIsScrolling: () => null,
});
type Props = {
deletePendingTransaction: (...args: Array<any>) => any;
formattedWalletAmount: (...args: Array<any>) => any;
hasMoreToLoad: boolean;
isLoadingTransactions: boolean;
isRestoreActive: boolean;
isRenderingAsVirtualList: boolean;
onShowMoreTransactions?: (...args: Array<any>) => any;
onOpenExternalLink: (...args: Array<any>) => any;
getUrlByType: (...args: Array<any>) => any;
showMoreTransactionsButton?: boolean;
transactions: Array<WalletTransaction>;
walletId: string;
isDeletingTransaction: boolean;
currentDateFormat: string;
currentTimeFormat: string;
hasAssetsEnabled: boolean;
getAsset: (...args: Array<any>) => any;
isInternalAddress: (...args: Array<any>) => any;
onCopyAssetParam: (...args: Array<any>) => any;
analyticsTracker: AnalyticsTracker;
};
type State = {
isPreloading: boolean;
};
const DATE_FORMAT = 'YYYY-MM-DD';
@observer
class WalletTransactionsList extends Component<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
static defaultProps = {
isRenderingAsVirtualList: false,
showMoreTransactionsButton: false,
onShowMoreTransactions: () => {},
onOpenExternalLink: () => {},
};
state = {
isPreloading: true,
};
// We need to track the mounted state in order to avoid calling
// setState promise handling code after the component was already unmounted:
// Read more: https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html
_isMounted = false;
componentDidMount() {
this._isMounted = true;
setTimeout(() => {
if (this._isMounted)
this.setState({
isPreloading: false,
});
}, 0);
}
componentWillUnmount() {
this._isMounted = false;
}
expandedTransactionIds: Map<string, WalletTransaction> = new Map();
transactionsShowingMetadata: Map<string, WalletTransaction> = new Map();
virtualList: VirtualTransactionList | null | undefined;
simpleList: SimpleTransactionList | null | undefined;
loadingSpinner: LoadingSpinner | null | undefined;
groupTransactionsByDay(
transactions: Array<WalletTransaction>
): Array<TransactionsGroup> {
const groups: Array<TransactionsGroup> = [];
for (const transaction of transactions) {
const date = moment(transaction.date);
let group = groups.find(
(g) => g.date.format(DATE_FORMAT) === date.format(DATE_FORMAT)
);
if (!group) {
group = new TransactionsGroup({
date,
transactions: [],
});
groups.push(group);
}
group.transactions.push(transaction);
}
return groups.sort(
(a: TransactionsGroup, b: TransactionsGroup) =>
b.date.valueOf() - a.date.valueOf()
);
}
isSpinnerVisible() {
const spinner = this.loadingSpinner;
if (spinner == null || spinner.root == null) return false;
const spinnerRect = spinner.root.getBoundingClientRect();
const clientHeight = document.documentElement
? document.documentElement.clientHeight
: 0;
const windowHeight = window.innerHeight;
const viewHeight = Math.max(clientHeight, windowHeight);
return !(spinnerRect.bottom < 0 || spinnerRect.top - viewHeight >= 0);
}
localizedDate(date: string) {
const { intl } = this.context;
const { currentDateFormat } = this.props;
// TODAY
const today = moment().format(DATE_FORMAT);
if (date === today) return intl.formatMessage(messages.today);
// YESTERDAY
const yesterday = moment().subtract(1, 'days').format(DATE_FORMAT);
if (date === yesterday) return intl.formatMessage(messages.yesterday);
// PAST DATE
return moment(date).format(currentDateFormat);
}
isTxExpanded = (tx: WalletTransaction) =>
this.expandedTransactionIds.has(tx.id);
isTxShowingMetadata = (tx: WalletTransaction) =>
this.transactionsShowingMetadata.has(tx.id);
toggleTransactionExpandedState = (tx: WalletTransaction) => {
const isExpanded = this.isTxExpanded(tx);
if (isExpanded) {
this.expandedTransactionIds.delete(tx.id);
} else {
this.expandedTransactionIds.set(tx.id, tx);
}
if (this.virtualList) {
this.virtualList.updateTxRowHeight(tx, !isExpanded, true);
} else if (this.simpleList) {
this.simpleList.forceUpdate();
}
};
/**
* Update the height of the transaction when metadata is shown
* @param tx
*/
onShowMetadata = (tx: WalletTransaction) => {
this.transactionsShowingMetadata.set(tx.id, tx);
if (this.virtualList) {
this.virtualList.updateTxRowHeight(tx, true, true);
} else if (this.simpleList) {
this.simpleList.forceUpdate();
}
};
onShowMoreTransactions = (walletId: string) => {
if (this.props.onShowMoreTransactions) {
this.props.onShowMoreTransactions(walletId);
this.props.analyticsTracker.sendEvent(
EventCategories.WALLETS,
'Clicked Show More Transactions button'
);
}
};
getExpandedTransactions = () => this.expandedTransactionIds;
renderGroup = (data: TransactionsGroup): Node => (
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'Moment' is not assignable to par... Remove this comment to see the full error message
<div className={styles.groupDate}>{this.localizedDate(data.date)}</div>
);
renderTransaction = (data: TransactionInfo): Node => {
const {
deletePendingTransaction,
formattedWalletAmount,
isRestoreActive,
onOpenExternalLink,
getUrlByType,
walletId,
isDeletingTransaction,
currentTimeFormat,
hasAssetsEnabled,
getAsset,
isInternalAddress,
onCopyAssetParam,
} = this.props;
const { isFirstInGroup, isLastInGroup, tx } = data;
const txClasses = classnames([
styles.transaction,
isFirstInGroup ? styles.firstInGroup : null,
isLastInGroup ? styles.lastInGroup : null,
]);
const txTokens = tx.assets;
const assetTokens = getNonZeroAssetTokens(txTokens, getAsset);
const totalRawAssets = tx.assets.length;
const totalAssets = assetTokens.length;
const hasRawAssets = tx.assets.length > 0;
const isLoadingAssets = hasRawAssets && totalAssets < totalRawAssets;
return (
<div id={`tx-${tx.id}`} className={txClasses}>
<Transaction
data={tx}
deletePendingTransaction={deletePendingTransaction}
formattedWalletAmount={formattedWalletAmount}
isExpanded={this.isTxExpanded(tx)}
isShowingMetadata={this.isTxShowingMetadata(tx)}
isLastInList={isLastInGroup}
isRestoreActive={isRestoreActive}
onDetailsToggled={() => this.toggleTransactionExpandedState(tx)}
onOpenExternalLink={onOpenExternalLink}
onShowMetadata={() => this.onShowMetadata(tx)}
getUrlByType={getUrlByType}
state={tx.state}
walletId={walletId}
isDeletingTransaction={isDeletingTransaction}
currentTimeFormat={currentTimeFormat}
assetTokens={assetTokens}
hasAssetsEnabled={hasAssetsEnabled}
isInternalAddress={isInternalAddress}
isLoadingAssets={isLoadingAssets}
onCopyAssetParam={onCopyAssetParam}
/>
</div>
);
};
renderItem = (row: Row) => {
if (row instanceof TransactionsGroup) {
return this.renderGroup(row);
}
if (row instanceof TransactionInfo) {
return this.renderTransaction(row);
}
return null;
};
render() {
const { intl } = this.context;
const { isPreloading } = this.state;
const {
hasMoreToLoad,
isLoadingTransactions,
isRenderingAsVirtualList,
isRestoreActive,
showMoreTransactionsButton,
transactions,
walletId,
} = this.props;
const transactionsGroups = this.groupTransactionsByDay(transactions);
const loadingSpinner =
(isLoadingTransactions || hasMoreToLoad) && !isRestoreActive ? (
<LoadingSpinner
ref={(component) => {
this.loadingSpinner = component;
}}
/>
) : null;
const syncingTransactionsSpinner = isRestoreActive ? (
<div className={styles.syncingTransactionsWrapper}>
<LoadingSpinner big />
<p className={styles.syncingTransactionsText}>
{intl.formatMessage(messages.syncingTransactionsMessage)}
</p>
</div>
) : null;
const buttonClasses = classnames([
'primary',
styles.showMoreTransactionsButton,
]);
// Generate flat list with dates in-between
const rows: Row[] = [];
transactionsGroups.forEach((group) => {
// First push the group into the list
rows.push(group);
// Followed by all transactions the tx in the group
group.transactions.forEach((transaction, transactionIndex) => {
const isFirstInGroup = transactionIndex === 0;
const isLastInGroup =
group.transactions.length === transactionIndex + 1;
rows.push(
new TransactionInfo({
tx: transaction,
isLastInGroup,
isFirstInGroup,
})
);
});
});
const showMoreTxButton = (
<Button
className={buttonClasses}
label={intl.formatMessage(messages.showMoreTransactionsButtonLabel)} // eslint-disable-next-line react/jsx-no-bind
onClick={this.onShowMoreTransactions.bind(this, walletId)}
skin={ButtonSkin}
/>
);
if (isPreloading)
return (
<div className={styles.preloadingBlockWrapper}>
<LoadingSpinner big />
</div>
);
return (
<div className={styles.component}>
{syncingTransactionsSpinner}
{isRenderingAsVirtualList ? (
<VirtualTransactionList
getExpandedTransactions={this.getExpandedTransactions}
ref={(list) => {
this.virtualList = list;
}}
renderRow={this.renderItem}
rows={rows}
isLoadingSpinnerShown={loadingSpinner !== null}
isSyncingSpinnerShown={isRestoreActive}
/>
) : (
<SimpleTransactionList
ref={(list) => {
this.simpleList = list;
}}
renderRow={this.renderItem}
rows={rows}
/>
)}
{showMoreTransactionsButton ? showMoreTxButton : null}
{loadingSpinner}
</div>
);
}
}
export default WalletTransactionsList;