Skip to content

Commit 98524a3

Browse files
committed
Move datasourceversion fetching into custom reusable hook
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent 41cd4e7 commit 98524a3

File tree

4 files changed

+33
-43
lines changed

4 files changed

+33
-43
lines changed

public/pages/workflow_detail/tools/query/query.tsx

+2-12
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ import {
3636
containsEmptyValues,
3737
containsSameValues,
3838
getDataSourceId,
39-
getEffectiveVersion,
4039
getPlaceholdersFromQuery,
4140
getSearchPipelineErrors,
4241
injectParameters,
42+
useDataSourceVersion,
4343
} from '../../../../utils';
4444
import { QueryParamsList, Results } from '../../../../general_components';
4545

@@ -65,17 +65,7 @@ const SEARCH_OPTIONS = [
6565
export function Query(props: QueryProps) {
6666
const dispatch = useAppDispatch();
6767
const dataSourceId = getDataSourceId();
68-
const [dataSourceVersion, setDataSourceVersion] = useState<
69-
string | undefined
70-
>(undefined);
71-
useEffect(() => {
72-
async function getVersion() {
73-
if (dataSourceId !== undefined) {
74-
setDataSourceVersion(await getEffectiveVersion(dataSourceId));
75-
}
76-
}
77-
getVersion();
78-
}, [dataSourceId]);
68+
const dataSourceVersion = useDataSourceVersion(dataSourceId);
7969

8070
const { loading } = useSelector((state: AppState) => state.opensearch);
8171

public/pages/workflow_detail/workflow_inputs/workflow_inputs.tsx

+4-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import React, { useEffect, useState } from 'react';
77
import { getIn, useFormikContext } from 'formik';
88
import { isEmpty, isEqual } from 'lodash';
9-
import semver from 'semver';
109
import {
1110
EuiSmallButton,
1211
EuiSmallButtonEmpty,
@@ -25,7 +24,6 @@ import {
2524
import {
2625
CONFIG_STEP,
2726
CachedFormikState,
28-
MINIMUM_FULL_SUPPORTED_VERSION,
2927
SimulateIngestPipelineResponseVerbose,
3028
TemplateNode,
3129
WORKFLOW_STEP_TYPE,
@@ -59,8 +57,9 @@ import {
5957
getDataSourceId,
6058
prepareDocsForSimulate,
6159
getIngestPipelineErrors,
62-
getEffectiveVersion,
6360
sleep,
61+
useDataSourceVersion,
62+
getIsPreV219,
6463
} from '../../../utils';
6564
import { BooleanField } from './input_fields';
6665
import '../workspace/workspace-styles.scss';
@@ -102,21 +101,8 @@ export function WorkflowInputs(props: WorkflowInputsProps) {
102101
} = useFormikContext<WorkflowFormValues>();
103102
const dispatch = useAppDispatch();
104103
const dataSourceId = getDataSourceId();
105-
const [dataSourceVersion, setDataSourceVersion] = useState<
106-
string | undefined
107-
>(undefined);
108-
useEffect(() => {
109-
async function getVersion() {
110-
if (dataSourceId !== undefined) {
111-
setDataSourceVersion(await getEffectiveVersion(dataSourceId));
112-
}
113-
}
114-
getVersion();
115-
}, [dataSourceId]);
116-
const isPreV219 =
117-
dataSourceVersion !== undefined
118-
? semver.lt(dataSourceVersion, MINIMUM_FULL_SUPPORTED_VERSION)
119-
: false;
104+
const dataSourceVersion = useDataSourceVersion(dataSourceId);
105+
const isPreV219 = getIsPreV219(dataSourceVersion);
120106

121107
// transient running states
122108
const [isUpdatingSearchPipeline, setIsUpdatingSearchPipeline] = useState<

public/pages/workflows/workflows.tsx

+2-12
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ import { DataSourceSelectableConfig } from '../../../../../src/plugins/data_sour
3838
import {
3939
dataSourceFilterFn,
4040
getDataSourceFromURL,
41-
getEffectiveVersion,
4241
isDataSourceReady,
42+
useDataSourceVersion,
4343
} from '../../utils/utils';
4444
import {
4545
getDataSourceManagementPlugin,
@@ -91,17 +91,7 @@ export function Workflows(props: WorkflowsProps) {
9191
const [dataSourceId, setDataSourceId] = useState<string | undefined>(
9292
queryParams.dataSourceId
9393
);
94-
const [dataSourceVersion, setDataSourceVersion] = useState<
95-
string | undefined
96-
>(undefined);
97-
useEffect(() => {
98-
async function getVersion() {
99-
if (dataSourceId !== undefined) {
100-
setDataSourceVersion(await getEffectiveVersion(dataSourceId));
101-
}
102-
}
103-
getVersion();
104-
}, [dataSourceId]);
94+
const dataSourceVersion = useDataSourceVersion(dataSourceId);
10595
const { workflows, loading } = useSelector(
10696
(state: AppState) => state.workflows
10797
);

public/utils/utils.tsx

+25-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import React, { ReactNode } from 'react';
6+
import React, { ReactNode, useEffect, useState } from 'react';
77
import yaml from 'js-yaml';
88
import jsonpath from 'jsonpath';
99
import { capitalize, escape, findKey, get, isEmpty, set, unset } from 'lodash';
@@ -43,6 +43,7 @@ import {
4343
MODEL_ID_PATTERN,
4444
WORKFLOW_TYPE,
4545
MIN_SUPPORTED_VERSION,
46+
MINIMUM_FULL_SUPPORTED_VERSION,
4647
} from '../../common';
4748
import {
4849
getCore,
@@ -528,6 +529,29 @@ export const getDataSourceId = () => {
528529
return mdsQueryParams.dataSourceId;
529530
};
530531

532+
export function useDataSourceVersion(
533+
dataSourceId: string | undefined
534+
): string | undefined {
535+
const [dataSourceVersion, setDataSourceVersion] = useState<
536+
string | undefined
537+
>(undefined);
538+
useEffect(() => {
539+
async function getVersion() {
540+
if (dataSourceId !== undefined) {
541+
setDataSourceVersion(await getEffectiveVersion(dataSourceId));
542+
}
543+
}
544+
getVersion();
545+
}, [dataSourceId]);
546+
return dataSourceVersion;
547+
}
548+
549+
export function getIsPreV219(dataSourceVersion: string | undefined): boolean {
550+
return dataSourceVersion !== undefined
551+
? semver.lt(dataSourceVersion, MINIMUM_FULL_SUPPORTED_VERSION)
552+
: false;
553+
}
554+
531555
export const isDataSourceReady = (dataSourceId?: string) => {
532556
const dataSourceEnabled = getDataSourceEnabled().enabled;
533557
return !dataSourceEnabled || (dataSourceId && dataSourceId !== '');

0 commit comments

Comments
 (0)