ghost-portal/packages/page-staking2/src/Validators/usePoints.ts
Jaco 4cbec89b76
Clear cache on unmount (#8907)
* Clear cache on unmount

* Cleanup value return
2023-01-31 21:33:04 +02:00

40 lines
1.2 KiB
TypeScript

// Copyright 2017-2023 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { PalletStakingEraRewardPoints } from '@polkadot/types/lookup';
import type { SessionInfo } from '../types';
import type { UsePoints } from './types';
import { useMemo } from 'react';
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
import { BN_ZERO } from '@polkadot/util';
import { useCacheValue } from '../useCache';
const OPT_POINTS = {
transform: ({ individual }: PalletStakingEraRewardPoints): UsePoints =>
[...individual.entries()]
.filter(([, points]) => points.gt(BN_ZERO))
.reduce((result: UsePoints, [stashId, points]): UsePoints => {
result[stashId.toString()] = points.toNumber();
return result;
}, {})
};
function usePointsImpl ({ activeEra }: SessionInfo): UsePoints | undefined {
const { api } = useApi();
const queryParams = useMemo(
() => activeEra && [activeEra],
[activeEra]
);
const points = useCall(queryParams && api.query.staking.erasRewardPoints, queryParams, OPT_POINTS);
return useCacheValue('usePoints', points);
}
export default createNamedHook('usePoints', usePointsImpl);