diff --git a/packages/app-explorer/src/BlockByHash/BlockByHash.tsx b/packages/app-explorer/src/BlockByHash/BlockByHash.tsx index 521f362637..c7c68e70da 100644 --- a/packages/app-explorer/src/BlockByHash/BlockByHash.tsx +++ b/packages/app-explorer/src/BlockByHash/BlockByHash.tsx @@ -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, rpc_chain_getBlock?: SignedBlock, value: string }; class BlockByHash extends React.PureComponent { 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 { /> , this.renderExtrinsics(), + , { } // 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 {

- {section}.{method} + #{numberFormat(index)}: {section}.{method}

{ meta && meta.documentation && meta.documentation.length @@ -117,5 +123,6 @@ class BlockByHash extends React.PureComponent { export default withMulti( BlockByHash, translate, - withCall('rpc.chain.getBlock', { paramProp: 'value' }) + withCall('rpc.chain.getBlock', { paramProp: 'value' }), + withCall('query.system.events', { atProp: 'value' }) ); diff --git a/packages/app-explorer/src/BlockByHash/Events.tsx b/packages/app-explorer/src/BlockByHash/Events.tsx new file mode 100644 index 0000000000..c2a70bb486 --- /dev/null +++ b/packages/app-explorer/src/BlockByHash/Events.tsx @@ -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 +}; + +class Events extends React.PureComponent { + render () { + const { t, value } = this.props; + + if (!value || !value.length) { + return null; + } + + return ( +
+

{t('block.events', { + defaultValue: 'events' + })}

+
+ +
+
+ ); + } +} + +export default translate(Events); diff --git a/packages/app-explorer/src/Events.tsx b/packages/app-explorer/src/Events.tsx index 3637e4da39..eb326081e7 100644 --- a/packages/app-explorer/src/Events.tsx +++ b/packages/app-explorer/src/Events.tsx @@ -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 + value?: Array, + emptyLabel?: React.ReactNode, + eventClassName?: string, + withoutIndex?: boolean }; -type State = { - prevEventHash: string; - recentEvents: Array; -}; - -class EventsDisplay extends React.PureComponent { - 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 { render () { - const { t } = this.props; - const { recentEvents } = this.state; + const { emptyLabel, eventClassName, value, t } = this.props; - if (recentEvents.length === 0) { - return ( -
{t('events.none', { - defaultValue: 'no non-system events available' - })}
- ); + if (!value || value.length === 0) { + return emptyLabel || t('events.none', { + defaultValue: 'no events available' + }); } - return ( -
- {recentEvents.map(this.renderEvent)} -
- ); + return value + .filter(({ event }) => event) // event.section !== 'system') + .map((event, index) => { + const rendered = this.renderEvent(event, index); + + return eventClassName + ? ( +
+ {rendered} +
+ ) + : 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 (
{ >

- {event.section}.{event.method} + {extIndex === -1 ? '' : `#${numberFormat(extIndex)}: `}{event.section}.{event.method}

{ @@ -99,8 +75,4 @@ class EventsDisplay extends React.PureComponent { } } -export default withMulti( - EventsDisplay, - translate, - withCall('query.system.events') -); +export default translate(Events); diff --git a/packages/app-explorer/src/EventsRecent.tsx b/packages/app-explorer/src/EventsRecent.tsx new file mode 100644 index 0000000000..fb3e4fb28a --- /dev/null +++ b/packages/app-explorer/src/EventsRecent.tsx @@ -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 +}; + +type State = { + prevEventHash: string; + recentEvents: Array; +}; + +class EventsRecent extends React.PureComponent { + 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 ( +
+ +
+ ); + } +} + +export default withMulti( + EventsRecent, + translate, + withCall('query.system.events') +); diff --git a/packages/app-explorer/src/Main.tsx b/packages/app-explorer/src/Main.tsx index 3133d46035..d1d469f55e 100644 --- a/packages/app-explorer/src/Main.tsx +++ b/packages/app-explorer/src/Main.tsx @@ -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 {

{t('main.recentEvents', { defaultValue: 'recent events' })}

- +
]; diff --git a/packages/ui-api/src/with/call.tsx b/packages/ui-api/src/with/call.tsx index 0f905a6d86..f398cd41e9 100644 --- a/packages/ui-api/src/with/call.tsx +++ b/packages/ui-api/src/with/call.tsx @@ -15,6 +15,11 @@ import { intervalTimer, isEqual, triggerChange } from '../util/index'; import echoTransform from '../transform/echo'; import withApi from './api'; +interface Method { + (...params: Array): Promise; + at: (hash: Uint8Array | string, ...params: Array) => Promise; +} + type State = RxProps & { destroy?: () => void, propName: string, @@ -29,7 +34,7 @@ const NOOP = () => { // FIXME proper types for attributes -export default function withCall (endpoint: string, { rxChange, params = [], paramProp = 'params', propName, transform = echoTransform }: Options = {}): HOC { +export default function withCall (endpoint: string, { at, atProp, rxChange, params = [], paramProp = 'params', propName, transform = echoTransform }: Options = {}): HOC { return (Inner: React.ComponentType): React.ComponentType => { class WithPromise extends React.Component> { state: State; @@ -83,6 +88,10 @@ export default function withCall (endpoint: string, { rxChange, params = [ private getParams (props: any): Array { const paramValue = props[paramProp]; + if (atProp) { + at = props[atProp]; + } + return isUndefined(paramValue) ? params : params.concat( @@ -92,7 +101,7 @@ export default function withCall (endpoint: string, { rxChange, params = [ ); } - private getApiMethod (newParams: Array): [(...params: Array) => Promise, Array, boolean] { + private getApiMethod (newParams: Array): [Method, Array, boolean] { const { apiPromise } = this.props; if (endpoint === 'subscribe') { @@ -109,6 +118,7 @@ export default function withCall (endpoint: string, { rxChange, params = [ assert(area.length && section.length && method.length && others.length === 0, `Invalid API format, expected .
., 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 (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 (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 (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); } diff --git a/packages/ui-api/src/with/types.ts b/packages/ui-api/src/with/types.ts index 4cb13a7084..3529b4036b 100644 --- a/packages/ui-api/src/with/types.ts +++ b/packages/ui-api/src/with/types.ts @@ -14,6 +14,8 @@ export type DefaultProps = { }; export type Options = { + at?: Uint8Array | string, + atProp?: string, rxChange?: OnChangeCb, params?: Array, paramProp?: string,