forked from nervosnetwork/neuron
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.tsx
138 lines (123 loc) · 4.31 KB
/
index.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
import React, { useEffect, useCallback, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ReactComponent as EditNetwork } from 'widgets/Icons/Edit.svg'
import { ReactComponent as DeleteNetwork } from 'widgets/Icons/Delete.svg'
import { ReactComponent as AddSimple } from 'widgets/Icons/AddSimple.svg'
import NetworkEditorDialog from 'components/NetworkEditorDialog'
import AlertDialog from 'widgets/AlertDialog'
import Toast from 'widgets/Toast'
import { chainState } from 'states'
import { setCurrentNetwork, deleteNetwork } from 'services/remote'
import RadioGroup from 'widgets/RadioGroup'
import { useOnWindowResize, useToggleChoiceGroupBorder, getNetworkLabelI18nkey } from 'utils'
import styles from './networkSetting.module.scss'
const NetworkSetting = ({ chain = chainState, settings: { networks = [] } }: State.AppWithNeuronWallet) => {
const [t] = useTranslation()
const [showEditorDialog, setShowEditorDialog] = useState(false)
const [showDeleteDialog, setShowDeleteDialog] = useState(false)
const [notice, setNotice] = useState('')
const { networkID: currentId } = chain
const [netId, setNetId] = useState('new')
const onHandleNetwork = useCallback(
(e: React.BaseSyntheticEvent) => {
const {
dataset: { action, id },
} = e.target
switch (action) {
case 'edit': {
setNetId(id)
setShowEditorDialog(true)
break
}
case 'delete': {
setNetId(id)
setShowDeleteDialog(true)
break
}
case 'add': {
setNetId('new')
setShowEditorDialog(true)
break
}
default:
// @ts-ignore
}
},
[setNetId, setShowEditorDialog, setShowDeleteDialog]
)
const toggleBottomBorder = useToggleChoiceGroupBorder(`.${styles.networks}`, styles.hasBottomBorder)
useEffect(() => {
if (networks.length) {
toggleBottomBorder()
}
}, [toggleBottomBorder, networks.length])
useOnWindowResize(toggleBottomBorder)
const handleChange = useCallback(
checked => {
if (checked !== currentId) {
setCurrentNetwork(checked)
}
},
[currentId]
)
const onEditSuccess = useCallback(() => {
setShowEditorDialog(false)
if (netId !== 'new') {
setNotice(t('settings.network.edit-success'))
}
}, [setShowEditorDialog, setNotice, netId])
return (
<div>
<RadioGroup
defaultValue={currentId}
onChange={handleChange}
itemClassName={styles.radioItem}
options={networks.map(network => ({
value: network.id,
label: (
<div className={styles.networkLabel}>
<p>{`${network.name} (${network.remote})`}</p>
<div className={styles.tag}>{t(getNetworkLabelI18nkey(network.chain))}</div>
</div>
),
suffix: (
<div className={styles.suffix}>
{network.readonly ? null : (
<button type="button" aria-label={t('common.edit')} onClick={onHandleNetwork}>
<EditNetwork data-action="edit" data-id={network.id} />
</button>
)}
{network.type && !network.readonly ? (
<button type="button" aria-label={t('common.delete')} onClick={onHandleNetwork}>
<DeleteNetwork data-action="delete" data-id={network.id} />
</button>
) : null}
</div>
),
}))}
/>
<button type="button" className={styles.addBtn} onClick={onHandleNetwork}>
<span data-action="add">
<AddSimple /> {t('settings.network.add-network')}
</span>
</button>
{showEditorDialog ? (
<NetworkEditorDialog onSuccess={onEditSuccess} onCancel={() => setShowEditorDialog(false)} id={netId} />
) : null}
<Toast content={notice} onDismiss={() => setNotice('')} />
<AlertDialog
show={showDeleteDialog}
title={t('settings.network.remove-network')}
message={t('settings.network.remove-network-msg')}
type="warning"
onCancel={() => setShowDeleteDialog(false)}
onOk={() => {
deleteNetwork(netId)
setShowDeleteDialog(false)
}}
/>
</div>
)
}
NetworkSetting.displayName = 'NetworkSetting'
export default NetworkSetting