forked from Koniverse/SubWallet-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTokenDetail.tsx
377 lines (340 loc) · 11.5 KB
/
TokenDetail.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
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { _ChainAsset } from '@subwallet/chain-list/types';
import { _getContractAddressOfToken, _isCustomAsset, _isSmartContractToken } from '@subwallet/extension-base/services/chain-service/utils';
import { Layout, PageWrapper } from '@subwallet/extension-koni-ui/components';
import { DataContext } from '@subwallet/extension-koni-ui/contexts/DataContext';
import useNotification from '@subwallet/extension-koni-ui/hooks/common/useNotification';
import useTranslation from '@subwallet/extension-koni-ui/hooks/common/useTranslation';
import useConfirmModal from '@subwallet/extension-koni-ui/hooks/modal/useConfirmModal';
import useDefaultNavigate from '@subwallet/extension-koni-ui/hooks/router/useDefaultNavigate';
import useFetchChainInfo from '@subwallet/extension-koni-ui/hooks/screen/common/useFetchChainInfo';
import useGetChainAssetInfo from '@subwallet/extension-koni-ui/hooks/screen/common/useGetChainAssetInfo';
import { deleteCustomAssets, upsertCustomToken } from '@subwallet/extension-koni-ui/messaging';
import { Theme, ThemeProps } from '@subwallet/extension-koni-ui/types';
import { Button, ButtonProps, Col, Field, Icon, Input, Logo, Row, Tooltip } from '@subwallet/react-ui';
import SwAvatar from '@subwallet/react-ui/es/sw-avatar';
import { CheckCircle, Copy, Trash } from 'phosphor-react';
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { useLocation } from 'react-router-dom';
import styled, { useTheme } from 'styled-components';
import { isEthereumAddress } from '@polkadot/util-crypto';
type Props = ThemeProps
function Component ({ className = '' }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const dataContext = useContext(DataContext);
const { token } = useTheme() as Theme;
const goBack = useDefaultNavigate().goBack;
const location = useLocation();
const showNotification = useNotification();
const tokenSlug = useMemo(() => {
return location.state as string;
}, [location.state]);
const tokenInfo = useGetChainAssetInfo(tokenSlug) as _ChainAsset;
useEffect(() => {
if (!tokenInfo) {
goBack();
}
}, [goBack, tokenInfo]);
const originChainInfo = useFetchChainInfo(tokenInfo.originChain);
const [priceId, setPriceId] = useState(tokenInfo.priceId || '');
const [loading, setLoading] = useState(false);
const { handleSimpleConfirmModal } = useConfirmModal({
title: t<string>('Delete token'),
maskClosable: true,
closable: true,
type: 'error',
subTitle: t<string>('You are about to delete this token'),
content: t<string>('Confirm delete this token'),
okText: t<string>('Remove')
});
const handleDeleteToken = useCallback(() => {
handleSimpleConfirmModal().then(() => {
deleteCustomAssets(tokenInfo.slug)
.then((result) => {
if (result) {
goBack();
showNotification({
message: t('Deleted token successfully')
});
} else {
showNotification({
message: t('Deleted token unsuccessfully')
});
}
})
.catch(() => {
showNotification({
message: t('Deleted token unsuccessfully')
});
});
}).catch(console.log);
}, [goBack, handleSimpleConfirmModal, showNotification, t, tokenInfo.slug]);
const subHeaderButton: ButtonProps[] = useMemo(() => {
return [
{
icon: <Icon
customSize={`${token.fontSizeHeading3}px`}
phosphorIcon={Trash}
type='phosphor'
weight={'light'}
/>,
onClick: handleDeleteToken,
disabled: !(_isCustomAsset(tokenInfo.slug) && _isSmartContractToken(tokenInfo))
}
];
}, [handleDeleteToken, token.fontSizeHeading3, tokenInfo]);
const contractAddressIcon = useCallback(() => {
const contractAddress = _getContractAddressOfToken(tokenInfo);
const theme = isEthereumAddress(contractAddress) ? 'ethereum' : 'polkadot';
return (
<SwAvatar
identPrefix={42}
size={token.fontSizeXL}
theme={theme}
value={contractAddress}
/>
);
}, [token.fontSizeXL, tokenInfo]);
const contractAddressInfo = useCallback(() => {
const contractAddress = _getContractAddressOfToken(tokenInfo);
return (
<span>{`${contractAddress.slice(0, 10)}...${contractAddress.slice(-10)}`}</span>
);
}, [tokenInfo]);
const handleCopyContractAddress = useCallback(() => {
const contractAddress = _getContractAddressOfToken(tokenInfo);
navigator.clipboard.writeText(contractAddress).then().catch(console.error);
showNotification({
message: t('Copied to clipboard')
});
}, [showNotification, t, tokenInfo]);
const contractAddressSuffix = useCallback(() => {
return (
<Button
icon={<Icon
customSize={'20px'}
iconColor={token.colorIcon}
phosphorIcon={Copy}
type='phosphor'
weight={'light'}
/>}
onClick={handleCopyContractAddress}
size={'xs'}
type={'ghost'}
/>
);
}, [handleCopyContractAddress, token.colorIcon]);
const onChangePriceId = useCallback((e: React.FormEvent<HTMLInputElement>) => {
setPriceId(e.currentTarget.value);
}, []);
const isSubmitDisabled = useCallback(() => {
return tokenInfo.priceId === priceId || priceId.length === 0;
}, [priceId, tokenInfo.priceId]);
const onSubmit = useCallback(() => {
setLoading(true);
upsertCustomToken({
...tokenInfo,
priceId
})
.then((result) => {
if (result) {
setLoading(false);
goBack();
} else {
setLoading(false);
showNotification({
message: t('Error')
});
}
})
.catch(() => {
setLoading(false);
showNotification({
message: t('Error')
});
});
}, [goBack, priceId, showNotification, t, tokenInfo]);
const goBackToSettingList = useCallback(() => {
goBack();
}, [goBack]);
const leftFooterButtonProps = useCallback(() => {
return _isCustomAsset(tokenInfo.slug)
? {
onClick: goBackToSettingList,
children: t('Cancel')
}
: undefined;
}, [goBackToSettingList, tokenInfo.slug, t]);
const rightFooterButtonProps = useCallback(() => {
return _isCustomAsset(tokenInfo.slug)
? {
block: true,
disabled: isSubmitDisabled(),
icon: (
<Icon
phosphorIcon={CheckCircle}
weight='fill'
/>
),
loading,
onClick: onSubmit,
children: t('Save')
}
: undefined;
}, [isSubmitDisabled, loading, onSubmit, t, tokenInfo.slug]);
return (
<PageWrapper
className={`token_detail ${className}`}
resolve={dataContext.awaitStores(['assetRegistry'])}
>
<Layout.Base
leftFooterButton={leftFooterButtonProps()}
onBack={goBack}
rightFooterButton={rightFooterButtonProps()}
showBackButton={true}
showSubHeader={true}
subHeaderBackground={'transparent'}
subHeaderCenter={true}
subHeaderIcons={subHeaderButton}
subHeaderPaddingVertical={true}
title={t<string>('Token detail')}
>
<div className={'token_detail__container'}>
<div className={'token_detail__header_container'}>
<div className={'token_detail__header_icon_wrapper'}>
<Logo
size={112}
token={tokenInfo.slug.toLowerCase()}
/>
</div>
<div className={'token_detail__header_text_container'}>
{tokenInfo.symbol}
</div>
</div>
<div className={'token_detail__content_container'}>
{
_isSmartContractToken(tokenInfo) && <Field
content={contractAddressInfo()}
label={t<string>('Contract address')}
placeholder={t<string>('Contract address')}
prefix={contractAddressIcon()}
suffix={contractAddressSuffix()}
/>
}
<Field
content={originChainInfo.name}
label={t<string>('Network')}
placeholder={t<string>('Network')}
prefix={<Logo
network={originChainInfo.slug}
size={20}
/>}
/>
<Row gutter={token.marginSM}>
<Col span={12}>
<Tooltip
placement={'topLeft'}
title={t('Symbol')}
>
<div>
<Field
content={tokenInfo.symbol}
placeholder={t<string>('Symbol')}
prefix={(
<Logo
size={20}
token={tokenInfo.slug.toLowerCase()}
/>
)}
/>
</div>
</Tooltip>
</Col>
<Col span={12}>
<Tooltip
placement={'topLeft'}
title={t('Token name')}
>
<div>
<Field
content={tokenInfo.name}
placeholder={t<string>('Token name')}
/>
</div>
</Tooltip>
</Col>
</Row>
<Row gutter={token.marginSM}>
<Col span={12}>
<Tooltip
placement={'topLeft'}
title={t('Price ID')}
>
<div>
<Input
disabled={!_isCustomAsset(tokenInfo.slug)}
onChange={onChangePriceId}
placeholder={t('Price ID')}
value={priceId}
/>
</div>
</Tooltip>
</Col>
<Col span={12}>
<Tooltip
placement={'topLeft'}
title={t('Decimals')}
>
<div>
<Field
content={tokenInfo.decimals}
placeholder={t<string>('Decimals')}
/>
</div>
</Tooltip>
</Col>
</Row>
</div>
</div>
</Layout.Base>
</PageWrapper>
);
}
const TokenDetail = styled(Component)<Props>(({ theme: { token } }: Props) => {
return ({
'.token_detail__container': {
marginLeft: token.margin,
marginRight: token.margin
},
'.token_detail__header_container': {
marginTop: 30,
display: 'flex',
flexWrap: 'wrap',
gap: token.padding,
flexDirection: 'column',
alignContent: 'center',
marginBottom: token.marginLG
},
'.token_detail__header_text_container': {
fontWeight: token.headingFontWeight,
textAlign: 'center',
fontSize: token.fontSizeHeading3,
color: token.colorText
},
'.token_detail__header_icon_wrapper': {
display: 'flex',
justifyContent: 'center'
},
'.token_detail__content_container': {
display: 'flex',
flexDirection: 'column',
gap: token.marginSM
},
'.ant-field-wrapper .ant-btn': {
margin: -token.marginXS,
height: 'auto'
}
});
});
export default TokenDetail;