Files
pezkuwi-sdk-ui/packages/page-staking2/src/Validators/useHeartbeat.ts
T
pezkuwichain d949863789 Initial commit: Pezkuwi SDK UI
Comprehensive web interface for interacting with Pezkuwi blockchain.

Features:
- Blockchain explorer
- Wallet management
- Staking interface
- Governance participation
- Developer tools

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-19 13:55:36 +03:00

57 lines
1.8 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Option, u32 } from '@pezkuwi/types';
import type { Codec } from '@pezkuwi/types/types';
import type { SessionInfo, Validator } from '../types.js';
import type { UseHeartbeat } from './types.js';
import { useMemo } from 'react';
import { createNamedHook, useApi, useCall } from '@pezkuwi/react-hooks';
import { isBoolean, isNumber } from '@pezkuwi/util';
import { useCacheMap } from '../useCache.js';
const EMPTY: UseHeartbeat = {};
const OPT_BLOCKS = {
transform: (authoredBlocks: u32): number =>
authoredBlocks.toNumber()
};
const OPT_BEATS = {
// Option<WrapperOpaque<PalletImOnlineBoundedOpaqueNetworkState>>
transform: (receivedHeartbeats: Option<Codec>): boolean =>
receivedHeartbeats.isSome
};
function useHeartbeatImpl ({ stashId, stashIndex }: Validator, { currentSession }: SessionInfo): UseHeartbeat {
const { api } = useApi();
const params = useMemo(
() => stashIndex === -1
? undefined
: currentSession && ({
authoredBlocks: [currentSession, stashId],
receivedHeartbeats: [currentSession, stashIndex]
}),
[currentSession, stashId, stashIndex]
);
const authoredBlocks = useCall(params && api.query.imOnline.authoredBlocks, params?.authoredBlocks, OPT_BLOCKS);
const receivedHeartbeats = useCall(params && api.query.imOnline.receivedHeartbeats, params?.receivedHeartbeats, OPT_BEATS);
const result = useMemo(
() => isNumber(authoredBlocks) && isBoolean(receivedHeartbeats) && ({
authoredBlocks,
isOnline: !!(authoredBlocks || receivedHeartbeats)
}),
[authoredBlocks, receivedHeartbeats]
);
return useCacheMap('useHeartbeat', stashId, result) || EMPTY;
}
export default createNamedHook('useHeartbeat', useHeartbeatImpl);