Support new preimages on v1 democracy (#8390)

This commit is contained in:
Jaco 2022-11-21 21:04:59 +02:00 committed by GitHub
parent 893a216d19
commit 7639ac4bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 27 deletions

View File

@ -23,21 +23,24 @@ function PreImageButton ({ imageHash, isImminent }: Props): React.ReactElement<P
const [isPreimageOpen, togglePreimage] = useToggle();
return (
<>
<Button
icon='plus'
isDisabled={!api.tx.democracy.notePreimage}
label={t<string>('Image')}
onClick={togglePreimage}
/>
{isPreimageOpen && (
<PreImage
imageHash={imageHash}
isImminent={isImminent}
onClose={togglePreimage}
/>
)}
</>
api.tx.democracy.notePreimage
? (
<>
<Button
icon='plus'
label={t<string>('Image')}
onClick={togglePreimage}
/>
{isPreimageOpen && (
<PreImage
imageHash={imageHash}
isImminent={isImminent}
onClose={togglePreimage}
/>
)}
</>
)
: null
);
}

View File

@ -3,9 +3,11 @@
import type { Compact } from '@polkadot/types';
import type { Hash, Proposal, ProposalIndex } from '@polkadot/types/interfaces';
import type { HexString } from '@polkadot/util/types';
import React from 'react';
import usePreimage from '@polkadot/app-preimages/usePreimage';
import { CallExpander } from '@polkadot/react-components';
import { useTranslation } from '../translate';
@ -14,7 +16,7 @@ import TreasuryCell from './TreasuryCell';
interface Props {
className?: string;
imageHash: Hash | string;
imageHash: Hash | HexString;
proposal?: Proposal | null;
}
@ -23,8 +25,15 @@ const METHOD_TREA = ['approveProposal', 'rejectProposal'];
function ProposalCell ({ className = '', imageHash, proposal }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const preimage = usePreimage(imageHash);
if (!proposal) {
const details = proposal
? [proposal, proposal.registry.findMetaCall(proposal.callIndex)]
: preimage?.proposal
? [preimage.proposal, preimage.registry.findMetaCall(preimage.proposal.callIndex)]
: null;
if (!details) {
const textHash = imageHash.toString();
return (
@ -34,7 +43,7 @@ function ProposalCell ({ className = '', imageHash, proposal }: Props): React.Re
);
}
const { method, section } = proposal.registry.findMetaCall(proposal.callIndex);
const { method, section } = details[1];
const isTreasury = section === 'treasury' && METHOD_TREA.includes(method);
const isExternal = section === 'democracy' && METHOD_EXTE.includes(method);
@ -46,10 +55,10 @@ function ProposalCell ({ className = '', imageHash, proposal }: Props): React.Re
withHash={!isTreasury && !isExternal}
>
{isExternal && (
<ExternalCell value={proposal.args[0] as Hash} />
<ExternalCell value={details[0].args[0] as Hash} />
)}
{isTreasury && (
<TreasuryCell value={proposal.args[0] as Compact<ProposalIndex>} />
<TreasuryCell value={details[0].args[0] as Compact<ProposalIndex>} />
)}
</CallExpander>
</td>

View File

@ -31,11 +31,13 @@ function Overview ({ className }: Props): React.ReactElement<Props> {
<div className={className}>
<Summary referendumCount={referendums?.length} />
<Button.Group>
<Button
icon='plus'
label={t<string>('Submit preimage')}
onClick={togglePreimage}
/>
{api.tx.democracy.notePreimage && (
<Button
icon='plus'
label={t<string>('Submit preimage')}
onClick={togglePreimage}
/>
)}
<Button
icon='plus'
label={t<string>('Submit proposal')}

View File

@ -4,6 +4,7 @@
import type { Bytes } from '@polkadot/types';
import type { Call } from '@polkadot/types/interfaces';
import type { PalletPreimageRequestStatus } from '@polkadot/types/lookup';
import type { Registry } from '@polkadot/types/types';
import type { BN } from '@polkadot/util';
import type { HexString } from '@polkadot/util/types';
@ -13,5 +14,6 @@ export interface Preimage {
proposal: Call | null;
proposalHash: HexString;
proposalLength: BN;
registry: Registry;
status: PalletPreimageRequestStatus | null;
}

View File

@ -44,6 +44,7 @@ function createResult (api: ApiPromise, optStatus: Option<PalletPreimageRequestS
proposal,
proposalHash,
proposalLength,
registry: api.registry,
status
};
}
@ -85,14 +86,14 @@ function usePreimageImpl (hashOrBounded?: Hash | HexString | FrameSupportPreimag
[hashOrBounded]
);
const optStatus = useCall<Option<PalletPreimageRequestStatus>>(hash && api.query.preimage.statusFor, [hash]);
const optStatus = useCall<Option<PalletPreimageRequestStatus>>(hash && api.query.preimage?.statusFor, [hash]);
const params = useMemo(
() => hash && optStatus && getParams(hash, optStatus),
[hash, optStatus]
);
const optBytes = useCall<Option<Bytes>>(params && api.query.preimage.preimageFor, [params]);
const optBytes = useCall<Option<Bytes>>(params && api.query.preimage?.preimageFor, [params]);
return useMemo(
() => optBytes && optStatus && params