ghost-portal/packages/app-explorer/src/EventsRecent.tsx
2019-01-12 17:54:15 +01:00

77 lines
1.9 KiB
TypeScript

// Copyright 2017-2019 @polkadot/app-explorer 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 { I18nProps } from '@polkadot/ui-app/types';
import React from 'react';
import { EventRecord } from '@polkadot/types';
import { withCall, withMulti } from '@polkadot/ui-api/index';
import { stringToU8a } from '@polkadot/util';
import { xxhashAsHex } from '@polkadot/util-crypto';
import { MAX_ITEMS } from './BlockHeaders';
import Events from './Events';
import translate from './translate';
type Props = I18nProps & {
query_system_events?: Array<EventRecord>
};
type State = {
prevEventHash: string;
recentEvents: Array<EventRecord>;
};
class EventsRecent extends React.PureComponent<Props, State> {
constructor (props: Props) {
super(props);
this.state = {
prevEventHash: '',
recentEvents: []
};
}
static getDerivedStateFromProps ({ query_system_events = [] }: Props, prevState: State): State | null {
const prevEventHash = xxhashAsHex(stringToU8a(JSON.stringify(query_system_events)));
if (prevEventHash === prevState.prevEventHash) {
return null;
}
const recentEvents = query_system_events
.filter(({ event }) => event.section !== 'system')
.concat(prevState.recentEvents)
.filter((_, index) => index < MAX_ITEMS);
return {
prevEventHash,
recentEvents
};
}
render () {
const { t } = this.props;
const { recentEvents } = this.state;
return (
<div>
<Events
emptyLabel={t('events.system-none', {
defaultValue: 'no recent non-system events available'
})}
value={recentEvents}
withoutIndex
/>
</div>
);
}
}
export default withMulti(
EventsRecent,
translate,
withCall('query.system.events')
);