-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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][Dashboard] Adding Lens to Dashboard #53110
Changes from 19 commits
283ca5b
1842088
57bef33
75a3d26
449d5f4
e50a051
3411d59
a316315
452b4dc
494b855
12c3142
3a39424
d28733e
e44ce7a
e335b91
1bfec16
bbfcc34
8022cea
64e6ceb
fe9ab85
25010ff
f78bdc7
9e15430
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,13 @@ import 'uiExports/visResponseHandlers'; | |
import 'uiExports/savedObjectTypes'; | ||
|
||
import React from 'react'; | ||
import { I18nProvider, FormattedMessage } from '@kbn/i18n/react'; | ||
import { HashRouter, Switch, Route, RouteComponentProps } from 'react-router-dom'; | ||
import { FormattedMessage, I18nProvider } from '@kbn/i18n/react'; | ||
import { HashRouter, Route, RouteComponentProps, Switch } from 'react-router-dom'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
import { CoreSetup, CoreStart, SavedObjectsClientContract } from 'src/core/public'; | ||
import { DataPublicPluginStart } from 'src/plugins/data/public'; | ||
import rison, { RisonObject, RisonValue } from 'rison-node'; | ||
import { isObject } from 'lodash'; | ||
import { DataStart } from '../../../../../../src/legacy/core_plugins/data/public'; | ||
import { Storage } from '../../../../../../src/plugins/kibana_utils/public'; | ||
import { editorFrameSetup, editorFrameStart, editorFrameStop } from '../editor_frame_plugin'; | ||
|
@@ -41,6 +43,12 @@ import { | |
import { NOT_INTERNATIONALIZED_PRODUCT_NAME } from '../../common'; | ||
import { KibanaLegacySetup } from '../../../../../../src/plugins/kibana_legacy/public'; | ||
import { EditorFrameStart } from '../types'; | ||
import { | ||
getKibanaBasePathFromDashboardUrl, | ||
addEmbeddableToDashboardUrl, | ||
getDashboardUrlWithQueryParams, | ||
getUrlVars, | ||
} from './url_helper'; | ||
|
||
export interface LensPluginSetupDependencies { | ||
kibana_legacy: KibanaLegacySetup; | ||
|
@@ -51,6 +59,9 @@ export interface LensPluginStartDependencies { | |
dataShim: DataStart; | ||
} | ||
|
||
export const isRisonObject = (value: RisonValue): value is RisonObject => { | ||
return isObject(value); | ||
}; | ||
export class AppPlugin { | ||
private startDependencies: { | ||
data: DataPublicPluginStart; | ||
|
@@ -93,9 +104,54 @@ export class AppPlugin { | |
http: core.http, | ||
}) | ||
); | ||
const updateUrlTime = (urlVars: Record<string, string>): void => { | ||
const decoded: RisonObject = rison.decode(urlVars._g) as RisonObject; | ||
if (!decoded) { | ||
return; | ||
} | ||
// @ts-ignore | ||
decoded.time = data.query.timefilter.timefilter.getTime(); | ||
urlVars._g = rison.encode((decoded as unknown) as RisonObject); | ||
}; | ||
const redirectTo = ( | ||
routeProps: RouteComponentProps<{ id?: string }>, | ||
addToDashboardMode: boolean, | ||
id?: string | ||
) => { | ||
if (!id) { | ||
routeProps.history.push('/lens'); | ||
} else if (!addToDashboardMode) { | ||
routeProps.history.push(`/lens/edit/${id}`); | ||
} else if (addToDashboardMode && id) { | ||
routeProps.history.push(`/lens/edit/${id}`); | ||
const url = context.core.chrome.navLinks.get('kibana:dashboard'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is adding more dependencies on |
||
if (!url) { | ||
return; | ||
} | ||
const lastDashboardAbsoluteUrl = url.url; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also seems like a bunch of detail that belongs to the dashboard module, not to Lens. Maybe something like this: const url = dashboardUrl.addEmbeddable(url.url, id, 'lens');
if (url) {
window.history.pushState({}, '', url);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the original intention, but then we figured the time range is not properly set. So for the hack with setting timerange to work, this won't be so clean. |
||
const lensUrl = `${getKibanaBasePathFromDashboardUrl( | ||
lastDashboardAbsoluteUrl | ||
)}/lens/edit/${id}`; | ||
if (lastDashboardAbsoluteUrl && lensUrl) { | ||
const urlVars = getUrlVars(lastDashboardAbsoluteUrl); | ||
updateUrlTime(urlVars); | ||
window.history.pushState({}, '', lensUrl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we generally avoid using pushState directly, and instead use a core service to do this, but I'm not sure. Might be worth looking into, anyway. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it exists in the new platform. I think the |
||
const dashboardUrl = getDashboardUrlWithQueryParams( | ||
lastDashboardAbsoluteUrl, | ||
urlVars | ||
); | ||
const dashboardParsedUrl = addEmbeddableToDashboardUrl(dashboardUrl, id, 'lens'); | ||
if (dashboardParsedUrl) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this returns null, we silently fail. Is that the right thing? It seems like a null here is a legit error that should be surfaced in some way. |
||
window.history.pushState({}, '', dashboardParsedUrl); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const renderEditor = (routeProps: RouteComponentProps<{ id?: string }>) => { | ||
trackUiEvent('loaded'); | ||
const addToDashboardMode = | ||
!!routeProps.location.search && routeProps.location.search.includes('addToDashboard'); | ||
return ( | ||
<App | ||
core={context.core} | ||
|
@@ -104,13 +160,8 @@ export class AppPlugin { | |
storage={new Storage(localStorage)} | ||
docId={routeProps.match.params.id} | ||
docStorage={new SavedObjectIndexStore(savedObjectsClient)} | ||
redirectTo={id => { | ||
if (!id) { | ||
routeProps.history.push('/lens'); | ||
} else { | ||
routeProps.history.push(`/lens/edit/${id}`); | ||
} | ||
}} | ||
redirectTo={id => redirectTo(routeProps, addToDashboardMode, id)} | ||
addToDashboardMode={addToDashboardMode} | ||
/> | ||
); | ||
}; | ||
|
@@ -119,6 +170,7 @@ export class AppPlugin { | |
trackUiEvent('loaded_404'); | ||
return <FormattedMessage id="xpack.lens.app404" defaultMessage="404 Not Found" />; | ||
} | ||
|
||
render( | ||
<I18nProvider> | ||
<HashRouter> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This piece of code can be simplified from a typing perspective: