// Copyright 2017-2022 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { DeriveStakerPoints } from '@polkadot/api-derive/types'; import type { ChartInfo, LineDataEntry, Props } from './types'; import React, { useMemo, useRef } from 'react'; import { Chart, Spinner } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; const COLORS_POINTS = [undefined, '#acacac']; function extractPoints (points: DeriveStakerPoints[] = []): ChartInfo { const labels: string[] = []; const avgSet: LineDataEntry = []; const idxSet: LineDataEntry = []; const [total, avgCount] = points.reduce(([total, avgCount], { points }) => { if (points.gtn(0)) { total += points.toNumber(); avgCount++; } return [total, avgCount]; }, [0, 0]); points.forEach(({ era, points }): void => { const avg = avgCount > 0 ? Math.ceil(total * 100 / avgCount) / 100 : 0; labels.push(era.toHuman()); avgSet.push(avg); idxSet.push(points); }); return { chart: [idxSet, avgSet], labels }; } function ChartPoints ({ validatorId }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const params = useMemo(() => [validatorId, false], [validatorId]); const stakerPoints = useCall(api.derive.staking.stakerPoints, params); const { chart, labels } = useMemo( () => extractPoints(stakerPoints), [stakerPoints] ); const legendsRef = useRef([ t('points'), t('average') ]); return (

{t('era points')}

{labels.length ? ( ) : }
); } export default React.memo(ChartPoints);