Skip to content

Commit

Permalink
feat: Enable click max when address is empty. (#2932)
Browse files Browse the repository at this point in the history
* feat: Enable click max when address is empty.

Use placeholder address to generate tx.

* fix: Fix test case

* fix: Add ignore for last address and only ignore address for max send.

* fix: Ignore last item when check the max button status

* fix: Change max to reset when is sending max.
  • Loading branch information
yanguoyu authored Nov 17, 2023
1 parent 7a11fb9 commit 04d344c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
23 changes: 18 additions & 5 deletions packages/neuron-ui/src/components/Send/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
shannonToCKBFormatter,
calculateFee,
validateOutputs,
DefaultLockInfo,
} from 'utils'
import { scriptToAddress } from '@nervosnetwork/ckb-sdk-utils'
import { PlaceHolderArgs } from 'utils/const'

let generateTxTimer: ReturnType<typeof setTimeout>

Expand Down Expand Up @@ -330,10 +333,7 @@ export const useInitialize = (
const [errorMessage, setErrorMessage] = useState('')
const [isSendMax, setIsSendMax] = useState(false)

const outputs = useMemo(
() => items.map(item => ({ ...item, disabled: isSendMax || sending })),
[items, isSendMax, sending]
)
const outputs = useMemo(() => items.map(item => ({ ...item, disabled: sending })), [items, sending])

const updateIsSendMax = useCallback(
(payload: boolean) => {
Expand All @@ -358,7 +358,19 @@ export const useInitialize = (
const updateSendingAllTransaction = useCallback(() => {
updateTransactionWith(generateSendingAllTx)({
walletID,
items,
items: items.map(v => ({
...v,
address:
v.address ||
scriptToAddress(
{
codeHash: DefaultLockInfo.CodeHash,
hashType: DefaultLockInfo.HashType,
args: PlaceHolderArgs,
},
isMainnet
),
})),
price,
setTotalAmount,
setErrorMessage,
Expand Down Expand Up @@ -413,6 +425,7 @@ export const useInitialize = (
setErrorMessage,
isSendMax,
onSendMaxClick,
updateIsSendMax,
}
}

Expand Down
22 changes: 19 additions & 3 deletions packages/neuron-ui/src/components/Send/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useCallback } from 'react'
import React, { useState, useCallback, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { List } from 'office-ui-fabric-react'
import { useState as useGlobalState, useDispatch, appState } from 'states'
Expand All @@ -21,6 +21,7 @@ import {
import { HIDE_BALANCE } from 'utils/const'

import { isErrorWithI18n } from 'exceptions'
import { useSearchParams } from 'react-router-dom'
import { useInitialize } from './hooks'
import styles from './send.module.scss'

Expand Down Expand Up @@ -81,8 +82,18 @@ const Send = () => {
setErrorMessage,
isSendMax,
onSendMaxClick: handleSendMaxClick,
updateIsSendMax,
} = useInitialize(walletID, send.outputs, send.generatedTx, send.price, sending, isMainnet, dispatch, t)

const [searchParams] = useSearchParams()

useEffect(() => {
if (searchParams.get('isSendMax')) {
updateIsSendMax(true)
}
// only when router change init send max
}, [searchParams, updateIsSendMax])

const [locktimeIndex, setLocktimeIndex] = useState<number>(-1)

const handleLocktimeClick = useCallback(
Expand Down Expand Up @@ -128,13 +139,18 @@ const Send = () => {
}
}

const disabled = connectionStatus === 'offline' || sending || !!errorMessageUnderTotal || !send.generatedTx
const disabled =
connectionStatus === 'offline' ||
sending ||
!!errorMessageUnderTotal ||
!send.generatedTx ||
outputs.some(v => !v.address)

const outputErrors = useOutputErrors(outputs, isMainnet)

const isMaxBtnDisabled = (() => {
try {
validateOutputs(outputs, isMainnet, true)
validateOutputs(outputs.slice(0, -1), isMainnet)
} catch {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions packages/neuron-ui/src/components/SendFieldset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ const SendFieldset = ({
value={item.amount ? localNumberFormatter(item.amount) : ''}
placeholder={t('send.input-amount')}
onChange={onItemChange}
disabled={item.disabled}
disabled={item.disabled || isSendMax}
suffix={
isMaxBtnShow && (
<Button disabled={isMaxBtnDisabled} type="text" onClick={onSendMaxClick} className={styles.max}>
Max
{isSendMax ? t('send.reset') : 'Max'}
</Button>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe(`Test outputs validator`, () => {
expect(e).not.toBeUndefined()
}
} else {
expect(validateOutputs()).toBeTruthy()
expect(validateOutputs(outputs, false, ignoreLastAmount)).toBeTruthy()
}
})
})
2 changes: 2 additions & 0 deletions packages/neuron-ui/src/utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ export const MILLISECONDS = HOURS_PER_EPOCH * 60 * 60 * 1000

export const ADDRESS_MIN_LENGTH = 86
export const ADDRESS_HEAD_TAIL_LENGTH = 34

export const PlaceHolderArgs = `0x${'00'.repeat(21)}`

2 comments on commit 04d344c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 6899454454

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 6899454429

Please sign in to comment.