From 50ec4215291dd63c7983ea3f19f3422e8578cf20 Mon Sep 17 00:00:00 2001 From: MiZiet <55240109+MiZiet@users.noreply.github.com> Date: Thu, 10 Dec 2020 16:47:14 +0100 Subject: [PATCH] Display bounties counter in menu (#4189) * Display bounties counter in menu * Remove account check from bounties useCounter --- packages/apps-routing/src/bounties.ts | 5 +++-- packages/page-bounties/src/index.tsx | 2 ++ packages/page-bounties/src/useCounter.ts | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 packages/page-bounties/src/useCounter.ts diff --git a/packages/apps-routing/src/bounties.ts b/packages/apps-routing/src/bounties.ts index e3486a9695..04039640ff 100644 --- a/packages/apps-routing/src/bounties.ts +++ b/packages/apps-routing/src/bounties.ts @@ -4,7 +4,7 @@ import type { TFunction } from 'i18next'; import type { Route } from './types'; -import Component from '@polkadot/app-bounties'; +import Component, { useCounter } from '@polkadot/app-bounties'; export default function create (t: TFunction): Route { return { @@ -17,6 +17,7 @@ export default function create (t: TFunction): Route { group: 'governance', icon: 'coins', name: 'bounties', - text: t('nav.bounties', 'Bounties', { ns: 'apps-routing' }) + text: t('nav.bounties', 'Bounties', { ns: 'apps-routing' }), + useCounter }; } diff --git a/packages/page-bounties/src/index.tsx b/packages/page-bounties/src/index.tsx index 0e08208e91..108d0fcead 100644 --- a/packages/page-bounties/src/index.tsx +++ b/packages/page-bounties/src/index.tsx @@ -8,6 +8,8 @@ import { Tabs } from '@polkadot/react-components'; import Bounties from './Bounties'; import { useTranslation } from './translate'; +export { default as useCounter } from './useCounter'; + interface Props { basePath: string; } diff --git a/packages/page-bounties/src/useCounter.ts b/packages/page-bounties/src/useCounter.ts new file mode 100644 index 0000000000..c345f1d2e6 --- /dev/null +++ b/packages/page-bounties/src/useCounter.ts @@ -0,0 +1,20 @@ +// Copyright 2017-2020 @polkadot/app-bounties authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { BountyIndex } from '@polkadot/types/interfaces'; + +import { useEffect, useState } from 'react'; + +import { useApi, useCall } from '@polkadot/react-hooks'; + +export default function useCounter (): number { + const { api, isApiReady } = useApi(); + const bounties = useCall(isApiReady && api.query.treasury?.bountyCount); + const [counter, setCounter] = useState(0); + + useEffect((): void => { + setCounter(bounties?.toNumber() || 0); + }, [bounties]); + + return counter; +}