Skip to content

Commit ce9e887

Browse files
authored
[ML] Transforms: Fixed custom KibanaContext deprecation. (#59410)
- Deprecates the custom KibanaContext. - Where applicable dependencies provided via KibanaContext are now passed on via AppDependencies. - The main feature of KibanaContext was to populate index pattern and saved search information for the transform wizard. This is now provided via the useSearchItems() custom hook.
1 parent c42bd33 commit ce9e887

32 files changed

+763
-1016
lines changed

x-pack/legacy/plugins/transform/public/__mocks__/shared_imports.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
jest.mock('ui/new_platform');
88

9-
export function XJsonMode() {}
10-
export function setDependencyCache() {}
11-
export const useRequest = () => ({
9+
export const expandLiteralStrings = jest.fn();
10+
export const XJsonMode = jest.fn();
11+
export const setDependencyCache = jest.fn();
12+
export const useRequest = jest.fn(() => ({
1213
isLoading: false,
1314
error: null,
1415
data: undefined,
15-
});
16+
}));
1617
export { mlInMemoryTableBasicFactory } from '../../../ml/public/application/components/ml_in_memory_table';
1718
export const SORT_DIRECTION = { ASC: 'asc' };
19+
export const KqlFilterBar = jest.fn(() => null);

x-pack/legacy/plugins/transform/public/app/common/request.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { DefaultOperator } from 'elasticsearch';
88

99
import { dictionaryToArray } from '../../../common/types/common';
10-
import { SavedSearchQuery } from '../lib/kibana';
10+
import { SavedSearchQuery } from '../hooks/use_search_items';
1111

1212
import { StepDefineExposedState } from '../sections/create_transform/components/step_define/step_define_form';
1313
import { StepDetailsExposedState } from '../sections/create_transform/components/step_details/step_details_form';

x-pack/legacy/plugins/transform/public/app/components/toast_notification_text.test.tsx

+3-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import React from 'react';
88
import { render } from '@testing-library/react';
99

10-
import { KibanaContext } from '../lib/kibana';
1110
import { createPublicShim } from '../../shim';
1211
import { getAppProviders } from '../app_dependencies';
1312

1413
import { ToastNotificationText } from './toast_notification_text';
1514

1615
jest.mock('../../shared_imports');
16+
jest.mock('ui/new_platform');
1717

1818
describe('ToastNotificationText', () => {
1919
test('should render the text as plain text', () => {
@@ -23,9 +23,7 @@ describe('ToastNotificationText', () => {
2323
};
2424
const { container } = render(
2525
<Providers>
26-
<KibanaContext.Provider value={{ initialized: false }}>
27-
<ToastNotificationText {...props} />
28-
</KibanaContext.Provider>
26+
<ToastNotificationText {...props} />
2927
</Providers>
3028
);
3129
expect(container.textContent).toBe('a short text message');
@@ -39,9 +37,7 @@ describe('ToastNotificationText', () => {
3937
};
4038
const { container } = render(
4139
<Providers>
42-
<KibanaContext.Provider value={{ initialized: false }}>
43-
<ToastNotificationText {...props} />
44-
</KibanaContext.Provider>
40+
<ToastNotificationText {...props} />
4541
</Providers>
4642
);
4743
expect(container.textContent).toBe(

x-pack/legacy/plugins/transform/public/app/lib/kibana/common.ts x-pack/legacy/plugins/transform/public/app/hooks/use_search_items/common.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414

1515
import { matchAllQuery } from '../../common';
1616

17+
export type SavedSearchQuery = object;
18+
1719
type IndexPatternId = string;
1820
type SavedSearchId = string;
1921

@@ -60,7 +62,7 @@ export function getIndexPatternIdByTitle(indexPatternTitle: string): string | un
6062
return indexPatternCache.find(d => d?.attributes?.title === indexPatternTitle)?.id;
6163
}
6264

63-
type CombinedQuery = Record<'bool', any> | unknown;
65+
type CombinedQuery = Record<'bool', any> | object;
6466

6567
export function loadCurrentIndexPattern(
6668
indexPatterns: IndexPatternsContract,
@@ -79,17 +81,20 @@ export function loadCurrentSavedSearch(savedSearches: any, savedSearchId: SavedS
7981
function isIndexPattern(arg: any): arg is IndexPattern {
8082
return arg !== undefined;
8183
}
84+
85+
export interface SearchItems {
86+
indexPattern: IndexPattern;
87+
savedSearch: any;
88+
query: any;
89+
combinedQuery: CombinedQuery;
90+
}
91+
8292
// Helper for creating the items used for searching and job creation.
8393
export function createSearchItems(
8494
indexPattern: IndexPattern | undefined,
8595
savedSearch: any,
8696
config: IUiSettingsClient
87-
): {
88-
indexPattern: IndexPattern;
89-
savedSearch: any;
90-
query: any;
91-
combinedQuery: CombinedQuery;
92-
} {
97+
): SearchItems {
9398
// query is only used by the data visualizer as it needs
9499
// a lucene query_string.
95100
// Using a blank query will cause match_all:{} to be used
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export { SavedSearchQuery, SearchItems } from './common';
8+
export { useSearchItems } from './use_search_items';

x-pack/legacy/plugins/transform/public/app/lib/kibana/kibana_provider.tsx x-pack/legacy/plugins/transform/public/app/hooks/use_search_items/use_search_items.ts

+26-30
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,36 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import React, { useEffect, useState, FC } from 'react';
7+
import { useEffect, useState } from 'react';
8+
9+
import { createSavedSearchesLoader } from '../../../shared_imports';
810

911
import { useAppDependencies } from '../../app_dependencies';
1012

1113
import {
1214
createSearchItems,
15+
getIndexPatternIdByTitle,
1316
loadCurrentIndexPattern,
1417
loadIndexPatterns,
1518
loadCurrentSavedSearch,
19+
SearchItems,
1620
} from './common';
1721

18-
import { InitializedKibanaContextValue, KibanaContext, KibanaContextValue } from './kibana_context';
19-
20-
interface Props {
21-
savedObjectId: string;
22-
}
22+
export const useSearchItems = (defaultSavedObjectId: string | undefined) => {
23+
const [savedObjectId, setSavedObjectId] = useState(defaultSavedObjectId);
2324

24-
export const KibanaProvider: FC<Props> = ({ savedObjectId, children }) => {
2525
const appDeps = useAppDependencies();
2626
const indexPatterns = appDeps.plugins.data.indexPatterns;
27+
const uiSettings = appDeps.core.uiSettings;
2728
const savedObjectsClient = appDeps.core.savedObjects.client;
28-
const savedSearches = appDeps.plugins.savedSearches.getClient();
29+
const savedSearches = createSavedSearchesLoader({
30+
savedObjectsClient,
31+
indexPatterns,
32+
chrome: appDeps.core.chrome,
33+
overlays: appDeps.core.overlays,
34+
});
2935

30-
const [contextValue, setContextValue] = useState<KibanaContextValue>({ initialized: false });
36+
const [searchItems, setSearchItems] = useState<SearchItems | undefined>(undefined);
3137

3238
async function fetchSavedObject(id: string) {
3339
await loadIndexPatterns(savedObjectsClient, indexPatterns);
@@ -47,31 +53,21 @@ export const KibanaProvider: FC<Props> = ({ savedObjectId, children }) => {
4753
// Just let fetchedSavedSearch stay undefined in case it doesn't exist.
4854
}
4955

50-
const kibanaConfig = appDeps.core.uiSettings;
51-
52-
const {
53-
indexPattern: currentIndexPattern,
54-
savedSearch: currentSavedSearch,
55-
combinedQuery,
56-
} = createSearchItems(fetchedIndexPattern, fetchedSavedSearch, kibanaConfig);
57-
58-
const kibanaContext: InitializedKibanaContextValue = {
59-
indexPatterns,
60-
initialized: true,
61-
kibanaConfig,
62-
combinedQuery,
63-
currentIndexPattern,
64-
currentSavedSearch,
65-
};
66-
67-
setContextValue(kibanaContext);
56+
setSearchItems(createSearchItems(fetchedIndexPattern, fetchedSavedSearch, uiSettings));
6857
}
6958

7059
useEffect(() => {
71-
fetchSavedObject(savedObjectId);
72-
// fetchSavedObject should not be tracked.
60+
if (savedObjectId !== undefined) {
61+
fetchSavedObject(savedObjectId);
62+
}
63+
// Run this only when savedObjectId changes.
7364
// eslint-disable-next-line react-hooks/exhaustive-deps
7465
}, [savedObjectId]);
7566

76-
return <KibanaContext.Provider value={contextValue}>{children}</KibanaContext.Provider>;
67+
return {
68+
getIndexPatternIdByTitle,
69+
loadIndexPatterns,
70+
searchItems,
71+
setSavedObjectId,
72+
};
7773
};

x-pack/legacy/plugins/transform/public/app/lib/kibana/index.ts

-17
This file was deleted.

x-pack/legacy/plugins/transform/public/app/lib/kibana/kibana_context.tsx

-72
This file was deleted.

x-pack/legacy/plugins/transform/public/app/lib/kibana/use_current_index_pattern.ts

-19
This file was deleted.

x-pack/legacy/plugins/transform/public/app/sections/clone_transform/clone_transform_section.tsx

+9-13
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,14 @@ import {
2222
} from '@elastic/eui';
2323

2424
import { useApi } from '../../hooks/use_api';
25+
import { useSearchItems } from '../../hooks/use_search_items';
2526

2627
import { APP_CREATE_TRANSFORM_CLUSTER_PRIVILEGES } from '../../../../common/constants';
2728

2829
import { useAppDependencies, useDocumentationLinks } from '../../app_dependencies';
2930
import { TransformPivotConfig } from '../../common';
3031
import { breadcrumbService, docTitleService, BREADCRUMB_SECTION } from '../../services/navigation';
3132
import { PrivilegesWrapper } from '../../lib/authorization';
32-
import {
33-
getIndexPatternIdByTitle,
34-
loadIndexPatterns,
35-
KibanaProvider,
36-
RenderOnlyWithInitializedKibanaContext,
37-
} from '../../lib/kibana';
3833

3934
import { Wizard } from '../create_transform/components/wizard';
4035

@@ -80,7 +75,12 @@ export const CloneTransformSection: FC<Props> = ({ match }) => {
8075
const [transformConfig, setTransformConfig] = useState<TransformPivotConfig>();
8176
const [errorMessage, setErrorMessage] = useState();
8277
const [isInitialized, setIsInitialized] = useState(false);
83-
const [savedObjectId, setSavedObjectId] = useState<string | undefined>(undefined);
78+
const {
79+
getIndexPatternIdByTitle,
80+
loadIndexPatterns,
81+
searchItems,
82+
setSavedObjectId,
83+
} = useSearchItems(undefined);
8484

8585
const fetchTransformConfig = async () => {
8686
try {
@@ -169,12 +169,8 @@ export const CloneTransformSection: FC<Props> = ({ match }) => {
169169
<pre>{JSON.stringify(errorMessage)}</pre>
170170
</EuiCallOut>
171171
)}
172-
{savedObjectId !== undefined && isInitialized === true && transformConfig !== undefined && (
173-
<KibanaProvider savedObjectId={savedObjectId}>
174-
<RenderOnlyWithInitializedKibanaContext>
175-
<Wizard cloneConfig={transformConfig} />
176-
</RenderOnlyWithInitializedKibanaContext>
177-
</KibanaProvider>
172+
{searchItems !== undefined && isInitialized === true && transformConfig !== undefined && (
173+
<Wizard cloneConfig={transformConfig} searchItems={searchItems} />
178174
)}
179175
</EuiPageContentBody>
180176
</EuiPageContent>

x-pack/legacy/plugins/transform/public/app/sections/create_transform/components/source_index_preview/__snapshots__/source_index_preview.test.tsx.snap

-24
This file was deleted.

0 commit comments

Comments
 (0)