diff --git a/packages/page-explorer/src/BlockInfo/ByHash.tsx b/packages/page-explorer/src/BlockInfo/ByHash.tsx index bf465d6e6e..b1233092d1 100644 --- a/packages/page-explorer/src/BlockInfo/ByHash.tsx +++ b/packages/page-explorer/src/BlockInfo/ByHash.tsx @@ -5,11 +5,11 @@ import { EventRecord, SignedBlock } from '@polkadot/types/interfaces'; import { KeyedEvent } from '@polkadot/react-query/types'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { HeaderExtended } from '@polkadot/api-derive'; import { AddressSmall, Columar, Column, LinkExternal, Table } from '@polkadot/react-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; +import { useApi, useIsMountedRef } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate'; @@ -19,25 +19,43 @@ import Logs from './Logs'; interface Props { className?: string; - value: string; + error?: Error | null; + value?: string | null; } -const transformEvents = { - isSingle: true, - transform: (events: EventRecord[]): KeyedEvent[] => +function transformResult ([events, getBlock, getHeader]: [EventRecord[], SignedBlock, HeaderExtended?]): [KeyedEvent[], SignedBlock, HeaderExtended?] { + return [ events.map((record, index) => ({ indexes: [index], key: `${Date.now()}-${index}-${record.hash.toHex()}`, record - })) -}; + })), + getBlock, + getHeader + ]; +} -function BlockByHash ({ className = '', value }: Props): React.ReactElement { +function BlockByHash ({ className = '', error, value }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const events = useCall(api.query.system.events.at, [value], transformEvents); - const getBlock = useCall(api.rpc.chain.getBlock, [value], { isSingle: true }); - const getHeader = useCall(api.derive.chain.getHeader, [value]); + const mountedRef = useIsMountedRef(); + const [[events, getBlock, getHeader], setState] = useState<[KeyedEvent[]?, SignedBlock?, HeaderExtended?]>([]); + const [myError, setError] = useState(error); + + useEffect((): void => { + value && Promise + .all([ + api.query.system.events.at(value), + api.rpc.chain.getBlock(value), + api.derive.chain.getHeader(value) + ]) + .then((result): void => { + mountedRef.current && setState(transformResult(result)); + }) + .catch((error: Error): void => { + mountedRef.current && setError(error); + }); + }, [api, mountedRef, value]); const blockNumber = getHeader?.number.unwrap(); const parentHash = getHeader?.parentHash.toHex(); @@ -60,29 +78,32 @@ function BlockByHash ({ className = '', value }: Props): React.ReactElement - {getBlock && !getBlock.isEmpty && getHeader && !getHeader.isEmpty && ( - - - {getHeader.author && ( - - )} - - {getHeader.hash.toHex()} - { - hasParent - ? {parentHash} - : parentHash - } - {getHeader.extrinsicsRoot.toHex()} - {getHeader.stateRoot.toHex()} - - - - - )} + {myError + ? {t('Unable to retrieve the specified block details. {{error}}', { replace: { error: myError.message } })} + : getBlock && !getBlock.isEmpty && getHeader && !getHeader.isEmpty && ( + + + {getHeader.author && ( + + )} + + {getHeader.hash.toHex()} + { + hasParent + ? {parentHash} + : parentHash + } + {getHeader.extrinsicsRoot.toHex()} + {getHeader.stateRoot.toHex()} + + + + + ) + } {getBlock && getHeader && ( <> diff --git a/packages/page-explorer/src/BlockInfo/ByNumber.tsx b/packages/page-explorer/src/BlockInfo/ByNumber.tsx index 7169b360f3..cf66e49b71 100644 --- a/packages/page-explorer/src/BlockInfo/ByNumber.tsx +++ b/packages/page-explorer/src/BlockInfo/ByNumber.tsx @@ -4,9 +4,9 @@ import { Hash } from '@polkadot/types/interfaces'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import { Spinner } from '@polkadot/react-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; +import { useApi, useIsMountedRef } from '@polkadot/react-hooks'; import BlockByHash from './ByHash'; @@ -16,14 +16,30 @@ interface Props { function BlockByNumber ({ value }: Props): React.ReactElement | null { const { api } = useApi(); - const getBlockHash = useCall(api.rpc.chain.getBlockHash, [value]); + const [getBlockHash, setState] = useState(null); + const mountedRef = useIsMountedRef(); + const [error, setError] = useState(null); - if (!getBlockHash) { + useEffect((): void => { + api.rpc.chain + .getBlockHash(value) + .then((result): void => { + mountedRef.current && setState(result); + }) + .catch((error: Error): void => { + mountedRef.current && setError(error); + }); + }, [api, mountedRef, value]); + + if (!getBlockHash && !error) { return ; } return ( - + ); } diff --git a/packages/react-hooks/src/useCall.ts b/packages/react-hooks/src/useCall.ts index 866074c65e..69e5862ffa 100644 --- a/packages/react-hooks/src/useCall.ts +++ b/packages/react-hooks/src/useCall.ts @@ -73,7 +73,7 @@ function subscribe (mountedRef: MountedRef, tracker: TrackerRef, fn: TrackFn tracker.current.subscriber = (fn as (...params: unknown[]) => Promise<() => void>)(...params, (value: Codec): void => { // when we don't have an active sub, or single-shot, ignore (we use the isActive flag here - // since .subscriber may not be set on immeditae callback) + // since .subscriber may not be set on immediate callback) if (mountedRef.current && tracker.current.isActive && (!isSingle || !tracker.current.count)) { tracker.current.count++;