From a79d909886437309de8a0054f1fd40b334cff294 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 8 Nov 2019 09:45:41 +0100 Subject: [PATCH] Add global event context (#1851) * Global event context * Cleanup events * Slice logs * event adjustments * Keep old events * Cleanup props --- packages/app-explorer/src/index.tsx | 43 ++++----------------- packages/apps/src/index.tsx | 14 ++++--- packages/react-query/src/Events.tsx | 60 +++++++++++++++++++++++++++++ packages/react-query/src/index.ts | 1 + 4 files changed, 77 insertions(+), 41 deletions(-) create mode 100644 packages/react-query/src/Events.tsx diff --git a/packages/app-explorer/src/index.tsx b/packages/app-explorer/src/index.tsx index f3950f40bd..efae407204 100644 --- a/packages/app-explorer/src/index.tsx +++ b/packages/app-explorer/src/index.tsx @@ -2,20 +2,16 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ApiProps } from '@polkadot/react-api/types'; import { AppProps, BareProps, I18nProps } from '@polkadot/react-components/types'; -import { EventRecord } from '@polkadot/types/interfaces'; import { KeyedEvent } from './types'; -import React, { useContext, useEffect, useState } from 'react'; +import React, { useContext } from 'react'; import { Route, Switch } from 'react-router'; import styled from 'styled-components'; -import { ApiContext, withCalls, withMulti } from '@polkadot/react-api'; +import { ApiContext } from '@polkadot/react-api'; import Tabs from '@polkadot/react-components/Tabs'; -import { BlockAuthorsContext } from '@polkadot/react-query'; +import { BlockAuthorsContext, EventsContext } from '@polkadot/react-query'; import uiSettings from '@polkadot/ui-settings'; -import { stringToU8a } from '@polkadot/util'; -import { xxhashAsHex } from '@polkadot/util-crypto'; import BlockInfo from './BlockInfo'; import Forks from './Forks'; @@ -23,27 +19,14 @@ import Main from './Main'; import NodeInfo from './NodeInfo'; import translate from './translate'; -interface Props extends ApiProps, AppProps, BareProps, I18nProps { +interface Props extends AppProps, BareProps, I18nProps { newEvents?: KeyedEvent[]; } -const MAX_ITEMS = 15; - -function ExplorerApp ({ basePath, className, newEvents, t }: Props): React.ReactElement { +function ExplorerApp ({ basePath, className, 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))); - - if (newEventHash !== prevEventHash && newEvents) { - setEvents({ - prevEventHash: newEventHash, - events: newEvents.concat(events).filter((_, index): boolean => index < MAX_ITEMS) - }); - } - }, [newEvents]); + const events = useContext(EventsContext); return (
@@ -93,20 +76,10 @@ function ExplorerApp ({ basePath, className, newEvents, t }: Props): React.React ); } -export default withMulti( +export default translate( styled(ExplorerApp)` .rx--updated { background: transparent !important; } - `, - translate, - withCalls( - ['query.system.events', { - propName: 'newEvents', - transform: (records: EventRecord[]): KeyedEvent[] => - records - .filter(({ event }): boolean => event.section !== 'system') - .map((record, index): KeyedEvent => ({ key: `${Date.now()}-${index}`, record })) - }] - ) + ` ); diff --git a/packages/apps/src/index.tsx b/packages/apps/src/index.tsx index 37f04c837c..9c184bfe07 100644 --- a/packages/apps/src/index.tsx +++ b/packages/apps/src/index.tsx @@ -18,7 +18,7 @@ import { getTypeRegistry } from '@polkadot/types'; import { Api } from '@polkadot/react-api'; import { QueueConsumer } from '@polkadot/react-components/Status/Context'; import Queue from '@polkadot/react-components/Status/Queue'; -import { BlockAuthors } from '@polkadot/react-query'; +import { BlockAuthors, Events } from '@polkadot/react-query'; import Apps from './Apps'; @@ -72,11 +72,13 @@ ReactDOM.render( url={wsEndpoint} > - - - - - + + + + + + + )} diff --git a/packages/react-query/src/Events.tsx b/packages/react-query/src/Events.tsx new file mode 100644 index 0000000000..98acc042ce --- /dev/null +++ b/packages/react-query/src/Events.tsx @@ -0,0 +1,60 @@ +// Copyright 2017-2019 @polkadot/react-query 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 { Codec } from '@polkadot/types/types'; + +import React, { useContext, useEffect, useState } from 'react'; +import { ApiContext } from '@polkadot/react-api'; +import { EventRecord } from '@polkadot/types/interfaces'; +import { stringToU8a } from '@polkadot/util'; +import { xxhashAsHex } from '@polkadot/util-crypto'; + +interface KeyedEvent { + key: string; + record: EventRecord; +} + +type Events = KeyedEvent[]; + +interface Props { + children: React.ReactNode; +} + +const MAX_EVENTS = 20; + +const EventsContext: React.Context = React.createContext([]); + +function Events ({ children }: Props): React.ReactElement { + const { api } = useContext(ApiContext); + const [state, setState] = useState([]); + + 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 => { + const prevEventHash = ''; + let events: Events = []; + + api.query.system.events((records: EventRecord[] & Codec): void => { + const newEvents = records + .filter(({ event }): boolean => event.section !== 'system') + .map((record, index): KeyedEvent => ({ key: `${Date.now()}-${index}`, record })); + const newEventHash = xxhashAsHex(stringToU8a(JSON.stringify(newEvents))); + + if (newEventHash !== prevEventHash) { + events = [...newEvents, ...events].slice(0, MAX_EVENTS); + setState(events); + } + }); + }); + }, []); + + return ( + + {children} + + ); +} + +export { EventsContext, Events }; diff --git a/packages/react-query/src/index.ts b/packages/react-query/src/index.ts index f3cba72e39..ff8534c762 100644 --- a/packages/react-query/src/index.ts +++ b/packages/react-query/src/index.ts @@ -13,6 +13,7 @@ export { default as BestFinalized } from './BestFinalized'; export { default as BestNumber } from './BestNumber'; export { default as Chain } from './Chain'; export { default as Elapsed } from './Elapsed'; +export { Events, EventsContext } from './Events'; export { default as LockedVote } from './LockedVote'; export { default as NodeName } from './NodeName'; export { default as NodeVersion } from './NodeVersion';