Files
pezkuwi-apps/packages/react-hooks/src/useAvailableSlashes.ts
T
Satoshi Qazi Muhammed 45d479d2ec Move to @pezkuwi/dev 0.86.4 (eslint 10 toolchain)
- dev ^0.86.4 at the root and in packages/apps; directives renamed by
  pezkuwi-dev-migrate-eslint10.
- eslint --fix with the 0.86.4 config; one fix broke syntax
  (typescript-eslint prefer-optional-chain turned
  `(isStarted && onStart) && onStart()` into `(isStarted && onStart?.();`
  in TxButton) and is corrected by hand.
- The remaining findings fixed rather than disabled: optional chains
  (each suggestion reviewed), `cause` on rethrown errors, `if` instead of
  `a && promise` in effects, parallel instead of sequential awaits in
  Promise.all, Error rejections, no `delete` on arrays (the storage query
  cache is a Map; an upload's file list no longer keeps a hole), no
  `[object Object]` stringification (TFunction's second argument is text
  or options), dead code removed (referenda's never-shown expand button,
  react-query's unused CoreDescriptor).
2026-10-10 02:06:12 +04:00

68 lines
2.5 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiPromise } from '@pezkuwi/api';
import type { DeriveSessionIndexes } from '@pezkuwi/api-derive/types';
import type { Option } from '@pezkuwi/types';
import type { EraIndex } from '@pezkuwi/types/interfaces';
import type { PezpalletStakingUnappliedSlash } from '@pezkuwi/types/lookup';
import { useEffect, useMemo, useState } from 'react';
import { BN, BN_HUNDRED, BN_ONE, BN_ZERO } from '@pezkuwi/util';
import { createNamedHook } from './createNamedHook.js';
import { useApi } from './useApi.js';
import { useCall } from './useCall.js';
import { useIsMountedRef } from './useIsMountedRef.js';
type Unsub = () => void;
function useAvailableSlashesImpl (apiOverride?: ApiPromise): [BN, PezpalletStakingUnappliedSlash[]][] {
const { api: connectedApi } = useApi();
const api = useMemo(() => apiOverride ?? connectedApi, [apiOverride, connectedApi]);
const indexes = useCall<DeriveSessionIndexes>(api.derive.session?.indexes);
const earliestSlash = useCall<Option<EraIndex>>(api.query.staking?.earliestUnappliedSlash);
const mountedRef = useIsMountedRef();
const [slashes, setSlashes] = useState<[BN, PezpalletStakingUnappliedSlash[]][]>([]);
useEffect((): Unsub => {
let unsub: Unsub | undefined;
const [from, offset] = api.query.staking?.earliestUnappliedSlash
? [earliestSlash?.unwrapOr(null), BN_ZERO]
// future depth (one more than activeEra for delay)
: [indexes?.activeEra, BN_ONE.add(api.consts.staking?.slashDeferDuration || BN_HUNDRED)];
if (mountedRef.current && indexes && from) {
const range: BN[] = [];
const end = indexes.activeEra.add(offset);
let start = new BN(from);
while (start.lte(end)) {
range.push(start);
start = start.add(BN_ONE);
}
if (range.length) {
(async (): Promise<void> => {
unsub = await api.query.staking.unappliedSlashes.multi(range, (values): void => {
mountedRef.current && setSlashes(
values
.map((value, index): [BN, PezpalletStakingUnappliedSlash[]] => [from.addn(index), value])
.filter(([, slashes]) => slashes.length)
);
});
})().catch(console.error);
}
}
return (): void => {
unsub?.();
};
}, [api, earliestSlash, indexes, mountedRef]);
return slashes;
}
export const useAvailableSlashes = createNamedHook('useAvailableSlashes', useAvailableSlashesImpl);