Consolidate rebag operations (#7319)
* Consolidate rebag operations * Don't pass stashNodes
This commit is contained in:
parent
32afeda22f
commit
a5372949a3
@ -25,14 +25,11 @@ interface Props {
|
||||
stashNodes?: StashNode[];
|
||||
}
|
||||
|
||||
function getRebags (bonded: ListNode[], stashNodes: StashNode[], bagUpper: BN, bagLower: BN): string[] {
|
||||
function getRebags (bonded: ListNode[], bagUpper: BN, bagLower: BN): string[] {
|
||||
return bonded
|
||||
.filter(({ bonded, stashId }) =>
|
||||
(
|
||||
bonded.gt(bagUpper) ||
|
||||
bonded.lt(bagLower)
|
||||
) &&
|
||||
stashNodes.every((n) => n.stashId !== stashId)
|
||||
.filter(({ bonded }) =>
|
||||
bonded.gt(bagUpper) ||
|
||||
bonded.lt(bagLower)
|
||||
)
|
||||
.map(({ stashId }) => stashId);
|
||||
}
|
||||
@ -58,8 +55,8 @@ function Bag ({ bagLower, bagUpper, info, stashNodes }: Props): React.ReactEleme
|
||||
}, [bonded, isCompleted, stashNodes]);
|
||||
|
||||
useEffect((): void => {
|
||||
!isLoading && bonded && stashNodes && setRebags(
|
||||
getRebags(bonded, stashNodes, bagUpper, bagLower)
|
||||
!isLoading && bonded && setRebags(
|
||||
getRebags(bonded, bagUpper, bagLower)
|
||||
);
|
||||
}, [bagLower, bagUpper, bonded, isLoading, stashNodes]);
|
||||
|
||||
|
||||
@ -2,10 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { BagMap } from './types';
|
||||
|
||||
import React, { useMemo, useState } from 'react';
|
||||
|
||||
import { Button, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { Button, InputAddress, InputAddressMulti, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi, useToggle, useTxBatch } from '@polkadot/react-hooks';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
@ -17,21 +18,32 @@ interface Props {
|
||||
stashIds: string[];
|
||||
}
|
||||
|
||||
function getAvailableIds (map: BagMap, bagUpper: BN): string[] {
|
||||
return Object
|
||||
.values(map[bagUpper.toString()] || {})
|
||||
.map(({ stashId }) => stashId);
|
||||
}
|
||||
|
||||
function Rebag ({ bagUpper, stashIds }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const [accountId, setAccountId] = useState<string | null>(null);
|
||||
const [isVisible, toggleVisible] = useToggle();
|
||||
const map = useBagsNodes(stashIds);
|
||||
const availableIds = useMemo(
|
||||
() => map
|
||||
? getAvailableIds(map, bagUpper)
|
||||
: [],
|
||||
[bagUpper, map]
|
||||
);
|
||||
const [selectedIds, setSelectedIds] = useState<string[]>([]);
|
||||
const changes = useMemo(
|
||||
() => map && Object
|
||||
.values(map[bagUpper.toString()] || {})
|
||||
.map(({ stashId }) => api.tx.bagsList.rebag(stashId)),
|
||||
[api, bagUpper, map]
|
||||
() => selectedIds.map((stashId) => api.tx.bagsList.rebag(stashId)),
|
||||
[api, selectedIds]
|
||||
);
|
||||
const tx = useTxBatch(changes);
|
||||
|
||||
if (!changes || !changes.length) {
|
||||
if (!availableIds.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -39,7 +51,7 @@ function Rebag ({ bagUpper, stashIds }: Props): React.ReactElement<Props> | null
|
||||
<>
|
||||
<Button
|
||||
icon='refresh'
|
||||
label={t<string>('Rebag {{count}}', { replace: { count: changes.length } })}
|
||||
label={t<string>('Rebag {{count}}', { replace: { count: availableIds.length } })}
|
||||
onClick={toggleVisible}
|
||||
/>
|
||||
{isVisible && (
|
||||
@ -57,13 +69,23 @@ function Rebag ({ bagUpper, stashIds }: Props): React.ReactElement<Props> | null
|
||||
type='account'
|
||||
/>
|
||||
</Modal.Columns>
|
||||
<Modal.Columns hint={t<string>('The accounts that will be rebagged as a result of this operation.')}>
|
||||
<InputAddressMulti
|
||||
available={availableIds}
|
||||
availableLabel={t<string>('unselected')}
|
||||
defaultValue={availableIds}
|
||||
maxCount={Number.MAX_SAFE_INTEGER}
|
||||
onChange={setSelectedIds}
|
||||
valueLabel={t<string>('to rebag')}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
<TxButton
|
||||
accountId={accountId}
|
||||
extrinsic={tx}
|
||||
icon='refresh'
|
||||
isDisabled={!tx}
|
||||
isDisabled={!tx || !selectedIds.length}
|
||||
label={t<string>('Rebag')}
|
||||
onStart={toggleVisible}
|
||||
/>
|
||||
|
||||
@ -23,7 +23,6 @@ interface Props {
|
||||
|
||||
interface Entry {
|
||||
canJump: boolean;
|
||||
canRebag: boolean;
|
||||
jumpCount: number;
|
||||
stashInfo: ListNode | null;
|
||||
}
|
||||
@ -34,10 +33,6 @@ function findEntry (upper: BN, bagLower: BN, stashId: string, list: ListNode[] =
|
||||
|
||||
return {
|
||||
canJump: !!other,
|
||||
canRebag: !!stashInfo && (
|
||||
stashInfo.bonded.gt(upper) ||
|
||||
stashInfo.bonded.lt(bagLower)
|
||||
),
|
||||
jumpCount: stashInfo && other
|
||||
? (stashInfo.index - other.index)
|
||||
: 0,
|
||||
@ -48,7 +43,7 @@ function findEntry (upper: BN, bagLower: BN, stashId: string, list: ListNode[] =
|
||||
function Stash ({ bagLower, bagUpper, className, isLoading, list, stashId }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { canJump, canRebag, jumpCount, stashInfo } = useMemo(
|
||||
const { canJump, jumpCount, stashInfo } = useMemo(
|
||||
() => findEntry(bagUpper, bagLower, stashId, list),
|
||||
[bagLower, bagUpper, list, stashId]
|
||||
);
|
||||
@ -60,29 +55,18 @@ function Stash ({ bagLower, bagUpper, className, isLoading, list, stashId }: Pro
|
||||
withBonded
|
||||
/>
|
||||
{stashInfo && (
|
||||
canRebag
|
||||
canJump
|
||||
? (
|
||||
<TxButton
|
||||
accountId={stashInfo.stashId}
|
||||
icon='refresh'
|
||||
icon='caret-up'
|
||||
isDisabled={isLoading}
|
||||
label={t<string>('Rebag')}
|
||||
params={[stashInfo.stashId]}
|
||||
tx={api.tx.bagsList.rebag}
|
||||
label={t<string>('Move up {{jumpCount}}', { replace: { jumpCount } })}
|
||||
params={[stashInfo.jump]}
|
||||
tx={api.tx.bagsList.putInFrontOf}
|
||||
/>
|
||||
)
|
||||
: canJump
|
||||
? (
|
||||
<TxButton
|
||||
accountId={stashInfo.stashId}
|
||||
icon='caret-up'
|
||||
isDisabled={isLoading}
|
||||
label={t<string>('Move up {{jumpCount}}', { replace: { jumpCount } })}
|
||||
params={[stashInfo.jump]}
|
||||
tx={api.tx.bagsList.putInFrontOf}
|
||||
/>
|
||||
)
|
||||
: null
|
||||
: null
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
@ -23,6 +23,18 @@ interface Props {
|
||||
valueLabel: React.ReactNode;
|
||||
}
|
||||
|
||||
function exclude (prev: string[], address: string): string[] {
|
||||
return prev.includes(address)
|
||||
? prev.filter((a) => a !== address)
|
||||
: prev;
|
||||
}
|
||||
|
||||
function include (prev: string[], address: string, maxCount: number): string[] {
|
||||
return !prev.includes(address) && (prev.length < maxCount)
|
||||
? prev.concat(address)
|
||||
: prev;
|
||||
}
|
||||
|
||||
function InputAddressMulti ({ available, availableLabel, className = '', defaultValue, maxCount, onChange, valueLabel }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const [_filter, setFilter] = useState<string>('');
|
||||
@ -39,24 +51,12 @@ function InputAddressMulti ({ available, availableLabel, className = '', default
|
||||
}, [onChange, selected]);
|
||||
|
||||
const _onSelect = useCallback(
|
||||
(address: string): void =>
|
||||
setSelected(
|
||||
(selected: string[]) =>
|
||||
!selected.includes(address) && (selected.length < maxCount)
|
||||
? selected.concat(address)
|
||||
: selected
|
||||
),
|
||||
(address: string) => setSelected((prev) => include(prev, address, maxCount)),
|
||||
[maxCount]
|
||||
);
|
||||
|
||||
const _onDeselect = useCallback(
|
||||
(address: string): void =>
|
||||
setSelected(
|
||||
(selected: string[]) =>
|
||||
selected.includes(address)
|
||||
? selected.filter((a) => a !== address)
|
||||
: selected
|
||||
),
|
||||
(address: string) => setSelected((prev) => exclude(prev, address)),
|
||||
[]
|
||||
);
|
||||
|
||||
@ -89,17 +89,15 @@ function InputAddressMulti ({ available, availableLabel, className = '', default
|
||||
<div className='ui--InputAddressMulti-items'>
|
||||
{isLoading
|
||||
? <Spinner />
|
||||
: (
|
||||
available.map((address) => (
|
||||
<Available
|
||||
address={address}
|
||||
filter={filter}
|
||||
isHidden={selected?.includes(address)}
|
||||
key={address}
|
||||
onSelect={_onSelect}
|
||||
/>
|
||||
))
|
||||
)
|
||||
: available.map((address) => (
|
||||
<Available
|
||||
address={address}
|
||||
filter={filter}
|
||||
isHidden={selected?.includes(address)}
|
||||
key={address}
|
||||
onSelect={_onSelect}
|
||||
/>
|
||||
))
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user