From f8af7ec644f8b10ece6ce9ffe4f92f5b2f8af8d4 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Thu, 19 Sep 2024 15:05:27 -0600 Subject: [PATCH] [dashboard] remove DashboardExternallyAccessibleApi and DashboardPluginInternalFunctions types (#193440) Consolidate to only exposing `DashboardApi` by removing `DashboardExternallyAccessibleApi` and `DashboardPluginInternalFunctions` types. --------- Co-authored-by: Elastic Machine (cherry picked from commit 885dfe30173fe15120f957cf6cbd836404c355f9) --- .../page_api.ts | 4 -- .../presentation_container_example/types.ts | 5 +-- .../copy_to_dashboard_action.tsx | 6 ++- .../filters_notification_action.test.tsx | 4 -- .../filters_notification_action.tsx | 4 +- .../filters_notification_popover.test.tsx | 9 ----- .../filters_notification_popover.tsx | 10 +++-- .../dashboard/public/dashboard_api/types.ts | 3 +- .../embeddable/dashboard_container.tsx | 14 +------ .../external_api/dashboard_api.ts | 40 ------------------- 10 files changed, 17 insertions(+), 82 deletions(-) delete mode 100644 src/plugins/dashboard/public/dashboard_container/external_api/dashboard_api.ts diff --git a/examples/embeddable_examples/public/app/presentation_container_example/page_api.ts b/examples/embeddable_examples/public/app/presentation_container_example/page_api.ts index a2e4114bf59a86..517f0534993ef6 100644 --- a/examples/embeddable_examples/public/app/presentation_container_example/page_api.ts +++ b/examples/embeddable_examples/public/app/presentation_container_example/page_api.ts @@ -198,10 +198,6 @@ export function getPageApi() { executionContext: { type: 'presentationContainerEmbeddableExample', }, - getAllDataViews: () => { - // TODO remove once dashboard converted to API and use `PublishesDataViews` interface - return []; - }, getPanelCount: () => { return panels$.value.length; }, diff --git a/examples/embeddable_examples/public/app/presentation_container_example/types.ts b/examples/embeddable_examples/public/app/presentation_container_example/types.ts index 66ac415ffebbe5..c3530ba71dbcff 100644 --- a/examples/embeddable_examples/public/app/presentation_container_example/types.ts +++ b/examples/embeddable_examples/public/app/presentation_container_example/types.ts @@ -7,7 +7,6 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import type { DataView } from '@kbn/data-views-plugin/public'; import { TimeRange } from '@kbn/es-query'; import { CanAddNewPanel, @@ -36,9 +35,7 @@ export type PageApi = PresentationContainer & PublishesViewMode & PublishesReload & PublishesTimeRange & - PublishesUnsavedChanges & { - getAllDataViews: () => DataView[]; - }; + PublishesUnsavedChanges; export interface LastSavedState { timeRange: TimeRange; diff --git a/src/plugins/dashboard/public/dashboard_actions/copy_to_dashboard_action.tsx b/src/plugins/dashboard/public/dashboard_actions/copy_to_dashboard_action.tsx index 735cf767a1a549..83762f407f4233 100644 --- a/src/plugins/dashboard/public/dashboard_actions/copy_to_dashboard_action.tsx +++ b/src/plugins/dashboard/public/dashboard_actions/copy_to_dashboard_action.tsx @@ -25,7 +25,7 @@ import { toMountPoint } from '@kbn/react-kibana-mount'; import { Action, IncompatibleActionError } from '@kbn/ui-actions-plugin/public'; import { DASHBOARD_CONTAINER_TYPE } from '../dashboard_container'; -import { DashboardPluginInternalFunctions } from '../dashboard_container/external_api/dashboard_api'; +import { DashboardApi } from '../dashboard_api/types'; import { pluginServices } from '../services/plugin_services'; import { CopyToDashboardModal } from './copy_to_dashboard_modal'; import { dashboardCopyToDashboardActionStrings } from './_dashboard_actions_strings'; @@ -41,7 +41,7 @@ export type CopyToDashboardAPI = HasType & HasUniqueId & HasParentApi< { type: typeof DASHBOARD_CONTAINER_TYPE } & PublishesSavedObjectId & - DashboardPluginInternalFunctions + Pick >; const apiIsCompatible = (api: unknown): api is CopyToDashboardAPI => { @@ -49,6 +49,8 @@ const apiIsCompatible = (api: unknown): api is CopyToDashboardAPI => { apiHasUniqueId(api) && apiHasParentApi(api) && apiIsOfType(api.parentApi, DASHBOARD_CONTAINER_TYPE) && + (api?.parentApi as unknown as Pick) + ?.getDashboardPanelFromId !== undefined && apiPublishesSavedObjectId(api.parentApi) ); }; diff --git a/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.test.tsx b/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.test.tsx index 884980c74b8ace..29b03539790733 100644 --- a/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.test.tsx +++ b/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.test.tsx @@ -58,10 +58,6 @@ describe('filters notification action', () => { embeddable: { uuid: 'testId', viewMode: viewModeSubject, - parentApi: { - getAllDataViews: jest.fn(), - getDashboardPanelFromId: jest.fn(), - }, filters$: filtersSubject, query$: querySubject, }, diff --git a/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.tsx b/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.tsx index 0240c83930a38b..ef179c006666fa 100644 --- a/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.tsx +++ b/src/plugins/dashboard/public/dashboard_actions/filters_notification_action.tsx @@ -24,9 +24,9 @@ import { HasParentApi, PublishesUnifiedSearch, HasUniqueId, + PublishesDataViews, } from '@kbn/presentation-publishing'; import { merge } from 'rxjs'; -import { DashboardPluginInternalFunctions } from '../dashboard_container/external_api/dashboard_api'; import { pluginServices } from '../services/plugin_services'; import { FiltersNotificationPopover } from './filters_notification_popover'; import { dashboardFilterNotificationActionStrings } from './_dashboard_actions_strings'; @@ -36,7 +36,7 @@ export const BADGE_FILTERS_NOTIFICATION = 'ACTION_FILTERS_NOTIFICATION'; export type FiltersNotificationActionApi = HasUniqueId & CanAccessViewMode & Partial & - HasParentApi; + Partial>>; const isApiCompatible = (api: unknown | null): api is FiltersNotificationActionApi => Boolean( diff --git a/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.test.tsx b/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.test.tsx index 3fffb80ccea098..b02443f01aaa8a 100644 --- a/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.test.tsx +++ b/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.test.tsx @@ -60,10 +60,6 @@ describe('filters notification popover', () => { api = { uuid: 'testId', viewMode: new BehaviorSubject('edit'), - parentApi: { - getAllDataViews: jest.fn(), - getDashboardPanelFromId: jest.fn(), - }, filters$: filtersSubject, query$: querySubject, }; @@ -79,11 +75,6 @@ describe('filters notification popover', () => { await waitForEuiPopoverOpen(); }; - it('calls get all dataviews from the parent', async () => { - render(); - expect(api.parentApi?.getAllDataViews).toHaveBeenCalled(); - }); - it('renders the filter section when given filters', async () => { updateFilters([getMockPhraseFilter('ay', 'oh')]); await renderAndOpenPopover(); diff --git a/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.tsx b/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.tsx index 05c76132fbcd98..bafd06297fe7e2 100644 --- a/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.tsx +++ b/src/plugins/dashboard/public/dashboard_actions/filters_notification_popover.tsx @@ -26,8 +26,10 @@ import { css } from '@emotion/react'; import { AggregateQuery, getAggregateQueryMode, isOfQueryType } from '@kbn/es-query'; import { getEditPanelAction } from '@kbn/presentation-panel-plugin/public'; import { FilterItems } from '@kbn/unified-search-plugin/public'; -import { FiltersNotificationActionApi } from './filters_notification_action'; +import { useStateFromPublishingSubject } from '@kbn/presentation-publishing'; +import { BehaviorSubject } from 'rxjs'; import { dashboardFilterNotificationActionStrings } from './_dashboard_actions_strings'; +import { FiltersNotificationActionApi } from './filters_notification_action'; export function FiltersNotificationPopover({ api }: { api: FiltersNotificationActionApi }) { const [isPopoverOpen, setIsPopoverOpen] = useState(false); @@ -57,7 +59,9 @@ export function FiltersNotificationPopover({ api }: { api: FiltersNotificationAc } }, [api, setDisableEditButton]); - const dataViews = useMemo(() => api.parentApi?.getAllDataViews(), [api]); + const dataViews = useStateFromPublishingSubject( + api.parentApi?.dataViews ? api.parentApi.dataViews : new BehaviorSubject(undefined) + ); return ( - + )} diff --git a/src/plugins/dashboard/public/dashboard_api/types.ts b/src/plugins/dashboard/public/dashboard_api/types.ts index 216081efbd8c89..4760d2fd953015 100644 --- a/src/plugins/dashboard/public/dashboard_api/types.ts +++ b/src/plugins/dashboard/public/dashboard_api/types.ts @@ -25,7 +25,7 @@ import { } from '@kbn/presentation-publishing'; import { ControlGroupApi } from '@kbn/controls-plugin/public'; import { Filter, Query, TimeRange } from '@kbn/es-query'; -import { DashboardPanelMap } from '../../common'; +import { DashboardPanelMap, DashboardPanelState } from '../../common'; import { SaveDashboardReturn } from '../services/dashboard_content_management/types'; export type DashboardApi = CanExpandPanels & @@ -44,6 +44,7 @@ export type DashboardApi = CanExpandPanels & fullScreenMode$: PublishingSubject; focusedPanelId$: PublishingSubject; forceRefresh: () => void; + getDashboardPanelFromId: (id: string) => Promise; getPanelsState: () => DashboardPanelMap; hasOverlays$: PublishingSubject; hasRunMigrations$: PublishingSubject; diff --git a/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx b/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx index b836b3a53a7262..32262c64d1d06f 100644 --- a/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx +++ b/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx @@ -78,7 +78,6 @@ import { pluginServices } from '../../services/plugin_services'; import { placePanel } from '../panel_placement'; import { runPanelPlacementStrategy } from '../panel_placement/place_new_panel_strategies'; import { DashboardViewport } from '../component/viewport/dashboard_viewport'; -import { DashboardExternallyAccessibleApi } from '../external_api/dashboard_api'; import { getDashboardPanelPlacementSetting } from '../panel_placement/panel_placement_registry'; import { dashboardContainerReducers } from '../state/dashboard_container_reducers'; import { getDiffingMiddleware } from '../state/diffing/dashboard_diffing_integration'; @@ -137,7 +136,6 @@ export const useDashboardContainer = (): DashboardContainer => { export class DashboardContainer extends Container implements - DashboardExternallyAccessibleApi, TrackContentfulRender, TracksQueryPerformance, HasSaveNotification, @@ -174,7 +172,6 @@ export class DashboardContainer private domNode?: HTMLElement; private overlayRef?: OverlayRef; - private allDataViews: DataView[] = []; // performance monitoring public lastLoadStartTime?: number; @@ -393,7 +390,7 @@ export class DashboardContainer }) ); - this.dataViews = new BehaviorSubject(this.getAllDataViews()); + this.dataViews = new BehaviorSubject([]); const query$ = new BehaviorSubject(this.getInput().query); this.query$ = query$; @@ -799,20 +796,11 @@ export class DashboardContainer dashboardContainerReady$.next(this); }; - /** - * Gets all the dataviews that are actively being used in the dashboard - * @returns An array of dataviews - */ - public getAllDataViews = () => { - return this.allDataViews; - }; - /** * Use this to set the dataviews that are used in the dashboard when they change/update * @param newDataViews The new array of dataviews that will overwrite the old dataviews array */ public setAllDataViews = (newDataViews: DataView[]) => { - this.allDataViews = newDataViews; (this.dataViews as BehaviorSubject).next(newDataViews); }; diff --git a/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_api.ts b/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_api.ts deleted file mode 100644 index 16f4347351fa70..00000000000000 --- a/src/plugins/dashboard/public/dashboard_container/external_api/dashboard_api.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 { DataView } from '@kbn/data-views-plugin/public'; -import { CanDuplicatePanels, CanExpandPanels, TracksOverlays } from '@kbn/presentation-containers'; -import { HasTypeDisplayName, PublishesSavedObjectId } from '@kbn/presentation-publishing'; -import { DashboardPanelState } from '../../../common'; -import { DashboardContainer } from '../embeddable/dashboard_container'; - -export const buildApiFromDashboardContainer = (container?: DashboardContainer) => container ?? null; - -export type DashboardExternallyAccessibleApi = HasTypeDisplayName & - CanDuplicatePanels & - TracksOverlays & - PublishesSavedObjectId & - DashboardPluginInternalFunctions & - CanExpandPanels; - -/** - * An interface that holds types for the methods that Dashboard publishes which should not be used - * outside of the Dashboard plugin. This is necessary for some actions which reside in the Dashboard plugin. - */ -export interface DashboardPluginInternalFunctions { - /** - * A temporary backdoor to allow some actions access to the Dashboard panels. This should eventually be replaced with a generic version - * on the PresentationContainer interface. - */ - getDashboardPanelFromId: (id: string) => Promise; - - /** - * A temporary backdoor to allow the filters notification popover to get the data views directly from the dashboard container - */ - getAllDataViews: () => DataView[]; -}