From f9b665401adbe94a37c5f9bbff0e9b6f33f5915f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Massot?= Date: Sun, 28 Jan 2024 10:57:39 +0100 Subject: [PATCH 1/2] Fix minified React error #185. --- .../BucketAggregationsEditor/state/reducer.test.ts | 4 ++-- .../QueryEditor/BucketAggregationsEditor/state/reducer.ts | 7 +++---- .../QueryEditor/ElasticsearchQueryContext.test.tsx | 6 +++--- src/components/QueryEditor/ElasticsearchQueryContext.tsx | 8 ++++---- .../MetricAggregationsEditor/MetricEditor.test.tsx | 2 +- .../QueryEditor/MetricAggregationsEditor/state/reducer.ts | 4 ++-- src/components/QueryEditor/index.tsx | 6 +++--- src/configuration/ConfigEditor.tsx | 4 ++-- src/datasource.ts | 2 -- src/hooks/useFields.test.tsx | 4 ++-- src/mocks.ts | 2 +- src/quickwit.ts | 2 +- src/test-helpers/render.tsx | 6 +++--- 13 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.test.ts b/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.test.ts index 3e7a011..961912e 100644 --- a/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.test.ts +++ b/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.test.ts @@ -1,4 +1,4 @@ -import { reducerTester } from 'dependencies/reducerTester'; +import { reducerTester } from './../../../../dependencies/reducerTester'; import { BucketAggregation, DateHistogram, ElasticsearchQuery } from '../../../../types'; import { changeMetricType } from '../../MetricAggregationsEditor/state/actions'; import { initQuery } from '../../state'; @@ -12,7 +12,7 @@ import { removeBucketAggregation, } from './actions'; import { createReducer } from './reducer'; -import { defaultBucketAgg } from 'queryDef'; +import { defaultBucketAgg } from './../../../../queryDef'; describe('Bucket Aggregations Reducer', () => { it('Should correctly add new aggregations', () => { diff --git a/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts b/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts index ec3aba5..2333733 100644 --- a/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts +++ b/src/components/QueryEditor/BucketAggregationsEditor/state/reducer.ts @@ -1,11 +1,11 @@ - import { Action } from '@reduxjs/toolkit'; + import { defaultBucketAgg } from '../../../../queryDef'; import { ElasticsearchQuery, Terms, BucketAggregation } from '../../../../types'; import { removeEmpty } from '../../../../utils'; import { changeMetricType } from '../../MetricAggregationsEditor/state/actions'; import { metricAggregationConfig } from '../../MetricAggregationsEditor/utils'; -import { initExploreQuery, initQuery } from '../../state'; +import { initQuery, initExploreQuery } from '../../state'; import { bucketAggregationConfig } from '../utils'; import { @@ -107,10 +107,9 @@ export const createReducer = } if (initQuery.match(action) || initExploreQuery.match(action)) { - if (state?.length || 0 > 0) { + if (state && state.length > 0) { return state; } - return [{ ...defaultBucketAgg('2'), field: defaultTimeField }]; } diff --git a/src/components/QueryEditor/ElasticsearchQueryContext.test.tsx b/src/components/QueryEditor/ElasticsearchQueryContext.test.tsx index 25c08ac..6704fbc 100644 --- a/src/components/QueryEditor/ElasticsearchQueryContext.test.tsx +++ b/src/components/QueryEditor/ElasticsearchQueryContext.test.tsx @@ -2,7 +2,7 @@ import { render } from '@testing-library/react'; import { renderHook } from '@testing-library/react-hooks'; import React, { PropsWithChildren } from 'react'; -import { getDefaultTimeRange } from '@grafana/data'; +import { CoreApp, getDefaultTimeRange } from '@grafana/data'; import { ElasticDatasource } from '../../datasource'; import { ElasticsearchQuery } from '../../types'; @@ -25,7 +25,7 @@ describe('ElasticsearchQueryContext', () => { render( { {}} onRunQuery={() => {}} range={getDefaultTimeRange()} diff --git a/src/components/QueryEditor/ElasticsearchQueryContext.tsx b/src/components/QueryEditor/ElasticsearchQueryContext.tsx index db8af06..04a0a3e 100644 --- a/src/components/QueryEditor/ElasticsearchQueryContext.tsx +++ b/src/components/QueryEditor/ElasticsearchQueryContext.tsx @@ -16,7 +16,7 @@ const RangeContext = createContext(undefined); interface Props { query: ElasticsearchQuery; - app: CoreApp | undefined; + app: CoreApp; onChange: (query: ElasticsearchQuery) => void; onRunQuery: () => void; datasource: ElasticDatasource; @@ -61,7 +61,7 @@ export const ElasticsearchProvider = ({ // This initializes the query by dispatching an init action to each reducer. // useStatelessReducer will then call `onChange` with the newly generated query useEffect(() => { - if (shouldRunInit) { + if (shouldRunInit && isUninitialized) { if (app === CoreApp.Explore) { dispatch(initExploreQuery()); } else { @@ -69,7 +69,7 @@ export const ElasticsearchProvider = ({ } setShouldRunInit(false); } - }, [shouldRunInit, dispatch, app]); + }, [shouldRunInit, dispatch, isUninitialized]); if (isUninitialized) { return null; @@ -97,7 +97,7 @@ const getHook: GetHook = (c) => () => { throw new Error('use ElasticsearchProvider first.'); } - return contextValue as NonNullable; + return contextValue; }; export const useQuery = getHook(QueryContext); diff --git a/src/components/QueryEditor/MetricAggregationsEditor/MetricEditor.test.tsx b/src/components/QueryEditor/MetricAggregationsEditor/MetricEditor.test.tsx index 20455cf..a48006b 100644 --- a/src/components/QueryEditor/MetricAggregationsEditor/MetricEditor.test.tsx +++ b/src/components/QueryEditor/MetricAggregationsEditor/MetricEditor.test.tsx @@ -33,7 +33,7 @@ describe('Metric Editor', () => { {}} onRunQuery={() => {}} diff --git a/src/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts b/src/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts index 493086f..e951826 100644 --- a/src/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts +++ b/src/components/QueryEditor/MetricAggregationsEditor/state/reducer.ts @@ -154,14 +154,14 @@ export const reducer = (state: ElasticsearchQuery['metrics'], action: Action): E } if (initQuery.match(action)) { - if (state?.length || 0 > 0) { + if (state && state.length > 0) { return state; } return [defaultMetricAgg('1')]; } if (initExploreQuery.match(action)) { - if (state?.length || 0 > 0) { + if (state && state.length > 0) { return state; } return [defaultLogsAgg('3')]; diff --git a/src/components/QueryEditor/index.tsx b/src/components/QueryEditor/index.tsx index 67ec64b..63cd7e2 100644 --- a/src/components/QueryEditor/index.tsx +++ b/src/components/QueryEditor/index.tsx @@ -2,7 +2,7 @@ import { css } from '@emotion/css'; import React from 'react'; -import { getDefaultTimeRange, GrafanaTheme2, QueryEditorProps } from '@grafana/data'; +import { CoreApp, getDefaultTimeRange, GrafanaTheme2, QueryEditorProps } from '@grafana/data'; import { InlineLabel, QueryField, useStyles2 } from '@grafana/ui'; import { ElasticDatasource } from '../../datasource'; @@ -15,7 +15,7 @@ import { ElasticsearchProvider } from './ElasticsearchQueryContext'; import { MetricAggregationsEditor } from './MetricAggregationsEditor'; import { metricAggregationConfig } from './MetricAggregationsEditor/utils'; import { changeQuery } from './state'; -import { QuickwitOptions } from 'quickwit'; +import { QuickwitOptions } from '../../quickwit'; import { QueryTypeSelector } from './QueryTypeSelector'; export type ElasticQueryEditorProps = QueryEditorProps; @@ -25,9 +25,9 @@ export const QueryEditor = ({ query, onChange, onRunQuery, datasource, range, ap diff --git a/src/configuration/ConfigEditor.tsx b/src/configuration/ConfigEditor.tsx index f756d3c..7053ee3 100644 --- a/src/configuration/ConfigEditor.tsx +++ b/src/configuration/ConfigEditor.tsx @@ -1,9 +1,9 @@ import React, { useCallback } from 'react'; import { DataSourceHttpSettings, Input, InlineField, FieldSet } from '@grafana/ui'; import { DataSourcePluginOptionsEditorProps, DataSourceSettings } from '@grafana/data'; -import { QuickwitOptions } from 'quickwit'; +import { QuickwitOptions } from '../quickwit'; import { coerceOptions } from './utils'; -import { Divider } from 'components/Divider'; +import { Divider } from '../components/Divider'; import { DataLinks } from './DataLinks'; interface Props extends DataSourcePluginOptionsEditorProps {} diff --git a/src/datasource.ts b/src/datasource.ts index de9fa09..81ae5d2 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -413,7 +413,6 @@ export class QuickwitDataSource * Get tag keys for adhoc filters */ getTagKeys() { - console.log("getTagKeys"); return lastValueFrom(this.getFields()); } @@ -558,7 +557,6 @@ export class QuickwitDataSource } metricFindQuery(query: string, options?: { range: TimeRange }): Promise { - console.log("metricFindQuery"); const range = options?.range; const parsedQuery = JSON.parse(query); if (query) { diff --git a/src/hooks/useFields.test.tsx b/src/hooks/useFields.test.tsx index 5ccc29e..aa801e0 100644 --- a/src/hooks/useFields.test.tsx +++ b/src/hooks/useFields.test.tsx @@ -1,7 +1,7 @@ import React, { PropsWithChildren } from 'react'; import { from } from 'rxjs'; -import { getDefaultTimeRange } from '@grafana/data'; +import { CoreApp, getDefaultTimeRange } from '@grafana/data'; import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext'; import { ElasticDatasource } from '../datasource'; @@ -29,7 +29,7 @@ describe('useFields hook', () => { {}} onRunQuery={() => {}} diff --git a/src/mocks.ts b/src/mocks.ts index 349c4b5..097d01f 100644 --- a/src/mocks.ts +++ b/src/mocks.ts @@ -1,7 +1,7 @@ import { DataSourceInstanceSettings, PluginType } from '@grafana/data'; import { QuickwitDataSource } from './datasource'; -import { QuickwitOptions } from 'quickwit'; +import { QuickwitOptions } from './quickwit'; export function createElasticDatasource( settings: Partial> = {}, diff --git a/src/quickwit.ts b/src/quickwit.ts index 7688a65..fa886db 100644 --- a/src/quickwit.ts +++ b/src/quickwit.ts @@ -1,5 +1,5 @@ import { DataSourceJsonData } from "@grafana/data"; -import { DataLinkConfig } from "types"; +import { DataLinkConfig } from "./types"; export interface QuickwitOptions extends DataSourceJsonData { timeField: string; diff --git a/src/test-helpers/render.tsx b/src/test-helpers/render.tsx index 670f237..d5c5de1 100644 --- a/src/test-helpers/render.tsx +++ b/src/test-helpers/render.tsx @@ -1,10 +1,10 @@ import { render } from '@testing-library/react'; import React, { ComponentProps, ReactNode } from 'react'; -import { getDefaultTimeRange } from '@grafana/data'; +import { CoreApp, getDefaultTimeRange } from '@grafana/data'; import { ElasticDatasource } from '../datasource'; -import { ElasticsearchProvider } from 'components/QueryEditor/ElasticsearchQueryContext'; +import { ElasticsearchProvider } from '../components/QueryEditor/ElasticsearchQueryContext'; const defaultProviderProps = { datasource: {} as ElasticDatasource, @@ -32,7 +32,7 @@ export const renderWithESProvider = ( return render( Date: Sun, 28 Jan 2024 15:08:04 +0100 Subject: [PATCH 2/2] Fix lint. --- src/components/QueryEditor/ElasticsearchQueryContext.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/QueryEditor/ElasticsearchQueryContext.tsx b/src/components/QueryEditor/ElasticsearchQueryContext.tsx index 04a0a3e..6b45973 100644 --- a/src/components/QueryEditor/ElasticsearchQueryContext.tsx +++ b/src/components/QueryEditor/ElasticsearchQueryContext.tsx @@ -69,7 +69,7 @@ export const ElasticsearchProvider = ({ } setShouldRunInit(false); } - }, [shouldRunInit, dispatch, isUninitialized]); + }, [shouldRunInit, dispatch, isUninitialized, app]); if (isUninitialized) { return null;