ghost-portal/packages/page-staking/src/index.tsx
Jaco Greeff 2542c017bf
Incremental staking rendering (#4503)
* Incremental staking rendering

* Apply suggestions from code review
2021-01-26 18:30:36 +01:00

221 lines
5.9 KiB
TypeScript

// Copyright 2017-2021 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { DeriveStakingOverview } from '@polkadot/api-derive/types';
import type { AppProps as Props, ThemeProps } from '@polkadot/react-components/types';
import type { ElectionStatus } from '@polkadot/types/interfaces';
import React, { useCallback, useMemo, useState } from 'react';
import { Route, Switch } from 'react-router';
import { useLocation } from 'react-router-dom';
import styled from 'styled-components';
import { HelpOverlay } from '@polkadot/react-components';
import Tabs from '@polkadot/react-components/Tabs';
import { useAccounts, useApi, useAvailableSlashes, useCall, useFavorites, useOwnStashInfos } from '@polkadot/react-hooks';
import basicMd from './md/basic.md';
import Summary from './Overview/Summary';
import Actions from './Actions';
import { STORE_FAVS_BASE } from './constants';
import Overview from './Overview';
import Payouts from './Payouts';
import Query from './Query';
import Slashes from './Slashes';
import Targets from './Targets';
import { useTranslation } from './translate';
import useSortedTargets from './useSortedTargets';
const HIDDEN_ACC = ['actions', 'payout'];
const transformElection = {
transform: (status: ElectionStatus) => status.isOpen
};
function StakingApp ({ basePath, className = '' }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const { hasAccounts } = useAccounts();
const { pathname } = useLocation();
const [withLedger, setWithLedger] = useState(false);
const [favorites, toggleFavorite] = useFavorites(STORE_FAVS_BASE);
const stakingOverview = useCall<DeriveStakingOverview>(api.derive.staking.overview);
const isInElection = useCall<boolean>(api.query.staking?.eraElectionStatus, undefined, transformElection);
const ownStashes = useOwnStashInfos();
const slashes = useAvailableSlashes();
const targets = useSortedTargets(favorites, withLedger);
const hasQueries = useMemo(
() => hasAccounts && !!(api.query.imOnline?.authoredBlocks) && !!(api.query.staking.activeEra),
[api, hasAccounts]
);
const ownValidators = useMemo(
() => (ownStashes || []).filter(({ isStashValidating }) => isStashValidating),
[ownStashes]
);
const toggleLedger = useCallback(
() => setWithLedger(true),
[]
);
const items = useMemo(() => [
{
isRoot: true,
name: 'overview',
text: t<string>('Staking overview')
},
{
name: 'actions',
text: t<string>('Account actions')
},
api.query.staking.activeEra && {
name: 'payout',
text: t<string>('Payouts')
},
{
alias: 'returns',
name: 'targets',
text: t<string>('Targets')
},
{
name: 'waiting',
text: t<string>('Waiting')
},
{
count: slashes.reduce((count, [, unapplied]) => count + unapplied.length, 0),
name: 'slashes',
text: t<string>('Slashes')
},
{
hasParams: true,
name: 'query',
text: t<string>('Validator stats')
}
].filter((q): q is { name: string; text: string } => !!q), [api, slashes, t]);
return (
<main className={`staking--App ${className}`}>
<HelpOverlay md={basicMd as string} />
<header>
<Tabs
basePath={basePath}
hidden={
hasAccounts
? undefined
: HIDDEN_ACC
}
items={items}
/>
</header>
<Summary
isVisible={pathname === basePath}
stakingOverview={stakingOverview}
targets={targets}
/>
<Switch>
<Route path={`${basePath}/payout`}>
<Payouts
isInElection={isInElection}
ownValidators={ownValidators}
/>
</Route>
<Route path={[`${basePath}/query/:value`, `${basePath}/query`]}>
<Query />
</Route>
<Route path={`${basePath}/slashes`}>
<Slashes
ownStashes={ownStashes}
slashes={slashes}
/>
</Route>
<Route path={`${basePath}/targets`}>
<Targets
isInElection={isInElection}
ownStashes={ownStashes}
stakingOverview={stakingOverview}
targets={targets}
toggleFavorite={toggleFavorite}
toggleLedger={toggleLedger}
/>
</Route>
<Route path={`${basePath}/waiting`}>
<Overview
favorites={favorites}
hasQueries={hasQueries}
isIntentions
stakingOverview={stakingOverview}
targets={targets}
toggleFavorite={toggleFavorite}
toggleLedger={toggleLedger}
/>
</Route>
</Switch>
<Actions
className={pathname === `${basePath}/actions` ? '' : 'staking--hidden'}
isInElection={isInElection}
ownStashes={ownStashes}
targets={targets}
/>
<Overview
className={basePath === pathname ? '' : 'staking--hidden'}
favorites={favorites}
hasQueries={hasQueries}
stakingOverview={stakingOverview}
targets={targets}
toggleFavorite={toggleFavorite}
/>
</main>
);
}
export default React.memo(styled(StakingApp)(({ theme }: ThemeProps) => `
.staking--hidden {
display: none;
}
.staking--Chart {
margin-top: 1.5rem;
h1 {
margin-bottom: 0.5rem;
}
.ui--Spinner {
margin: 2.5rem auto;
}
}
.staking--optionsBar {
margin: 0.5rem 0 1rem;
text-align: center;
white-space: normal;
.staking--buttonToggle {
display: inline-block;
margin-right: 1rem;
margin-top: 0.5rem;
}
}
.ui--Expander.stakeOver {
.ui--Expander-summary {
color: ${theme.colorError};
${theme.theme === 'dark'
? `
font-weight: bold;
.ui--FormatBalance-value {
> .ui--FormatBalance-postfix {
opacity: 1;
}
}
`
: ''};
}
}
`));