This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AddressBook.tsx
267 lines (245 loc) · 7.78 KB
/
AddressBook.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
import {Button, message, Popover} from 'antd'
import React, {useState} from 'react'
import {EmptyProps} from 'antd/lib/empty'
import {CopyOutlined, DeleteOutlined, EditOutlined} from '@ant-design/icons'
import {Trans} from '../common/Trans'
import {useLocalizedUtilities, useTranslation} from '../settings-state'
import {Dialog} from '../common/Dialog'
import {DialogInput} from '../common/dialog/DialogInput'
import {DialogMessage} from '../common/dialog/DialogMessage'
import {ModalLocker, wrapWithModal} from '../common/MantisModal'
import {fillActionHandlers, toAntValidator, validateAddress} from '../common/util'
import {LoadedState} from '../common/wallet-state'
import {PropsWithWalletState, withStatusGuard} from '../common/wallet-status-guard'
import {NoWallet} from '../wallets/NoWallet'
import './AddressBook.scss'
const LABEL_MAX_LENGTH = 80
const LABEL_FIELD_NAME = 'contact-label'
const ADDRESS_FIELD_NAME = 'contact-address'
type Setter<T> = React.Dispatch<React.SetStateAction<T>>
type ModalId = 'Edit' | 'Delete' | 'none'
type EditMode = 'Add' | 'Edit'
interface EditContactModalProps {
onSave(address: string, label: string): void
onCancel(): void
address: string
label: string
setAddress: Setter<string>
setLabel: Setter<string>
editMode: EditMode
}
const _EditContactModal = ({
onSave,
onCancel,
address,
label,
setAddress,
setLabel,
editMode,
}: EditContactModalProps): JSX.Element => {
const {t} = useTranslation()
const addressValidator = toAntValidator(t, validateAddress)
const modalLocker = ModalLocker.useContainer()
return (
<Dialog
title={
editMode === 'Edit'
? t(['addressBook', 'book', 'editContact'])
: t(['addressBook', 'book', 'newContact'])
}
leftButtonProps={{
onClick: onCancel,
disabled: modalLocker.isLocked,
}}
rightButtonProps={{
children: t(['addressBook', 'book', 'saveContact']),
disabled: !address || !label || modalLocker.isLocked,
onClick: (): void => {
onSave(address, label)
onCancel()
},
}}
onSetLoading={modalLocker.setLocked}
initialValues={{
[ADDRESS_FIELD_NAME]: address,
[LABEL_FIELD_NAME]: label,
}}
>
<DialogInput
label={t(['addressBook', 'label', 'address'])}
onChange={(e): void => setAddress(e.target.value)}
formItem={{
name: ADDRESS_FIELD_NAME,
rules: [
{required: true, message: t(['addressBook', 'error', 'addressMustBeSet'])},
addressValidator,
],
}}
disabled={editMode === 'Edit'}
id={ADDRESS_FIELD_NAME}
/>
<DialogInput
label={t(['addressBook', 'label', 'label'])}
onChange={(e): void => setLabel(e.target.value)}
formItem={{
name: LABEL_FIELD_NAME,
rules: [
{required: true, message: t(['addressBook', 'error', 'labelMustBeSet'])},
{
max: LABEL_MAX_LENGTH,
message: t(['addressBook', 'error', 'labelMaxLength'], {
replace: {max: LABEL_MAX_LENGTH},
}),
},
],
}}
id={LABEL_FIELD_NAME}
/>
</Dialog>
)
}
interface DeleteContactModalProps {
onDelete(address: string): void
onCancel(): void
address: string
label: string
}
const _DeleteContactModal = ({
onDelete,
onCancel,
address,
label,
}: DeleteContactModalProps): JSX.Element => {
const {t} = useTranslation()
return (
<Dialog
title={t(['addressBook', 'book', 'deleteContact'])}
leftButtonProps={{
onClick: onCancel,
}}
rightButtonProps={{
children: t(['addressBook', 'book', 'deleteContactBtn']),
onClick: (): void => {
onDelete(address)
onCancel()
},
}}
>
<DialogMessage>
<Trans k={['addressBook', 'book', 'deleteContactWarning']} values={{label, address}} />
</DialogMessage>
</Dialog>
)
}
const DeleteContactModal = wrapWithModal(_DeleteContactModal)
const EditContactModal = wrapWithModal(_EditContactModal)
const _AddressBook = ({
walletState: {addressBook, editContact, deleteContact},
}: PropsWithWalletState<EmptyProps, LoadedState>): JSX.Element => {
const {t} = useTranslation()
const {copyToClipboard} = useLocalizedUtilities()
const [shownModal, setShownModal] = useState<ModalId>('none')
const [editMode, setEditMode] = useState<EditMode>('Edit')
const [selectedAddress, setAddress] = useState('')
const [selectedLabel, setLabel] = useState('')
// eslint-disable-next-line fp/no-mutating-methods
const sortedLabels = [...Object.keys(addressBook)].sort()
const selectContact = (address: string, label: string): void => {
setAddress(address)
setLabel(label)
}
const onStartEdit = (address: string) => () => {
selectContact(address, addressBook[address])
setEditMode('Edit')
setShownModal('Edit')
}
const onStartAddNew = (): void => {
selectContact('', '')
setEditMode('Add')
setShownModal('Edit')
}
const onStartDelete = (address: string) => () => {
selectContact(address, addressBook[address])
setShownModal('Delete')
}
const onDelete = (): void => {
deleteContact(selectedAddress)
message.success(t(['addressBook', 'message', 'contactDeleted']))
}
const onCancel = (): void => {
setShownModal('none')
}
return (
<div className="AddressBook">
<div className="main-buttons">
<Button
data-testid="add-contact-button"
type="primary"
className="action right-diagonal"
onClick={onStartAddNew}
>
<Trans k={['addressBook', 'book', 'addNew']} />
</Button>
</div>
<div className="toolbar">
<div className="main-title">
<Trans k={['addressBook', 'book', 'title']} />
</div>
</div>
<div className="address-list">
{sortedLabels.length === 0 ? (
<Trans k={['addressBook', 'book', 'noData']} />
) : (
sortedLabels.map((address) => {
const label = addressBook[address]
return (
<div key={address} className="row">
<span className="label">{label}</span>
<span className="address">
{address}{' '}
<Popover content={t(['addressBook', 'address', 'clickToCopy'])} placement="top">
<CopyOutlined className="clickable" onClick={() => copyToClipboard(address)} />
</Popover>
</span>
<span className="actions">
<span
className="delete"
title={t(['addressBook', 'book', 'deleteContact'])}
{...fillActionHandlers(onStartDelete(address))}
>
<DeleteOutlined />
</span>
<span
className="edit"
title={t(['addressBook', 'book', 'editContact'])}
{...fillActionHandlers(onStartEdit(address))}
>
<EditOutlined />
</span>
</span>
</div>
)
})
)}
</div>
<EditContactModal
visible={shownModal === 'Edit'}
onSave={editContact}
onCancel={onCancel}
setAddress={setAddress}
setLabel={setLabel}
address={selectedAddress}
label={selectedLabel}
editMode={editMode}
/>
<DeleteContactModal
visible={shownModal === 'Delete'}
onDelete={onDelete}
onCancel={onCancel}
address={selectedAddress}
label={selectedLabel}
/>
</div>
)
}
export const AddressBook = withStatusGuard(_AddressBook, 'LOADED', () => <NoWallet />)