-
Notifications
You must be signed in to change notification settings - Fork 295
/
SidebarWalletsMenu.tsx
221 lines (208 loc) · 7.11 KB
/
SidebarWalletsMenu.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
// @flow
import React, { Component } from 'react';
import SVGInline from 'react-svg-inline';
import { observer } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import classNames from 'classnames';
import { Scrollbars } from 'react-custom-scrollbars';
import { map, noop } from 'lodash';
import Fuse from 'fuse.js';
import { FUZZY_SEARCH_THRESHOLD } from '../../../config/sidebarConfig';
import SidebarSubMenu from '../SidebarMenu';
import styles from './SidebarWalletsMenu.scss';
import addWalletIcon from '../../../assets/images/sidebar/add-wallet-ic.inline.svg';
import SidebarWalletMenuItem from './SidebarWalletMenuItem';
import type {
SidebarWalletType,
WalletSortByOptions,
WalletSortOrderOptions,
} from '../../../types/sidebarTypes';
import { WalletSortBy, WalletSortOrder } from '../../../types/sidebarTypes';
import { WalletSortButton } from './WalletSortButton';
import { WalletSearch } from './WalletSearch';
const messages = defineMessages({
addAdaWallet: {
id: 'sidebar.wallets.addWallet',
defaultMessage: '!!!Add wallet',
description: 'Label for the "Add wallet" button in wallet sidebar menu.',
},
sortByDateButton: {
id: 'sidebar.wallets.sortByDateButton',
defaultMessage: '!!!Date',
description: 'Label for the "Date" sort button',
},
sortByDateTooltip: {
id: 'sidebar.wallets.sortByDateTooltip',
defaultMessage: '!!!Sort wallets by creation date',
description: 'Tooltip message for Date sort button',
},
sortByBalanceButton: {
id: 'sidebar.wallets.sortByBalanceButton',
defaultMessage: '!!!Balance',
description: 'Label for the "Balance" sort button',
},
sortByBalanceTooltip: {
id: 'sidebar.wallets.sortByBalanceTooltip',
defaultMessage: '!!!Sort wallets by balance',
description: 'Tooltip message for Balance sort button',
},
sortByNameButton: {
id: 'sidebar.wallets.sortByNameButton',
defaultMessage: '!!!A – Z',
description: 'Label for the "Name" sort button',
},
sortByNameTooltip: {
id: 'sidebar.wallets.sortByNameTooltip',
defaultMessage: '!!!Sort wallets by name',
description: 'Tooltip message for Name sort button',
},
});
type Props = {
wallets: Array<SidebarWalletType>,
isActiveWallet: Function,
onAddWallet: Function,
onWalletItemClick: Function,
visible: boolean,
isAddWalletButtonActive: boolean,
isShelleyActivated: boolean,
onWalletSortBy?: ({
sortBy: WalletSortByOptions,
sortOrder: WalletSortOrderOptions,
}) => void,
sortBy?: WalletSortByOptions,
sortOrder?: WalletSortOrderOptions,
searchValue?: string,
onSearch?: (term: string) => void,
};
@observer
export default class SidebarWalletsMenu extends Component<Props> {
static contextTypes = {
intl: intlShape.isRequired,
};
renderThumb = (props: any) => (
<div {...props} className={styles.scrollbarThumb} />
);
walletSort = (sortBy: WalletSortByOptions) => {
const {
sortOrder = WalletSortOrder.asc,
onWalletSortBy = noop,
} = this.props;
return onWalletSortBy({
sortBy,
sortOrder,
});
};
filterWalletsBySearchValue = (
searchValue: string,
wallets: SidebarWalletType[]
): SidebarWalletType[] => {
if (searchValue.length > 0) {
const fuse = new Fuse(
wallets.map((w) => ({
...w,
// fix encode issue on fuse lib related to empty space
// https://github.com/krisk/Fuse/issues/610
title: w.title.replace(/\s/g, ' '),
})),
{
keys: ['title'],
includeScore: true,
ignoreLocation: true,
threshold: FUZZY_SEARCH_THRESHOLD,
}
);
return fuse.search(searchValue).map((r) => r.item);
}
return wallets;
};
render() {
const { intl } = this.context;
const {
wallets,
onAddWallet,
isActiveWallet,
onWalletItemClick,
isAddWalletButtonActive,
isShelleyActivated,
sortBy = WalletSortBy.Date,
sortOrder = WalletSortOrder.Asc,
searchValue = '',
onSearch = noop,
} = this.props;
const addWalletButtonStyles = classNames([
styles.addWalletButton,
isAddWalletButtonActive ? styles.active : null,
]);
const filteredWallets = this.filterWalletsBySearchValue(
searchValue,
wallets
);
return (
<SidebarSubMenu visible={this.props.visible}>
<div className={styles.walletSearchContainer}>
<WalletSearch searchValue={searchValue} onSearch={onSearch} />
</div>
<div className={styles.wallets}>
<Scrollbars
renderThumbHorizontal={() => <div className={styles.hideThumb} />}
renderThumbVertical={this.renderThumb}
hideTracksWhenNotNeeded
>
{map(filteredWallets, (wallet) => (
<SidebarWalletMenuItem
title={wallet.title}
amount={wallet.amount}
active={isActiveWallet(wallet.id)}
onClick={() => onWalletItemClick(wallet.id)}
key={wallet.id}
className={`Wallet_${wallet.id}`}
isRestoreActive={wallet.isRestoreActive}
isShelleyActivated={isShelleyActivated}
restoreProgress={wallet.restoreProgress}
isNotResponding={wallet.isNotResponding}
isLegacy={wallet.isLegacy}
isHardwareWallet={wallet.isHardwareWallet}
hasNotification={wallet.hasNotification}
searchValue={searchValue}
isHardwareWalletDisconnected={
wallet.isHardwareWalletDisconnected
}
/>
))}
</Scrollbars>
</div>
<div className={styles.walletSortControls}>
<div className={styles.walletSortOffset}>
<WalletSortButton
isActive={sortBy === WalletSortBy.Date}
sortOrder={sortOrder}
onClick={() => this.walletSort(WalletSortBy.Date)}
label={intl.formatMessage(messages.sortByDateButton)}
tooltip={intl.formatMessage(messages.sortByDateTooltip)}
/>
</div>
<div className={styles.walletSortOffset}>
<WalletSortButton
isActive={sortBy === WalletSortBy.Balance}
sortOrder={sortOrder}
onClick={() => this.walletSort(WalletSortBy.Balance)}
label={intl.formatMessage(messages.sortByBalanceButton)}
tooltip={intl.formatMessage(messages.sortByBalanceTooltip)}
/>
</div>
<WalletSortButton
isActive={sortBy === WalletSortBy.Name}
sortOrder={sortOrder}
onClick={() => this.walletSort(WalletSortBy.Name)}
label={intl.formatMessage(messages.sortByNameButton)}
tooltip={intl.formatMessage(messages.sortByNameTooltip)}
/>
</div>
<button className={addWalletButtonStyles} onClick={onAddWallet}>
<SVGInline svg={addWalletIcon} className={styles.icon} />
<span>{intl.formatMessage(messages.addAdaWallet)}</span>
</button>
</SidebarSubMenu>
);
}
}