From eae11f53f34337868b97a35f4a5ca7f8b91cf430 Mon Sep 17 00:00:00 2001 From: Dominik Prokop Date: Wed, 22 Apr 2020 19:21:48 +0200 Subject: [PATCH] Transformations: Make sidebar subscribe to panel's query runner (#23785) * Make panel edit sidebar options use lates data from panel query runner * Update select's z-index * Review --- .../PanelEditor/OptionsPaneContent.tsx | 20 ++++++------ .../components/PanelEditor/PanelEditor.tsx | 3 +- .../PanelEditor/usePanelLatestData.ts | 31 +++++++++++++++++++ 3 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 public/app/features/dashboard/components/PanelEditor/usePanelLatestData.ts diff --git a/public/app/features/dashboard/components/PanelEditor/OptionsPaneContent.tsx b/public/app/features/dashboard/components/PanelEditor/OptionsPaneContent.tsx index 2a5907ea1a0..038af5ff97d 100644 --- a/public/app/features/dashboard/components/PanelEditor/OptionsPaneContent.tsx +++ b/public/app/features/dashboard/components/PanelEditor/OptionsPaneContent.tsx @@ -1,17 +1,17 @@ import React, { useCallback, useState, CSSProperties } from 'react'; import Transition from 'react-transition-group/Transition'; -import { FieldConfigSource, GrafanaTheme, PanelData, PanelPlugin, SelectableValue } from '@grafana/data'; +import { FieldConfigSource, GrafanaTheme, PanelPlugin, SelectableValue } from '@grafana/data'; import { DashboardModel, PanelModel } from '../../state'; import { CustomScrollbar, stylesFactory, Tab, TabContent, TabsBar, Select, useTheme, Icon, Input } from '@grafana/ui'; import { DefaultFieldConfigEditor, OverrideFieldConfigEditor } from './FieldConfigEditor'; import { css } from 'emotion'; import { PanelOptionsTab } from './PanelOptionsTab'; import { DashNavButton } from 'app/features/dashboard/components/DashNav/DashNavButton'; +import { usePanelLatestData } from './usePanelLatestData'; interface Props { plugin: PanelPlugin; panel: PanelModel; - data: PanelData; width: number; dashboard: DashboardModel; onClose: () => void; @@ -23,7 +23,6 @@ interface Props { export const OptionsPaneContent: React.FC = ({ plugin, panel, - data, width, onFieldConfigsChange, onPanelOptionsChanged, @@ -35,12 +34,13 @@ export const OptionsPaneContent: React.FC = ({ const styles = getStyles(theme); const [activeTab, setActiveTab] = useState('options'); const [isSearching, setSearchMode] = useState(false); + const [currentData, hasSeries] = usePanelLatestData(panel); const renderFieldOptions = useCallback( (plugin: PanelPlugin) => { const fieldConfig = panel.getFieldConfig(); - if (!fieldConfig) { + if (!fieldConfig || !hasSeries) { return null; } @@ -49,18 +49,18 @@ export const OptionsPaneContent: React.FC = ({ config={fieldConfig} plugin={plugin} onChange={onFieldConfigsChange} - data={data.series} + data={currentData.series} /> ); }, - [data, plugin, panel, onFieldConfigsChange] + [currentData, plugin, panel, onFieldConfigsChange] ); const renderFieldOverrideOptions = useCallback( (plugin: PanelPlugin) => { const fieldConfig = panel.getFieldConfig(); - if (!fieldConfig) { + if (!fieldConfig || !hasSeries) { return null; } @@ -69,11 +69,11 @@ export const OptionsPaneContent: React.FC = ({ config={fieldConfig} plugin={plugin} onChange={onFieldConfigsChange} - data={data.series} + data={currentData.series} /> ); }, - [data, plugin, panel, onFieldConfigsChange] + [currentData, plugin, panel, onFieldConfigsChange] ); // When the panel has no query only show the main tab @@ -103,7 +103,7 @@ export const OptionsPaneContent: React.FC = ({ panel={panel} plugin={plugin} dashboard={dashboard} - data={data} + data={currentData} onPanelConfigChange={onPanelConfigChange} onFieldConfigsChange={onFieldConfigsChange} onPanelOptionsChanged={onPanelOptionsChanged} diff --git a/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx b/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx index bef1f014420..e76bc3b9998 100644 --- a/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx +++ b/public/app/features/dashboard/components/PanelEditor/PanelEditor.tsx @@ -275,7 +275,7 @@ export class PanelEditorUnconnected extends PureComponent { } renderOptionsPane() { - const { plugin, dashboard, data, panel, uiState } = this.props; + const { plugin, dashboard, panel, uiState } = this.props; if (!plugin) { return
; @@ -285,7 +285,6 @@ export class PanelEditorUnconnected extends PureComponent { { + const querySubscription = useRef(null); + const [latestData, setLatestData] = useState(null); + + useEffect(() => { + querySubscription.current = panel + .getQueryRunner() + .getData() + .subscribe({ + next: data => setLatestData(data), + }); + + return () => { + if (querySubscription.current) { + console.log('unsubscribing'); + querySubscription.current.unsubscribe(); + } + }; + }, [panel]); + + return [ + latestData, + // TODO: make this more clever, use PanelData.state + !!(latestData && latestData.series), + ]; +};