From 3c1a9a9eae509b00b0a7c3cdfdcb4fde24f330b8 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Mon, 12 Jul 2021 09:30:24 -0400 Subject: [PATCH] Explore: Refactor QueryRow to use ConnectedProps (#36604) * Refactor QueryRow * Upddate import in test * Simplify typing * Add copy when loading instance --- public/app/features/explore/QueryRow.test.tsx | 6 +- public/app/features/explore/QueryRow.tsx | 67 ++++++------------- 2 files changed, 23 insertions(+), 50 deletions(-) diff --git a/public/app/features/explore/QueryRow.test.tsx b/public/app/features/explore/QueryRow.test.tsx index cf0b04e3bcf..b75e482dd59 100644 --- a/public/app/features/explore/QueryRow.test.tsx +++ b/public/app/features/explore/QueryRow.test.tsx @@ -1,11 +1,11 @@ -import React from 'react'; -import { QueryRow, QueryRowProps } from './QueryRow'; +import React, { ComponentProps } from 'react'; +import { QueryRow } from './QueryRow'; import { shallow } from 'enzyme'; import { ExploreId } from 'app/types/explore'; import { DataSourceApi, TimeRange, AbsoluteTimeRange, PanelData, EventBusExtended } from '@grafana/data'; const setup = (propOverrides?: object) => { - const props: QueryRowProps = { + const props: ComponentProps = { exploreId: ExploreId.left, index: 1, exploreEvents: {} as EventBusExtended, diff --git a/public/app/features/explore/QueryRow.tsx b/public/app/features/explore/QueryRow.tsx index dd5816fadb3..c54390e7bcf 100644 --- a/public/app/features/explore/QueryRow.tsx +++ b/public/app/features/explore/QueryRow.tsx @@ -2,53 +2,24 @@ import React, { PureComponent } from 'react'; import { debounce, has } from 'lodash'; import { hot } from 'react-hot-loader'; -// @ts-ignore -import { connect } from 'react-redux'; -// Components +import { connect, ConnectedProps } from 'react-redux'; import AngularQueryEditor from './QueryEditor'; import { QueryRowActions } from './QueryRowActions'; -// Types import { StoreState } from 'app/types'; -import { - DataQuery, - DataSourceApi, - PanelData, - HistoryItem, - TimeRange, - AbsoluteTimeRange, - LoadingState, - EventBusExtended, -} from '@grafana/data'; +import { DataQuery, LoadingState, DataSourceApi } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; - import { ExploreItemState, ExploreId } from 'app/types/explore'; import { highlightLogsExpressionAction } from './state/explorePane'; import { ErrorContainer } from './ErrorContainer'; import { changeQuery, modifyQueries, removeQueryRowAction, runQueries } from './state/query'; import { HelpToggle } from '../query/components/HelpToggle'; -interface PropsFromParent { +interface OwnProps { exploreId: ExploreId; index: number; } -export interface QueryRowProps extends PropsFromParent { - changeQuery: typeof changeQuery; - className?: string; - exploreId: ExploreId; - datasourceInstance: DataSourceApi; - highlightLogsExpressionAction: typeof highlightLogsExpressionAction; - history: HistoryItem[]; - query: DataQuery; - modifyQueries: typeof modifyQueries; - range: TimeRange; - absoluteRange: AbsoluteTimeRange; - removeQueryRowAction: typeof removeQueryRowAction; - runQueries: typeof runQueries; - queryResponse: PanelData; - latency: number; - exploreEvents: EventBusExtended; -} +type QueryRowProps = OwnProps & ConnectedProps; interface QueryRowState { textEditModeEnabled: boolean; @@ -70,7 +41,7 @@ export class QueryRow extends PureComponent { onChange = (query: DataQuery, override?: boolean) => { const { datasourceInstance, exploreId, index } = this.props; this.props.changeQuery(exploreId, query, index, override); - if (query && !override && datasourceInstance.getHighlighterExpression && index === 0) { + if (query && !override && datasourceInstance?.getHighlighterExpression && index === 0) { // Live preview of log search matches. Only use on first row for now this.updateLogsHighlights(query); } @@ -95,10 +66,8 @@ export class QueryRow extends PureComponent { this.setState({ textEditModeEnabled: !this.state.textEditModeEnabled }); }; - setReactQueryEditor = () => { - const { datasourceInstance } = this.props; + setReactQueryEditor = (datasourceInstance: DataSourceApi) => { let QueryEditor; - // TODO:unification if (datasourceInstance.components?.ExploreMetricsQueryField) { QueryEditor = datasourceInstance.components.ExploreMetricsQueryField; @@ -112,12 +81,12 @@ export class QueryRow extends PureComponent { return QueryEditor; }; - renderQueryEditor = () => { - const { datasourceInstance, history, query, exploreEvents, range, queryResponse, exploreId } = this.props; + renderQueryEditor = (datasourceInstance: DataSourceApi) => { + const { history, query, exploreEvents, range, queryResponse, exploreId } = this.props; const queryErrors = queryResponse.error && queryResponse.error.refId === query.refId ? [queryResponse.error] : []; - const ReactQueryEditor = this.setReactQueryEditor(); + const ReactQueryEditor = this.setReactQueryEditor(datasourceInstance); let QueryEditor: JSX.Element; if (ReactQueryEditor) { @@ -155,7 +124,7 @@ export class QueryRow extends PureComponent { {QueryEditor} {DatasourceCheatsheet && ( - this.onChange(query)} datasource={datasourceInstance} /> + this.onChange(query)} datasource={datasourceInstance!} /> )} @@ -164,7 +133,7 @@ export class QueryRow extends PureComponent { updateLogsHighlights = debounce((value: DataQuery) => { const { datasourceInstance } = this.props; - if (datasourceInstance.getHighlighterExpression) { + if (datasourceInstance?.getHighlighterExpression) { const { exploreId } = this.props; const expressions = datasourceInstance.getHighlighterExpression(value); this.props.highlightLogsExpressionAction({ exploreId, expressions }); @@ -174,6 +143,10 @@ export class QueryRow extends PureComponent { render() { const { datasourceInstance, query, queryResponse, latency } = this.props; + if (!datasourceInstance) { + return <>Loading data source; + } + const canToggleEditorModes = has(datasourceInstance, 'components.QueryCtrl.prototype.toggleEditorMode'); const isNotStarted = queryResponse.state === LoadingState.NotStarted; @@ -183,7 +156,7 @@ export class QueryRow extends PureComponent { return ( <>
-
{this.renderQueryEditor()}
+
{this.renderQueryEditor(datasourceInstance)}
{ } } -function mapStateToProps(state: StoreState, { exploreId, index }: QueryRowProps) { +function mapStateToProps(state: StoreState, { exploreId, index }: OwnProps) { const explore = state.explore; const item: ExploreItemState = explore[exploreId]!; const { datasourceInstance, history, queries, range, absoluteRange, queryResponse, latency, eventBridge } = item; @@ -226,6 +199,6 @@ const mapDispatchToProps = { runQueries, }; -export default hot(module)( - connect(mapStateToProps, mapDispatchToProps)(QueryRow) as React.ComponentType -); +const connector = connect(mapStateToProps, mapDispatchToProps); + +export default hot(module)(connector(QueryRow));