Display all events on expanded block view (#591)

This commit is contained in:
Jaco Greeff 2019-01-12 17:54:15 +01:00 committed by GitHub
parent a6b808466d
commit 42409e6b72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 187 additions and 75 deletions

View File

@ -7,22 +7,24 @@ import { ApiProps } from '@polkadot/ui-api/types';
import React from 'react';
import { AddressMini, Call } from '@polkadot/ui-app/index';
import { Extrinsic, Method, SignedBlock } from '@polkadot/types';
import { EventRecord, Extrinsic, Method, SignedBlock } from '@polkadot/types';
import { withCall, withMulti } from '@polkadot/ui-api/index';
import { numberFormat } from '@polkadot/ui-reactive/util/index';
import BlockHeader from '../BlockHeader';
import translate from '../translate';
import Events from './Events';
import Logs from './Logs';
type Props = ApiProps & I18nProps & {
query_system_events?: Array<EventRecord>,
rpc_chain_getBlock?: SignedBlock,
value: string
};
class BlockByHash extends React.PureComponent<Props> {
render () {
const { rpc_chain_getBlock } = this.props;
const { query_system_events, rpc_chain_getBlock } = this.props;
if (!rpc_chain_getBlock || !rpc_chain_getBlock.block) {
return null;
@ -38,6 +40,10 @@ class BlockByHash extends React.PureComponent<Props> {
/>
</header>,
this.renderExtrinsics(),
<Events
key='events'
value={query_system_events}
/>,
<Logs
key='logs'
value={header.digest.logs}
@ -67,7 +73,7 @@ class BlockByHash extends React.PureComponent<Props> {
}
// FIXME This is _very_ similar to what we have in democracy/Item
private renderExtrinsic = (extrinsic: Extrinsic, index?: number) => {
private renderExtrinsic = (extrinsic: Extrinsic, index: number) => {
const { value } = this.props;
const { meta, method, section } = Method.findFunction(extrinsic.callIndex);
@ -79,7 +85,7 @@ class BlockByHash extends React.PureComponent<Props> {
<article className='explorer--Container'>
<div className='header'>
<h3>
{section}.{method}
#{numberFormat(index)}:&nbsp;{section}.{method}
</h3>
<div className='description'>{
meta && meta.documentation && meta.documentation.length
@ -117,5 +123,6 @@ class BlockByHash extends React.PureComponent<Props> {
export default withMulti(
BlockByHash,
translate,
withCall('rpc.chain.getBlock', { paramProp: 'value' })
withCall('rpc.chain.getBlock', { paramProp: 'value' }),
withCall('query.system.events', { atProp: 'value' })
);

View File

@ -0,0 +1,41 @@
// Copyright 2017-2019 @polkadot/app-explorer authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { I18nProps } from '@polkadot/ui-app/types';
import React from 'react';
import { EventRecord } from '@polkadot/types';
import EventsDisplay from '../Events';
import translate from '../translate';
type Props = I18nProps & {
value?: Array<EventRecord>
};
class Events extends React.PureComponent<Props> {
render () {
const { t, value } = this.props;
if (!value || !value.length) {
return null;
}
return (
<section>
<h1>{t('block.events', {
defaultValue: 'events'
})}</h1>
<div className='explorer--BlockByHash-flexable'>
<EventsDisplay
eventClassName='explorer--BlockByHash-block'
value={value}
/>
</div>
</section>
);
}
}
export default translate(Events);

View File

@ -2,80 +2,56 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// Copyright 2017-2019 @polkadot/app-explorer authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { I18nProps } from '@polkadot/ui-app/types';
import React from 'react';
import { Event, EventRecord } from '@polkadot/types';
import { EventRecord } from '@polkadot/types';
import { Event as EventDisplay } from '@polkadot/ui-app/index';
import { withCall, withMulti } from '@polkadot/ui-api/index';
import { stringToU8a } from '@polkadot/util';
import { xxhashAsHex } from '@polkadot/util-crypto';
import { numberFormat } from '@polkadot/ui-reactive/util/index';
import { MAX_ITEMS } from './BlockHeaders';
import translate from './translate';
type Props = I18nProps & {
query_system_events?: Array<EventRecord>
value?: Array<EventRecord>,
emptyLabel?: React.ReactNode,
eventClassName?: string,
withoutIndex?: boolean
};
type State = {
prevEventHash: string;
recentEvents: Array<Event>;
};
class EventsDisplay extends React.PureComponent<Props, State> {
constructor (props: Props) {
super(props);
this.state = {
prevEventHash: '',
recentEvents: []
};
}
static getDerivedStateFromProps ({ query_system_events = [] }: Props, prevState: State): State | null {
const prevEventHash = xxhashAsHex(stringToU8a(JSON.stringify(query_system_events)));
if (prevEventHash === prevState.prevEventHash) {
return null;
}
const recentEvents = query_system_events
.filter(({ event }) => event.section !== 'system')
.map(({ event }) => event)
.concat(prevState.recentEvents)
.filter((_, index) => index < MAX_ITEMS);
return {
prevEventHash,
recentEvents
};
}
class Events extends React.PureComponent<Props> {
render () {
const { t } = this.props;
const { recentEvents } = this.state;
const { emptyLabel, eventClassName, value, t } = this.props;
if (recentEvents.length === 0) {
return (
<div>{t('events.none', {
defaultValue: 'no non-system events available'
})}</div>
);
if (!value || value.length === 0) {
return emptyLabel || t('events.none', {
defaultValue: 'no events available'
});
}
return (
<div>
{recentEvents.map(this.renderEvent)}
</div>
);
return value
.filter(({ event }) => event) // event.section !== 'system')
.map((event, index) => {
const rendered = this.renderEvent(event, index);
return eventClassName
? (
<div
className={eventClassName}
key={index}
>
{rendered}
</div>
)
: rendered;
});
}
private renderEvent = (event: Event, index: number) => {
private renderEvent = ({ event, phase }: EventRecord, index: number) => {
const { withoutIndex } = this.props;
const extIndex = !withoutIndex && phase.type === 'ApplyExtrinsic'
? phase.asApplyExtrinsic
: -1;
return (
<article
className='explorer--Container'
@ -83,7 +59,7 @@ class EventsDisplay extends React.PureComponent<Props, State> {
>
<div className='header'>
<h3>
{event.section}.{event.method}
{extIndex === -1 ? '' : `#${numberFormat(extIndex)}: `}{event.section}.{event.method}
</h3>
<div className='description'>
{
@ -99,8 +75,4 @@ class EventsDisplay extends React.PureComponent<Props, State> {
}
}
export default withMulti(
EventsDisplay,
translate,
withCall('query.system.events')
);
export default translate(Events);

View File

@ -0,0 +1,76 @@
// Copyright 2017-2019 @polkadot/app-explorer authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { I18nProps } from '@polkadot/ui-app/types';
import React from 'react';
import { EventRecord } from '@polkadot/types';
import { withCall, withMulti } from '@polkadot/ui-api/index';
import { stringToU8a } from '@polkadot/util';
import { xxhashAsHex } from '@polkadot/util-crypto';
import { MAX_ITEMS } from './BlockHeaders';
import Events from './Events';
import translate from './translate';
type Props = I18nProps & {
query_system_events?: Array<EventRecord>
};
type State = {
prevEventHash: string;
recentEvents: Array<EventRecord>;
};
class EventsRecent extends React.PureComponent<Props, State> {
constructor (props: Props) {
super(props);
this.state = {
prevEventHash: '',
recentEvents: []
};
}
static getDerivedStateFromProps ({ query_system_events = [] }: Props, prevState: State): State | null {
const prevEventHash = xxhashAsHex(stringToU8a(JSON.stringify(query_system_events)));
if (prevEventHash === prevState.prevEventHash) {
return null;
}
const recentEvents = query_system_events
.filter(({ event }) => event.section !== 'system')
.concat(prevState.recentEvents)
.filter((_, index) => index < MAX_ITEMS);
return {
prevEventHash,
recentEvents
};
}
render () {
const { t } = this.props;
const { recentEvents } = this.state;
return (
<div>
<Events
emptyLabel={t('events.system-none', {
defaultValue: 'no recent non-system events available'
})}
value={recentEvents}
withoutIndex
/>
</div>
);
}
}
export default withMulti(
EventsRecent,
translate,
withCall('query.system.events')
);

View File

@ -7,7 +7,7 @@ import { I18nProps } from '@polkadot/ui-app/types';
import React from 'react';
import BlockHeaders from './BlockHeaders';
import Events from './Events';
import EventsRecent from './EventsRecent';
import Query from './Query';
import Summary from './Summary';
import translate from './translate';
@ -35,7 +35,7 @@ class Main extends React.PureComponent<Props> {
<h1>{t('main.recentEvents', {
defaultValue: 'recent events'
})}</h1>
<Events />
<EventsRecent />
</div>
</div>
];

View File

@ -15,6 +15,11 @@ import { intervalTimer, isEqual, triggerChange } from '../util/index';
import echoTransform from '../transform/echo';
import withApi from './api';
interface Method {
(...params: Array<any>): Promise<any>;
at: (hash: Uint8Array | string, ...params: Array<any>) => Promise<any>;
}
type State<T> = RxProps<T> & {
destroy?: () => void,
propName: string,
@ -29,7 +34,7 @@ const NOOP = () => {
// FIXME proper types for attributes
export default function withCall<T, P> (endpoint: string, { rxChange, params = [], paramProp = 'params', propName, transform = echoTransform }: Options<T> = {}): HOC<T> {
export default function withCall<T, P> (endpoint: string, { at, atProp, rxChange, params = [], paramProp = 'params', propName, transform = echoTransform }: Options<T> = {}): HOC<T> {
return (Inner: React.ComponentType<ApiProps>): React.ComponentType<any> => {
class WithPromise extends React.Component<Props, State<T>> {
state: State<T>;
@ -83,6 +88,10 @@ export default function withCall<T, P> (endpoint: string, { rxChange, params = [
private getParams (props: any): Array<any> {
const paramValue = props[paramProp];
if (atProp) {
at = props[atProp];
}
return isUndefined(paramValue)
? params
: params.concat(
@ -92,7 +101,7 @@ export default function withCall<T, P> (endpoint: string, { rxChange, params = [
);
}
private getApiMethod (newParams: Array<any>): [(...params: Array<any>) => Promise<any>, Array<any>, boolean] {
private getApiMethod (newParams: Array<any>): [Method, Array<any>, boolean] {
const { apiPromise } = this.props;
if (endpoint === 'subscribe') {
@ -109,6 +118,7 @@ export default function withCall<T, P> (endpoint: string, { rxChange, params = [
assert(area.length && section.length && method.length && others.length === 0, `Invalid API format, expected <area>.<section>.<method>, found ${endpoint}`);
assert(['rpc', 'query', 'derive'].includes(area), `Unknown api.${area}, expected rpc, query or derive`);
assert(!at || area === 'query', 'Only able todo an at query on the api.query interface');
if (area === 'derive') {
const apiSection = (derive as any)[section];
@ -129,7 +139,7 @@ export default function withCall<T, P> (endpoint: string, { rxChange, params = [
return [
apiSection[method],
newParams,
area === 'query' || method.startsWith('subscribe')
(area === 'query' && (!at && !atProp)) || method.startsWith('subscribe')
];
}
@ -141,6 +151,8 @@ export default function withCall<T, P> (endpoint: string, { rxChange, params = [
try {
const [apiMethod, params, isSubscription] = this.getApiMethod(newParams);
assert(at || !atProp, 'Unable to perform query on non-existent at hash');
this.unsubscribe();
if (isSubscription) {
@ -150,7 +162,9 @@ export default function withCall<T, P> (endpoint: string, { rxChange, params = [
this.setState({ destroy });
} else {
const value: T = await apiMethod(...params);
const value: T = at
? await apiMethod.at(at, ...params)
: await apiMethod(...params);
this.triggerUpdate(this.props, value);
}

View File

@ -14,6 +14,8 @@ export type DefaultProps<T> = {
};
export type Options<T> = {
at?: Uint8Array | string,
atProp?: string,
rxChange?: OnChangeCb<T>,
params?: Array<any>,
paramProp?: string,