2020-05-01 12:57:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* 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.
|
2020-05-01 12:57:29 +02:00
|
|
|
*/
|
2021-02-04 03:12:39 +01:00
|
|
|
|
2021-01-29 00:15:13 +01:00
|
|
|
import React, { FC, useCallback } from 'react';
|
2020-05-01 12:57:29 +02:00
|
|
|
|
|
|
|
import { AppMountParameters, CoreSetup } from 'kibana/public';
|
|
|
|
import { FormattedMessage, I18nProvider } from '@kbn/i18n/react';
|
|
|
|
import { HashRouter, Route, RouteComponentProps, Switch } from 'react-router-dom';
|
2020-12-21 14:26:14 +01:00
|
|
|
import { History } from 'history';
|
2020-05-01 12:57:29 +02:00
|
|
|
import { render, unmountComponentAtNode } from 'react-dom';
|
2020-05-13 09:19:24 +02:00
|
|
|
import { i18n } from '@kbn/i18n';
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
import { DashboardFeatureFlagConfig } from 'src/plugins/dashboard/public';
|
2020-06-23 18:21:48 +02:00
|
|
|
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
|
2020-05-01 12:57:29 +02:00
|
|
|
|
|
|
|
import { LensReportManager, setReportManager, trackUiEvent } from '../lens_ui_telemetry';
|
|
|
|
|
|
|
|
import { App } from './app';
|
|
|
|
import { EditorFrameStart } from '../types';
|
|
|
|
import { addHelpMenuToAppChrome } from '../help_menu_util';
|
|
|
|
import { LensPluginStartDependencies } from '../plugin';
|
2021-02-08 21:31:09 +01:00
|
|
|
import { LENS_EMBEDDABLE_TYPE, LENS_EDIT_BY_VALUE, APP_ID } from '../../common';
|
2020-09-23 23:40:52 +02:00
|
|
|
import {
|
|
|
|
LensEmbeddableInput,
|
|
|
|
LensByReferenceInput,
|
|
|
|
LensByValueInput,
|
|
|
|
} from '../editor_frame_service/embeddable/embeddable';
|
2020-10-06 10:15:41 +02:00
|
|
|
import { ACTION_VISUALIZE_LENS_FIELD } from '../../../../../src/plugins/ui_actions/public';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { LensAttributeService } from '../lens_attribute_service';
|
2020-10-06 10:15:41 +02:00
|
|
|
import { LensAppServices, RedirectToOriginProps, HistoryLocationState } from './types';
|
2020-09-23 23:40:52 +02:00
|
|
|
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
|
2020-05-01 12:57:29 +02:00
|
|
|
|
|
|
|
export async function mountApp(
|
|
|
|
core: CoreSetup<LensPluginStartDependencies, void>,
|
|
|
|
params: AppMountParameters,
|
2020-09-23 23:40:52 +02:00
|
|
|
mountProps: {
|
|
|
|
createEditorFrame: EditorFrameStart['createInstance'];
|
|
|
|
getByValueFeatureFlag: () => Promise<DashboardFeatureFlagConfig>;
|
2020-10-19 17:21:25 +02:00
|
|
|
attributeService: () => Promise<LensAttributeService>;
|
2021-01-29 00:15:13 +01:00
|
|
|
getPresentationUtilContext: () => Promise<FC>;
|
2020-09-23 23:40:52 +02:00
|
|
|
}
|
2020-05-01 12:57:29 +02:00
|
|
|
) {
|
2021-01-29 00:15:13 +01:00
|
|
|
const {
|
|
|
|
createEditorFrame,
|
|
|
|
getByValueFeatureFlag,
|
|
|
|
attributeService,
|
|
|
|
getPresentationUtilContext,
|
|
|
|
} = mountProps;
|
2020-05-01 12:57:29 +02:00
|
|
|
const [coreStart, startDependencies] = await core.getStartServices();
|
2020-11-03 10:33:18 +01:00
|
|
|
const { data, navigation, embeddable, savedObjectsTagging } = startDependencies;
|
2020-09-23 23:40:52 +02:00
|
|
|
|
|
|
|
const instance = await createEditorFrame();
|
|
|
|
const storage = new Storage(localStorage);
|
2020-12-15 19:18:36 +01:00
|
|
|
const stateTransfer = embeddable?.getStateTransfer();
|
2020-10-06 10:15:41 +02:00
|
|
|
const historyLocationState = params.history.location.state as HistoryLocationState;
|
2021-02-08 21:31:09 +01:00
|
|
|
const embeddableEditorIncomingState = stateTransfer?.getIncomingEditorState(APP_ID);
|
2020-09-23 23:40:52 +02:00
|
|
|
|
|
|
|
const lensServices: LensAppServices = {
|
|
|
|
data,
|
|
|
|
storage,
|
|
|
|
navigation,
|
2020-12-15 19:18:36 +01:00
|
|
|
stateTransfer,
|
2020-11-03 10:33:18 +01:00
|
|
|
savedObjectsTagging,
|
2020-10-19 17:21:25 +02:00
|
|
|
attributeService: await attributeService(),
|
2020-09-23 23:40:52 +02:00
|
|
|
http: coreStart.http,
|
|
|
|
chrome: coreStart.chrome,
|
|
|
|
overlays: coreStart.overlays,
|
|
|
|
uiSettings: coreStart.uiSettings,
|
|
|
|
application: coreStart.application,
|
|
|
|
notifications: coreStart.notifications,
|
|
|
|
savedObjectsClient: coreStart.savedObjects.client,
|
|
|
|
getOriginatingAppName: () => {
|
|
|
|
return embeddableEditorIncomingState?.originatingApp
|
|
|
|
? stateTransfer?.getAppNameFromId(embeddableEditorIncomingState.originatingApp)
|
|
|
|
: undefined;
|
|
|
|
},
|
2020-05-01 12:57:29 +02:00
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
// Temporarily required until the 'by value' paradigm is default.
|
|
|
|
dashboardFeatureFlag: await getByValueFeatureFlag(),
|
|
|
|
};
|
|
|
|
|
|
|
|
addHelpMenuToAppChrome(coreStart.chrome, coreStart.docLinks);
|
2020-05-13 09:19:24 +02:00
|
|
|
coreStart.chrome.docTitle.change(
|
|
|
|
i18n.translate('xpack.lens.pageTitle', { defaultMessage: 'Lens' })
|
|
|
|
);
|
|
|
|
|
2020-05-01 12:57:29 +02:00
|
|
|
setReportManager(
|
|
|
|
new LensReportManager({
|
|
|
|
http: core.http,
|
2020-09-23 23:40:52 +02:00
|
|
|
storage,
|
2020-05-01 12:57:29 +02:00
|
|
|
})
|
|
|
|
);
|
2020-09-23 23:40:52 +02:00
|
|
|
|
2020-12-21 14:26:14 +01:00
|
|
|
const getInitialInput = (id?: string, editByValue?: boolean): LensEmbeddableInput | undefined => {
|
2020-12-15 19:18:36 +01:00
|
|
|
if (editByValue) {
|
|
|
|
return embeddableEditorIncomingState?.valueInput as LensByValueInput;
|
|
|
|
}
|
2020-12-21 14:26:14 +01:00
|
|
|
if (id) {
|
|
|
|
return { savedObjectId: id } as LensByReferenceInput;
|
2020-09-23 23:40:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-21 14:26:14 +01:00
|
|
|
const redirectTo = (history: History<unknown>, savedObjectId?: string) => {
|
2020-09-23 23:40:52 +02:00
|
|
|
if (!savedObjectId) {
|
2020-12-21 14:26:14 +01:00
|
|
|
history.push({ pathname: '/', search: history.location.search });
|
2020-09-23 23:40:52 +02:00
|
|
|
} else {
|
2020-12-21 14:26:14 +01:00
|
|
|
history.push({
|
2020-10-27 11:43:56 +01:00
|
|
|
pathname: `/edit/${savedObjectId}`,
|
2020-12-21 14:26:14 +01:00
|
|
|
search: history.location.search,
|
2020-10-27 11:43:56 +01:00
|
|
|
});
|
2020-05-01 12:57:29 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-08 22:39:24 +01:00
|
|
|
const redirectToDashboard = (embeddableInput: LensEmbeddableInput, dashboardId: string) => {
|
|
|
|
if (!lensServices.dashboardFeatureFlag.allowByValueEmbeddables) {
|
|
|
|
throw new Error('redirectToDashboard called with by-value embeddables disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = {
|
|
|
|
input: embeddableInput,
|
|
|
|
type: LENS_EMBEDDABLE_TYPE,
|
|
|
|
};
|
|
|
|
|
|
|
|
const path = dashboardId === 'new' ? '#/create' : `#/view/${dashboardId}`;
|
|
|
|
stateTransfer.navigateToWithEmbeddablePackage('dashboards', {
|
|
|
|
state,
|
|
|
|
path,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-23 23:40:52 +02:00
|
|
|
const redirectToOrigin = (props?: RedirectToOriginProps) => {
|
|
|
|
if (!embeddableEditorIncomingState?.originatingApp) {
|
|
|
|
throw new Error('redirectToOrigin called without an originating app');
|
|
|
|
}
|
|
|
|
if (stateTransfer && props?.input) {
|
|
|
|
const { input, isCopied } = props;
|
|
|
|
stateTransfer.navigateToWithEmbeddablePackage(embeddableEditorIncomingState?.originatingApp, {
|
|
|
|
state: {
|
|
|
|
embeddableId: isCopied ? undefined : embeddableEditorIncomingState.embeddableId,
|
|
|
|
type: LENS_EMBEDDABLE_TYPE,
|
|
|
|
input,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
coreStart.application.navigateToApp(embeddableEditorIncomingState?.originatingApp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-21 14:26:14 +01:00
|
|
|
// const featureFlagConfig = await getByValueFeatureFlag();
|
|
|
|
const EditorRenderer = React.memo(
|
|
|
|
(props: { id?: string; history: History<unknown>; editByValue?: boolean }) => {
|
|
|
|
const redirectCallback = useCallback(
|
|
|
|
(id?: string) => {
|
|
|
|
redirectTo(props.history, id);
|
|
|
|
},
|
|
|
|
[props.history]
|
|
|
|
);
|
|
|
|
trackUiEvent('loaded');
|
|
|
|
return (
|
|
|
|
<App
|
|
|
|
incomingState={embeddableEditorIncomingState}
|
|
|
|
editorFrame={instance}
|
|
|
|
initialInput={getInitialInput(props.id, props.editByValue)}
|
|
|
|
redirectTo={redirectCallback}
|
|
|
|
redirectToOrigin={redirectToOrigin}
|
|
|
|
redirectToDashboard={redirectToDashboard}
|
|
|
|
onAppLeave={params.onAppLeave}
|
|
|
|
setHeaderActionMenu={params.setHeaderActionMenu}
|
|
|
|
history={props.history}
|
|
|
|
initialContext={
|
|
|
|
historyLocationState && historyLocationState.type === ACTION_VISUALIZE_LENS_FIELD
|
|
|
|
? historyLocationState.payload
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const EditorRoute = (
|
|
|
|
routeProps: RouteComponentProps<{ id?: string }> & { editByValue?: boolean }
|
2020-12-15 19:18:36 +01:00
|
|
|
) => {
|
2020-05-01 12:57:29 +02:00
|
|
|
return (
|
2020-12-21 14:26:14 +01:00
|
|
|
<EditorRenderer
|
|
|
|
id={routeProps.match.params.id}
|
2020-06-10 15:44:59 +02:00
|
|
|
history={routeProps.history}
|
2020-12-21 14:26:14 +01:00
|
|
|
editByValue={routeProps.editByValue}
|
2020-05-01 12:57:29 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function NotFound() {
|
|
|
|
trackUiEvent('loaded_404');
|
|
|
|
return <FormattedMessage id="xpack.lens.app404" defaultMessage="404 Not Found" />;
|
|
|
|
}
|
2020-11-03 14:29:56 +01:00
|
|
|
// dispatch synthetic hash change event to update hash history objects
|
|
|
|
// this is necessary because hash updates triggered by using popState won't trigger this event naturally.
|
|
|
|
const unlistenParentHistory = params.history.listen(() => {
|
|
|
|
window.dispatchEvent(new HashChangeEvent('hashchange'));
|
|
|
|
});
|
2020-05-01 12:57:29 +02:00
|
|
|
|
2020-05-13 09:19:24 +02:00
|
|
|
params.element.classList.add('lnsAppWrapper');
|
2021-01-29 00:15:13 +01:00
|
|
|
|
|
|
|
const PresentationUtilContext = await getPresentationUtilContext();
|
|
|
|
|
2020-05-01 12:57:29 +02:00
|
|
|
render(
|
|
|
|
<I18nProvider>
|
2020-09-23 23:40:52 +02:00
|
|
|
<KibanaContextProvider services={lensServices}>
|
2021-01-29 00:15:13 +01:00
|
|
|
<PresentationUtilContext>
|
|
|
|
<HashRouter>
|
|
|
|
<Switch>
|
|
|
|
<Route exact path="/edit/:id" component={EditorRoute} />
|
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={`/${LENS_EDIT_BY_VALUE}`}
|
|
|
|
render={(routeProps) => <EditorRoute {...routeProps} editByValue />}
|
|
|
|
/>
|
|
|
|
<Route exact path="/" component={EditorRoute} />
|
|
|
|
<Route path="/" component={NotFound} />
|
|
|
|
</Switch>
|
|
|
|
</HashRouter>
|
|
|
|
</PresentationUtilContext>
|
2020-09-23 23:40:52 +02:00
|
|
|
</KibanaContextProvider>
|
2020-05-01 12:57:29 +02:00
|
|
|
</I18nProvider>,
|
|
|
|
params.element
|
|
|
|
);
|
|
|
|
return () => {
|
2020-12-29 14:18:30 +01:00
|
|
|
data.search.session.clear();
|
2020-05-01 12:57:29 +02:00
|
|
|
instance.unmount();
|
|
|
|
unmountComponentAtNode(params.element);
|
2020-11-03 14:29:56 +01:00
|
|
|
unlistenParentHistory();
|
2020-05-01 12:57:29 +02:00
|
|
|
};
|
|
|
|
}
|