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

[discover] lazy load actions #211974

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion packages/kbn-optimizer/limits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pageLoadAssetSize:
dataViews: 65000
dataVisualizer: 30000
devTools: 38637
discover: 99999
discover: 41000
discoverEnhanced: 42730
discoverShared: 17111
embeddable: 24000
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export const ACTION_VIEW_SAVED_SEARCH = 'ACTION_VIEW_SAVED_SEARCH';
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { discoverServiceMock } from '../../__mocks__/services';
import { createStartContractMock } from '../../__mocks__/start_contract';
import { SearchEmbeddableApi } from '../types';
import { getDiscoverLocatorParams } from '../utils/get_discover_locator_params';
import { ViewSavedSearchAction } from './view_saved_search_action';
import { getViewDiscoverSessionAction } from './view_discover_session_action';

const applicationMock = createStartContractMock();
const services = discoverServiceMock;
Expand All @@ -37,30 +37,30 @@ jest

describe('view saved search action', () => {
it('is compatible when embeddable is of type saved search, in view mode && appropriate permissions are set', async () => {
const action = new ViewSavedSearchAction(applicationMock, services.locator);
expect(await action.isCompatible({ embeddable: compatibleEmbeddableApi })).toBe(true);
const action = getViewDiscoverSessionAction(applicationMock, services.locator);
expect(await action.isCompatible?.({ embeddable: compatibleEmbeddableApi })).toBe(true);
});

it('is not compatible when embeddable not of type saved search', async () => {
const action = new ViewSavedSearchAction(applicationMock, services.locator);
const action = getViewDiscoverSessionAction(applicationMock, services.locator);
expect(
await action.isCompatible({
await action.isCompatible?.({
embeddable: { ...compatibleEmbeddableApi, type: 'CONTACT_CARD_EMBEDDABLE' },
})
).toBe(false);
});

it('is not visible when in edit mode', async () => {
const action = new ViewSavedSearchAction(applicationMock, services.locator);
const action = getViewDiscoverSessionAction(applicationMock, services.locator);
expect(
await action.isCompatible({
await action.isCompatible?.({
embeddable: { ...compatibleEmbeddableApi, viewMode$: new BehaviorSubject(ViewMode.EDIT) },
})
).toBe(false);
});

it('execute navigates to a saved search', async () => {
const action = new ViewSavedSearchAction(applicationMock, services.locator);
const action = getViewDiscoverSessionAction(applicationMock, services.locator);
await new Promise((resolve) => setTimeout(resolve, 0));
await action.execute({ embeddable: compatibleEmbeddableApi });
expect(discoverServiceMock.locator.navigate).toHaveBeenCalledWith(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { ApplicationStart } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { apiCanAccessViewMode, getInheritedViewMode, type EmbeddableApiContext, apiIsOfType, CanAccessViewMode, HasType } from '@kbn/presentation-publishing';
import { IncompatibleActionError, type Action } from '@kbn/ui-actions-plugin/public';

import type { DiscoverAppLocator } from '../../../common';
import { getDiscoverLocatorParams } from '../utils/get_discover_locator_params';
import { ACTION_VIEW_SAVED_SEARCH } from './constants';
import { SEARCH_EMBEDDABLE_TYPE } from '@kbn/discover-utils';
import { PublishesSavedSearch, apiPublishesSavedSearch } from '../types';
import { ActionDefinition } from '@kbn/ui-actions-plugin/public/actions';

type ViewSavedSearchActionApi = CanAccessViewMode & HasType & PublishesSavedSearch;

export const compatibilityCheck = (
api: EmbeddableApiContext['embeddable']
): api is ViewSavedSearchActionApi => {
return (
apiCanAccessViewMode(api) &&
getInheritedViewMode(api) === 'view' &&
apiIsOfType(api, SEARCH_EMBEDDABLE_TYPE) &&
apiPublishesSavedSearch(api)
);
};

export function getViewDiscoverSessionAction(application: ApplicationStart, locator: DiscoverAppLocator) {
return {
id: ACTION_VIEW_SAVED_SEARCH,
type: ACTION_VIEW_SAVED_SEARCH,
order: 20, // Same order as ACTION_OPEN_IN_DISCOVER
execute: async ({ embeddable }: EmbeddableApiContext) => {
if (!compatibilityCheck(embeddable)) throw new IncompatibleActionError();

const locatorParams = getDiscoverLocatorParams(embeddable);
await locator.navigate(locatorParams);
},
getDisplayName: () => i18n.translate('discover.savedSearchEmbeddable.action.viewSavedSearch.displayName', {
defaultMessage: 'Open in Discover',
}),
getIconType: () => 'discoverApp',
isCompatible: async ({ embeddable }: EmbeddableApiContext) => {
const { capabilities } = application;
const hasDiscoverPermissions =
(capabilities.discover_v2.show as boolean) || (capabilities.discover_v2.save as boolean);
return hasDiscoverPermissions && compatibilityCheck(embeddable);
}
} as ActionDefinition<EmbeddableApiContext>
}

This file was deleted.

This file was deleted.

7 changes: 6 additions & 1 deletion src/platform/plugins/shared/discover/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@ export {
type SearchEmbeddableApi,
type NonPersistedDisplayOptions,
} from './embeddable';
export { loadSharingDataHelpers } from './utils';


export async function loadSharingDataHelpers() {
return await import('./utils/get_sharing_data');
}

export type { DiscoverServices } from './build_services';
9 changes: 5 additions & 4 deletions src/platform/plugins/shared/discover/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { i18n } from '@kbn/i18n';
import { PLUGIN_ID } from '../common';
import { registerFeature } from './register_feature';
import { buildServices, UrlTracker } from './build_services';
import { ViewSavedSearchAction } from './embeddable/actions/view_saved_search_action';
import { initializeKbnUrlTracking } from './utils/initialize_kbn_url_tracking';
import {
DiscoverContextAppLocator,
Expand Down Expand Up @@ -59,6 +58,7 @@ import { DataSourceProfileService } from './context_awareness/profiles/data_sour
import { DocumentProfileService } from './context_awareness/profiles/document_profile';
import { ProfilesManager } from './context_awareness/profiles_manager';
import { DiscoverEBTManager } from './services/discover_ebt_manager';
import { ACTION_VIEW_SAVED_SEARCH } from './embeddable/actions/constants';

/**
* Contains Discover, one of the oldest parts of Kibana
Expand Down Expand Up @@ -254,9 +254,10 @@ export class DiscoverPlugin
}

start(core: CoreStart, plugins: DiscoverStartPlugins): DiscoverStart {
const viewSavedSearchAction = new ViewSavedSearchAction(core.application, this.locator!);

plugins.uiActions.addTriggerAction('CONTEXT_MENU_TRIGGER', viewSavedSearchAction);
plugins.uiActions.addTriggerActionAsync('CONTEXT_MENU_TRIGGER', ACTION_VIEW_SAVED_SEARCH, async () => {
const { getViewDiscoverSessionAction } = await import('./embeddable/actions/view_discover_session_action');
return getViewDiscoverSessionAction(core.application, this.locator!);
});
plugins.uiActions.registerTrigger(SEARCH_EMBEDDABLE_CELL_ACTIONS_TRIGGER);
plugins.uiActions.registerTrigger(DISCOVER_CELL_ACTIONS_TRIGGER);

Expand Down
7 changes: 0 additions & 7 deletions src/platform/plugins/shared/discover/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,4 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved loadSharingDataHelpers to public/index.ts to avoid loading getSortForEmbeddable in page load bundle.

* Allows the getSharingData function to be lazy loadable
*/
export async function loadSharingDataHelpers() {
return await import('./get_sharing_data');
}

export { getSortForEmbeddable } from './sorting';