Skip to content

Commit 15d0cc8

Browse files
committed
feat(neuron-ui): add an alert when past epochs are less than 5
1 parent cd44e43 commit 15d0cc8

5 files changed

Lines changed: 45 additions & 8 deletions

File tree

packages/neuron-ui/src/components/CustomRows/DAORecordRow.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React, { useEffect, useState, useMemo } from 'react'
22
import { DefaultButton } from 'office-ui-fabric-react'
33
import { useTranslation } from 'react-i18next'
44
import { ckbCore, getBlockByNumber } from 'services/chain'
5+
import { showMessage } from 'services/remote'
56
import calculateAPY from 'utils/calculateAPY'
67
import { shannonToCKBFormatter, uniformTimeFormatter, localNumberFormatter } from 'utils/formatters'
78
import calculateClaimEpochNumber from 'utils/calculateClaimEpochNumber'
@@ -21,19 +22,28 @@ const DAORecord = ({
2122
depositOutPoint,
2223
epoch,
2324
withdraw,
25+
connectionStatus,
2426
}: State.NervosDAORecord & {
2527
actionLabel: string
2628
onClick: any
2729
tipBlockNumber: string
2830
epoch: string
2931
withdraw: string | null
32+
connectionStatus: 'online' | 'offline'
3033
}) => {
3134
const [t] = useTranslation()
3235
const [withdrawingEpoch, setWithdrawingEpoch] = useState('')
3336
const [depositEpoch, setDepositEpoch] = useState('')
3437

3538
useEffect(() => {
3639
if (!depositOutPoint) {
40+
getBlockByNumber(BigInt(blockNumber))
41+
.then(b => {
42+
setDepositEpoch(b.header.epoch)
43+
})
44+
.catch((err: Error) => {
45+
console.error(err)
46+
})
3747
return
3848
}
3949
const depositBlockNumber = ckbCore.utils.bytesToHex(ckbCore.utils.hexToBytes(daoData).reverse())
@@ -81,6 +91,26 @@ const DAORecord = ({
8191
}
8292
}
8393

94+
const onActionClick = useMemo(() => {
95+
const currentEpochInfo = epochParser(epoch)
96+
const thresholdEpoch = withdrawingEpoch || depositEpoch
97+
if (thresholdEpoch) {
98+
const thresholdEpochInfo = epochParser(thresholdEpoch)
99+
if (thresholdEpochInfo.number + BigInt(4) >= currentEpochInfo.number) {
100+
return () =>
101+
showMessage(
102+
{
103+
title: t('nervos-dao.insufficient-period-alert-title'),
104+
message: t('nervos-dao.insufficient-period-alert-title'),
105+
detail: t('nervos-dao.insufficient-period-alert-message'),
106+
},
107+
() => {}
108+
)
109+
}
110+
}
111+
return onClick
112+
}, [onClick, epoch, depositEpoch, withdrawingEpoch])
113+
84114
return (
85115
<div className={`${styles.daoRecord} ${depositOutPoint ? styles.isClaim : ''}`}>
86116
<div className={styles.primaryInfo}>
@@ -95,8 +125,8 @@ const DAORecord = ({
95125
text={actionLabel}
96126
data-tx-hash={txHash}
97127
data-index={index}
98-
onClick={onClick}
99-
disabled={depositOutPoint && !ready}
128+
onClick={onActionClick}
129+
disabled={connectionStatus === 'offline' || (depositOutPoint && !ready)}
100130
styles={{
101131
flexContainer: {
102132
pointerEvents: 'none',

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const NervosDAO = ({
3333
wallet,
3434
dispatch,
3535
nervosDAO: { records },
36+
chain: { connectionStatus },
3637
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => {
3738
const [t] = useTranslation()
3839
const [depositValue, setDepositValue] = useState(`${MIN_DEPOSIT_AMOUNT}`)
@@ -226,13 +227,14 @@ const NervosDAO = ({
226227
onClick={onActionClick}
227228
tipBlockNumber={tipBlockNumber}
228229
epoch={epoch}
230+
connectionStatus={connectionStatus}
229231
/>
230232
)
231233
})}
232234
</Stack>
233235
</>
234236
)
235-
}, [records, withdrawList, t, onActionClick, tipBlockNumber, epoch])
237+
}, [records, withdrawList, t, onActionClick, tipBlockNumber, epoch, connectionStatus])
236238

237239
const free = BigInt(wallet.balance)
238240
const locked = withdrawList.reduce((acc, w) => acc + BigInt(w || 0), BigInt(0))
@@ -271,7 +273,7 @@ const NervosDAO = ({
271273
<Stack horizontal verticalAlign="center" tokens={{ childrenGap: 15 }}>
272274
<DefaultButton
273275
text={t('nervos-dao.deposit')}
274-
disabled={sending}
276+
disabled={connectionStatus === 'offline' || sending}
275277
onClick={() => setShowDepositDialog(true)}
276278
/>
277279
<TooltipHost

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const Send = ({
3838
loadings: { sending = false },
3939
},
4040
wallet: { id: walletID = '', balance = '' },
41+
chain: { connectionStatus },
4142
dispatch,
4243
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps<{ address: string }>>) => {
4344
const { t } = useTranslation()
@@ -234,7 +235,7 @@ const Send = ({
234235
<PrimaryButton
235236
type="submit"
236237
onClick={onSubmit(walletID)}
237-
disabled={sending || !!errorMessageUnderTotal || !send.generatedTx}
238+
disabled={connectionStatus === 'offline' || sending || !!errorMessageUnderTotal || !send.generatedTx}
238239
text={t('send.send')}
239240
/>
240241
)}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@
333333
"minimal-fee-required": "The minimum deposit capacity is {{minimal}} CKB",
334334
"interest-accumulated": "{{blockNumber}} blocks interest accumulated",
335335
"blocks-left": "{{epochs}} epochs {{blocks}} blocks left(~{{days}} days)",
336-
"withdraw-alert": "Alert: these are only {{epochs}} epochs left before the next start withdrawing epoch number conforming to Nervos DAO, and it is possible that you have to do the withdraw after the next period(~{{days}}) due to the jam on CKB."
336+
"withdraw-alert": "Alert: these are only {{epochs}} epochs left before the next start withdrawing epoch number conforming to Nervos DAO, and it is possible that you have to do the withdraw after the next period(~{{days}}) due to the jam on CKB.",
337+
"insufficient-period-alert-title": "Insufficient Period",
338+
"insufficient-period-alert-message": "Nervos DAO needs at least 4 epochs to handle your request."
337339
}
338340
}
339341
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@
333333
"minimal-fee-required": "存入金额应不少于 {{minimal}} CKB",
334334
"interest-accumulated": "已累计 {{blockNumber}} 个块的利息",
335335
"blocks-left": " 还需等待 {{epochs}} epochs {{blocks}} 个块(~{{days}} 天)",
336-
"withdraw-alert": "风险提示:距离 NervosDAO 规定的最近一个允许提现 epoch 仅剩下 {{epochs}} 个 epoch,存在提现交易拥堵无法上链从而导致只能在下一个提现周期(约 {{days}} 天)的风险"
336+
"withdraw-alert": "风险提示:距离 NervosDAO 规定的最近一个允许提现 epoch 仅剩下 {{epochs}} 个 epoch,存在提现交易拥堵无法上链从而导致只能在下一个提现周期(约 {{days}} 天)的风险",
337+
"insufficient-alert-title": "Insufficient Period",
338+
"insufficient-alert-message": "Nervos DAO 要求您在至少 4 个 epochs 后执行此操作"
337339
}
338340
}
339341
}

0 commit comments

Comments
 (0)