Skip to content

Commit

Permalink
Use Discover locator to generate URL (#124282)
Browse files Browse the repository at this point in the history
* use Discover locator to generate URL

* improve locator check

* do not import discover plugin

* allow for share plugin to be missing

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
vadimkibana and kibanamachine authored Feb 15, 2022
1 parent 181d04c commit ea059d4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { LocationDescriptorObject } from 'history';
import { HttpSetup } from 'kibana/public';

import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public';
import { sharePluginMock } from '../../../../../../src/plugins/share/public/mocks';
import {
notificationServiceMock,
docLinksServiceMock,
Expand Down Expand Up @@ -48,11 +49,7 @@ const appServices = {
notifications: notificationServiceMock.createSetupContract(),
history,
uiSettings: uiSettingsServiceMock.createSetupContract(),
urlGenerators: {
getUrlGenerator: jest.fn().mockReturnValue({
createUrl: jest.fn(),
}),
},
url: sharePluginMock.createStartContract().url,
fileUpload: {
getMaxBytes: jest.fn().mockReturnValue(100),
getMaxBytesFormatted: jest.fn().mockReturnValue('100'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';

import { EuiAccordion, EuiText, EuiSpacer, EuiLink } from '@elastic/eui';
import { UrlGeneratorsDefinition } from 'src/plugins/share/public';

import { useKibana } from '../../../../../../../../shared_imports';
import { useIsMounted } from '../../../../../use_is_mounted';
import { AddDocumentForm } from '../add_document_form';

import './add_docs_accordion.scss';

const DISCOVER_URL_GENERATOR_ID = 'DISCOVER_APP_URL_GENERATOR';

const i18nTexts = {
addDocumentsButton: i18n.translate(
'xpack.ingestPipelines.pipelineEditor.addDocumentsAccordion.addDocumentsButtonLabel',
Expand All @@ -46,34 +43,18 @@ export const AddDocumentsAccordion: FunctionComponent<Props> = ({ onAddDocuments

useEffect(() => {
const getDiscoverUrl = async (): Promise<void> => {
let isDeprecated: UrlGeneratorsDefinition<typeof DISCOVER_URL_GENERATOR_ID>['isDeprecated'];
let createUrl: UrlGeneratorsDefinition<typeof DISCOVER_URL_GENERATOR_ID>['createUrl'];

// This try/catch may not be necessary once
// https://github.com/elastic/kibana/issues/78344 is addressed
try {
({ isDeprecated, createUrl } =
services.urlGenerators.getUrlGenerator(DISCOVER_URL_GENERATOR_ID));
} catch (e) {
// Discover plugin is not enabled
const locator = services.share?.url.locators.get('DISCOVER_APP_LOCATOR');
if (!locator) {
setDiscoverLink(undefined);
return;
}

if (isDeprecated) {
setDiscoverLink(undefined);
return;
}

const discoverUrl = await createUrl({ indexPatternId: undefined });

if (isMounted.current) {
setDiscoverLink(discoverUrl);
}
const discoverUrl = await locator.getUrl({ indexPatternId: undefined });
if (!isMounted.current) return;
setDiscoverLink(discoverUrl);
};

getDiscoverUrl();
}, [isMounted, services.urlGenerators]);
}, [isMounted, services.share]);

return (
<EuiAccordion
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/ingest_pipelines/public/application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Observable } from 'rxjs';

import { NotificationsSetup, IUiSettingsClient, CoreTheme } from 'kibana/public';
import { ManagementAppMountParams } from 'src/plugins/management/public';
import { SharePluginStart } from 'src/plugins/share/public';
import type { SharePluginStart } from 'src/plugins/share/public';
import type { FileUploadPluginStart } from '../../../file_upload/public';
import { KibanaContextProvider, KibanaThemeProvider } from '../shared_imports';

Expand All @@ -38,7 +38,7 @@ export interface AppServices {
notifications: NotificationsSetup;
history: ManagementAppMountParams['history'];
uiSettings: IUiSettingsClient;
urlGenerators: SharePluginStart['urlGenerators'];
share: SharePluginStart;
fileUpload: FileUploadPluginStart;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function mountManagementSection(
notifications,
history,
uiSettings: coreStart.uiSettings,
urlGenerators: depsStart.share.urlGenerators,
share: depsStart.share,
fileUpload: depsStart.fileUpload,
};

Expand Down

0 comments on commit ea059d4

Please sign in to comment.