From e3444ae6a9395ff7d2700d962e1f4bbf19998f79 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 12 Jan 2023 13:27:37 +0200 Subject: [PATCH 1/3] Preimage loading status indicator --- packages/page-preimages/src/usePreimage.ts | 38 +++++++++++------ .../page-referenda/src/Referenda/Submit.tsx | 11 +++-- .../react-components/src/Modal/Actions.tsx | 2 +- packages/react-components/src/Modal/Base.tsx | 42 ++++++++++--------- .../react-components/src/Modal/Header.tsx | 6 +++ 5 files changed, 62 insertions(+), 37 deletions(-) diff --git a/packages/page-preimages/src/usePreimage.ts b/packages/page-preimages/src/usePreimage.ts index 7385cecea45..a5c4f4ad57f 100644 --- a/packages/page-preimages/src/usePreimage.ts +++ b/packages/page-preimages/src/usePreimage.ts @@ -24,6 +24,8 @@ interface InterimResult { type Result = 'unknown' | 'hash' | 'hashAndLen'; +const OPT_STATUS = { withParamsTransform: true }; + export function getParamType (api: ApiPromise): Result { if (( api.query.preimage && @@ -96,10 +98,8 @@ function convertDeposit (deposit?: [AccountId, Balance] | null): PreimageDeposit : undefined; } -function getBytesParams (api: ApiPromise, hash: Hash | HexString, optStatus: Option): InterimResult { - const isHashParam = getParamType(api) === 'hash'; - const status = optStatus.unwrapOr(null); - const preimageStatus: PreimageStatus = { +function createStatus (api: ApiPromise, hash: HexString | Hash, status: PalletPreimageRequestStatus | null = null): PreimageStatus { + return { count: 0, isCompleted: false, proposalHash: isString(hash) @@ -108,6 +108,12 @@ function getBytesParams (api: ApiPromise, hash: Hash | HexString, optStatus: Opt registry: api.registry, status }; +} + +function getBytesParams (api: ApiPromise, [[proposalHash], optStatus]: [[HexString], Option]): InterimResult { + const isHashParam = getParamType(api) === 'hash'; + const status = optStatus.unwrapOr(null); + const preimageStatus = createStatus(api, proposalHash, status); if (status) { if (status.isRequested) { @@ -167,26 +173,32 @@ export function getPreimageHash (hashOrBounded: Hash | HexString | FrameSupportP : hashOrBounded.toHex(); } +function getStatusParam (hashOrBounded?: Hash | HexString | FrameSupportPreimagesBounded | null): [HexString] | undefined { + const hash = hashOrBounded && getPreimageHash(hashOrBounded); + + return hash && hash.length === 66 + ? [hash] + : undefined; +} + function usePreimageImpl (hashOrBounded?: Hash | HexString | FrameSupportPreimagesBounded | null): Preimage | undefined { const { api } = useApi(); // retrieve the status using only the hash of the image const paramsStatus = useMemo( - // we need a hash _and_ be on the newest supported version of the pallet - // (after the application of bounded calls) - () => hashOrBounded - ? [getPreimageHash(hashOrBounded)] - : undefined, + () => getStatusParam(hashOrBounded), [hashOrBounded] ); - const optStatus = useCall>(paramsStatus && api.query.preimage?.statusFor, paramsStatus); + const optStatus = useCall<[[HexString], Option]>(paramsStatus && api.query.preimage?.statusFor, paramsStatus, OPT_STATUS); // from the retrieved status (if any), get the on-chain stored bytes const { paramsBytes, preimageStatus } = useMemo( - () => paramsStatus && optStatus - ? getBytesParams(api, paramsStatus[0], optStatus) - : {}, + () => optStatus + ? getBytesParams(api, optStatus) + : paramsStatus + ? { preimageStatus: createStatus(api, paramsStatus[0]) } + : {}, [api, optStatus, paramsStatus] ); diff --git a/packages/page-referenda/src/Referenda/Submit.tsx b/packages/page-referenda/src/Referenda/Submit.tsx index 613be587709..b5199b30cce 100644 --- a/packages/page-referenda/src/Referenda/Submit.tsx +++ b/packages/page-referenda/src/Referenda/Submit.tsx @@ -181,12 +181,14 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: [] ); + const isLoadingPreimage = isImageHashValid && (!preimage || !preimage.isCompleted || preimage.proposalHash !== imageHash); + return ( <> {trackOpts && isSubmitOpen && ( ('Submit proposal')} + header={t('Create referendum')} onClose={toggleSubmit} size='large' > @@ -252,8 +254,9 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: /> ('preimage length')} onChange={_onChangeImageLen} @@ -307,7 +310,7 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: accountId={accountId} icon='plus' isDisabled={!selectedOrigin || !isImageHashValid || !isImageLenValid || !accountId || isInvalidAt || !preimage?.proposalHash} - label={t('Submit proposal')} + label={t('Create referendum')} onStart={toggleSubmit} params={[ selectedOrigin, @@ -328,7 +331,7 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }: