-
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
Refactor observability plugin breadcrumbs #102290
Changes from 4 commits
394b994
630b52d
db2561d
59196b6
eba7482
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* 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 { renderHook } from '@testing-library/react-hooks'; | ||
import React, { ReactNode } from 'react'; | ||
import { MemoryRouter } from 'react-router-dom'; | ||
import { CoreStart } from '../../../../../src/core/public'; | ||
import { createKibanaReactContext } from '../../../../../src/plugins/kibana_react/public'; | ||
import { useBreadcrumbs } from './use_breadcrumbs'; | ||
|
||
const setBreadcrumbs = jest.fn(); | ||
const setTitle = jest.fn(); | ||
const kibanaServices = ({ | ||
application: { getUrlForApp: () => {}, navigateToApp: () => {} }, | ||
chrome: { setBreadcrumbs, docTitle: { change: setTitle } }, | ||
uiSettings: { get: () => true }, | ||
} as unknown) as Partial<CoreStart>; | ||
const KibanaContext = createKibanaReactContext(kibanaServices); | ||
|
||
function Wrapper({ children }: { children?: ReactNode }) { | ||
return ( | ||
<MemoryRouter> | ||
<KibanaContext.Provider>{children}</KibanaContext.Provider> | ||
</MemoryRouter> | ||
); | ||
} | ||
|
||
describe('useBreadcrumbs', () => { | ||
afterEach(() => { | ||
setBreadcrumbs.mockClear(); | ||
setTitle.mockClear(); | ||
}); | ||
|
||
describe('when setBreadcrumbs and setTitle are not defined', () => { | ||
it('does not set breadcrumbs or the title', () => { | ||
renderHook(() => useBreadcrumbs([]), { | ||
wrapper: ({ children }) => ( | ||
<MemoryRouter> | ||
<KibanaContext.Provider | ||
services={ | ||
({ ...kibanaServices, chrome: { docTitle: {} } } as unknown) as Partial<CoreStart> | ||
} | ||
> | ||
{children} | ||
</KibanaContext.Provider> | ||
</MemoryRouter> | ||
), | ||
}); | ||
|
||
expect(setBreadcrumbs).not.toHaveBeenCalled(); | ||
expect(setTitle).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('with an empty array', () => { | ||
it('sets the overview breadcrumb', () => { | ||
renderHook(() => useBreadcrumbs([]), { wrapper: Wrapper }); | ||
|
||
expect(setBreadcrumbs).toHaveBeenCalledWith([ | ||
{ href: '/overview', onClick: expect.any(Function), text: 'Observability' }, | ||
]); | ||
}); | ||
|
||
it('sets the overview title', () => { | ||
renderHook(() => useBreadcrumbs([]), { wrapper: Wrapper }); | ||
|
||
expect(setTitle).toHaveBeenCalledWith(['Observability']); | ||
}); | ||
}); | ||
|
||
describe('given breadcrumbs', () => { | ||
it('sets the breadcrumbs', () => { | ||
renderHook( | ||
() => | ||
useBreadcrumbs([ | ||
{ text: 'One', href: '/one' }, | ||
{ | ||
text: 'Two', | ||
}, | ||
]), | ||
{ wrapper: Wrapper } | ||
); | ||
|
||
expect(setBreadcrumbs).toHaveBeenCalledWith([ | ||
{ href: '/overview', onClick: expect.any(Function), text: 'Observability' }, | ||
{ | ||
href: '/one', | ||
onClick: expect.any(Function), | ||
text: 'One', | ||
}, | ||
{ | ||
text: 'Two', | ||
}, | ||
]); | ||
}); | ||
|
||
it('sets the title', () => { | ||
renderHook( | ||
() => | ||
useBreadcrumbs([ | ||
{ text: 'One', href: '/one' }, | ||
{ | ||
text: 'Two', | ||
}, | ||
]), | ||
{ wrapper: Wrapper } | ||
); | ||
|
||
expect(setTitle).toHaveBeenCalledWith(['Two', 'One', 'Observability']); | ||
}); | ||
}); | ||
}); | ||
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. Thanks for adding tests! |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,13 @@ | |
* 2.0. | ||
*/ | ||
|
||
import { ChromeBreadcrumb } from 'kibana/public'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { ChromeBreadcrumb } from 'kibana/public'; | ||
import { MouseEvent, useEffect } from 'react'; | ||
import { EuiBreadcrumb } from '@elastic/eui'; | ||
import { useQueryParams } from './use_query_params'; | ||
import { useKibana } from '../utils/kibana_react'; | ||
import { useQueryParams } from './use_query_params'; | ||
|
||
function handleBreadcrumbClick( | ||
function addClickHandlers( | ||
breadcrumbs: ChromeBreadcrumb[], | ||
navigateToHref?: (url: string) => Promise<void> | ||
) { | ||
|
@@ -31,52 +30,37 @@ function handleBreadcrumbClick( | |
})); | ||
} | ||
|
||
export const makeBaseBreadcrumb = (href: string): EuiBreadcrumb => { | ||
return { | ||
text: i18n.translate('xpack.observability.breadcrumbs.observability', { | ||
defaultMessage: 'Observability', | ||
}), | ||
href, | ||
}; | ||
}; | ||
export const casesBreadcrumbs = { | ||
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. These were moved into pages/cases/links.ts. |
||
cases: { | ||
text: i18n.translate('xpack.observability.breadcrumbs.observability.cases', { | ||
defaultMessage: 'Cases', | ||
}), | ||
}, | ||
create: { | ||
text: i18n.translate('xpack.observability.breadcrumbs.observability.cases.create', { | ||
defaultMessage: 'Create', | ||
}), | ||
}, | ||
configure: { | ||
text: i18n.translate('xpack.observability.breadcrumbs.observability.cases.configure', { | ||
defaultMessage: 'Configure', | ||
}), | ||
}, | ||
}; | ||
function getTitleFromBreadCrumbs(breadcrumbs: ChromeBreadcrumb[]) { | ||
return breadcrumbs.map(({ text }) => text?.toString() ?? '').reverse(); | ||
} | ||
|
||
export const useBreadcrumbs = (extraCrumbs: ChromeBreadcrumb[]) => { | ||
const params = useQueryParams(); | ||
|
||
const { | ||
services: { | ||
chrome: { setBreadcrumbs }, | ||
chrome: { docTitle, setBreadcrumbs }, | ||
application: { getUrlForApp, navigateToUrl }, | ||
}, | ||
} = useKibana(); | ||
|
||
const setTitle = docTitle.change; | ||
const appPath = getUrlForApp('observability-overview') ?? ''; | ||
const navigate = navigateToUrl; | ||
|
||
useEffect(() => { | ||
const breadcrumbs = [ | ||
{ | ||
text: i18n.translate('xpack.observability.breadcrumbs.observabilityLinkText', { | ||
defaultMessage: 'Observability', | ||
}), | ||
href: appPath + '/overview', | ||
}, | ||
...extraCrumbs, | ||
]; | ||
if (setBreadcrumbs) { | ||
setBreadcrumbs( | ||
handleBreadcrumbClick( | ||
[makeBaseBreadcrumb(appPath + '/overview')].concat(extraCrumbs), | ||
navigate | ||
) | ||
); | ||
setBreadcrumbs(addClickHandlers(breadcrumbs, navigateToUrl)); | ||
} | ||
if (setTitle) { | ||
setTitle(getTitleFromBreadCrumbs(breadcrumbs)); | ||
} | ||
}, [appPath, extraCrumbs, navigate, params, setBreadcrumbs]); | ||
}, [appPath, extraCrumbs, navigateToUrl, params, setBreadcrumbs, setTitle]); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import { useHistory } from 'react-router-dom'; | |
import { ParsedTechnicalFields } from '../../../../rule_registry/common/parse_technical_fields'; | ||
import type { AlertStatus } from '../../../common/typings'; | ||
import { ExperimentalBadge } from '../../components/shared/experimental_badge'; | ||
import { useBreadcrumbs } from '../../hooks/use_breadcrumbs'; | ||
import { useFetcher } from '../../hooks/use_fetcher'; | ||
import { usePluginContext } from '../../hooks/use_plugin_context'; | ||
import { RouteParams } from '../../routes'; | ||
|
@@ -44,6 +45,14 @@ export function AlertsPage({ routeParams }: AlertsPageProps) { | |
query: { rangeFrom = 'now-15m', rangeTo = 'now', kuery = '', status = 'open' }, | ||
} = routeParams; | ||
|
||
useBreadcrumbs([ | ||
{ | ||
text: i18n.translate('xpack.observability.breadcrumbs.alertsLinkText', { | ||
defaultMessage: 'Alerts', | ||
}), | ||
}, | ||
]); | ||
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 looks like a good way to set breadcrumbs. Should we do something similar for APM? Btw. I'm wondering if Kibana should provide something like this. Seems odd that every plugin reinvents the wheel. 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. Agreed, if this can be moved up the hierarchy, we had this hook originally in uptime, and we moved it here, to be used in UX app and other places. 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 can use this mostly as-is in APM or other places (it's currently used in UX as Shahzad mentioned.) There's ongoing discussion in elastic/observability-design#53. |
||
|
||
// In a future milestone we'll have a page dedicated to rule management in | ||
// observability. For now link to the settings page. | ||
const manageDetectionRulesHref = prepend( | ||
|
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.
Sorting imports here.