diff --git a/packages/page-democracy/src/Overview/Propose.tsx b/packages/page-democracy/src/Overview/Propose.tsx index 1f2eab0b2a..f483bf33dc 100644 --- a/packages/page-democracy/src/Overview/Propose.tsx +++ b/packages/page-democracy/src/Overview/Propose.tsx @@ -4,12 +4,12 @@ import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; -import React, { useCallback, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; -import { Input, InputAddress, InputBalance, Modal, TxButton } from '@polkadot/react-components'; +import { Input, InputAddress, InputBalance, InputNumber, Modal, TxButton } from '@polkadot/react-components'; import { useApi, useCall, usePreimage } from '@polkadot/react-hooks'; import { Available } from '@polkadot/react-query'; -import { isFunction, isHex } from '@polkadot/util'; +import { BN_ZERO, isFunction, isHex } from '@polkadot/util'; import { useTranslation } from '../translate.js'; @@ -19,8 +19,14 @@ interface Props { } interface HashState { - hash?: HexString; - isHashValid: boolean; + imageHash?: HexString | null; + isImageHashValid: boolean; +} + +interface ImageState { + imageLen: BN; + imageLenDefault?: BN; + isImageLenValid: boolean; } function Propose ({ className = '', onClose }: Props): React.ReactElement { @@ -28,13 +34,36 @@ function Propose ({ className = '', onClose }: Props): React.ReactElement const { api } = useApi(); const [accountId, setAccountId] = useState(null); const [balance, setBalance] = useState(); - const [{ hash, isHashValid }, setHash] = useState({ isHashValid: false }); + const [{ imageHash, isImageHashValid }, setImageHash] = useState({ imageHash: null, isImageHashValid: false }); + const [{ imageLen, imageLenDefault, isImageLenValid }, setImageLen] = useState({ imageLen: BN_ZERO, isImageLenValid: false }); const publicProps = useCall(api.query.democracy.publicProps); - const preimage = usePreimage(hash); + const preimage = usePreimage(imageHash); - const _onChangeHash = useCallback( - (hash?: string): void => - setHash({ hash: hash as HexString, isHashValid: isHex(hash, 256) }), + useEffect((): void => { + preimage?.proposalLength && setImageLen((prev) => ({ + imageLen: prev.imageLen, + imageLenDefault: preimage.proposalLength, + isImageLenValid: prev.isImageLenValid + })); + }, [preimage]); + + const _onChangeImageHash = useCallback( + (h?: string) => + setImageHash({ + imageHash: h as HexString, + isImageHashValid: isHex(h, 256) + }), + [] + ); + + const _onChangeImageLen = useCallback( + (value?: BN): void => { + value && setImageLen((prev) => ({ + imageLen: value, + imageLenDefault: prev.imageLenDefault, + isImageLenValid: !value.isZero() + })); + }, [] ); @@ -61,13 +90,29 @@ function Propose ({ className = '', onClose }: Props): React.ReactElement type='account' /> - + +

{t('The hash of the preimage for the proposal as previously submitted or intended.')}

+

{t('The length value will be auto-populated from the on-chain value if it is found.')}

+ + } + > +
@@ -88,15 +133,15 @@ function Propose ({ className = '', onClose }: Props): React.ReactElement