mirror of
https://github.com/pezkuwichain/pezkuwi-sdk-ui.git
synced 2026-06-18 02:31:08 +00:00
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>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Copyright 2017-2026 @pezkuwi/react-hooks authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ApiPromise } from '@pezkuwi/api';
|
||||
import type { BN } from '@pezkuwi/util';
|
||||
import type { Time } from '@pezkuwi/util/types';
|
||||
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { BN_MAX_INTEGER, BN_ONE, bnMin, bnToBn, extractTime } from '@pezkuwi/util';
|
||||
|
||||
import { createNamedHook } from './createNamedHook.js';
|
||||
import { useTranslation } from './translate.js';
|
||||
import { useBlockInterval } from './useBlockInterval.js';
|
||||
|
||||
type Result = [blockInterval: number, timeStr: string, time: Time];
|
||||
|
||||
export function calcBlockTime (blockTime: BN, blocks: BN, t: (key: string, options?: { replace: Record<string, unknown> }) => string): Result {
|
||||
// in the case of excessively large locks, limit to the max JS integer value
|
||||
const value = bnMin(BN_MAX_INTEGER, blockTime.mul(blocks)).toNumber();
|
||||
|
||||
// time calculations are using the absolute value (< 0 detection only on strings)
|
||||
const time = extractTime(Math.abs(value));
|
||||
const { days, hours, minutes, seconds } = time;
|
||||
|
||||
return [
|
||||
blockTime.toNumber(),
|
||||
`${value < 0 ? '+' : ''}${[
|
||||
days
|
||||
? (days > 1)
|
||||
? t('{{days}} days', { replace: { days } })
|
||||
: t('1 day')
|
||||
: null,
|
||||
hours
|
||||
? (hours > 1)
|
||||
? t('{{hours}} hrs', { replace: { hours } })
|
||||
: t('1 hr')
|
||||
: null,
|
||||
minutes
|
||||
? (minutes > 1)
|
||||
? t('{{minutes}} mins', { replace: { minutes } })
|
||||
: t('1 min')
|
||||
: null,
|
||||
seconds
|
||||
? (seconds > 1)
|
||||
? t('{{seconds}} s', { replace: { seconds } })
|
||||
: t('1 s')
|
||||
: null
|
||||
]
|
||||
.filter((s): s is string => !!s)
|
||||
.slice(0, 2)
|
||||
.join(' ')}`,
|
||||
time
|
||||
];
|
||||
}
|
||||
|
||||
function useBlockTimeImpl (blocks: number | BN = BN_ONE, apiOverride?: ApiPromise | null): Result {
|
||||
const { t } = useTranslation();
|
||||
const blockTime = useBlockInterval(apiOverride);
|
||||
|
||||
return useMemo(
|
||||
() => calcBlockTime(blockTime, bnToBn(blocks), t),
|
||||
[blockTime, blocks, t]
|
||||
);
|
||||
}
|
||||
|
||||
export const useBlockTime = createNamedHook('useBlockTime', useBlockTimeImpl);
|
||||
Reference in New Issue
Block a user