Place authors on a context (#1808)
* Pleace authors on a context * Cleanup empty effect * Cleanup context, no new object
This commit is contained in:
parent
8bccc37a0d
commit
d3bd098f9a
@ -21,11 +21,11 @@ import translate from '../translate';
|
||||
|
||||
interface Props extends I18nProps {
|
||||
address: string;
|
||||
authorsMap: Record<string, string>;
|
||||
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<Props> | null {
|
||||
function Address ({ authorsMap, className, defaultName, filter, lastAuthor, recentlyOnline, stakingInfo, t, withNominations }: Props): React.ReactElement<Props> | null {
|
||||
const { isSubstrateV2 } = useContext(ApiContext);
|
||||
const [isNominatorMe, seIsNominatorMe] = useState(false);
|
||||
const [{ hasOfflineWarnings, onlineStatus }, setOnlineStatus] = useState<OnlineState>({
|
||||
@ -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 (
|
||||
<AddressCard
|
||||
buttons={
|
||||
<div className='staking--Address-info'>
|
||||
{myLastBlock && (
|
||||
<div className={`blockNumberV${isSubstrateV2 ? '2' : '1'} ${isAuthor && 'isCurrent'}`}>#{myLastBlock}</div>
|
||||
{lastBlockNumber && (
|
||||
<div className={`blockNumberV${isSubstrateV2 ? '2' : '1'} ${isAuthor && 'isCurrent'}`}>#{lastBlockNumber}</div>
|
||||
)}
|
||||
{controllerId && (
|
||||
<div>
|
||||
<label className={classes('staking--label', isSubstrateV2 && !myLastBlock && 'controllerSpacer')}>{t('controller')}</label>
|
||||
<label className={classes('staking--label', isSubstrateV2 && !lastBlockNumber && 'controllerSpacer')}>{t('controller')}</label>
|
||||
<AddressMini value={controllerId} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
@ -13,25 +13,25 @@ import translate from '../translate';
|
||||
import Address from './Address';
|
||||
|
||||
interface Props extends I18nProps {
|
||||
authorsMap: Record<string, string>;
|
||||
currentValidators: string[];
|
||||
lastAuthor?: string;
|
||||
lastBlock: string;
|
||||
next: string[];
|
||||
recentlyOnline?: DerivedHeartbeats;
|
||||
}
|
||||
|
||||
function CurrentList ({ currentValidators, lastAuthor, lastBlock, next, recentlyOnline, t }: Props): React.ReactElement<Props> {
|
||||
function CurrentList ({ authorsMap, currentValidators, lastAuthor, next, recentlyOnline, t }: Props): React.ReactElement<Props> {
|
||||
const [filter, setFilter] = useState<ValidatorFilter>('all');
|
||||
|
||||
const _renderColumn = (addresses: string[], defaultName: string, withNominations: boolean): React.ReactNode => {
|
||||
return addresses.map((address): React.ReactNode => (
|
||||
<Address
|
||||
address={address}
|
||||
authorsMap={authorsMap}
|
||||
defaultName={defaultName}
|
||||
key={address}
|
||||
filter={filter}
|
||||
lastAuthor={lastAuthor}
|
||||
lastBlock={lastBlock}
|
||||
recentlyOnline={recentlyOnline}
|
||||
withNominations={withNominations}
|
||||
/>
|
||||
|
||||
@ -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<Props> {
|
||||
export default function Overview (props: Props): React.ReactElement<Props> {
|
||||
const { isSubstrateV2 } = useContext(ApiContext);
|
||||
const [{ lastAuthor, lastBlock }, setLast] = useState({ lastAuthor: '', lastBlock: '' });
|
||||
const { byAuthor, lastBlockAuthor, lastBlockNumber } = useContext(BlockAuthorsContext);
|
||||
const [nextSorted, setNextSorted] = useState<string[]>([]);
|
||||
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<Props> {
|
||||
<Summary
|
||||
allControllers={allControllers}
|
||||
currentValidators={currentValidators}
|
||||
lastBlock={lastBlock}
|
||||
lastAuthor={lastAuthor}
|
||||
lastBlock={lastBlockNumber}
|
||||
lastAuthor={lastBlockAuthor}
|
||||
next={nextSorted}
|
||||
/>
|
||||
<CurrentList
|
||||
authorsMap={byAuthor}
|
||||
currentValidators={currentValidators}
|
||||
lastBlock={lastBlock}
|
||||
lastAuthor={lastAuthor}
|
||||
lastAuthor={lastBlockAuthor}
|
||||
next={nextSorted}
|
||||
recentlyOnline={recentlyOnline}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default withMulti(
|
||||
Overview,
|
||||
withCalls<Props>(
|
||||
'derive.chain.subscribeNewHeads'
|
||||
)
|
||||
);
|
||||
|
||||
@ -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}
|
||||
>
|
||||
<HashRouter>
|
||||
<ThemeProvider theme={theme}>
|
||||
<Apps />
|
||||
</ThemeProvider>
|
||||
</HashRouter>
|
||||
<BlockAuthors>
|
||||
<HashRouter>
|
||||
<ThemeProvider theme={theme}>
|
||||
<Apps />
|
||||
</ThemeProvider>
|
||||
</HashRouter>
|
||||
</BlockAuthors>
|
||||
</Api>
|
||||
)}
|
||||
</QueueConsumer>
|
||||
|
||||
54
packages/react-query/src/BlockAuthors.tsx
Normal file
54
packages/react-query/src/BlockAuthors.tsx
Normal file
@ -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<string, string>;
|
||||
lastBlockAuthor?: string;
|
||||
lastBlockNumber?: string;
|
||||
lastHeader?: HeaderExtended;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const byAuthor: Record<string, string> = {};
|
||||
const BlockAuthorsContext: React.Context<Authors> = React.createContext<Authors>({ byAuthor });
|
||||
|
||||
function BlockAuthors ({ children }: Props): React.ReactElement<Props> {
|
||||
const { api } = useContext(ApiContext);
|
||||
const [state, setState] = useState<Authors>({ 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 (
|
||||
<BlockAuthorsContext.Provider value={state}>
|
||||
{children}
|
||||
</BlockAuthorsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export { BlockAuthorsContext, BlockAuthors };
|
||||
@ -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';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user