PanelContext: Adds app property of type CoreApp enum to inform panel about what the outer container/app is (#39952)

* PanelContext: Adds a container enum / string to inform panel about what the outer container/app state is

* Changing to use existing CoreApp

* fixing unified alerting type errors
pull/39984/head
Torkel Ödegaard 4 years ago committed by GitHub
parent 3e01db9a1e
commit e0b576fff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      packages/grafana-data/src/types/app.ts
  2. 5
      packages/grafana-ui/src/components/PanelChrome/PanelContext.ts
  3. 37
      public/app/features/dashboard/dashgrid/PanelChrome.tsx
  4. 3
      public/app/features/query/components/QueryEditorRow.tsx
  5. 8
      public/app/plugins/panel/debug/StateView.tsx

@ -3,11 +3,17 @@ import { KeyValue } from './data';
import { NavModel } from './navModel'; import { NavModel } from './navModel';
import { PluginMeta, GrafanaPlugin, PluginIncludeType } from './plugin'; import { PluginMeta, GrafanaPlugin, PluginIncludeType } from './plugin';
/**
* @public
* The app container that is loading another plugin (panel or query editor)
* */
export enum CoreApp { export enum CoreApp {
CloudAlerting = 'cloud-alerting',
Dashboard = 'dashboard', Dashboard = 'dashboard',
Explore = 'explore', Explore = 'explore',
Unknown = 'unknown', Unknown = 'unknown',
CloudAlerting = 'cloud-alerting', PanelEditor = 'panel-editor',
PanelViewer = 'panel-viewer',
} }
export interface AppRootProps<T = KeyValue> { export interface AppRootProps<T = KeyValue> {

@ -5,6 +5,7 @@ import {
AnnotationEventUIModel, AnnotationEventUIModel,
ThresholdsConfig, ThresholdsConfig,
SplitOpen, SplitOpen,
CoreApp,
} from '@grafana/data'; } from '@grafana/data';
import React from 'react'; import React from 'react';
import { SeriesVisibilityChangeMode } from '.'; import { SeriesVisibilityChangeMode } from '.';
@ -16,6 +17,9 @@ export interface PanelContext {
/** Dashboard panels sync */ /** Dashboard panels sync */
sync?: DashboardCursorSync; sync?: DashboardCursorSync;
/** Information on what the outer container is */
app?: CoreApp | 'string';
/** /**
* Called when a component wants to change the color for a series * Called when a component wants to change the color for a series
* *
@ -43,6 +47,7 @@ export interface PanelContext {
* @alpha -- experimental * @alpha -- experimental
*/ */
onThresholdsChange?: (thresholds: ThresholdsConfig) => void; onThresholdsChange?: (thresholds: ThresholdsConfig) => void;
/** /**
* onSplitOpen is used in Explore to open the split view. It can be used in panels which has intercations and used in Explore as well. * onSplitOpen is used in Explore to open the split view. It can be used in panels which has intercations and used in Explore as well.
* For example TimeSeries panel. * For example TimeSeries panel.

@ -6,6 +6,7 @@ import {
AbsoluteTimeRange, AbsoluteTimeRange,
AnnotationChangeEvent, AnnotationChangeEvent,
AnnotationEventUIModel, AnnotationEventUIModel,
CoreApp,
DashboardCursorSync, DashboardCursorSync,
EventFilterOptions, EventFilterOptions,
FieldConfigSource, FieldConfigSource,
@ -78,8 +79,9 @@ export class PanelChrome extends Component<Props, State> {
renderCounter: 0, renderCounter: 0,
refreshWhenInView: false, refreshWhenInView: false,
context: { context: {
sync: props.isEditing ? DashboardCursorSync.Off : props.dashboard.graphTooltip,
eventBus, eventBus,
sync: props.isEditing ? DashboardCursorSync.Off : props.dashboard.graphTooltip,
app: this.getPanelContextApp(),
onSeriesColorChange: this.onSeriesColorChange, onSeriesColorChange: this.onSeriesColorChange,
onToggleSeriesVisibility: this.onSeriesVisibilityChange, onToggleSeriesVisibility: this.onSeriesVisibilityChange,
onAnnotationCreate: this.onAnnotationCreate, onAnnotationCreate: this.onAnnotationCreate,
@ -104,6 +106,17 @@ export class PanelChrome extends Component<Props, State> {
store.dispatch(setPanelInstanceState({ panelId: this.props.panel.id, value })); store.dispatch(setPanelInstanceState({ panelId: this.props.panel.id, value }));
}; };
getPanelContextApp() {
if (this.props.isEditing) {
return CoreApp.PanelEditor;
}
if (this.props.isViewing) {
return CoreApp.PanelViewer;
}
return CoreApp.Dashboard;
}
onSeriesColorChange = (label: string, color: string) => { onSeriesColorChange = (label: string, color: string) => {
this.onFieldConfigChange(changeSeriesColorConfigFactory(label, color, this.props.panel.fieldConfig)); this.onFieldConfigChange(changeSeriesColorConfigFactory(label, color, this.props.panel.fieldConfig));
}; };
@ -177,20 +190,18 @@ export class PanelChrome extends Component<Props, State> {
componentDidUpdate(prevProps: Props) { componentDidUpdate(prevProps: Props) {
const { isInView, isEditing, width } = this.props; const { isInView, isEditing, width } = this.props;
const { context } = this.state;
if (prevProps.dashboard.graphTooltip !== this.props.dashboard.graphTooltip) { const app = this.getPanelContextApp();
this.setState((s) => { const sync = isEditing ? DashboardCursorSync.Off : this.props.dashboard.graphTooltip;
return {
context: { ...s.context, sync: isEditing ? DashboardCursorSync.Off : this.props.dashboard.graphTooltip },
};
});
}
if (isEditing !== prevProps.isEditing) { if (context.sync !== sync || context.app !== app) {
this.setState((s) => { this.setState({
return { context: {
context: { ...s.context, sync: isEditing ? DashboardCursorSync.Off : this.props.dashboard.graphTooltip }, ...context,
}; sync,
app,
},
}); });
} }

@ -212,6 +212,7 @@ export class QueryEditorRow<TQuery extends DataQuery> extends PureComponent<Prop
ds.components?.ExploreQueryField || ds.components?.ExploreQueryField ||
ds.components?.QueryEditor ds.components?.QueryEditor
); );
case CoreApp.PanelEditor:
case CoreApp.Dashboard: case CoreApp.Dashboard:
default: default:
return ds.components?.QueryEditor; return ds.components?.QueryEditor;
@ -219,7 +220,7 @@ export class QueryEditorRow<TQuery extends DataQuery> extends PureComponent<Prop
} }
renderPluginEditor = () => { renderPluginEditor = () => {
const { query, onChange, queries, onRunQuery, app = CoreApp.Dashboard, history } = this.props; const { query, onChange, queries, onRunQuery, app = CoreApp.PanelEditor, history } = this.props;
const { datasource, data } = this.state; const { datasource, data } = this.state;
if (datasource?.components?.QueryCtrl) { if (datasource?.components?.QueryCtrl) {

@ -13,9 +13,11 @@ export function StateView(props: PanelProps<DebugPanelOptions>) {
}; };
return ( return (
<Field label="State name"> <>
<Input value={context.instanceState?.name ?? ''} onChange={onChangeName} /> <Field label="State name">
</Field> <Input value={context.instanceState?.name ?? ''} onChange={onChangeName} />
</Field>
</>
); );
} }

Loading…
Cancel
Save