ghost-portal/packages/page-staking/src/Bags/useBagsList.ts
Jaco c339e7166a
Rebag all dislocated (#7289)
* Perform per-loaded bag rebag calculations

* Allow global rebag
2022-04-02 11:27:12 +03:00

61 lines
1.8 KiB
TypeScript

// Copyright 2017-2022 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Option } from '@polkadot/types';
import type { PalletBagsListListBag } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import type { BagInfo } from './types';
import { useEffect, useState } from 'react';
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
import { BN_ZERO } from '@polkadot/util';
const MULTI_OPTS = {
transform: ([[ids], opts]: [[BN[]], Option<PalletBagsListListBag>[]]): BagInfo[] => {
const sorted = ids
.map((id, index): [BN, Option<PalletBagsListListBag>] => [id, opts[index]])
.filter(([, o]) => o.isSome)
.sort(([a], [b]) => b.cmp(a))
.map(([bagUpper, o], index): BagInfo => ({
bagLower: BN_ZERO,
bagUpper,
index,
info: o.unwrap(),
key: bagUpper.toString()
}));
return sorted.map((entry, index) =>
(index === (sorted.length - 1))
? entry
// We could probably use a .add(BN_ONE) here
: { ...entry, bagLower: sorted[index + 1].bagUpper }
);
},
withParamsTransform: true
};
function merge (prev: BagInfo[] | undefined, curr: BagInfo[]): BagInfo[] {
return !prev || curr.length !== prev.length
? curr
: curr.map((q, i) =>
JSON.stringify(q) === JSON.stringify(prev[i])
? prev[i]
: q
);
}
function useBagsListImpl (ids?: BN[]): BagInfo[] | undefined {
const { api } = useApi();
const [result, setResult] = useState<BagInfo[] | undefined>();
const query = useCall(ids && ids.length !== 0 && api.query.bagsList.listBags.multi, [ids], MULTI_OPTS);
useEffect((): void => {
query && setResult((prev) => merge(prev, query));
}, [query]);
return result;
}
export default createNamedHook('useBagsList', useBagsListImpl);