diff --git a/packages/page-staking/src/Bags/Bag.tsx b/packages/page-staking/src/Bags/Bag.tsx index 40b185f375..31a6d940c1 100644 --- a/packages/page-staking/src/Bags/Bag.tsx +++ b/packages/page-staking/src/Bags/Bag.tsx @@ -5,9 +5,10 @@ import type { u64 } from '@polkadot/types'; import type { PalletBagsListListBag } from '@polkadot/types/lookup'; import type { StashNode } from './types'; -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { AddressMini, Spinner } from '@polkadot/react-components'; +import { useIncrement } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; import Stash from './Stash'; @@ -20,22 +21,27 @@ interface Props { stashNodes?: StashNode[]; } -const NO_NODES: StashNode[] = []; - -export default function Bag ({ id, info, stashNodes = NO_NODES }: Props): React.ReactElement { +export default function Bag ({ id, info, stashNodes }: Props): React.ReactElement { const [isLoading, setLoading] = useState(true); - const { isCompleted, list } = useBagEntries(stashNodes.length ? info.head.unwrapOr(null) : null); - const bonded = useBonded(isCompleted && list); + const [trigger, doRefresh] = useIncrement(1); + const headId = useMemo( + () => stashNodes && stashNodes.length + ? info.head.unwrapOr(null) + : null, + [info, stashNodes] + ); + const list = useBagEntries(headId, trigger); + const bonded = useBonded(list); useEffect((): void => { setLoading( - stashNodes.length - ? isCompleted + stashNodes && stashNodes.length + ? list ? !bonded : true : false ); - }, [bonded, isCompleted, list, stashNodes]); + }, [bonded, list, stashNodes]); return ( @@ -45,6 +51,8 @@ export default function Bag ({ id, info, stashNodes = NO_NODES }: Props): React. {stashNodes?.map(({ stashId }) => ( void; + isLoading: boolean; list?: ListNode[]; stashId: string; } @@ -24,7 +26,7 @@ function findEntry (stashId: string, list: ListNode[] = []): [ListNode | null, b return [entry, !!other, entry && other ? entry.index - other.index : 0]; } -function Stash ({ className, list, stashId }: Props): React.ReactElement { +function Stash ({ className, doRefresh, isLoading, list, stashId }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const [stashInfo, canJump, jumpCount] = useMemo( @@ -32,8 +34,6 @@ function Stash ({ className, list, stashId }: Props): React.ReactElement [list, stashId] ); - // FIXME The button should disappear when we have moved to the correct position - return (
('Move up {{jumpCount}}', { replace: { jumpCount } })} + onSuccess={doRefresh} params={[stashInfo?.jump]} tx={api.tx.bagsList.putInFrontOf} /> diff --git a/packages/page-staking/src/Bags/useBagEntries.tsx b/packages/page-staking/src/Bags/useBagEntries.tsx index 6986c29cad..0be4aa72dd 100644 --- a/packages/page-staking/src/Bags/useBagEntries.tsx +++ b/packages/page-staking/src/Bags/useBagEntries.tsx @@ -15,19 +15,20 @@ interface Result { } const EMPTY: [AccountId32 | null, Result] = [null, { isCompleted: false, list: [] }]; +const EMPTY_LIST: AccountId32[] = []; -function useBagEntriesImpl (headId: AccountId32 | null): Result { +function useBagEntriesImpl (headId: AccountId32 | null, trigger: number): AccountId32[] { const { api } = useApi(); - const [[currId, result], setCurrent] = useState<[AccountId32 | null, Result]>(EMPTY); + const [[currId, { isCompleted, list }], setCurrent] = useState<[AccountId32 | null, Result]>(EMPTY); const node = useCall>(!!currId && api.query.bagsList.listNodes, [currId]); useEffect( () => setCurrent( - headId + headId && trigger ? [headId, { isCompleted: false, list: [headId] }] : [null, { isCompleted: true, list: [] }] ), - [headId] + [headId, trigger] ); useEffect((): void => { @@ -44,7 +45,9 @@ function useBagEntriesImpl (headId: AccountId32 | null): Result { } }, [node]); - return result; + return isCompleted + ? list + : EMPTY_LIST; } export default createNamedHook('useBagEntries', useBagEntriesImpl);