Skip to content

Commit

Permalink
cleaning up embeddable types (elastic#75560)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Aug 25, 2020
1 parent b82e4d8 commit 40d8edc
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 33 deletions.
4 changes: 2 additions & 2 deletions examples/embeddable_examples/public/book/edit_book_action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export const createEditBookAction = (getStartServices: () => Promise<StartServic
const onSave = async (attributes: BookSavedObjectAttributes, useRefType: boolean) => {
const newInput = await attributeService.wrapAttributes(attributes, useRefType, embeddable);
if (!useRefType && (embeddable.getInput() as SavedObjectEmbeddableInput).savedObjectId) {
// Remove the savedObejctId when un-linking
newInput.savedObjectId = null;
// Set the saved object ID to null so that update input will remove the existing savedObjectId...
(newInput as BookByValueInput & { savedObjectId: unknown }).savedObjectId = null;
}
embeddable.updateInput(newInput);
if (useRefType) {
Expand Down
21 changes: 10 additions & 11 deletions examples/embeddable_explorer/public/list_container_example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,17 @@ import {
EuiText,
EuiTitle,
} from '@elastic/eui';
import {
EmbeddableInput,
EmbeddableRenderer,
ViewMode,
} from '../../../src/plugins/embeddable/public';
import { EmbeddableRenderer, ViewMode } from '../../../src/plugins/embeddable/public';
import {
HELLO_WORLD_EMBEDDABLE,
MULTI_TASK_TODO_EMBEDDABLE,
TODO_EMBEDDABLE,
ListContainerFactory,
SearchableListContainerFactory,
} from '../../embeddable_examples/public';
import { SearchableContainerInput } from '../../embeddable_examples/public/searchable_list_container/searchable_list_container';
import { TodoInput } from '../../embeddable_examples/public/todo';
import { MultiTaskTodoInput } from '../../embeddable_examples/public/multi_task_todo';

interface Props {
listContainerEmbeddableFactory: ListContainerFactory;
Expand All @@ -51,7 +50,7 @@ export function ListContainerExample({
listContainerEmbeddableFactory,
searchableListContainerEmbeddableFactory,
}: Props) {
const listInput: EmbeddableInput = {
const listInput: SearchableContainerInput = {
id: 'hello',
title: 'My todo list',
viewMode: ViewMode.VIEW,
Expand All @@ -69,20 +68,20 @@ export function ListContainerExample({
task: 'Goes out on Wednesdays!',
icon: 'broom',
title: 'Take out the trash',
},
} as TodoInput,
},
'3': {
type: TODO_EMBEDDABLE,
explicitInput: {
id: '3',
icon: 'broom',
title: 'Vaccum the floor',
},
} as TodoInput,
},
},
};

const searchableInput: EmbeddableInput = {
const searchableInput: SearchableContainerInput = {
id: '1',
title: 'My searchable todo list',
viewMode: ViewMode.VIEW,
Expand All @@ -101,7 +100,7 @@ export function ListContainerExample({
task: 'Goes out on Wednesdays!',
icon: 'broom',
title: 'Take out the trash',
},
} as TodoInput,
},
'3': {
type: MULTI_TASK_TODO_EMBEDDABLE,
Expand All @@ -110,7 +109,7 @@ export function ListContainerExample({
icon: 'searchProfilerApp',
title: 'Learn more',
tasks: ['Go to school', 'Watch planet earth', 'Read the encyclopedia'],
},
} as MultiTaskTodoInput,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/
import { isErrorEmbeddable, IContainer, ReferenceOrValueEmbeddable } from '../../embeddable_plugin';
import {
isErrorEmbeddable,
IContainer,
ReferenceOrValueEmbeddable,
EmbeddableInput,
} from '../../embeddable_plugin';
import { DashboardContainer } from '../embeddable';
import { getSampleDashboardInput } from '../test_helpers';
import {
Expand Down Expand Up @@ -145,7 +150,7 @@ test('Add to library returns reference type input', async () => {

embeddable = embeddablePluginMock.mockRefOrValEmbeddable<ContactCardEmbeddable>(embeddable, {
mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: embeddable.id },
mockedByValueInput: { attributes: complicatedAttributes, id: embeddable.id },
mockedByValueInput: { attributes: complicatedAttributes, id: embeddable.id } as EmbeddableInput,
});
const dashboard = embeddable.getRoot() as IContainer;
const originalPanelKeySet = new Set(Object.keys(dashboard.getInput().panels));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import _ from 'lodash';
import { ActionByType, IncompatibleActionError } from '../../ui_actions_plugin';
import { ViewMode, PanelState, IEmbeddable } from '../../embeddable_plugin';
import { SavedObject } from '../../../../saved_objects/public';
import { PanelNotFoundError, EmbeddableInput } from '../../../../embeddable/public';
import {
PanelNotFoundError,
EmbeddableInput,
SavedObjectEmbeddableInput,
} from '../../../../embeddable/public';
import {
placePanelBeside,
IPanelPlacementBesideArgs,
Expand Down Expand Up @@ -143,7 +147,7 @@ export class ClonePanelAction implements ActionByType<typeof ACTION_CLONE_PANEL>
},
{ references: _.cloneDeep(savedObjectToClone.references) }
);
panelState.explicitInput.savedObjectId = clonedSavedObject.id;
(panelState.explicitInput as SavedObjectEmbeddableInput).savedObjectId = clonedSavedObject.id;
}
this.core.notifications.toasts.addSuccess({
title: i18n.translate('dashboard.panel.clonedToast', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { coreMock } from '../../../../../core/public/mocks';
import { CoreStart } from 'kibana/public';
import { UnlinkFromLibraryAction } from '.';
import { embeddablePluginMock } from 'src/plugins/embeddable/public/mocks';
import { ViewMode } from '../../../../embeddable/public';
import { ViewMode, SavedObjectEmbeddableInput } from '../../../../embeddable/public';

const { setup, doStart } = embeddablePluginMock.createInstance();
setup.registerEmbeddableFactory(
Expand Down Expand Up @@ -142,7 +142,11 @@ test('Unlink unwraps all attributes from savedObject', async () => {
attribute4: { nestedattribute: 'hello from the nest' },
};

embeddable = embeddablePluginMock.mockRefOrValEmbeddable<ContactCardEmbeddable>(embeddable, {
embeddable = embeddablePluginMock.mockRefOrValEmbeddable<
ContactCardEmbeddable,
{ attributes: unknown; id: string },
SavedObjectEmbeddableInput
>(embeddable, {
mockedByReferenceInput: { savedObjectId: 'testSavedObjectId', id: embeddable.id },
mockedByValueInput: { attributes: complicatedAttributes, id: embeddable.id },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ export class DashboardAppController {
: undefined;
container.addOrUpdateEmbeddable<EmbeddableInput>(
incomingEmbeddable.type,
explicitInput,
// This ugly solution is temporary - https://github.com/elastic/kibana/pull/70272 fixes this whole section
(explicitInput as unknown) as EmbeddableInput,
embeddableId
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from './embeddable_saved_object_converters';
import { SavedDashboardPanel } from '../../types';
import { DashboardPanelState } from '../embeddable';
import { EmbeddableInput } from '../../../../embeddable/public';

test('convertSavedDashboardPanelToPanelState', () => {
const savedDashboardPanel: SavedDashboardPanel = {
Expand Down Expand Up @@ -93,7 +94,7 @@ test('convertPanelStateToSavedDashboardPanel', () => {
something: 'hi!',
id: '123',
savedObjectId: 'savedObjectId',
},
} as EmbeddableInput,
type: 'search',
};

Expand Down Expand Up @@ -127,7 +128,7 @@ test('convertPanelStateToSavedDashboardPanel will not add an undefined id when n
explicitInput: {
id: '123',
something: 'hi!',
},
} as EmbeddableInput,
type: 'search',
};

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/embeddable/public/lib/containers/i_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
IEmbeddable,
} from '../embeddables';

export interface PanelState<E extends { id: string; [key: string]: unknown } = { id: string }> {
export interface PanelState<E extends EmbeddableInput & { id: string } = { id: string }> {
// The type of embeddable in this panel. Will be used to find the factory in which to
// load the embeddable.
type: string;
Expand All @@ -43,7 +43,7 @@ export interface ContainerOutput extends EmbeddableOutput {
export interface ContainerInput<PanelExplicitInput = {}> extends EmbeddableInput {
hidePanelTitles?: boolean;
panels: {
[key: string]: PanelState<PanelExplicitInput & { [id: string]: unknown } & { id: string }>;
[key: string]: PanelState<PanelExplicitInput & EmbeddableInput & { id: string }>;
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/plugins/embeddable/public/lib/embeddables/i_embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ export interface EmbeddableInput {
* Visualization filters used to narrow down results.
*/
filters?: Filter[];

[key: string]: unknown;
}

export interface EmbeddableOutput {
Expand Down
12 changes: 9 additions & 3 deletions src/plugins/embeddable/public/tests/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@

import * as Rx from 'rxjs';
import { skip } from 'rxjs/operators';
import { isErrorEmbeddable, EmbeddableOutput, ContainerInput, ViewMode } from '../lib';
import {
isErrorEmbeddable,
EmbeddableOutput,
ContainerInput,
ViewMode,
SavedObjectEmbeddableInput,
} from '../lib';
import {
FilterableEmbeddableInput,
FilterableEmbeddable,
Expand Down Expand Up @@ -648,7 +654,7 @@ test('container stores ErrorEmbeddables when a saved object cannot be found', as
panels: {
'123': {
type: 'vis',
explicitInput: { id: '123', savedObjectId: '456' },
explicitInput: { id: '123', savedObjectId: '456' } as SavedObjectEmbeddableInput,
},
},
viewMode: ViewMode.EDIT,
Expand All @@ -669,7 +675,7 @@ test('ErrorEmbeddables get updated when parent does', async (done) => {
panels: {
'123': {
type: 'vis',
explicitInput: { id: '123', savedObjectId: '456' },
explicitInput: { id: '123', savedObjectId: '456' } as SavedObjectEmbeddableInput,
},
},
viewMode: ViewMode.EDIT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import {
ExpressionRenderError,
} from '../../../../plugins/expressions/public';
import { buildPipeline } from '../legacy/build_pipeline';
import { Vis } from '../vis';
import { Vis, SerializedVis } from '../vis';
import { getExpressions, getUiActions } from '../services';
import { VIS_EVENT_TO_TRIGGER } from './events';
import { VisualizeEmbeddableFactoryDeps } from './visualize_embeddable_factory';
Expand All @@ -63,6 +63,7 @@ export interface VisualizeInput extends EmbeddableInput {
vis?: {
colors?: { [key: string]: string };
};
savedVis?: SerializedVis;
table?: unknown;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { StartServicesGetter } from '../../../../../../../src/plugins/kibana_utils/public';
import { StartDependencies } from '../../../plugin';
import { Config, FactoryContext } from './types';
import { SearchInput } from '../../../../../../../src/plugins/discover/public';

export interface Params {
start: StartServicesGetter<Pick<StartDependencies, 'data' | 'uiActionsEnhanced'>>;
Expand Down Expand Up @@ -89,7 +90,7 @@ export class DashboardToDashboardDrilldown
};

if (context.embeddable) {
const input = context.embeddable.getInput();
const input = context.embeddable.getInput() as Readonly<SearchInput>;
if (isQuery(input.query) && config.useCurrentFilters) state.query = input.query;

// if useCurrentDashboardDataRange is enabled, then preserve current time range
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/

import { Action } from '../../../../../../src/plugins/ui_actions/public';
import { DiscoverUrlGeneratorState } from '../../../../../../src/plugins/discover/public';
import {
DiscoverUrlGeneratorState,
SearchInput,
} from '../../../../../../src/plugins/discover/public';
import {
ApplyGlobalFilterActionContext,
esFilters,
Expand Down Expand Up @@ -59,7 +62,7 @@ export class ExploreDataChartAction extends AbstractExploreDataAction<ExploreDat
if (embeddable) {
state.indexPatternId = shared.getIndexPatterns(embeddable)[0] || undefined;

const input = embeddable.getInput();
const input = embeddable.getInput() as Readonly<SearchInput>;

if (input.timeRange && !state.timeRange) state.timeRange = input.timeRange;
if (input.query) state.query = input.query;
Expand Down

0 comments on commit 40d8edc

Please sign in to comment.