diff --git a/packages/page-council/src/Motions/Motion.tsx b/packages/page-council/src/Motions/Motion.tsx index 8f448b7c49..92d40322d9 100644 --- a/packages/page-council/src/Motions/Motion.tsx +++ b/packages/page-council/src/Motions/Motion.tsx @@ -2,20 +2,19 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AccountId, BlockNumber, Votes } from '@polkadot/types/interfaces'; +import { AccountId } from '@polkadot/types/interfaces'; import { DeriveCollectiveProposal } from '@polkadot/api-derive/types'; -import React, { useEffect, useState } from 'react'; -import { ApiPromise } from '@polkadot/api'; +import React from 'react'; import ProposalCell from '@polkadot/app-democracy/Overview/ProposalCell'; import { LinkExternal } from '@polkadot/react-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; import { BlockToTime } from '@polkadot/react-query'; import { formatNumber } from '@polkadot/util'; import Close from './Close'; import Voters from './Votes'; import Voting from './Voting'; +import useVotingStatus from './useVotingStatus'; interface Props { className?: string; @@ -25,44 +24,8 @@ interface Props { prime: AccountId | null; } -interface Status { - isCloseable: boolean; - isVotable: boolean; -} - -function getStatus (api: ApiPromise, bestNumber: BlockNumber, votes: Votes): Status { - if (!votes.end) { - return { - isCloseable: false, - isVotable: true - }; - } - - const isEnd = bestNumber.gte(votes.end); - - if (api.tx.council.close.meta.args.length === 2) { - return { - isCloseable: isEnd, - isVotable: !isEnd - }; - } - - return { - isCloseable: isEnd || votes.threshold.eq(votes.ayes.length), - isVotable: !isEnd - }; -} - function Motion ({ className = '', isMember, members, motion: { hash, proposal, votes }, prime }: Props): React.ReactElement | null { - const { api } = useApi(); - const bestNumber = useCall(api.derive.chain.bestNumber, []); - const [{ isCloseable, isVotable }, setStatus] = useState({ isCloseable: false, isVotable: false }); - - useEffect((): void => { - bestNumber && votes && setStatus( - getStatus(api, bestNumber, votes) - ); - }, [api, bestNumber, votes]); + const { isCloseable, isVoteable, remainingBlocks } = useVotingStatus(votes, members.length); if (!votes) { return null; @@ -81,9 +44,9 @@ function Motion ({ className = '', isMember, members, motion: { hash, proposal, {formatNumber(ayes.length)}/{formatNumber(threshold)} - {bestNumber && end && ( + {remainingBlocks && end && ( <> - + #{formatNumber(end)} )} @@ -91,7 +54,7 @@ function Motion ({ className = '', isMember, members, motion: { hash, proposal, - {isVotable && ( + {isVoteable && ( (api.derive.chain.bestNumber, []); + const [state, setState] = useState({ isCloseable: false, isVoteable: false, remainingBlocks: null }); + + useEffect((): void => { + bestNumber && votes && setState( + getStatus(api, bestNumber, votes, numMembers) + ); + }, [api, bestNumber, numMembers, votes]); + + return state; +} diff --git a/packages/page-tech-comm/src/Proposals/Proposal.tsx b/packages/page-tech-comm/src/Proposals/Proposal.tsx index 26449044f6..04f2e3afe6 100644 --- a/packages/page-tech-comm/src/Proposals/Proposal.tsx +++ b/packages/page-tech-comm/src/Proposals/Proposal.tsx @@ -22,14 +22,16 @@ interface Props { function Proposal ({ className = '', imageHash, prime }: Props): React.ReactElement | null { const { api } = useApi(); const optProposal = useCall>(api.query.technicalCommittee.proposalOf, [imageHash]); - const votes = useCall>(api.query.technicalCommittee.voting, [imageHash]); + const votes = useCall(api.query.technicalCommittee.voting, [imageHash], { + transform: (optVotes: Option) => optVotes.unwrapOr(null) + }); - if (!optProposal?.isSome || !votes?.isSome) { + if (!optProposal?.isSome || !votes) { return null; } const proposal = optProposal.unwrap(); - const { ayes, index, nays, threshold } = votes.unwrap(); + const { ayes, index, nays, threshold } = votes; return (