From 8e95b06e7ba971b7d203d833ba3c5e3b64ae988c Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Thu, 11 Apr 2019 00:58:03 -0700 Subject: [PATCH] Auto-show latest block on BlockQuery (#960) * Auto-show latest block on BlockQuery * use bestNumber * s/Block query/Block details/ --- .../app-explorer/src/BlockQuery/index.tsx | 38 +++++++++++++++++-- packages/app-explorer/src/index.tsx | 2 +- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/app-explorer/src/BlockQuery/index.tsx b/packages/app-explorer/src/BlockQuery/index.tsx index 4fd35071e2..94c459828f 100644 --- a/packages/app-explorer/src/BlockQuery/index.tsx +++ b/packages/app-explorer/src/BlockQuery/index.tsx @@ -5,13 +5,16 @@ import { BareProps } from '@polkadot/ui-app/types'; import React from 'react'; +import { withCall, withMulti } from '@polkadot/ui-api'; import { isHex } from '@polkadot/util'; +import { BlockNumber } from '@polkadot/types'; import BlockByHash from './ByHash'; import BlockByNumber from './ByNumber'; import Query from './Query'; type Props = BareProps & { + chain_bestNumber?: BlockNumber, match: { params: { value: string @@ -19,9 +22,33 @@ type Props = BareProps & { } }; -export default class Entry extends React.PureComponent { +type State = { + value?: string +}; + +class Entry extends React.Component { + state: State = { + value: undefined + }; + + static getDerivedStateFromProps ({ chain_bestNumber, match: { params } }: Props): State { + let { value } = params; + if ((!value || !value.length) && chain_bestNumber) { + value = chain_bestNumber.toString(); + } + + return { + value + } as State; + } + + shouldComponentUpdate (nextProps: Props) { + return this.props.match !== nextProps.match || + !this.state.value; + } + render () { - const { match: { params: { value } } } = this.props; + const { value } = this.state; return ( <> @@ -32,7 +59,7 @@ export default class Entry extends React.PureComponent { } private renderBlock () { - const { match: { params: { value } } } = this.props; + const { value } = this.state; if (!value) { return null; @@ -53,3 +80,8 @@ export default class Entry extends React.PureComponent { ); } } + +export default withMulti( + Entry, + withCall('derive.chain.bestNumber') +); diff --git a/packages/app-explorer/src/index.tsx b/packages/app-explorer/src/index.tsx index 1d66aacf7f..2957595653 100644 --- a/packages/app-explorer/src/index.tsx +++ b/packages/app-explorer/src/index.tsx @@ -36,7 +36,7 @@ class ExplorerApp extends React.Component { { hasParams: true, name: 'query', - text: t('Block query') + text: t('Block details') }, { name: 'node',