Skip to content

Commit

Permalink
[Links] Migrate Links panel to the React embeddable framework (elasti…
Browse files Browse the repository at this point in the history
…c#178670)

Fixes elastic#174970
Fixes elastic#186044

## Summary

Migrates the legacy Links embeddable to the React embeddable framework.

Additionally, the new embeddable factory now resolves the dashboards
info prior to rendering the Links component. Prior to this change, the
`DashboardLinkComponent` would be responsible for asynchronously loading
dashboards info and rendering a loading icon. This change reduces the
complexity of the `DashboardLinkComponent` as the resolved state is now
passed in as props.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Hannah Mudge <Heenawter@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 6, 2024
1 parent 7968b14 commit 6bb0e7c
Show file tree
Hide file tree
Showing 60 changed files with 1,770 additions and 1,626 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,19 @@ export class DashboardContainer
if (reactEmbeddableRegistryHasKey(panelPackage.panelType)) {
const newId = v4();

const getCustomPlacementSettingFunc = await getDashboardPanelPlacementSetting(
panelPackage.panelType
);

const customPlacementSettings = getCustomPlacementSettingFunc
? await getCustomPlacementSettingFunc(panelPackage.initialState)
: {};

const placementSettings = {
width: DEFAULT_PANEL_WIDTH,
height: DEFAULT_PANEL_HEIGHT,
strategy: PanelPlacementStrategy.findTopLeftMostOpenSpace,
...getDashboardPanelPlacementSetting(panelPackage.panelType)?.(panelPackage.initialState),
...customPlacementSettings,
};

const { width, height, strategy } = placementSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { panelPlacementStrings } from '../_dashboard_container_strings';

const registry = new Map<string, GetPanelPlacementSettings<object>>();

export const registerDashboardPanelPlacementSetting = (
export const registerDashboardPanelPlacementSetting = <SerializedState extends object = object>(
embeddableType: string,
getPanelPlacementSettings: GetPanelPlacementSettings
getPanelPlacementSettings: GetPanelPlacementSettings<SerializedState>
) => {
if (registry.has(embeddableType)) {
throw new Error(panelPlacementStrings.getPanelPlacementSettingsExistsError(embeddableType));
}
registry.set(embeddableType, getPanelPlacementSettings);
registry.set(embeddableType, getPanelPlacementSettings as GetPanelPlacementSettings<object>);
};

export const getDashboardPanelPlacementSetting = (embeddableType: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { EmbeddableInput } from '@kbn/embeddable-plugin/public';
import { MaybePromise } from '@kbn/utility-types';
import { DashboardPanelState } from '../../../common';
import { GridData } from '../../../common/content_management';
import { PanelPlacementStrategy } from '../../dashboard_constants';
Expand Down Expand Up @@ -40,4 +41,4 @@ export interface IProvidesLegacyPanelPlacementSettings<

export type GetPanelPlacementSettings<SerializedState extends object = object> = (
serializedState?: SerializedState
) => PanelPlacementSettings;
) => MaybePromise<PanelPlacementSettings>;
4 changes: 2 additions & 2 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ export interface DashboardStart {
locator?: DashboardAppLocator;
dashboardFeatureFlagConfig: DashboardFeatureFlagConfig;
findDashboardsService: () => Promise<FindDashboardsService>;
registerDashboardPanelPlacementSetting: (
registerDashboardPanelPlacementSetting: <SerializedState extends object = object>(
embeddableType: string,
getPanelPlacementSettings: GetPanelPlacementSettings
getPanelPlacementSettings: GetPanelPlacementSettings<SerializedState>
) => void;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
EmbeddableInput,
SavedObjectEmbeddableInput,
isSavedObjectEmbeddableInput,
EmbeddableFactoryNotFoundError,
EmbeddableFactory,
} from '..';

Expand Down Expand Up @@ -74,9 +73,7 @@ export class AttributeService<
) {
if (getEmbeddableFactory) {
const factory = getEmbeddableFactory(this.type);
if (!factory) {
throw new EmbeddableFactoryNotFoundError(this.type);
}

this.embeddableFactory = factory;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

type GetPanelPlacementSettings<StateType> = (serializedState: StateType) => {
width?: number;
height?: number;
strategy?: string;
};

const registry: Map<string, GetPanelPlacementSettings<any>> = new Map();

export const registerEmbeddablePlacementStrategy = <StateType>(
panelType: string,
getPanelPlacementSettings: GetPanelPlacementSettings<StateType>
) => {
if (registry.has(panelType)) {
throw new Error(`Embeddable placement for embeddable type ${panelType} already exists`);
}

registry.set(panelType, getPanelPlacementSettings);
};

export const getEmbeddablePlacementStrategy = (panelType: string) => {
return registry.get(panelType);
};
4 changes: 4 additions & 0 deletions src/plugins/links/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ export const APP_ICON = 'link';
export const APP_NAME = i18n.translate('links.visTypeAlias.title', {
defaultMessage: 'Links',
});

export const DISPLAY_NAME = i18n.translate('links.displayName', {
defaultMessage: 'links',
});
4 changes: 2 additions & 2 deletions src/plugins/links/common/content_management/v1/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {

export type LinksCrudTypes = ContentManagementCrudTypes<
LinksContentType,
LinksAttributes,
Omit<LinksAttributes, 'title'> & { title: string }, // saved object attributes always have a title
Pick<SavedObjectCreateOptions, 'references'>,
Pick<SavedObjectUpdateOptions, 'references'>,
{
Expand Down Expand Up @@ -60,7 +60,7 @@ export type LinksLayoutType = typeof LINKS_HORIZONTAL_LAYOUT | typeof LINKS_VERT

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type LinksAttributes = {
title: string;
title?: string;
description?: string;
links?: Link[];
layout?: LinksLayoutType;
Expand Down
64 changes: 0 additions & 64 deletions src/plugins/links/common/embeddable/extract.test.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/plugins/links/common/embeddable/extract.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/plugins/links/common/embeddable/index.ts

This file was deleted.

67 changes: 0 additions & 67 deletions src/plugins/links/common/embeddable/inject.test.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/plugins/links/common/embeddable/inject.ts

This file was deleted.

14 changes: 0 additions & 14 deletions src/plugins/links/common/embeddable/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/plugins/links/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* Side Public License, v 1.
*/

export { APP_ICON, APP_NAME, CONTENT_ID, LATEST_VERSION } from './constants';
export { APP_ICON, APP_NAME, CONTENT_ID, DISPLAY_NAME, LATEST_VERSION } from './constants';
Loading

0 comments on commit 6bb0e7c

Please sign in to comment.