diff --git a/packages/app-explorer/src/Events.tsx b/packages/app-explorer/src/Events.tsx index c5bc74496d..d08997bcdd 100644 --- a/packages/app-explorer/src/Events.tsx +++ b/packages/app-explorer/src/Events.tsx @@ -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 ( -
-
-

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

-
-
- - { - event.meta && event.meta.documentation - ? event.meta.documentation.join(' ') - : 'Details' - } - - -
-
- ); + if (!event.method || !event.section) { + return null; } - )} + + return ( +
+
+

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

+
+
+ + { + event.meta && event.meta.documentation + ? event.meta.documentation.join(' ') + : 'Details' + } + + +
+
+ ); + })} ); } diff --git a/packages/app-explorer/src/index.tsx b/packages/app-explorer/src/index.tsx index d5637cb750..f3950f40bd 100644 --- a/packages/app-explorer/src/index.tsx +++ b/packages/app-explorer/src/index.tsx @@ -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 { - const [headers, setHeaders] = useState([]); - const [{ prevEventHash, events }, setEvents] = useState<{ prevEventHash: string; events: KeyedEvent[] }>({ prevEventHash: '', events: [] }); +function ExplorerApp ({ basePath, className, newEvents, t }: Props): React.ReactElement { 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 (
@@ -101,7 +85,7 @@ function ExplorerApp ({ basePath, className, newEvents, newHeader, t }: Props): => (
)} /> @@ -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' }] + }] ) ); diff --git a/packages/react-query/src/BlockAuthors.tsx b/packages/react-query/src/BlockAuthors.tsx index c7e1d90711..bbb4cd88b4 100644 --- a/packages/react-query/src/BlockAuthors.tsx +++ b/packages/react-query/src/BlockAuthors.tsx @@ -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 = {}; -const BlockAuthorsContext: React.Context = React.createContext({ byAuthor }); +const BlockAuthorsContext: React.Context = React.createContext({ byAuthor, lastHeaders: [] }); function BlockAuthors ({ children }: Props): React.ReactElement { const { api } = useContext(ApiContext); - const [state, setState] = useState({ byAuthor }); + const [state, setState] = useState({ 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 }); } }); });