Don't retrieve proposals, only ids (#4382)
* Don't rerive proposals, only ids * types
This commit is contained in:
parent
bcf98f4c17
commit
9731339ecb
@ -1,7 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ProposalExt } from './types';
|
||||
import type { ParaId } from '@polkadot/types/interfaces';
|
||||
import type { ScheduledProposals } from './useProposals';
|
||||
|
||||
import React, { useMemo } from 'react';
|
||||
|
||||
@ -11,16 +12,20 @@ import { FormatBalance } from '@polkadot/react-query';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import { useProposal } from './useProposals';
|
||||
import { sliceHex } from './util';
|
||||
|
||||
interface Props {
|
||||
proposal: ProposalExt;
|
||||
approvedIds: ParaId[];
|
||||
id: ParaId;
|
||||
scheduled: ScheduledProposals[];
|
||||
}
|
||||
|
||||
function Proposal ({ proposal: { id, isApproved, isScheduled, proposal: { balance, genesisHead, name, proposer, validationCode, validators } } }: Props): React.ReactElement<Props> {
|
||||
function Proposal ({ approvedIds, id, scheduled }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { hasSudoKey, sudoKey } = useSudo();
|
||||
const proposal = useProposal(id, approvedIds, scheduled);
|
||||
|
||||
const approveTx = useMemo(
|
||||
() => api.tx.proposeParachain && api.tx.sudo && api.tx.sudo.sudo(api.tx.proposeParachain.approveProposal(id)),
|
||||
@ -28,39 +33,39 @@ function Proposal ({ proposal: { id, isApproved, isScheduled, proposal: { balanc
|
||||
);
|
||||
|
||||
const initialHex = useMemo(
|
||||
() => sliceHex(genesisHead, 8),
|
||||
[genesisHead]
|
||||
() => proposal && sliceHex(proposal.proposal.genesisHead, 8),
|
||||
[proposal]
|
||||
);
|
||||
|
||||
const validationHex = useMemo(
|
||||
() => sliceHex(validationCode, 8),
|
||||
[validationCode]
|
||||
() => proposal && sliceHex(proposal.proposal.validationCode, 8),
|
||||
[proposal]
|
||||
);
|
||||
|
||||
return (
|
||||
<tr>
|
||||
<td className='number'><h1>{formatNumber(id)}</h1></td>
|
||||
<td className='badge'>
|
||||
{(isApproved || isScheduled) && (
|
||||
{proposal && (proposal.isApproved || proposal.isScheduled) && (
|
||||
<Badge
|
||||
color='green'
|
||||
icon={isScheduled ? 'clock' : 'check'}
|
||||
icon={proposal.isScheduled ? 'clock' : 'check'}
|
||||
/>
|
||||
)}
|
||||
</td>
|
||||
<td className='start together'>{name.toUtf8()}</td>
|
||||
<td className='address'><AddressSmall value={proposer} /></td>
|
||||
<td className='balance'><FormatBalance value={balance} /></td>
|
||||
<td className='start together'>{proposal?.proposal.name.toUtf8()}</td>
|
||||
<td className='address'>{proposal && <AddressSmall value={proposal.proposal.proposer} />}</td>
|
||||
<td className='balance'>{proposal && <FormatBalance value={proposal.proposal.balance} />}</td>
|
||||
<td className='start hash together'>{initialHex}</td>
|
||||
<td className='start hash together'>{validationHex}</td>
|
||||
<td className='address all'>{validators.map((validatorId) => (
|
||||
<td className='address all'>{proposal?.proposal.validators.map((validatorId) => (
|
||||
<AddressMini
|
||||
key={validatorId.toString()}
|
||||
value={validatorId}
|
||||
/>
|
||||
))}</td>
|
||||
<td className='button'>
|
||||
{!isApproved && (
|
||||
{proposal && !proposal.isApproved && (
|
||||
<TxButton
|
||||
accountId={sudoKey}
|
||||
extrinsic={approveTx}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ProposalExt } from './types';
|
||||
import type { Proposals as UseProposals } from './useProposals';
|
||||
|
||||
import React, { useRef } from 'react';
|
||||
|
||||
@ -11,7 +11,7 @@ import { useTranslation } from '../translate';
|
||||
import Proposal from './Proposal';
|
||||
|
||||
interface Props {
|
||||
proposals?: ProposalExt[];
|
||||
proposals?: UseProposals;
|
||||
}
|
||||
|
||||
function Proposals ({ proposals }: Props): React.ReactElement<Props> {
|
||||
@ -33,10 +33,12 @@ function Proposals ({ proposals }: Props): React.ReactElement<Props> {
|
||||
empty={proposals && t<string>('There are no pending proposals')}
|
||||
header={headerRef.current}
|
||||
>
|
||||
{proposals?.map((proposal): React.ReactNode => (
|
||||
{proposals?.proposalIds.map((id): React.ReactNode => (
|
||||
<Proposal
|
||||
key={proposal.id.toString()}
|
||||
proposal={proposal}
|
||||
approvedIds={proposals.approvedIds}
|
||||
id={id}
|
||||
key={id.toString()}
|
||||
scheduled={proposals.scheduled}
|
||||
/>
|
||||
))}
|
||||
</Table>
|
||||
|
||||
@ -28,7 +28,7 @@ function Overview (): React.ReactElement<Props> {
|
||||
<>
|
||||
<Summary
|
||||
parachainCount={paraIds?.length}
|
||||
proposalCount={proposals?.length}
|
||||
proposalCount={proposals?.proposalIds.length}
|
||||
upcomingCount={upcomingIds?.length}
|
||||
/>
|
||||
<Actions />
|
||||
|
||||
@ -1,30 +1,42 @@
|
||||
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Option } from '@polkadot/types';
|
||||
import type { EventRecord, ParachainProposal, ParaId } from '@polkadot/types/interfaces';
|
||||
import type { Codec } from '@polkadot/types/types';
|
||||
// import type { Option } from '@polkadot/types';
|
||||
// import type { Codec } from '@polkadot/types/types';
|
||||
|
||||
import type { EventRecord, ParaId, SessionIndex } from '@polkadot/types/interfaces';
|
||||
import type { ProposalExt } from './types';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { useApi, useCall, useIsMountedRef } from '@polkadot/react-hooks';
|
||||
|
||||
function createExt (approvedIds: ParaId[], proposals: [{ args: [ParaId] }, Option<ParachainProposal>][], scheduledProposals: [Codec, ParaId[]][]): ProposalExt[] {
|
||||
return proposals
|
||||
.filter(([, opt]) => opt.isSome)
|
||||
.map(([{ args: [id] }, optProposal]) => ({
|
||||
id,
|
||||
isApproved: approvedIds.some((a) => a.eq(id)),
|
||||
isScheduled: scheduledProposals.some(([, ids]) => ids.some((s) => s.eq(id))),
|
||||
proposal: optProposal.unwrap()
|
||||
}));
|
||||
export interface ScheduledProposals {
|
||||
scheduledIds: ParaId[];
|
||||
sessionIndex: SessionIndex;
|
||||
}
|
||||
|
||||
export default function useProposals (): ProposalExt[] | undefined {
|
||||
export interface Proposals {
|
||||
approvedIds: ParaId[];
|
||||
proposalIds: ParaId[];
|
||||
scheduled: ScheduledProposals[];
|
||||
}
|
||||
|
||||
function createResult (approvedIds: ParaId[], proposalKeys: { args: [ParaId] }[], scheduledProposals: [{ args: [SessionIndex] }, ParaId[]][]): Proposals {
|
||||
return {
|
||||
approvedIds,
|
||||
proposalIds: proposalKeys.map(({ args: [id] }) => id),
|
||||
scheduled: scheduledProposals.map(([{ args: [sessionIndex] }, scheduledIds]) => ({
|
||||
scheduledIds,
|
||||
sessionIndex
|
||||
}))
|
||||
};
|
||||
}
|
||||
|
||||
export default function useProposals (): Proposals | undefined {
|
||||
const { api } = useApi();
|
||||
const mountedRef = useIsMountedRef();
|
||||
const [state, setState] = useState<ProposalExt[] | undefined>();
|
||||
const [state, setState] = useState<Proposals | undefined>();
|
||||
const [trigger, setTrigger] = useState(Date.now());
|
||||
const approvedIds = useCall<ParaId[]>(api.query.proposeParachain?.approvedProposals);
|
||||
const events = useCall<EventRecord[]>(api.query.system.events);
|
||||
@ -43,15 +55,33 @@ export default function useProposals (): ProposalExt[] | undefined {
|
||||
approvedIds && trigger &&
|
||||
Promise
|
||||
.all([
|
||||
api.query.proposeParachain.proposals.entries(),
|
||||
api.query.proposeParachain.proposals.keys(),
|
||||
api.query.proposeParachain.scheduledProposals.entries()
|
||||
])
|
||||
.then(([proposals, scheduledProposals]) =>
|
||||
mountedRef.current &&
|
||||
setState(createExt(approvedIds, proposals as any, scheduledProposals as any))
|
||||
setState(createResult(approvedIds, proposals as any, scheduledProposals as any))
|
||||
)
|
||||
.catch(console.error);
|
||||
}, [api, approvedIds, mountedRef, trigger]);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function useProposal (id: ParaId, approvedIds: ParaId[], scheduled: ScheduledProposals[]): ProposalExt | undefined {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// export function useProposal (id: ParaId, approvedIds: ParaId[], scheduled: Scheduled[]): ProposalExt | undefined {
|
||||
// const { api } = useApi();
|
||||
// const proposal = useCall<Option<ParachainProposal>>(api.query.proposeParachain.proposals, [id]);
|
||||
|
||||
// return useMemo(
|
||||
// (): ProposalExt | undefined => {
|
||||
// proposal && proposal?.isSome
|
||||
// ?
|
||||
// ),
|
||||
// [approvedIds, id, proposal, scheduled]
|
||||
// );
|
||||
// }
|
||||
|
||||
@ -91,7 +91,7 @@ function Propose ({ className, onClose }: Props): React.ReactElement<Props> {
|
||||
size='large'
|
||||
>
|
||||
<Modal.Content>
|
||||
<Modal.Columns>b
|
||||
<Modal.Columns>
|
||||
<Modal.Column>
|
||||
<InputAddress
|
||||
label={t<string>('propose from')}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user