ghost-portal/packages/page-referenda/src/useCounter.ts
2024-01-08 15:06:30 +02:00

33 lines
825 B
TypeScript

// Copyright 2017-2024 @polkadot/app-referenda authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { PalletReferenda } from './types.js';
import { useMemo } from 'react';
import { createNamedHook } from '@polkadot/react-hooks';
import useReferenda from './useReferenda.js';
export function useCounterNamed (palletReferenda: PalletReferenda): number {
const [grouped] = useReferenda(palletReferenda);
return useMemo(
() => grouped
? grouped.reduce((total, { referenda }) =>
total + (
referenda
? referenda.filter(({ info }) => info.isOngoing).length
: 0
), 0)
: 0,
[grouped]
);
}
function useCounterImpl (): number {
return useCounterNamed('referenda');
}
export default createNamedHook('useCounter', useCounterImpl);