From 06a2943cb667f140da9ac33368315bf4407911f5 Mon Sep 17 00:00:00 2001 From: Shirley <4163034+fridgepoet@users.noreply.github.com> Date: Thu, 15 Dec 2022 10:54:05 +0100 Subject: [PATCH] Cloudwatch: Refactor logs query field (#59503) * Refactor LogsQueryField to function component (#59503) --- .betterer.results | 5 +- .../cloudwatch/components/LogsQueryField.tsx | 166 ++++++------------ 2 files changed, 59 insertions(+), 112 deletions(-) diff --git a/.betterer.results b/.betterer.results index 9ea6bdfc787..b329dcfffa5 100644 --- a/.betterer.results +++ b/.betterer.results @@ -5350,9 +5350,8 @@ exports[`better eslint`] = { [0, 0, 0, "Unexpected any. Specify a different type.", "3"] ], "public/app/plugins/datasource/cloudwatch/components/LogsQueryField.tsx:5381": [ - [0, 0, 0, "Unexpected any. Specify a different type.", "0"], - [0, 0, 0, "Do not use any type assertions.", "1"], - [0, 0, 0, "Do not use any type assertions.", "2"] + [0, 0, 0, "Do not use any type assertions.", "0"], + [0, 0, 0, "Do not use any type assertions.", "1"] ], "public/app/plugins/datasource/cloudwatch/components/MetricsQueryEditor/Alias.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], diff --git a/public/app/plugins/datasource/cloudwatch/components/LogsQueryField.tsx b/public/app/plugins/datasource/cloudwatch/components/LogsQueryField.tsx index 1fcc8b456ad..77c0fc7d7f2 100644 --- a/public/app/plugins/datasource/cloudwatch/components/LogsQueryField.tsx +++ b/public/app/plugins/datasource/cloudwatch/components/LogsQueryField.tsx @@ -1,4 +1,3 @@ -import { css, cx } from '@emotion/css'; import { LanguageMap, languages as prismLanguages } from 'prismjs'; import React, { ReactNode } from 'react'; import { Node, Plugin } from 'slate'; @@ -9,16 +8,15 @@ import { BracesPlugin, QueryField, SlatePrism, + Themeable2, TypeaheadInput, TypeaheadOutput, - Themeable2, withTheme2, - clearButtonStyles, } from '@grafana/ui'; import { ExploreId } from 'app/types'; + // Utils & Services // dom also includes Element polyfills - import { CloudWatchDatasource } from '../datasource'; import { CloudWatchLanguageProvider } from '../language_provider'; import syntax from '../syntax'; @@ -37,60 +35,33 @@ export interface CloudWatchLogsQueryFieldProps exploreId: ExploreId; query: CloudWatchLogsQuery; } +const plugins: Array> = [ + BracesPlugin(), + SlatePrism( + { + onlyIn: (node: Node) => node.object === 'block' && node.type === 'code_block', + getSyntax: (node: Node) => 'cloudwatch', + }, + { ...(prismLanguages as LanguageMap), cloudwatch: syntax } + ), +]; +export const CloudWatchLogsQueryField = (props: CloudWatchLogsQueryFieldProps) => { + const { query, datasource, onChange, onRunQuery, ExtraFieldElement, data } = props; -const addPaddingToButton = css` - padding: 1px 4px; -`; -interface State { - hint: - | { - message: string; - fix: { - label: string; - action: () => void; - }; - } - | undefined; -} + const showError = data?.error?.refId === query.refId; + const cleanText = datasource.languageProvider.cleanText; -class CloudWatchLogsQueryField extends React.PureComponent { - state: State = { - hint: undefined, - }; - - plugins: Array>; - - constructor(props: CloudWatchLogsQueryFieldProps, context: React.Context) { - super(props, context); - - this.plugins = [ - BracesPlugin(), - SlatePrism( - { - onlyIn: (node: Node) => node.object === 'block' && node.type === 'code_block', - getSyntax: (node: Node) => 'cloudwatch', - }, - { ...(prismLanguages as LanguageMap), cloudwatch: syntax } - ), - ]; - } - - onChangeQuery = (value: string) => { + const onChangeQuery = (value: string) => { // Send text change to parent - const { query, onChange } = this.props; - - if (onChange) { - const nextQuery = { - ...query, - expression: value, - statsGroups: getStatsGroups(value), - }; - onChange(nextQuery); - } + const nextQuery = { + ...query, + expression: value, + statsGroups: getStatsGroups(value), + }; + onChange(nextQuery); }; - onTypeahead = async (typeahead: TypeaheadInput): Promise => { - const { datasource, query } = this.props; + const onTypeahead = async (typeahead: TypeaheadInput): Promise => { const { logGroupNames } = query; if (!datasource.languageProvider) { @@ -98,7 +69,7 @@ class CloudWatchLogsQueryField extends React.PureComponent - - -
-
- -
- {ExtraFieldElement} + return ( + <> + + +
+
+
- {hint && ( -
-
- {hint.message} - -
-
- )} - {showError ? ( -
-
{data?.error?.message}
-
- ) : null} - - ); - } -} + {ExtraFieldElement} +
+ {showError ? ( +
+
{data?.error?.message}
+
+ ) : null} + + ); +}; export default withTheme2(CloudWatchLogsQueryField);