Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Transforms: Deprecate custom KibanaContext. #59133

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { DefaultOperator } from 'elasticsearch';

import { dictionaryToArray } from '../../../common/types/common';
import { SavedSearchQuery } from '../lib/kibana';
import { SavedSearchQuery } from '../hooks/use_search_items';

import { StepDefineExposedState } from '../sections/create_transform/components/step_define/step_define_form';
import { StepDetailsExposedState } from '../sections/create_transform/components/step_details/step_details_form';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import React from 'react';
import { render } from '@testing-library/react';

import { KibanaContext } from '../lib/kibana';
import { createPublicShim } from '../../shim';
import { getAppProviders } from '../app_dependencies';

import { ToastNotificationText } from './toast_notification_text';

jest.mock('../../shared_imports');
jest.mock('ui/new_platform');

describe('ToastNotificationText', () => {
test('should render the text as plain text', () => {
Expand All @@ -23,9 +23,7 @@ describe('ToastNotificationText', () => {
};
const { container } = render(
<Providers>
<KibanaContext.Provider value={{ initialized: false }}>
<ToastNotificationText {...props} />
</KibanaContext.Provider>
<ToastNotificationText {...props} />
</Providers>
);
expect(container.textContent).toBe('a short text message');
Expand All @@ -39,9 +37,7 @@ describe('ToastNotificationText', () => {
};
const { container } = render(
<Providers>
<KibanaContext.Provider value={{ initialized: false }}>
<ToastNotificationText {...props} />
</KibanaContext.Provider>
<ToastNotificationText {...props} />
</Providers>
);
expect(container.textContent).toBe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {

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

export type SavedSearchQuery = object;

type IndexPatternId = string;
type SavedSearchId = string;

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

type CombinedQuery = Record<'bool', any> | unknown;
type CombinedQuery = Record<'bool', any> | object;

export function loadCurrentIndexPattern(
indexPatterns: IndexPatternsContract,
Expand All @@ -79,17 +81,20 @@ export function loadCurrentSavedSearch(savedSearches: any, savedSearchId: SavedS
function isIndexPattern(arg: any): arg is IndexPattern {
return arg !== undefined;
}

export interface SearchItems {
indexPattern: IndexPattern;
savedSearch: any;
query: any;
combinedQuery: CombinedQuery;
}

// Helper for creating the items used for searching and job creation.
export function createSearchItems(
indexPattern: IndexPattern | undefined,
savedSearch: any,
config: IUiSettingsClient
): {
indexPattern: IndexPattern;
savedSearch: any;
query: any;
combinedQuery: CombinedQuery;
} {
): SearchItems {
// query is only used by the data visualizer as it needs
// a lucene query_string.
// Using a blank query will cause match_all:{} to be used
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { SavedSearchQuery, SearchItems } from './common';
export { useSearchItems } from './use_search_items';
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,36 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React, { useEffect, useState, FC } from 'react';
import { useEffect, useState } from 'react';

import { createSavedSearchesLoader } from '../../../shared_imports';

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

import {
createSearchItems,
getIndexPatternIdByTitle,
loadCurrentIndexPattern,
loadIndexPatterns,
loadCurrentSavedSearch,
SearchItems,
} from './common';

import { InitializedKibanaContextValue, KibanaContext, KibanaContextValue } from './kibana_context';

interface Props {
savedObjectId: string;
}
export const useSearchItems = (defaultSavedObjectId: string | undefined) => {
const [savedObjectId, setSavedObjectId] = useState(defaultSavedObjectId);

export const KibanaProvider: FC<Props> = ({ savedObjectId, children }) => {
const appDeps = useAppDependencies();
const indexPatterns = appDeps.plugins.data.indexPatterns;
const uiSettings = appDeps.core.uiSettings;
const savedObjectsClient = appDeps.core.savedObjects.client;
const savedSearches = appDeps.plugins.savedSearches.getClient();
const savedSearches = createSavedSearchesLoader({
savedObjectsClient,
indexPatterns,
chrome: appDeps.core.chrome,
overlays: appDeps.core.overlays,
});

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

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

const kibanaConfig = appDeps.core.uiSettings;

const {
indexPattern: currentIndexPattern,
savedSearch: currentSavedSearch,
combinedQuery,
} = createSearchItems(fetchedIndexPattern, fetchedSavedSearch, kibanaConfig);

const kibanaContext: InitializedKibanaContextValue = {
indexPatterns,
initialized: true,
kibanaConfig,
combinedQuery,
currentIndexPattern,
currentSavedSearch,
};

setContextValue(kibanaContext);
setSearchItems(createSearchItems(fetchedIndexPattern, fetchedSavedSearch, uiSettings));
}

useEffect(() => {
fetchSavedObject(savedObjectId);
// fetchSavedObject should not be tracked.
if (savedObjectId !== undefined) {
fetchSavedObject(savedObjectId);
}
// Run this only when savedObjectId changes.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [savedObjectId]);

return <KibanaContext.Provider value={contextValue}>{children}</KibanaContext.Provider>;
return {
getIndexPatternIdByTitle,
loadIndexPatterns,
searchItems,
setSavedObjectId,
};
};
17 changes: 0 additions & 17 deletions x-pack/legacy/plugins/transform/public/app/lib/kibana/index.ts

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,14 @@ import {
} from '@elastic/eui';

import { useApi } from '../../hooks/use_api';
import { useSearchItems } from '../../hooks/use_search_items';

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

import { useAppDependencies, useDocumentationLinks } from '../../app_dependencies';
import { TransformPivotConfig } from '../../common';
import { breadcrumbService, docTitleService, BREADCRUMB_SECTION } from '../../services/navigation';
import { PrivilegesWrapper } from '../../lib/authorization';
import {
getIndexPatternIdByTitle,
loadIndexPatterns,
KibanaProvider,
RenderOnlyWithInitializedKibanaContext,
} from '../../lib/kibana';

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

Expand Down Expand Up @@ -80,7 +75,12 @@ export const CloneTransformSection: FC<Props> = ({ match }) => {
const [transformConfig, setTransformConfig] = useState<TransformPivotConfig>();
const [errorMessage, setErrorMessage] = useState();
const [isInitialized, setIsInitialized] = useState(false);
const [savedObjectId, setSavedObjectId] = useState<string | undefined>(undefined);
const {
getIndexPatternIdByTitle,
loadIndexPatterns,
searchItems,
setSavedObjectId,
} = useSearchItems(undefined);

const fetchTransformConfig = async () => {
try {
Expand Down Expand Up @@ -169,12 +169,8 @@ export const CloneTransformSection: FC<Props> = ({ match }) => {
<pre>{JSON.stringify(errorMessage)}</pre>
</EuiCallOut>
)}
{savedObjectId !== undefined && isInitialized === true && transformConfig !== undefined && (
<KibanaProvider savedObjectId={savedObjectId}>
<RenderOnlyWithInitializedKibanaContext>
<Wizard cloneConfig={transformConfig} />
</RenderOnlyWithInitializedKibanaContext>
</KibanaProvider>
{searchItems !== undefined && isInitialized === true && transformConfig !== undefined && (
<Wizard cloneConfig={transformConfig} searchItems={searchItems} />
)}
</EuiPageContentBody>
</EuiPageContent>
Expand Down
Loading