Files
grafana/public/app/plugins/panel/debug/EventBusLogger.tsx
Oscar Kilhed d0239ac958 [grafana/UI] Hoovering over a legend label highlights the corresponding pie slice (#32941)
* Hoovering over a legend label hightlighs that pie slice

* Change to event bus

* Adds EventBusWithSource to help identify the origin of the event

* Add tests and fix bug with incorrect source

* Clean up PieChart and EventBus a bit

* Fix bug when payload.source is undefined

* Add some documentation and adjust naming

* useState instead of useSetState

* Clean up some more documentation

* Move eventbus to state

* add event bus actions to the debug panel

* add event bus actions to the debug panel

* Try to make the naming a bit clearer

* Try passing eventbus as context

* Fix lint issues

* Move event bus context to panel chrome

* Fix event handler functions

* switch to using useCallback for legend item callbacks

* Remove unused parameters

* Add id to panel fixture of PanelChrome test

* Simplify event source

* Place eventBus inside more generic context

* Push handling of context up the tree to VizLegend

only export usePanelContext and PanelContextProvider

implement isOwnEvent on EventBus

some cleanup

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
2021-04-26 16:13:15 +02:00

76 lines
1.7 KiB
TypeScript

import React, { PureComponent } from 'react';
import { CustomScrollbar } from '@grafana/ui';
import {
BusEvent,
CircularVector,
DataHoverPayload,
DataHoverEvent,
DataHoverClearEvent,
DataSelectEvent,
EventBus,
BusEventHandler,
} from '@grafana/data';
import { PartialObserver, Unsubscribable } from 'rxjs';
interface Props {
eventBus: EventBus;
}
interface State {
isError?: boolean;
counter: number;
}
interface BusEventEx {
key: number;
type: string;
payload: DataHoverPayload;
}
let counter = 100;
export class EventBusLoggerPanel extends PureComponent<Props, State> {
history = new CircularVector<BusEventEx>({ capacity: 40, append: 'head' });
subs: Unsubscribable[] = [];
constructor(props: Props) {
super(props);
this.state = { counter };
this.subs.push(props.eventBus.subscribe(DataHoverEvent, this.hoverHandler));
props.eventBus.getStream(DataHoverClearEvent).subscribe(this.eventObserver);
props.eventBus.getStream(DataSelectEvent).subscribe(this.eventObserver);
}
componentWillUnmount() {
for (const sub of this.subs) {
sub.unsubscribe();
}
}
hoverHandler: BusEventHandler<DataHoverEvent> = (event: DataHoverEvent) => {
this.history.add({
key: counter++,
type: event.type,
payload: event.payload,
});
this.setState({ counter });
};
eventObserver: PartialObserver<BusEvent> = {
next: (v: BusEvent) => {},
};
render() {
return (
<CustomScrollbar autoHeightMin="100%" autoHeightMax="100%">
{this.history.map((v, idx) => (
<div key={v.key}>
{v.key} {v.type} / X:{JSON.stringify(v.payload.x)} / Y:{JSON.stringify(v.payload.y)}
</div>
))}
</CustomScrollbar>
);
}
}