PanelEditor: allow access to the eventBus from panel options (#29327)

This commit is contained in:
Ryan McKinley
2020-11-25 11:32:14 -08:00
committed by GitHub
parent 9dbf54eb61
commit a33249fecc
4 changed files with 17 additions and 2 deletions
@@ -2,10 +2,12 @@ import { Registry, RegistryItem } from '../utils/Registry';
import { ComponentType } from 'react'; import { ComponentType } from 'react';
import { FieldConfigOptionsRegistry } from './FieldConfigOptionsRegistry'; import { FieldConfigOptionsRegistry } from './FieldConfigOptionsRegistry';
import { DataFrame, InterpolateFunction, VariableSuggestionsScope, VariableSuggestion } from '../types'; import { DataFrame, InterpolateFunction, VariableSuggestionsScope, VariableSuggestion } from '../types';
import { EventBus } from '../events';
export interface StandardEditorContext<TOptions> { export interface StandardEditorContext<TOptions> {
data?: DataFrame[]; // All results data?: DataFrame[]; // All results
replaceVariables?: InterpolateFunction; replaceVariables?: InterpolateFunction;
eventBus?: EventBus;
getSuggestions?: (scope?: VariableSuggestionsScope) => VariableSuggestion[]; getSuggestions?: (scope?: VariableSuggestionsScope) => VariableSuggestion[];
options?: TOptions; options?: TOptions;
} }
@@ -22,7 +22,9 @@ interface DataResponse {
} }
/** /**
* Parse the results from `/api/ds/query * Parse the results from /api/ds/query into a DataQueryResponse
*
* @public
*/ */
export function toDataQueryResponse(res: any): DataQueryResponse { export function toDataQueryResponse(res: any): DataQueryResponse {
const rsp: DataQueryResponse = { data: [], state: LoadingState.Done }; const rsp: DataQueryResponse = { data: [], state: LoadingState.Done };
@@ -94,6 +96,8 @@ export function toDataQueryResponse(res: any): DataQueryResponse {
/** /**
* Convert an object into a DataQueryError -- if this is an HTTP response, * Convert an object into a DataQueryError -- if this is an HTTP response,
* it will put the correct values in the error field * it will put the correct values in the error field
*
* @public
*/ */
export function toDataQueryError(err: any): DataQueryError { export function toDataQueryError(err: any): DataQueryError {
const error = (err || {}) as DataQueryError; const error = (err || {}) as DataQueryError;
@@ -119,7 +123,11 @@ export function toDataQueryError(err: any): DataQueryError {
return error; return error;
} }
/** Return the first string or non-time field as the value */ /**
* Return the first string or non-time field as the value
*
* @beta
*/
export function frameToMetricFindValue(frame: DataFrame): MetricFindValue[] { export function frameToMetricFindValue(frame: DataFrame): MetricFindValue[] {
if (!frame || !frame.length) { if (!frame || !frame.length) {
return []; return [];
@@ -1,6 +1,7 @@
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import { import {
DataFrame, DataFrame,
EventBus,
InterpolateFunction, InterpolateFunction,
PanelOptionsEditorItem, PanelOptionsEditorItem,
PanelPlugin, PanelPlugin,
@@ -17,6 +18,7 @@ interface PanelOptionsEditorProps<TOptions> {
plugin: PanelPlugin; plugin: PanelPlugin;
data?: DataFrame[]; data?: DataFrame[];
replaceVariables: InterpolateFunction; replaceVariables: InterpolateFunction;
eventBus: EventBus;
options: TOptions; options: TOptions;
onChange: (options: TOptions) => void; onChange: (options: TOptions) => void;
} }
@@ -26,6 +28,7 @@ export const PanelOptionsEditor: React.FC<PanelOptionsEditorProps<any>> = ({
options, options,
onChange, onChange,
data, data,
eventBus,
replaceVariables, replaceVariables,
}) => { }) => {
const optionEditors = useMemo<Record<string, PanelOptionsEditorItem[]>>(() => { const optionEditors = useMemo<Record<string, PanelOptionsEditorItem[]>>(() => {
@@ -43,6 +46,7 @@ export const PanelOptionsEditor: React.FC<PanelOptionsEditorProps<any>> = ({
data: data || [], data: data || [],
replaceVariables, replaceVariables,
options, options,
eventBus,
getSuggestions: (scope?: VariableSuggestionsScope) => { getSuggestions: (scope?: VariableSuggestionsScope) => {
return getPanelOptionsVariableSuggestions(plugin, data); return getPanelOptionsVariableSuggestions(plugin, data);
}, },
@@ -88,6 +88,7 @@ export const PanelOptionsTab: FC<Props> = ({
replaceVariables={panel.replaceVariables} replaceVariables={panel.replaceVariables}
plugin={plugin} plugin={plugin}
data={data?.series} data={data?.series}
eventBus={dashboard.events}
/> />
); );
} }