From d3bd098f9a4adab10f1f08d59066ba79d3e0c679 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 29 Oct 2019 15:00:03 +0100 Subject: [PATCH] Place authors on a context (#1808) * Pleace authors on a context * Cleanup empty effect * Cleanup context, no new object --- packages/app-staking/src/Overview/Address.tsx | 23 ++++---- .../app-staking/src/Overview/CurrentList.tsx | 6 +-- packages/app-staking/src/Overview/index.tsx | 33 +++--------- packages/apps/src/index.tsx | 13 +++-- packages/react-query/src/BlockAuthors.tsx | 54 +++++++++++++++++++ packages/react-query/src/index.ts | 1 + 6 files changed, 85 insertions(+), 45 deletions(-) create mode 100644 packages/react-query/src/BlockAuthors.tsx diff --git a/packages/app-staking/src/Overview/Address.tsx b/packages/app-staking/src/Overview/Address.tsx index a02e2f7e22..7d83c27906 100644 --- a/packages/app-staking/src/Overview/Address.tsx +++ b/packages/app-staking/src/Overview/Address.tsx @@ -21,11 +21,11 @@ import translate from '../translate'; interface Props extends I18nProps { address: string; + authorsMap: Record; className?: string; defaultName: string; filter: ValidatorFilter; - lastAuthor: string; - lastBlock: string; + lastAuthor?: string; recentlyOnline?: DerivedHeartbeats; stakingInfo?: DerivedStaking; withNominations?: boolean; @@ -49,7 +49,7 @@ interface OnlineState { const WITH_VALIDATOR_PREFS = { validatorPayment: true }; -function Address ({ address, className, defaultName, filter, lastAuthor, lastBlock, recentlyOnline, stakingInfo, t, withNominations }: Props): React.ReactElement | null { +function Address ({ authorsMap, className, defaultName, filter, lastAuthor, recentlyOnline, stakingInfo, t, withNominations }: Props): React.ReactElement | null { const { isSubstrateV2 } = useContext(ApiContext); const [isNominatorMe, seIsNominatorMe] = useState(false); const [{ hasOfflineWarnings, onlineStatus }, setOnlineStatus] = useState({ @@ -63,7 +63,6 @@ function Address ({ address, className, defaultName, filter, lastAuthor, lastBlo stashActive: null, stashTotal: null }); - const [myLastBlock, setMyLastBlock] = useState(''); const [isAuthor, setIsAuthor] = useState(false); useEffect((): void => { @@ -111,11 +110,8 @@ function Address ({ address, className, defaultName, filter, lastAuthor, lastBlo }, [recentlyOnline, stakingInfo]); useEffect((): void => { - const isAuthor = !!lastAuthor && !!lastBlock && stashId === lastAuthor; - - setIsAuthor(isAuthor); - isAuthor && setMyLastBlock(lastBlock); - }, [lastAuthor, lastBlock, address, controllerId, stashId]); + setIsAuthor(lastAuthor === stashId); + }, [lastAuthor, stashId]); if (!stashId || (filter === 'hasNominators' && !hasNominators) || (filter === 'noNominators' && hasNominators) || @@ -124,16 +120,19 @@ function Address ({ address, className, defaultName, filter, lastAuthor, lastBlo (filter === 'iNominated' && !isNominatorMe)) { return null; } + + const lastBlockNumber = authorsMap[stashId]; + return ( - {myLastBlock && ( -
#{myLastBlock}
+ {lastBlockNumber && ( +
#{lastBlockNumber}
)} {controllerId && (
- +
)} diff --git a/packages/app-staking/src/Overview/CurrentList.tsx b/packages/app-staking/src/Overview/CurrentList.tsx index 344fc99dcd..078c534965 100644 --- a/packages/app-staking/src/Overview/CurrentList.tsx +++ b/packages/app-staking/src/Overview/CurrentList.tsx @@ -13,25 +13,25 @@ import translate from '../translate'; import Address from './Address'; interface Props extends I18nProps { + authorsMap: Record; currentValidators: string[]; lastAuthor?: string; - lastBlock: string; next: string[]; recentlyOnline?: DerivedHeartbeats; } -function CurrentList ({ currentValidators, lastAuthor, lastBlock, next, recentlyOnline, t }: Props): React.ReactElement { +function CurrentList ({ authorsMap, currentValidators, lastAuthor, next, recentlyOnline, t }: Props): React.ReactElement { const [filter, setFilter] = useState('all'); const _renderColumn = (addresses: string[], defaultName: string, withNominations: boolean): React.ReactNode => { return addresses.map((address): React.ReactNode => (
diff --git a/packages/app-staking/src/Overview/index.tsx b/packages/app-staking/src/Overview/index.tsx index bd5335a87e..aaca140125 100644 --- a/packages/app-staking/src/Overview/index.tsx +++ b/packages/app-staking/src/Overview/index.tsx @@ -7,30 +7,20 @@ import { BareProps } from '@polkadot/react-components/types'; import { ComponentProps } from '../types'; import React, { useContext, useEffect, useState } from 'react'; -import { HeaderExtended } from '@polkadot/api-derive'; import { ApiContext } from '@polkadot/react-api'; -import { withCalls, withMulti } from '@polkadot/react-api/with'; -import { formatNumber } from '@polkadot/util'; +import { BlockAuthorsContext } from '@polkadot/react-query'; import CurrentList from './CurrentList'; import Summary from './Summary'; interface Props extends BareProps, ComponentProps { - chain_subscribeNewHeads?: HeaderExtended; } -function Overview (props: Props): React.ReactElement { +export default function Overview (props: Props): React.ReactElement { const { isSubstrateV2 } = useContext(ApiContext); - const [{ lastAuthor, lastBlock }, setLast] = useState({ lastAuthor: '', lastBlock: '' }); + const { byAuthor, lastBlockAuthor, lastBlockNumber } = useContext(BlockAuthorsContext); const [nextSorted, setNextSorted] = useState([]); - const { chain_subscribeNewHeads, allControllers, allStashes, currentValidators, recentlyOnline } = props; - - useEffect((): void => { - chain_subscribeNewHeads && setLast({ - lastAuthor: (chain_subscribeNewHeads.author || '').toString(), - lastBlock: formatNumber(chain_subscribeNewHeads.number) - }); - }, [chain_subscribeNewHeads]); + const { allControllers, allStashes, currentValidators, recentlyOnline } = props; useEffect((): void => { setNextSorted( @@ -47,24 +37,17 @@ function Overview (props: Props): React.ReactElement { ); } - -export default withMulti( - Overview, - withCalls( - 'derive.chain.subscribeNewHeads' - ) -); diff --git a/packages/apps/src/index.tsx b/packages/apps/src/index.tsx index 680f683f16..37f04c837c 100644 --- a/packages/apps/src/index.tsx +++ b/packages/apps/src/index.tsx @@ -18,6 +18,7 @@ import { getTypeRegistry } from '@polkadot/types'; import { Api } from '@polkadot/react-api'; import { QueueConsumer } from '@polkadot/react-components/Status/Context'; import Queue from '@polkadot/react-components/Status/Queue'; +import { BlockAuthors } from '@polkadot/react-query'; import Apps from './Apps'; @@ -70,11 +71,13 @@ ReactDOM.render( queueSetTxStatus={queueSetTxStatus} url={wsEndpoint} > - - - - - + + + + + + + )} diff --git a/packages/react-query/src/BlockAuthors.tsx b/packages/react-query/src/BlockAuthors.tsx new file mode 100644 index 0000000000..c7e1d90711 --- /dev/null +++ b/packages/react-query/src/BlockAuthors.tsx @@ -0,0 +1,54 @@ +// Copyright 2017-2019 @polkadot/react-query authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import React, { useContext, useEffect, useState } from 'react'; +import { HeaderExtended } from '@polkadot/api-derive'; +import { ApiContext } from '@polkadot/react-api'; +import { formatNumber } from '@polkadot/util'; + +interface Authors { + byAuthor: Record; + lastBlockAuthor?: string; + lastBlockNumber?: string; + lastHeader?: HeaderExtended; +} + +interface Props { + children: React.ReactNode; +} + +const byAuthor: Record = {}; +const BlockAuthorsContext: React.Context = React.createContext({ byAuthor }); + +function BlockAuthors ({ children }: Props): React.ReactElement { + const { api } = useContext(ApiContext); + const [state, setState] = useState({ byAuthor }); + + useEffect((): void => { + // TODO We should really unsub - but since this should just be used once, + // atm I'm rather typing this than doing it the way it is supposed to be + api.isReady.then((): void => { + api.derive.chain.subscribeNewHeads((lastHeader): void => { + if (lastHeader && lastHeader.number) { + const lastBlockAuthor = lastHeader.author ? lastHeader.author.toString() : undefined; + const lastBlockNumber = formatNumber(lastHeader.number.unwrap()); + + if (lastBlockAuthor) { + byAuthor[lastBlockAuthor] = lastBlockNumber; + } + + setState({ byAuthor, lastBlockAuthor, lastBlockNumber, lastHeader }); + } + }); + }); + }, []); + + return ( + + {children} + + ); +} + +export { BlockAuthorsContext, BlockAuthors }; diff --git a/packages/react-query/src/index.ts b/packages/react-query/src/index.ts index 5a4e07f703..22f708237a 100644 --- a/packages/react-query/src/index.ts +++ b/packages/react-query/src/index.ts @@ -6,6 +6,7 @@ export { default as AccountIndex } from './AccountIndex'; export { default as AccountName } from './AccountName'; export { default as Available } from './Available'; export { default as Balance } from './Balance'; +export { BlockAuthors, BlockAuthorsContext } from './BlockAuthors'; export { default as Bonded } from './Bonded'; export { default as BestFinalized } from './BestFinalized'; export { default as BestNumber } from './BestNumber';