-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathindex.tsx
More file actions
140 lines (125 loc) · 4.33 KB
/
index.tsx
File metadata and controls
140 lines (125 loc) · 4.33 KB
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
import React, { useMemo, useCallback, useState, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { useState as useGlobalState } from 'states'
import Dialog from 'widgets/Dialog'
import Toast from 'widgets/Toast'
import Button from 'widgets/Button'
import CopyZone from 'widgets/CopyZone'
import QRCode from 'widgets/QRCode'
import Tooltip from 'widgets/Tooltip'
import { AddressTransform, Download, Copy, Attention, SuccessNoBorder } from 'widgets/Icons/icon'
import VerifyHardwareAddress from './VerifyHardwareAddress'
import styles from './receive.module.scss'
import { useCopyAndDownloadQrCode, useSwitchAddress } from './hooks'
type AddressTransformWithCopyZoneProps = {
showAddress: string
isInShortFormat: boolean
onClick: () => void
}
export const AddressQrCodeWithCopyZone = ({
showAddress,
isInShortFormat,
onClick,
}: AddressTransformWithCopyZoneProps) => {
const [t] = useTranslation()
const transformLabel = t(
isInShortFormat ? 'receive.turn-into-full-version-format' : 'receive.turn-into-deprecated-format'
)
const [isCopySuccess, setIsCopySuccess] = useState(false)
const timer = useRef<ReturnType<typeof setTimeout>>()
const { ref, onCopyQrCode, onDownloadQrCode, showCopySuccess } = useCopyAndDownloadQrCode()
const stopPropagation = useCallback((e: React.SyntheticEvent) => {
e.stopPropagation()
}, [])
const onCopy = useCallback(() => {
onCopyQrCode()
setIsCopySuccess(true)
clearTimeout(timer.current!)
timer.current = setTimeout(() => {
setIsCopySuccess(false)
}, 1000)
}, [showAddress, setIsCopySuccess, timer])
return (
<div className={styles.addressRoot}>
<div className={styles.qrCode} data-copy-success-text={t('common.copied')}>
<QRCode value={showAddress} size={128} includeMargin ref={ref} />
<div className={styles.actions}>
<Button type="text" className={styles.actionBtn} onClick={onDownloadQrCode}>
<Download />
</Button>
{isCopySuccess ? (
<Button type="text">
<SuccessNoBorder />
</Button>
) : (
<Button type="text" className={styles.actionBtn} onClick={onCopy}>
<Copy />
</Button>
)}
</div>
{showCopySuccess && <Toast content={t('common.copied')} />}
</div>
<div className={styles.copyAddress}>
<CopyZone content={showAddress} className={styles.showAddress}>
{showAddress}
</CopyZone>
<button
type="button"
className={styles.addressToggle}
onClick={onClick}
title={transformLabel}
onFocus={stopPropagation}
onMouseOver={stopPropagation}
onMouseUp={stopPropagation}
>
<AddressTransform />
{transformLabel}
</button>
</div>
</div>
)
}
const Receive = ({ onClose, address }: { onClose?: () => void; address?: string }) => {
const [t] = useTranslation()
const { wallet } = useGlobalState()
const { addresses, isHD } = wallet
const isSingleAddress = addresses.length === 1
const isHardwareWallet = !isHD && isSingleAddress
const accountAddress = useMemo(() => {
if (isSingleAddress) {
return addresses[0].address
}
return (address || addresses.find(a => a.type === 0 && a.txCount === 0)?.address) ?? ''
}, [address, addresses, isSingleAddress])
if (!accountAddress) {
return <div>{t('receive.address-not-found')}</div>
}
const { isInShortFormat, setIsInShortFormat, address: showAddress } = useSwitchAddress(accountAddress)
return (
<Dialog
show
title={
<Tooltip tip={<div className={styles.tip}>{t('receive.prompt')}</div>} placement="right-bottom">
<div className={styles.dialogTitle}>
{t('receive.title')}
<Attention />
</div>
</Tooltip>
}
onCancel={onClose}
showFooter={false}
className={styles.dialog}
>
<div>
<AddressQrCodeWithCopyZone
showAddress={showAddress}
isInShortFormat={isInShortFormat}
onClick={() => setIsInShortFormat(is => !is)}
/>
{isHardwareWallet && <VerifyHardwareAddress address={accountAddress} wallet={wallet} onClose={onClose} />}
</div>
</Dialog>
)
}
Receive.displayName = 'Receive'
export default Receive