-
Notifications
You must be signed in to change notification settings - Fork 295
/
WalletTokensList.tsx
207 lines (200 loc) · 6.28 KB
/
WalletTokensList.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
// @flow
import React, { useState, useMemo, useCallback } from 'react';
import { intlShape, injectIntl } from 'react-intl';
import { Button } from 'react-polymorph/lib/components/Button';
import classnames from 'classnames';
import { observer } from 'mobx-react';
import SVGInline from 'react-svg-inline';
import { searchAssets, sortAssets } from '../../../../utils/assets';
import styles from './WalletTokensList.scss';
import Wallet from '../../../../domains/Wallet';
import BorderedBox from '../../../widgets/BorderedBox';
import LoadingSpinner from '../../../widgets/LoadingSpinner';
import WalletToken from '../wallet-token/WalletToken';
import WalletNoTokens from '../wallet-no-tokens/WalletNoTokens';
import sortIcon from '../../../../assets/images/ascending.inline.svg';
import { messages } from './WalletTokensList.messages';
import type { AssetToken } from '../../../../api/assets/types';
import type { SortBy, SortDirection } from '../../../../utils/assets';
type Props = {
assets: Array<AssetToken>,
assetSettingsDialogWasOpened: boolean,
currentLocale: string,
insertingAssetUniqueId: ?string,
intl: intlShape.isRequired,
isLoadingAssets: boolean,
onAssetSettings: Function,
onCopyAssetParam: Function,
onExternalLinkClick: Function,
onOpenAssetSend: Function,
onToggleFavorite: Function,
onViewAllButtonClick?: Function,
removingAssetUniqueId: ?string,
searchValue?: string,
title: string,
tokenFavorites: Object,
wallet: Wallet,
};
const getSortIconClasses = (
item: SortBy,
sortBy: SortBy,
sortDirection: SortDirection
) => {
const isSorted = item === sortBy;
return classnames([
styles.sortIcon,
isSorted ? styles.sorted : null,
isSorted ? styles[`${sortDirection}Sorting`] : styles.ascSorting,
]);
};
const WalletTokensList = observer((props: Props) => {
const [sortDirection, setSortDirection] = useState<SortDirection>('asc');
const [sortBy, setSortBy] = useState<SortBy>('token');
const {
assets,
assetSettingsDialogWasOpened,
insertingAssetUniqueId,
intl,
isLoadingAssets,
onAssetSettings,
onCopyAssetParam,
onExternalLinkClick,
onOpenAssetSend,
onToggleFavorite,
onViewAllButtonClick,
removingAssetUniqueId,
searchValue = '',
title,
tokenFavorites,
wallet,
} = props;
const isRestoreActive = wallet.isRestoring;
const sortedAssets = useMemo(() => {
return [...assets].sort(sortAssets(sortBy, sortDirection));
}, [assets, sortBy, sortDirection]);
const filteredAssets = searchAssets(searchValue, sortedAssets) || [];
const hasSearch =
!isLoadingAssets && !!searchValue && searchValue.trim().length >= 3;
const noResults = hasSearch && !filteredAssets.length;
const viewAllButtonStyles = classnames(['flat', styles.viewAllButton]);
const hasSorting = filteredAssets.length && filteredAssets.length > 1;
const columnsStyles = classnames([
styles.columns,
hasSorting ? styles.hasSorting : null,
]);
const sortIconClassesToken = useMemo(
() => getSortIconClasses('token', sortBy, sortDirection),
[sortBy, sortDirection]
);
const sortIconClassesAmount = useMemo(
() => getSortIconClasses('quantity', sortBy, sortDirection),
[sortBy, sortDirection]
);
const toggleSortDirection = useCallback(() => {
if (sortDirection === 'asc') {
setSortDirection('desc');
} else {
setSortDirection('asc');
}
}, [sortDirection]);
const onSortBy = useCallback(
(newSortBy: SortBy) => {
if (!hasSorting) return;
if (newSortBy === sortBy) {
toggleSortDirection();
} else {
setSortDirection('asc');
setSortBy(newSortBy);
}
},
[sortDirection, hasSorting, sortBy]
);
const onSortByToken = useCallback(() => onSortBy('token'), [
sortDirection,
sortBy,
hasSorting,
]);
const onSortByAmount = useCallback(() => onSortBy('quantity'), [
sortDirection,
sortBy,
hasSorting,
]);
const hasTokens = assets.length || isLoadingAssets;
if (!hasTokens)
return (
<WalletNoTokens
numberOfAssets={assets.length}
isLoadingAssets={isLoadingAssets}
onExternalLinkClick={onExternalLinkClick}
/>
);
let content;
if (isLoadingAssets) {
content = (
<div className={styles.syncingWrapper}>
<LoadingSpinner big />
</div>
);
} else if (noResults) {
content = (
<p className={styles.noResults}>
{intl.formatMessage(messages.noResults)}
</p>
);
} else {
content = filteredAssets.map((asset) => {
return (
<WalletToken
key={asset.uniqueId}
asset={asset}
onOpenAssetSend={onOpenAssetSend}
onCopyAssetParam={onCopyAssetParam}
onAssetSettings={onAssetSettings}
anyAssetWasHovered
isLoading={isRestoreActive}
assetSettingsDialogWasOpened={assetSettingsDialogWasOpened}
onToggleFavorite={onToggleFavorite}
isFavorite={tokenFavorites[asset.uniqueId]}
isInsertingAsset={insertingAssetUniqueId === asset.uniqueId}
isRemovingAsset={removingAssetUniqueId === asset.uniqueId}
/>
);
});
}
return (
<div className={styles.component}>
<div className={styles.outerHeader}>
<div className={styles.title}>
{title}
{hasSearch && !noResults && (
<>
{intl.formatMessage(messages.searchResults)} (
{filteredAssets.length})
</>
)}
</div>
</div>
<BorderedBox>
<div className={columnsStyles}>
<div className={styles.column} onClick={onSortByToken}>
{intl.formatMessage(messages.columnToken)}
<SVGInline svg={sortIcon} className={sortIconClassesToken} />
</div>
<div className={styles.column} onClick={onSortByAmount}>
{intl.formatMessage(messages.columnAmount)}
<SVGInline svg={sortIcon} className={sortIconClassesAmount} />
</div>
</div>
{content}
{onViewAllButtonClick && (
<Button
className={viewAllButtonStyles}
onClick={onViewAllButtonClick}
label={intl.formatMessage(messages.viewAllButtonLabel)}
/>
)}
</BorderedBox>
</div>
);
});
export default injectIntl(WalletTokensList);