Add global event context (#1851)
* Global event context * Cleanup events * Slice logs * event adjustments * Keep old events * Cleanup props
This commit is contained in:
parent
6bdd948aa8
commit
a79d909886
@ -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<Props> {
|
||||
function ExplorerApp ({ basePath, className, 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)));
|
||||
|
||||
if (newEventHash !== prevEventHash && newEvents) {
|
||||
setEvents({
|
||||
prevEventHash: newEventHash,
|
||||
events: newEvents.concat(events).filter((_, index): boolean => index < MAX_ITEMS)
|
||||
});
|
||||
}
|
||||
}, [newEvents]);
|
||||
const events = useContext(EventsContext);
|
||||
|
||||
return (
|
||||
<main className={className}>
|
||||
@ -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<Props>(
|
||||
['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 }))
|
||||
}]
|
||||
)
|
||||
`
|
||||
);
|
||||
|
||||
@ -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}
|
||||
>
|
||||
<BlockAuthors>
|
||||
<HashRouter>
|
||||
<ThemeProvider theme={theme}>
|
||||
<Apps />
|
||||
</ThemeProvider>
|
||||
</HashRouter>
|
||||
<Events>
|
||||
<HashRouter>
|
||||
<ThemeProvider theme={theme}>
|
||||
<Apps />
|
||||
</ThemeProvider>
|
||||
</HashRouter>
|
||||
</Events>
|
||||
</BlockAuthors>
|
||||
</Api>
|
||||
)}
|
||||
|
||||
60
packages/react-query/src/Events.tsx
Normal file
60
packages/react-query/src/Events.tsx
Normal file
@ -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<Events> = React.createContext<Events>([]);
|
||||
|
||||
function Events ({ children }: Props): React.ReactElement<Props> {
|
||||
const { api } = useContext(ApiContext);
|
||||
const [state, setState] = useState<Events>([]);
|
||||
|
||||
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 (
|
||||
<EventsContext.Provider value={state}>
|
||||
{children}
|
||||
</EventsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export { EventsContext, Events };
|
||||
@ -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';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user