Skip to content

Commit

Permalink
Merge existing embeddable input with incoming embeddable input
Browse files Browse the repository at this point in the history
  • Loading branch information
cqliu1 committed Oct 25, 2021
1 parent 151ceac commit eecc31c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { fromExpression } from '@kbn/interpreter/common';
import { CANVAS_APP } from '../../../../common/lib';
import { encode } from '../../../../common/lib/embeddable_dataurl';
import { decode, encode } from '../../../../common/lib/embeddable_dataurl';
import { CanvasElement, CanvasPage } from '../../../../types';
import { useEmbeddablesService, useLabsService } from '../../../services';
// @ts-expect-error unconverted file
import { addElement } from '../../../state/actions/elements';
Expand All @@ -21,7 +23,7 @@ import {
} from '../../../state/actions/embeddable';
import { clearValue } from '../../../state/actions/resolved_args';

export const useIncomingEmbeddable = (pageId: string) => {
export const useIncomingEmbeddable = (selectedPage: CanvasPage) => {
const embeddablesService = useEmbeddablesService();
const labsService = useLabsService();
const dispatch = useDispatch();
Expand All @@ -33,31 +35,52 @@ export const useIncomingEmbeddable = (pageId: string) => {

useEffect(() => {
if (isByValueEnabled && incomingEmbeddable) {
const { embeddableId, input, type } = incomingEmbeddable;
const { embeddableId, input: incomingInput, type } = incomingEmbeddable;

const config = encode(input);
const expression = `embeddable config="${config}"
type="${type}"
| render`;
// retrieve existing element
const originalElement = selectedPage.elements.find(
({ id }: CanvasElement) => id === embeddableId
);

if (originalElement) {
const originalAst = fromExpression(originalElement!.expression);

const functionIndex = originalAst.chain.findIndex(
({ function: fn }) => fn === 'embeddable'
);

const originalInput = decode(
originalAst.chain[functionIndex].arguments.config[0] as string
);

if (embeddableId) {
// clear out resolved arg for old embeddable
const argumentPath = [embeddableId, 'expressionRenderable'];
dispatch(clearValue({ path: argumentPath }));

// update existing embeddable expression
const updatedInput = { ...originalInput, ...incomingInput };

const expression = `embeddable config="${encode(updatedInput)}"
type="${type}"
| render`;

dispatch(
updateEmbeddableExpression({ elementId: embeddableId, embeddableExpression: expression })
updateEmbeddableExpression({
elementId: originalElement.id,
embeddableExpression: expression,
})
);

// update resolved args
dispatch(fetchEmbeddableRenderable(embeddableId));
dispatch(fetchEmbeddableRenderable(originalElement.id));

// select new embeddable element
dispatch(selectToplevelNodes([embeddableId]));
} else {
dispatch(addElement(pageId, { expression }));
const expression = `embeddable config="${encode(incomingInput)}"
type="${type}"
| render`;
dispatch(addElement(selectedPage.id, { expression }));
}
}
}, [dispatch, pageId, incomingEmbeddable, isByValueEnabled]);
}, [dispatch, selectedPage, incomingEmbeddable, isByValueEnabled]);
};
4 changes: 2 additions & 2 deletions x-pack/plugins/canvas/public/components/workpad/workpad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export const Workpad: FC<ContainerProps> = (props) => {
};
});

const pageId = propsFromState.pages[propsFromState.selectedPageNumber - 1].id;
useIncomingEmbeddable(pageId);
const selectedPage = propsFromState.pages[propsFromState.selectedPageNumber - 1];
useIncomingEmbeddable(selectedPage);

const fetchAllRenderables = useCallback(() => {
dispatch(fetchAllRenderablesAction());
Expand Down

0 comments on commit eecc31c

Please sign in to comment.