// Copyright 2017-2023 @polkadot/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { DeriveStakerPoints } from '@polkadot/api-derive/types'; import type { LineData, Props } from './types'; import React, { useEffect, useMemo, useRef, useState } from 'react'; import { useApi, useCall } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; import Chart from './Chart'; const COLORS_POINTS = [undefined, '#acacac']; function extractPoints (labels: string[], points: DeriveStakerPoints[]): LineData { const avgSet = new Array(labels.length); const idxSet = new Array(labels.length); 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; const index = labels.indexOf(era.toHuman()); if (index !== -1) { avgSet[index] = avg; idxSet[index] = points.toNumber(); } }); return [idxSet, avgSet]; } function ChartPoints ({ labels, 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 [values, setValues] = useState([]); useEffect( () => setValues([]), [validatorId] ); useEffect( () => stakerPoints && setValues(extractPoints(labels, stakerPoints)), [labels, stakerPoints] ); const legendsRef = useRef([ t('points'), t('average') ]); return ( ('era points')} values={values} /> ); } export default React.memo(ChartPoints);