Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

53 changes: 53 additions & 0 deletions x-pack/platform/plugins/shared/lens/common/references/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { cloneDeep } from 'lodash';

import type { Reference } from '@kbn/content-management-utils';

import type { LensSerializedState } from '../../public';

export const injectLensReferences = (
state: LensSerializedState,
references: Reference[] = []
): LensSerializedState => {
const clonedState = cloneDeep(state);

if (clonedState.savedObjectId || !clonedState.attributes) {
return clonedState;
}

// match references based on name, so only references associated with this lens panel are injected.
const matchedReferences: Reference[] = [];

if (Array.isArray(clonedState.attributes.references)) {
clonedState.attributes.references.forEach((serializableRef) => {
const internalReference = serializableRef;
const matchedReference = references.find(
(reference) => reference.name === internalReference.name
);
if (matchedReference) matchedReferences.push(matchedReference);
});
}

// Keep refs on attributes for now, need to clean this up.
clonedState.attributes.references = matchedReferences;

return clonedState;
};

export const extractLensReferences = (
state: LensSerializedState
): {
state: LensSerializedState;
references: Reference[];
} => {
return {
state,
references: state.references ?? state.attributes?.references ?? [],
};
};

This file was deleted.

11 changes: 10 additions & 1 deletion x-pack/platform/plugins/shared/lens/common/transforms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@
* 2.0.
*/

export { ConfigBuilderStub } from './config_builder_stub';
import type { LensTransforms } from './types';
import { getTransformIn } from './transform_in';
import { getTransformOut } from './transform_out';

export function getLensTransforms(): LensTransforms {
return {
transformIn: getTransformIn(),
transformOut: getTransformOut(),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { extractLensReferences } from '../references';
import type {
LensByRefTransformInResult,
LensByValueTransformInResult,
LensTransformIn,
} from './types';
import { isByRefLensState } from './utils';

/**
* Transform from Lens API format to Lens Serialized State
*/
export const getTransformIn = (): LensTransformIn => {
return function transformIn(state) {
if (isByRefLensState(state)) {
return {
state,
} satisfies LensByRefTransformInResult;
}

return extractLensReferences(state) satisfies LensByValueTransformInResult;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { LensByValueSerializedState } from '../../public/react_embeddable/types';
import { LENS_ITEM_VERSION_V1, transformToV1LensItemAttributes } from '../content_management/v1';
import { injectLensReferences } from '../references';
import type {
LensByRefTransformOutResult,
LensByValueTransformOutResult,
LensTransformOut,
} from './types';
import { isByRefLensState } from './utils';

/**
* Transform from Lens Serialized State to Lens API format
*/
export const getTransformOut = (): LensTransformOut => {
return function transformOut(state, references) {
if (isByRefLensState(state)) {
return {
...state,
} satisfies LensByRefTransformOutResult;
}

const migratedAttributes = migrateAttributes(state.attributes);
const injectedState = injectLensReferences(
{
...state,
attributes: migratedAttributes,
},
references
);

return injectedState satisfies LensByValueTransformOutResult;
};
};

/**
* Handles transforming old lens SO in dashboard to v1 Lens SO
*/
function migrateAttributes(attributes: LensByValueSerializedState['attributes']) {
if (!attributes) {
throw new Error('Why are attributes undefined?');
}

const { visualizationType } = attributes;

if (!visualizationType) {
throw new Error('Missing visualizationType');
}

const version = attributes.version ?? 0;

let newAttributes = { ...attributes };
if (version < LENS_ITEM_VERSION_V1) {
newAttributes = {
...newAttributes,
...transformToV1LensItemAttributes({ ...attributes, visualizationType }),
};
}

return newAttributes;
}
40 changes: 40 additions & 0 deletions x-pack/platform/plugins/shared/lens/common/transforms/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { EmbeddableTransforms } from '@kbn/embeddable-plugin/common';

import type {
LensSerializedState,
LensByRefSerializedState,
LensByValueSerializedState,
} from '../../public/react_embeddable/types';

export type LensTransforms = Required<
EmbeddableTransforms<LensSerializedState, LensSerializedState>
>;

/**
* Transform from Lens API format to Lens Serialized State
*/
export type LensTransformIn = LensTransforms['transformIn'];

/**
* Transform from to Lens Serialized State to Lens API format
*/
export type LensTransformOut = LensTransforms['transformOut'];

type LensByRefTransforms = Required<
EmbeddableTransforms<LensByRefSerializedState, LensByRefSerializedState>
>;
export type LensByRefTransformInResult = ReturnType<LensByRefTransforms['transformIn']>;
export type LensByRefTransformOutResult = ReturnType<LensByRefTransforms['transformOut']>;

type LensByValueTransforms = Required<
EmbeddableTransforms<LensByValueSerializedState, LensByValueSerializedState>
>;
export type LensByValueTransformInResult = ReturnType<LensByValueTransforms['transformIn']>;
export type LensByValueTransformOutResult = ReturnType<LensByValueTransforms['transformOut']>;
13 changes: 13 additions & 0 deletions x-pack/platform/plugins/shared/lens/common/transforms/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { LensSerializedState } from '../../public';
import type { LensByRefSerializedState } from '../../public/react_embeddable/types';

export function isByRefLensState(state: LensSerializedState): state is LensByRefSerializedState {
return !state.attributes;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
Storage,
withNotifyOnErrors,
} from '@kbn/kibana-utils-plugin/public';
import type { EmbeddableStateWithType } from '@kbn/embeddable-plugin/common';
import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public';
import { ACTION_VISUALIZE_LENS_FIELD } from '@kbn/ui-actions-plugin/public';
import { ACTION_CONVERT_TO_LENS } from '@kbn/visualizations-plugin/public';
Expand All @@ -37,7 +36,7 @@ import type {
} from '../types';
import { addHelpMenuToAppChrome } from '../help_menu_util';
import type { LensPluginStartDependencies } from '../plugin';
import { extract } from '../../common/embeddable_factory';
import { extractLensReferences } from '../../common/references';
import { LENS_EMBEDDABLE_TYPE, LENS_EDIT_BY_VALUE, APP_ID } from '../../common/constants';
import type { LensAttributesService } from '../lens_attribute_service';
import type { LensAppServices, RedirectToOriginProps, HistoryLocationState } from './types';
Expand Down Expand Up @@ -216,7 +215,7 @@ export async function mountApp(
}
if (stateTransfer && props?.state) {
const { state: rawState, isCopied } = props;
const { references } = extract(rawState as unknown as EmbeddableStateWithType);
const { references } = extractLensReferences(rawState);
stateTransfer.navigateToWithEmbeddablePackage<LensSerializedState>(mergedOriginatingApp, {
path: embeddableEditorIncomingState?.originatingPath,
state: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import {
DEFAULT_AUTO_APPLY_SELECTIONS,
CONTROLS_GROUP_TYPE,
} from '@kbn/controls-constants';
import type { EmbeddableStateWithType } from '@kbn/embeddable-plugin/common';
import type { LensAppServices } from './types';
import { LENS_EMBEDDABLE_TYPE } from '../../common/constants';
import { extract } from '../../common/embeddable_factory';
import { extractLensReferences } from '../../common/references';
import type { LensSerializedState } from '../react_embeddable/types';

/**
Expand Down Expand Up @@ -62,7 +61,7 @@ export const redirectToDashboard = ({
stateTransfer: LensAppServices['stateTransfer'];
controlsState?: ControlPanelsState;
}) => {
const { references } = extract(rawState as unknown as EmbeddableStateWithType);
const { references } = extractLensReferences(rawState);

const appId = originatingApp || 'dashboards';

Expand Down
Loading