Share BlockAuthor context with explorer (#1809)

This commit is contained in:
Jaco Greeff 2019-10-29 15:36:38 +01:00 committed by GitHub
parent c6b34f3430
commit 11cc4c57d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 66 deletions

View File

@ -29,47 +29,45 @@ function Events ({ emptyLabel, eventClassName, events, withoutIndex, t }: Props)
return (
<>
{events.map(
({ key, record: { event, phase } }: KeyedEvent): React.ReactNode => {
const extIndex = !withoutIndex && phase.isApplyExtrinsic
? phase.asApplyExtrinsic
: -1;
{events.map(({ key, record: { event, phase } }: KeyedEvent): React.ReactNode => {
const extIndex = !withoutIndex && phase.isApplyExtrinsic
? phase.asApplyExtrinsic
: -1;
if (!event.method || !event.section) {
return null;
}
return (
<article
className={`explorer--Container ${eventClassName}`}
key={key}
>
<div className='header'>
<h3>
{event.section}.{event.method}&nbsp;{
extIndex !== -1
? `(#${formatNumber(extIndex)})`
: ''
}
</h3>
</div>
<details>
<summary>
{
event.meta && event.meta.documentation
? event.meta.documentation.join(' ')
: 'Details'
}
</summary>
<EventDisplay
className='details'
value={event}
/>
</details>
</article>
);
if (!event.method || !event.section) {
return null;
}
)}
return (
<article
className={`explorer--Container ${eventClassName}`}
key={key}
>
<div className='header'>
<h3>
{event.section}.{event.method}&nbsp;{
extIndex !== -1
? `(#${formatNumber(extIndex)})`
: ''
}
</h3>
</div>
<details>
<summary>
{
event.meta && event.meta.documentation
? event.meta.documentation.join(' ')
: 'Details'
}
</summary>
<EventDisplay
className='details'
value={event}
/>
</details>
</article>
);
})}
</>
);
}

View File

@ -10,9 +10,9 @@ import { KeyedEvent } from './types';
import React, { useContext, useEffect, useState } from 'react';
import { Route, Switch } from 'react-router';
import styled from 'styled-components';
import { HeaderExtended } from '@polkadot/api-derive';
import { ApiContext, withCalls, withMulti } from '@polkadot/react-api';
import Tabs from '@polkadot/react-components/Tabs';
import { BlockAuthorsContext } from '@polkadot/react-query';
import uiSettings from '@polkadot/ui-settings';
import { stringToU8a } from '@polkadot/util';
import { xxhashAsHex } from '@polkadot/util-crypto';
@ -24,16 +24,15 @@ import NodeInfo from './NodeInfo';
import translate from './translate';
interface Props extends ApiProps, AppProps, BareProps, I18nProps {
newHeader?: HeaderExtended;
newEvents?: KeyedEvent[];
}
const MAX_ITEMS = 15;
function ExplorerApp ({ basePath, className, newEvents, newHeader, t }: Props): React.ReactElement<Props> {
const [headers, setHeaders] = useState<HeaderExtended[]>([]);
const [{ prevEventHash, events }, setEvents] = useState<{ prevEventHash: string; events: KeyedEvent[] }>({ prevEventHash: '', events: [] });
function ExplorerApp ({ basePath, className, newEvents, t }: Props): React.ReactElement<Props> {
const { api } = useContext(ApiContext);
const { lastHeaders } = useContext(BlockAuthorsContext);
const [{ prevEventHash, events }, setEvents] = useState<{ prevEventHash: string; events: KeyedEvent[] }>({ prevEventHash: '', events: [] });
useEffect((): void => {
const newEventHash = xxhashAsHex(stringToU8a(JSON.stringify(newEvents)));
@ -46,21 +45,6 @@ function ExplorerApp ({ basePath, className, newEvents, newHeader, t }: Props):
}
}, [newEvents]);
useEffect((): void => {
if (newHeader) {
setHeaders(
headers
.filter((old, index): boolean => index < MAX_ITEMS && old.number.unwrap().lt(newHeader.number.unwrap()))
.reduce((next, header): HeaderExtended[] => {
next.push(header);
return next;
}, [newHeader])
.sort((a, b): number => b.number.unwrap().cmp(a.number.unwrap()))
);
}
}, [newHeader]);
return (
<main className={className}>
<header>
@ -101,7 +85,7 @@ function ExplorerApp ({ basePath, className, newEvents, newHeader, t }: Props):
<Route render={(): React.ReactElement<{}> => (
<Main
events={events}
headers={headers}
headers={lastHeaders}
/>
)} />
</Switch>
@ -123,7 +107,6 @@ export default withMulti(
records
.filter(({ event }): boolean => event.section !== 'system')
.map((record, index): KeyedEvent => ({ key: `${Date.now()}-${index}`, record }))
}],
['derive.chain.subscribeNewHeads', { propName: 'newHeader' }]
}]
)
);

View File

@ -12,33 +12,48 @@ interface Authors {
lastBlockAuthor?: string;
lastBlockNumber?: string;
lastHeader?: HeaderExtended;
lastHeaders: HeaderExtended[];
}
interface Props {
children: React.ReactNode;
}
const MAX_HEADERS = 20;
const byAuthor: Record<string, string> = {};
const BlockAuthorsContext: React.Context<Authors> = React.createContext<Authors>({ byAuthor });
const BlockAuthorsContext: React.Context<Authors> = React.createContext<Authors>({ byAuthor, lastHeaders: [] });
function BlockAuthors ({ children }: Props): React.ReactElement<Props> {
const { api } = useContext(ApiContext);
const [state, setState] = useState<Authors>({ byAuthor });
const [state, setState] = useState<Authors>({ byAuthor, lastHeaders: [] });
useEffect((): void => {
// TODO We should really unsub - but since this should just be used once,
// atm I'm rather typing this than doing it the way it is supposed to be
api.isReady.then((): void => {
let lastHeaders: HeaderExtended[] = [];
api.derive.chain.subscribeNewHeads((lastHeader): void => {
if (lastHeader && lastHeader.number) {
const blockNumber = lastHeader.number.unwrap();
const lastBlockAuthor = lastHeader.author ? lastHeader.author.toString() : undefined;
const lastBlockNumber = formatNumber(lastHeader.number.unwrap());
const lastBlockNumber = formatNumber(blockNumber);
if (lastBlockAuthor) {
byAuthor[lastBlockAuthor] = lastBlockNumber;
}
setState({ byAuthor, lastBlockAuthor, lastBlockNumber, lastHeader });
lastHeaders = lastHeaders
.filter((old, index): boolean => index < MAX_HEADERS && old.number.unwrap().lt(blockNumber))
.reduce((next, header): HeaderExtended[] => {
next.push(header);
return next;
}, [lastHeader])
.sort((a, b): number => b.number.unwrap().cmp(a.number.unwrap()));
setState({ byAuthor, lastBlockAuthor, lastBlockNumber, lastHeader, lastHeaders });
}
});
});