Display explorer retrieval errors (instead of spinning) (#3652)

This commit is contained in:
Jaco Greeff 2020-09-10 12:52:38 +02:00 committed by GitHub
parent c0d53fa95f
commit e91ed4cf41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 41 deletions

View File

@ -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<Props> {
function BlockByHash ({ className = '', error, value }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const events = useCall<KeyedEvent[]>(api.query.system.events.at, [value], transformEvents);
const getBlock = useCall<SignedBlock>(api.rpc.chain.getBlock, [value], { isSingle: true });
const getHeader = useCall<HeaderExtended>(api.derive.chain.getHeader, [value]);
const mountedRef = useIsMountedRef();
const [[events, getBlock, getHeader], setState] = useState<[KeyedEvent[]?, SignedBlock?, HeaderExtended?]>([]);
const [myError, setError] = useState<Error | null | undefined>(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<Prop
}
isFixed
>
{getBlock && !getBlock.isEmpty && getHeader && !getHeader.isEmpty && (
<tr>
<td className='address'>
{getHeader.author && (
<AddressSmall value={getHeader.author} />
)}
</td>
<td className='hash overflow'>{getHeader.hash.toHex()}</td>
<td className='hash overflow'>{
hasParent
? <Link to={`/explorer/query/${parentHash || ''}`}>{parentHash}</Link>
: parentHash
}</td>
<td className='hash overflow'>{getHeader.extrinsicsRoot.toHex()}</td>
<td className='hash overflow'>{getHeader.stateRoot.toHex()}</td>
<td>
<LinkExternal
data={value}
type='block'
/>
</td>
</tr>
)}
{myError
? <tr><td colSpan={6}>{t('Unable to retrieve the specified block details. {{error}}', { replace: { error: myError.message } })}</td></tr>
: getBlock && !getBlock.isEmpty && getHeader && !getHeader.isEmpty && (
<tr>
<td className='address'>
{getHeader.author && (
<AddressSmall value={getHeader.author} />
)}
</td>
<td className='hash overflow'>{getHeader.hash.toHex()}</td>
<td className='hash overflow'>{
hasParent
? <Link to={`/explorer/query/${parentHash || ''}`}>{parentHash}</Link>
: parentHash
}</td>
<td className='hash overflow'>{getHeader.extrinsicsRoot.toHex()}</td>
<td className='hash overflow'>{getHeader.stateRoot.toHex()}</td>
<td>
<LinkExternal
data={value}
type='block'
/>
</td>
</tr>
)
}
</Table>
{getBlock && getHeader && (
<>

View File

@ -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<Props> | null {
const { api } = useApi();
const getBlockHash = useCall<Hash>(api.rpc.chain.getBlockHash, [value]);
const [getBlockHash, setState] = useState<Hash | null>(null);
const mountedRef = useIsMountedRef();
const [error, setError] = useState<Error | null>(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 <Spinner />;
}
return (
<BlockByHash value={getBlockHash.toHex()} />
<BlockByHash
error={error}
value={getBlockHash ? getBlockHash.toHex() : null}
/>
);
}

View File

@ -73,7 +73,7 @@ function subscribe <T> (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++;