Skip to content

Commit 7d86454

Browse files
committed
feat(neuron-ui): add copy address and copy tx hash context menus on the tx detail view.
1 parent dd48b47 commit 7d86454

4 files changed

Lines changed: 99 additions & 5 deletions

File tree

packages/neuron-ui/src/components/Transaction/index.tsx

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import React, { useEffect, useState, useMemo, useCallback } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import { Stack, DetailsList, Text, CheckboxVisibility, IColumn, Icon } from 'office-ui-fabric-react'
44
import { currentWallet as currentWalletCache } from 'services/localCache'
5-
import { getTransaction, showErrorMessage, getAllNetworks, getCurrentNetworkID, openExternal } from 'services/remote'
5+
import {
6+
getTransaction,
7+
showErrorMessage,
8+
getAllNetworks,
9+
getCurrentNetworkID,
10+
openExternal,
11+
openContextMenu,
12+
} from 'services/remote'
613
import { ckbCore } from 'services/chain'
714

815
import { transactionState } from 'states/initStates/chain'
@@ -111,7 +118,7 @@ const Transaction = () => {
111118
name: t('transaction.index'),
112119
minWidth: 60,
113120
maxWidth: 60,
114-
onRender: (item?: any | State.DetailedOutput) => {
121+
onRender: (item?: State.DetailedOutput) => {
115122
if (item) {
116123
return item.outPoint.index
117124
}
@@ -216,7 +223,7 @@ const Transaction = () => {
216223
return
217224
}
218225
getTransaction({ hash, walletID: currentWallet.id })
219-
.then((res: any) => {
226+
.then(res => {
220227
if (res.status) {
221228
setTransaction(res.result)
222229
} else {
@@ -270,6 +277,75 @@ const Transaction = () => {
270277
[t, transaction]
271278
)
272279

280+
const onBasicInfoContextMenu = useCallback(
281+
(property: { label: string; value: string }, index?: number) => {
282+
if (index === 0 && property && property.value) {
283+
const menuTemplate = [
284+
{
285+
label: t('common.copy-tx-hash'),
286+
click: () => {
287+
window.clipboard.writeText(property.value)
288+
},
289+
},
290+
]
291+
openContextMenu(menuTemplate)
292+
}
293+
},
294+
[t]
295+
)
296+
297+
const onInputContextMenu = useCallback(
298+
(input?: State.DetailedInput) => {
299+
if (input && input.lock && input.lock.args) {
300+
try {
301+
const address = ckbCore.utils.bech32Address(input.lock.args, {
302+
prefix: addressPrefix,
303+
type: ckbCore.utils.AddressType.HashIdx,
304+
codeHashOrCodeHashIndex: '0x00',
305+
})
306+
const menuTemplate = [
307+
{
308+
label: t('common.copy-address'),
309+
click: () => {
310+
window.clipboard.writeText(address)
311+
},
312+
},
313+
]
314+
openContextMenu(menuTemplate)
315+
} catch (err) {
316+
console.error(err)
317+
}
318+
}
319+
},
320+
[addressPrefix, t]
321+
)
322+
323+
const onOutputContextMenu = useCallback(
324+
(output?: State.DetailedOutput) => {
325+
if (output && output.lock && output.lock.args) {
326+
try {
327+
const address = ckbCore.utils.bech32Address(output.lock.args, {
328+
prefix: addressPrefix,
329+
type: ckbCore.utils.AddressType.HashIdx,
330+
codeHashOrCodeHashIndex: '0x00',
331+
})
332+
const menuTemplate = [
333+
{
334+
label: t('common.copy-address'),
335+
click: () => {
336+
window.clipboard.writeText(address)
337+
},
338+
},
339+
]
340+
openContextMenu(menuTemplate)
341+
} catch (err) {
342+
console.error(err)
343+
}
344+
}
345+
},
346+
[addressPrefix, t]
347+
)
348+
273349
if (error.code) {
274350
return (
275351
<Stack verticalFill verticalAlign="center" horizontalAlign="center">
@@ -290,6 +366,7 @@ const Transaction = () => {
290366
checkboxVisibility={CheckboxVisibility.hidden}
291367
compact
292368
isHeaderVisible={false}
369+
onItemContextMenu={onBasicInfoContextMenu}
293370
/>
294371
</Stack>
295372
<Stack tokens={{ childrenGap: 15 }} verticalFill>
@@ -303,6 +380,7 @@ const Transaction = () => {
303380
items={transaction.inputs}
304381
columns={inputColumns}
305382
checkboxVisibility={CheckboxVisibility.hidden}
383+
onItemContextMenu={onInputContextMenu}
306384
compact
307385
isHeaderVisible
308386
/>
@@ -317,6 +395,7 @@ const Transaction = () => {
317395
items={transaction.outputs}
318396
columns={outputColumns}
319397
checkboxVisibility={CheckboxVisibility.hidden}
398+
onItemContextMenu={onOutputContextMenu}
320399
compact
321400
isHeaderVisible
322401
/>

packages/neuron-ui/src/locales/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@
241241
"toggle": {
242242
"on": "On",
243243
"off": "Off"
244-
}
244+
},
245+
"copy-tx-hash": "Copy transaction hash",
246+
"copy-address": "Copy address"
245247
},
246248
"notification-panel": {
247249
"title": "Notifications"

packages/neuron-ui/src/locales/zh.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@
241241
"toggle": {
242242
"on": "",
243243
"off": ""
244-
}
244+
},
245+
"copy-tx-hash": "复制交易 Hash",
246+
"copy-address": "复制地址"
245247
},
246248
"notification-panel": {
247249
"title": "通知中心"

packages/neuron-ui/src/services/remote/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ export const openExternal = (url: string) => {
8181
}
8282
}
8383

84+
export const openContextMenu = (template: { label: string; click: Function }[]): void => {
85+
if (!window.remote) {
86+
window.alert(REMOTE_MODULE_NOT_FOUND)
87+
} else {
88+
const { Menu } = window.remote.require('electron')
89+
const menu = Menu.buildFromTemplate(template)
90+
menu.popup()
91+
}
92+
}
93+
8494
export default {
8595
getLocale,
8696
validateMnemonic,
@@ -90,4 +100,5 @@ export default {
90100
showOpenDialog,
91101
getWinID,
92102
openExternal,
103+
openContextMenu,
93104
}

0 commit comments

Comments
 (0)