Skip to content

Commit 7821ab3

Browse files
committed
feat(neuron-ui): separate the transaction view from the app
1 parent b37dc7e commit 7821ab3

9 files changed

Lines changed: 66 additions & 78 deletions

File tree

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

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import React, { useEffect, useMemo } from 'react'
2-
import { RouteComponentProps } from 'react-router-dom'
1+
import React, { useEffect, useState, useMemo } from 'react'
32
import { useTranslation } from 'react-i18next'
43
import { Stack, DetailsList, Text, DetailsListLayoutMode, CheckboxVisibility, IColumn } from 'office-ui-fabric-react'
4+
import { currentWallet as currentWalletCache } from 'utils/localCache'
5+
import { getTransaction } from 'services/remote'
56

6-
import { isMainWindow, getWinID } from 'services/remote'
7-
8-
import { AppActions, StateWithDispatch } from 'states/stateProvider/reducer'
9-
import { updateTransaction } from 'states/stateProvider/actionCreators'
10-
import chainState from 'states/initStates/chain'
7+
import { transactionState } from 'states/initStates/chain'
118

129
import { localNumberFormatter, uniformTimeFormatter } from 'utils/formatters'
1310

@@ -96,26 +93,38 @@ const basicInfoColumns: IColumn[] = [
9693
...col,
9794
})
9895
)
99-
const Transaction = ({
100-
wallet: { id: walletID = '' },
101-
chain: { transaction = chainState.transaction },
102-
match,
103-
dispatch,
104-
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps<{ hash: string }>>) => {
96+
const Transaction = () => {
10597
const [t] = useTranslation()
98+
const [transaction, setTransaction] = useState(transactionState)
99+
const [error, setError] = useState({ code: '', message: '' })
106100
useEffect(() => {
107-
if ((walletID && !isMainWindow(getWinID())) || !walletID) {
108-
window.close()
101+
const currentWallet = currentWalletCache.load()
102+
if (currentWallet) {
103+
const hash = window.location.href.split('/').pop()
104+
getTransaction({ hash, walletID: currentWallet.id })
105+
.then(res => {
106+
if (res.status) {
107+
setTransaction(res.result)
108+
} else {
109+
throw new Error(res.message.title)
110+
}
111+
})
112+
.catch((err: Error) => {
113+
setError({
114+
code: '-1',
115+
message: err.message,
116+
})
117+
})
109118
}
110-
}, [walletID])
119+
}, [])
111120

112121
useEffect(() => {
113-
dispatch({
114-
type: AppActions.CleanTransaction,
115-
payload: null,
122+
window.addEventListener('storage', (e: StorageEvent) => {
123+
if (e.key === 'currentWallet') {
124+
window.close()
125+
}
116126
})
117-
updateTransaction({ walletID, hash: match.params.hash })(dispatch)
118-
}, [match.params.hash, dispatch, walletID])
127+
}, [])
119128

120129
const basicInfoItems = useMemo(
121130
() => [
@@ -138,6 +147,14 @@ const Transaction = ({
138147
[t, transaction]
139148
)
140149

150+
if (error.code) {
151+
return (
152+
<Stack verticalFill verticalAlign="center" horizontalAlign="center">
153+
{error.message || t('messages.transaction-not-found')}
154+
</Stack>
155+
)
156+
}
157+
141158
return (
142159
<Stack tokens={{ childrenGap: 15 }}>
143160
<Stack tokens={{ childrenGap: 15 }}>

packages/neuron-ui/src/containers/Main/hooks.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { NeuronWalletActions, StateDispatch, AppActions } from 'states/stateProv
44
import {
55
toggleAddressBook,
66
updateTransactionList,
7-
updateTransaction,
87
updateCurrentWallet,
98
updateWalletList,
109
updateAddressListAndBalance,
@@ -126,7 +125,6 @@ export const useSubscription = ({
126125
dispatch: StateDispatch
127126
}) => {
128127
const { pageNo, pageSize, keywords } = chain.transactions
129-
const { hash: txHash } = chain.transaction
130128
useEffect(() => {
131129
const systemScriptSubscription = SystemScriptSubject.subscribe(({ codeHash = '' }: { codeHash: string }) => {
132130
systemScriptCache.save({ codeHash })
@@ -151,7 +149,6 @@ export const useSubscription = ({
151149
pageNo,
152150
pageSize,
153151
})(dispatch)
154-
updateTransaction({ walletID, hash: txHash })
155152
break
156153
}
157154
case 'current-wallet': {
@@ -242,7 +239,7 @@ export const useSubscription = ({
242239
syncedBlockNumberSubscription.unsubscribe()
243240
commandSubscription.unsubscribe()
244241
}
245-
}, [walletID, pageNo, pageSize, keywords, txHash, history, dispatch])
242+
}, [walletID, pageNo, pageSize, keywords, history, dispatch])
246243
}
247244

248245
export default {

packages/neuron-ui/src/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Navbar from 'containers/Navbar'
1111
import Notification from 'containers/Notification'
1212
import Main from 'containers/Main'
1313
import Footer from 'containers/Footer'
14+
import Transaction from 'components/Transaction'
1415
import ErrorBoundary from 'components/ErrorBoundary'
1516
import withProviders from 'states/stateProvider'
1617

@@ -63,7 +64,11 @@ Object.defineProperty(App, 'displayName', {
6364
value: 'App',
6465
})
6566

66-
ReactDOM.render(<App />, document.getElementById('root'))
67+
if (window.location.hash.startsWith('#/transaction/')) {
68+
ReactDOM.render(<Transaction />, document.getElementById('root'))
69+
} else {
70+
ReactDOM.render(<App />, document.getElementById('root'))
71+
}
6772

6873
serviceWorker.register()
6974

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@
238238
"delete-network-successfully": "Delete the network successfully",
239239
"addr-copied": "Address has been copied to the clipboard",
240240
"qrcode-copied": "QR Code has been copied to the clipboard",
241-
"lock-arg-copied": "Lock Arg has been copied to the clipboard"
241+
"lock-arg-copied": "Lock Arg has been copied to the clipboard",
242+
"transaction-not-found": "The transaction is not found"
242243
},
243244
"sync": {
244245
"syncing": "Syncing",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@
238238
"delete-network-successfully": "节点已删除",
239239
"addr-copied": "地址已复制到剪贴板",
240240
"qrcode-copied": "二维码已复制到剪贴板",
241-
"lock-arg-copied": "Lock Arg 已复制到剪贴板"
241+
"lock-arg-copied": "Lock Arg 已复制到剪贴板",
242+
"transaction-not-found": "未找到交易"
242243
},
243244
"sync": {
244245
"syncing": "同步中",

packages/neuron-ui/src/states/initStates/chain.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import { currentNetworkID, systemScript } from 'utils/localCache'
22
import { ConnectionStatus } from 'utils/const'
33

4+
export const transactionState: State.DetailedTransaction = {
5+
value: '',
6+
hash: '',
7+
type: 'other',
8+
createdAt: '0',
9+
updatedAt: '0',
10+
timestamp: '0',
11+
description: '',
12+
status: 'pending',
13+
inputs: [],
14+
outputs: [],
15+
deps: [],
16+
blockNumber: '',
17+
blockHash: '',
18+
witnesses: [],
19+
}
20+
421
const chainState: State.Chain = {
522
networkID: currentNetworkID.load(),
623
connectionStatus: ConnectionStatus.Offline,
724
tipBlockNumber: '',
825
codeHash: systemScript.load().codeHash,
9-
transaction: {
10-
value: '',
11-
hash: '',
12-
type: 'other',
13-
createdAt: '0',
14-
updatedAt: '0',
15-
timestamp: '0',
16-
description: '',
17-
status: 'pending',
18-
inputs: [],
19-
outputs: [],
20-
deps: [],
21-
blockNumber: '',
22-
blockHash: '',
23-
witnesses: [],
24-
},
2526
transactions: {
2627
pageNo: 1,
2728
pageSize: 15,

packages/neuron-ui/src/states/stateProvider/actionCreators/transactions.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
import { NeuronWalletActions, AppActions, StateDispatch } from 'states/stateProvider/reducer'
22
import {
33
GetTransactionListParams,
4-
getTransaction,
54
getTransactionList,
65
updateTransactionDescription as updateRemoteTransactionDescription,
76
} from 'services/remote'
87
import { addNotification } from './app'
98

10-
export const updateTransaction = (params: { walletID: string; hash: string }) => (dispatch: StateDispatch) => {
11-
getTransaction(params).then(res => {
12-
if (res.status) {
13-
dispatch({
14-
type: NeuronWalletActions.UpdateTransaction,
15-
payload: res.result,
16-
})
17-
} else {
18-
addNotification({ type: 'alert', content: res.message.title })(dispatch)
19-
}
20-
})
21-
}
229
export const updateTransactionList = (params: GetTransactionListParams) => (dispatch: StateDispatch) => {
2310
getTransactionList(params).then(res => {
2411
if (res.status) {
@@ -66,6 +53,5 @@ export const updateTransactionDescription = (params: Controller.UpdateTransactio
6653
}
6754

6855
export default {
69-
updateTransaction,
7056
updateTransactionList,
7157
}

packages/neuron-ui/src/states/stateProvider/reducer.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export enum NeuronWalletActions {
1111
UpdateAddressDescription = 'updateAddressDescription',
1212
// transactions
1313
UpdateTransactionList = 'updateTransactionList',
14-
UpdateTransaction = 'updateTransaction',
1514
UpdateTransactionDescription = 'updateTransactionDescription',
1615
// networks
1716
UpdateNetworkList = 'updateNetworkList',
@@ -189,15 +188,6 @@ export const reducer = (
189188
},
190189
}
191190
}
192-
case NeuronWalletActions.UpdateTransaction: {
193-
return {
194-
...state,
195-
chain: {
196-
...chain,
197-
transaction: payload,
198-
},
199-
}
200-
}
201191
case NeuronWalletActions.UpdateNetworkList: {
202192
return {
203193
...state,
@@ -382,15 +372,6 @@ export const reducer = (
382372
},
383373
}
384374
}
385-
case AppActions.CleanTransaction: {
386-
return {
387-
...state,
388-
chain: {
389-
...chain,
390-
transaction: initStates.chain.transaction,
391-
},
392-
}
393-
}
394375
case AppActions.RequestPassword: {
395376
return {
396377
...state,

packages/neuron-ui/src/types/App/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ declare namespace State {
125125
connectionStatus: 'online' | 'offline'
126126
tipBlockNumber: string
127127
codeHash: string
128-
transaction: DetailedTransaction
129128
transactions: {
130129
pageNo: number
131130
pageSize: number

0 commit comments

Comments
 (0)