Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Fixes the transition to a dashboard when originatingApp is not given #172543

Merged
merged 1 commit into from
Dec 5, 2023
Merged
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
37 changes: 2 additions & 35 deletions x-pack/plugins/lens/public/app_plugin/save_modal_container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import type { LensAppProps, LensAppServices } from './types';
import type { SaveProps } from './app';
import { Document, checkForDuplicateTitle, SavedObjectIndexStore } from '../persistence';
import type { LensByReferenceInput, LensEmbeddableInput } from '../embeddable';
import { APP_ID, getFullPath, LENS_EMBEDDABLE_TYPE } from '../../common/constants';
import { APP_ID, getFullPath } from '../../common/constants';
import type { LensAppState } from '../state_management';
import { getPersisted } from '../state_management/init_middleware/load_initial';
import { VisualizeEditorContext } from '../types';
import { redirectToDashboard } from './save_modal_container_helpers';

type ExtraProps = Pick<LensAppProps, 'initialInput'> &
Partial<Pick<LensAppProps, 'redirectToOrigin' | 'redirectTo' | 'onAppLeave'>>;
Expand Down Expand Up @@ -171,40 +172,6 @@ export function SaveModalContainer({
);
}

const redirectToDashboard = ({
embeddableInput,
dashboardFeatureFlag,
dashboardId,
originatingApp,
getOriginatingPath,
stateTransfer,
}: {
embeddableInput: LensEmbeddableInput;
dashboardId: string;
dashboardFeatureFlag: LensAppServices['dashboardFeatureFlag'];
originatingApp?: string;
getOriginatingPath?: (dashboardId: string) => string | undefined;
stateTransfer: LensAppServices['stateTransfer'];
}) => {
if (!dashboardFeatureFlag.allowByValueEmbeddables) {
throw new Error('redirectToDashboard called with by-value embeddables disabled');
}

const state = {
input: embeddableInput,
type: LENS_EMBEDDABLE_TYPE,
};

const path =
getOriginatingPath?.(dashboardId) ??
(dashboardId === 'new' ? '#/create' : `#/view/${dashboardId}`);
const appId = originatingApp ?? 'dashboards';
stateTransfer.navigateToWithEmbeddablePackage(appId, {
state,
path,
});
};

const getDocToSave = (
lastKnownDoc: Document,
saveProps: SaveProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 { makeDefaultServices } from '../mocks';
import type { LensEmbeddableInput } from '../embeddable';
import type { LensAppServices } from './types';
import { redirectToDashboard } from './save_modal_container_helpers';

describe('redirectToDashboard', () => {
const embeddableInput = {
test: 'test',
} as unknown as LensEmbeddableInput;
const mockServices = makeDefaultServices();

it('should return error in case of allowByValueEmbeddables false', () => {
expect(() => {
redirectToDashboard({
embeddableInput,
dashboardFeatureFlag: {
allowByValueEmbeddables: false,
},
dashboardId: 'id',
originatingApp: '',
getOriginatingPath: jest.fn(),
stateTransfer: mockServices.stateTransfer,
});
}).toThrow('redirectToDashboard called with by-value embeddables disabled');
});

it('should call the navigateToWithEmbeddablePackage with the correct args if originatingApp is given', () => {
const navigateToWithEmbeddablePackageSpy = jest.fn();
const transferService = {
...mockServices.stateTransfer,
navigateToWithEmbeddablePackage: navigateToWithEmbeddablePackageSpy,
} as unknown as LensAppServices['stateTransfer'];
redirectToDashboard({
embeddableInput,
dashboardFeatureFlag: {
allowByValueEmbeddables: true,
},
dashboardId: 'id',
originatingApp: 'security',
getOriginatingPath: jest.fn(),
stateTransfer: transferService,
});
expect(navigateToWithEmbeddablePackageSpy).toHaveBeenCalledWith('security', {
path: '#/view/id',
state: { input: { test: 'test' }, type: 'lens' },
});
});

it('should call the navigateToWithEmbeddablePackage with the correct args if originatingApp is an empty string', () => {
const navigateToWithEmbeddablePackageSpy = jest.fn();
const transferService = {
...mockServices.stateTransfer,
navigateToWithEmbeddablePackage: navigateToWithEmbeddablePackageSpy,
} as unknown as LensAppServices['stateTransfer'];
redirectToDashboard({
embeddableInput,
dashboardFeatureFlag: {
allowByValueEmbeddables: true,
},
dashboardId: 'id',
originatingApp: '',
getOriginatingPath: jest.fn(),
stateTransfer: transferService,
});
expect(navigateToWithEmbeddablePackageSpy).toHaveBeenCalledWith('dashboards', {
path: '#/view/id',
state: { input: { test: 'test' }, type: 'lens' },
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 { LensAppServices } from './types';
import type { LensEmbeddableInput } from '../embeddable';
import { LENS_EMBEDDABLE_TYPE } from '../../common/constants';

export const redirectToDashboard = ({
embeddableInput,
dashboardFeatureFlag,
dashboardId,
originatingApp,
getOriginatingPath,
stateTransfer,
}: {
embeddableInput: LensEmbeddableInput;
dashboardId: string;
dashboardFeatureFlag: LensAppServices['dashboardFeatureFlag'];
originatingApp?: string;
getOriginatingPath?: (dashboardId: string) => string | undefined;
stateTransfer: LensAppServices['stateTransfer'];
}) => {
if (!dashboardFeatureFlag.allowByValueEmbeddables) {
throw new Error('redirectToDashboard called with by-value embeddables disabled');
}

const state = {
input: embeddableInput,
type: LENS_EMBEDDABLE_TYPE,
};

const path =
getOriginatingPath?.(dashboardId) ??
(dashboardId === 'new' ? '#/create' : `#/view/${dashboardId}`);
const appId = originatingApp || 'dashboards';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the fix. It was originatingApp ?? 'dashboards' causing the problematic redirect

stateTransfer.navigateToWithEmbeddablePackage(appId, {
state,
path,
});
};
Loading