-
Notifications
You must be signed in to change notification settings - Fork 295
/
Asset.tsx
333 lines (312 loc) · 9.68 KB
/
Asset.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
// @flow
import React, { Component } from 'react';
import SVGInline from 'react-svg-inline';
import classnames from 'classnames';
import { PopOver } from 'react-polymorph/lib/components/PopOver';
import { defineMessages, intlShape } from 'react-intl';
import { observer } from 'mobx-react';
import styles from './Asset.scss';
import { ellipsis } from '../../utils/strings';
import AssetContent from './AssetContent';
import settingsIcon from '../../assets/images/asset-token-settings-ic.inline.svg';
import warningIcon from '../../assets/images/asset-token-warning-ic.inline.svg';
import { ASSET_TOKEN_DISPLAY_DELAY } from '../../config/timingConfig';
import type { Asset as AssetProps } from '../../api/assets/types';
const messages = defineMessages({
fingerprintItem: {
id: 'assets.assetToken.param.fingerprint',
defaultMessage: '!!!Fingerprint',
description: '"fingerprint" item.',
},
policyIdItem: {
id: 'assets.assetToken.param.policyId',
defaultMessage: '!!!Policy Id',
description: '"policyId" item.',
},
assetNameItem: {
id: 'assets.assetToken.param.assetName',
defaultMessage: '!!!Asset name',
description: '"assetName" item.',
},
nameItem: {
id: 'assets.assetToken.param.name',
defaultMessage: '!!!Name',
description: '"name" item.',
},
tickerItem: {
id: 'assets.assetToken.param.ticker',
defaultMessage: '!!!Ticker',
description: '"ticker" item.',
},
descriptionItem: {
id: 'assets.assetToken.param.description',
defaultMessage: '!!!Description',
description: '"description" item.',
},
blank: {
id: 'assets.assetToken.param.blank',
defaultMessage: '!!!Blank',
description: '"Blank" item value.',
},
settingsCogPopOver: {
id: 'assets.assetToken.settings.cogPopOver',
defaultMessage:
'!!!You can configure the number of decimal places for this native token.',
description: 'Asset settings pop over content',
},
settingsWarningPopOverAvailable: {
id: 'assets.warning.available',
defaultMessage:
'!!!Recommended configuration for decimal places for this native token is available.',
description: 'Asset settings recommended pop over content',
},
settingsWarningPopOverNotUsing: {
id: 'assets.warning.notUsing',
defaultMessage:
'!!!You are not using the recommended decimal place configuration for this native token.',
description: 'Asset settings recommended pop over content',
},
});
type Props = {
asset: AssetProps,
small?: boolean,
hidePopOver?: boolean,
onCopyAssetParam?: Function,
onClickSettings?: Function,
assetSettingsDialogWasOpened?: ?boolean,
anyAssetWasHovered?: ?boolean,
fullFingerprint?: ?boolean,
hasWarning?: ?boolean,
className?: string,
// In case it's not possible to calculate the container width
// this props defines after how many characters the `metadata.name` text will cut off
metadataNameChars?: number,
hasError?: boolean,
};
type State = {
isPillPopOverVisible: boolean,
isHoveringSettingsIcon: boolean,
};
@observer
export default class Asset extends Component<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
copyNotificationTimeout: TimeoutID;
displayDelayTimeout: TimeoutID;
state = {
isPillPopOverVisible: false,
isHoveringSettingsIcon: false,
};
// 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;
}
componentWillUnmount() {
this._isMounted = false;
}
handleShowPillPopOver = () => {
clearTimeout(this.displayDelayTimeout);
this.displayDelayTimeout = setTimeout(() => {
if (this._isMounted) {
this.setState({
isPillPopOverVisible: true,
});
}
}, ASSET_TOKEN_DISPLAY_DELAY);
};
handleHidePillPopOver = () => {
clearTimeout(this.displayDelayTimeout);
this.displayDelayTimeout = setTimeout(() => {
if (this._isMounted) {
this.setState({
isPillPopOverVisible: false,
});
}
}, ASSET_TOKEN_DISPLAY_DELAY);
};
handleSettingsMouseEnter = () => {
this.setState({
isHoveringSettingsIcon: true,
});
};
handleSettingsMouseLeave = () => {
this.setState({
isHoveringSettingsIcon: false,
});
};
get isSettingsPopOverVisible() {
const { assetSettingsDialogWasOpened, anyAssetWasHovered } = this.props;
const { isHoveringSettingsIcon } = this.state;
if (isHoveringSettingsIcon) {
return true;
}
if (
assetSettingsDialogWasOpened === false &&
anyAssetWasHovered === false
) {
return true;
}
return false;
}
renderPillContent() {
const { intl } = this.context;
const {
asset,
metadataNameChars,
small = true,
fullFingerprint,
hasWarning,
hasError,
} = this.props;
const { fingerprint, metadata, decimals, recommendedDecimals } = asset;
const { name } = metadata || {};
const contentStyles = classnames([
styles.pill,
hasError ? styles.error : null,
]);
const [startCharAmount, endCharAmoout] = small ? [9, 4] : [12, 12];
let warningPopOverMessage;
if (hasWarning) {
warningPopOverMessage =
typeof decimals === 'number'
? messages.settingsWarningPopOverNotUsing
: messages.settingsWarningPopOverAvailable;
}
return (
<div className={contentStyles}>
<div className={styles.fingerprint}>
{fullFingerprint
? fingerprint
: ellipsis(fingerprint || '', startCharAmount, endCharAmoout)}
</div>
{name && (
<div className={styles.metadataName}>
{metadataNameChars ? ellipsis(name, metadataNameChars) : name}
</div>
)}
{hasWarning && (
<div className={styles.warningIconWrapper}>
<PopOver
content={intl.formatMessage(warningPopOverMessage, {
recommendedDecimals,
})}
className={styles.warningIconWrapper}
>
<SVGInline className={styles.warningIcon} svg={warningIcon} />
</PopOver>
</div>
)}
</div>
);
}
renderPillPopOverContainer = () => {
const {
asset,
onCopyAssetParam,
assetSettingsDialogWasOpened,
} = this.props;
const pillContent = this.renderPillContent();
const popOverContent = (
<AssetContent
asset={asset}
onCopyAssetParam={onCopyAssetParam}
assetSettingsDialogWasOpened={assetSettingsDialogWasOpened}
className={styles.popOverContent}
highlightFingerprint
/>
);
const { isPillPopOverVisible } = this.state;
return (
<div
className={styles.popOverContainer}
onMouseEnter={this.handleShowPillPopOver}
onMouseLeave={this.handleHidePillPopOver}
>
<PopOver
themeVariables={{
'--rp-pop-over-bg-color':
'var(--theme-widgets-asset-token-background-color)',
'--rp-pop-over-text-color': 'var(--theme-bordered-box-text-color)',
'--rp-pop-over-border-color':
'var(--theme-staking-stake-pool-tooltip-border-color)',
'--rp-pop-over-border-width': '1px',
'--rp-pop-over-border-style': 'solid',
'--rp-pop-over-box-shadow':
'0 5px 20px 0 var(--theme-widgets-asset-token-box-shadow)',
}}
contentClassName={styles.popOver}
content={popOverContent}
visible={isPillPopOverVisible}
appendTo="parent"
maxWidth={376}
allowHTML
interactive
>
{pillContent}
</PopOver>
</div>
);
};
renderSettingsContent = () => {
const { intl } = this.context;
const { asset, onClickSettings, hasWarning } = this.props;
if (!onClickSettings) return null;
const {
isSettingsPopOverVisible,
handleSettingsMouseEnter,
handleSettingsMouseLeave,
} = this;
const onClickSettingsBind = () => onClickSettings && onClickSettings(asset);
const { decimals, recommendedDecimals } = asset;
let warningPopOverMessage;
if (hasWarning) {
warningPopOverMessage =
typeof decimals === 'number'
? messages.settingsWarningPopOverNotUsing
: messages.settingsWarningPopOverAvailable;
}
return (
<button className={styles.settingsButton} onClick={onClickSettingsBind}>
<PopOver
className={styles.test}
content={intl.formatMessage(messages.settingsCogPopOver)}
visible={isSettingsPopOverVisible}
>
<SVGInline
onMouseEnter={handleSettingsMouseEnter}
onMouseLeave={handleSettingsMouseLeave}
className={styles.settingsIcon}
svg={settingsIcon}
/>
</PopOver>
{hasWarning && (
<PopOver
content={intl.formatMessage(warningPopOverMessage, {
recommendedDecimals,
})}
>
<SVGInline className={styles.warningIcon} svg={warningIcon} />
</PopOver>
)}
</button>
);
};
render() {
const { hidePopOver, className } = this.props;
const content = hidePopOver
? this.renderPillContent()
: this.renderPillPopOverContainer();
const settingsContent = this.renderSettingsContent();
const componentClassnames = classnames([styles.component, className]);
return (
<div className={componentClassnames}>
{content}
{settingsContent}
</div>
);
}
}