Skip to content

Commit

Permalink
[Embeddable Rebuild] [Discover] Fix bundle size (elastic#189206)
Browse files Browse the repository at this point in the history
## Summary

This PR is a follow up to elastic#180536,
where `8.8KB` was added to the Discover bundle size - now, after async
importing from the `presentation-publishing` package where necessary,
I've reduced that increase to `3.4KB` (a decrease of `5.4KB` from the
previous PR).

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
  • Loading branch information
Heenawter authored Jul 30, 2024
1 parent 4cf9d18 commit a35f773
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,15 @@
*/

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

import type { DiscoverAppLocator } from '../../../common';
import { PublishesSavedSearch, apiPublishesSavedSearch } from '../types';
import { getDiscoverLocatorParams } from '../utils/get_discover_locator_params';

export const ACTION_VIEW_SAVED_SEARCH = 'ACTION_VIEW_SAVED_SEARCH';

type ViewSavedSearchActionApi = CanAccessViewMode & HasType & PublishesSavedSearch;

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

export class ViewSavedSearchAction implements Action<EmbeddableApiContext> {
public id = ACTION_VIEW_SAVED_SEARCH;
public readonly type = ACTION_VIEW_SAVED_SEARCH;
Expand All @@ -51,6 +26,7 @@ export class ViewSavedSearchAction implements Action<EmbeddableApiContext> {
) {}

async execute({ embeddable }: EmbeddableApiContext): Promise<void> {
const { compatibilityCheck } = await import('./view_saved_search_compatibility_check');
if (!compatibilityCheck(embeddable)) {
return;
}
Expand All @@ -73,6 +49,9 @@ export class ViewSavedSearchAction implements Action<EmbeddableApiContext> {
const { capabilities } = this.application;
const hasDiscoverPermissions =
(capabilities.discover.show as boolean) || (capabilities.discover.save as boolean);
return compatibilityCheck(embeddable) && hasDiscoverPermissions;

if (!hasDiscoverPermissions) return false; // early return to delay async import until absolutely necessary
const { compatibilityCheck } = await import('./view_saved_search_compatibility_check');
return compatibilityCheck(embeddable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { SEARCH_EMBEDDABLE_TYPE } from '@kbn/discover-utils';
import { ViewMode } from '@kbn/embeddable-plugin/public';
import {
apiCanAccessViewMode,
apiHasType,
apiIsOfType,
CanAccessViewMode,
EmbeddableApiContext,
getInheritedViewMode,
HasType,
} from '@kbn/presentation-publishing';

import { apiPublishesSavedSearch, PublishesSavedSearch } from '../types';

type ViewSavedSearchActionApi = CanAccessViewMode & HasType & PublishesSavedSearch;

export const compatibilityCheck = (
api: EmbeddableApiContext['embeddable']
): api is ViewSavedSearchActionApi => {
return (
apiCanAccessViewMode(api) &&
getInheritedViewMode(api) === ViewMode.VIEW &&
apiHasType(api) &&
apiIsOfType(api, SEARCH_EMBEDDABLE_TYPE) &&
apiPublishesSavedSearch(api)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import type { Filter } from '@kbn/es-query';
import { PublishesSavedObjectId, PublishesUnifiedSearch } from '@kbn/presentation-publishing';
import type { PublishesSavedObjectId, PublishesUnifiedSearch } from '@kbn/presentation-publishing';
import { DiscoverAppLocatorParams } from '../../../common';
import { PublishesSavedSearch } from '../types';

Expand Down

0 comments on commit a35f773

Please sign in to comment.