-
Notifications
You must be signed in to change notification settings - Fork 875
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
rightNavigationButton
component in chrome service for app…
…lications to register and add dev tool to top right navigation (#6553) (#6684) * feat: add dev tool to top right navigation Signed-off-by: tygao <[email protected]> * doc: update changelog Signed-off-by: tygao <[email protected]> * test: update component props snapshot Signed-off-by: tygao <[email protected]> * update nav_controls_service.ts Co-authored-by: SuZhou-Joe <[email protected]> Signed-off-by: tygao <[email protected]> * update dependency props and constants Signed-off-by: tygao <[email protected]> * update nav control test Signed-off-by: tygao <[email protected]> * Update CHANGELOG.md Co-authored-by: SuZhou-Joe <[email protected]> Signed-off-by: tygao <[email protected]> * test: update snapshots Signed-off-by: tygao <[email protected]> * doc: update changelog Signed-off-by: tygao <[email protected]> * update click handle Signed-off-by: tygao <[email protected]> * update click handle Signed-off-by: tygao <[email protected]> * doc: add doc for registerRightNavigation Signed-off-by: tygao <[email protected]> * Add `rightNavigationButton` component in chrome service for applications to register and add dev tool to top right navigation. Signed-off-by: tygao <[email protected]> * test: update core start type Signed-off-by: tygao <[email protected]> * update RightNavigationOrder value Signed-off-by: tygao <[email protected]> * extract left click function Signed-off-by: tygao <[email protected]> --------- Signed-off-by: tygao <[email protected]> Co-authored-by: SuZhou-Joe <[email protected]> Co-authored-by: Yulong Ruan <[email protected]> (cherry picked from commit cfe7cd1) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
882412c
commit 521b943
Showing
12 changed files
with
187 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/core/public/chrome/ui/header/__snapshots__/right_navigation_button.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/core/public/chrome/ui/header/right_navigation_button.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { RightNavigationButton } from './right_navigation_button'; | ||
import { applicationServiceMock, httpServiceMock } from '../../../../../core/public/mocks'; | ||
|
||
const mockProps = { | ||
application: applicationServiceMock.createStartContract(), | ||
http: httpServiceMock.createStartContract(), | ||
appId: 'app_id', | ||
iconType: 'mock_icon', | ||
title: 'title', | ||
}; | ||
|
||
describe('Right navigation button', () => { | ||
it('should render base element normally', () => { | ||
const { baseElement } = render(<RightNavigationButton {...mockProps} />); | ||
expect(baseElement).toMatchSnapshot(); | ||
}); | ||
|
||
it('should call application getUrlForApp and navigateToUrl after clicked', () => { | ||
const navigateToUrl = jest.fn(); | ||
const getUrlForApp = jest.fn(); | ||
const props = { | ||
...mockProps, | ||
application: { | ||
...applicationServiceMock.createStartContract(), | ||
getUrlForApp, | ||
navigateToUrl, | ||
}, | ||
}; | ||
const { getByTestId } = render(<RightNavigationButton {...props} />); | ||
const icon = getByTestId('rightNavigationButton'); | ||
fireEvent.click(icon); | ||
expect(getUrlForApp).toHaveBeenCalledWith('app_id', { | ||
path: '/', | ||
absolute: false, | ||
}); | ||
expect(navigateToUrl).toHaveBeenCalled(); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
src/core/public/chrome/ui/header/right_navigation_button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { EuiHeaderSectionItemButton, EuiIcon } from '@elastic/eui'; | ||
import React, { useMemo } from 'react'; | ||
import { CoreStart } from '../../..'; | ||
|
||
import { isModifiedOrPrevented } from './nav_link'; | ||
export interface RightNavigationButtonProps { | ||
application: CoreStart['application']; | ||
http: CoreStart['http']; | ||
appId: string; | ||
iconType: string; | ||
title: string; | ||
} | ||
|
||
export const RightNavigationButton = ({ | ||
application, | ||
http, | ||
appId, | ||
iconType, | ||
title, | ||
}: RightNavigationButtonProps) => { | ||
const targetUrl = useMemo(() => { | ||
const appUrl = application.getUrlForApp(appId, { | ||
path: '/', | ||
absolute: false, | ||
}); | ||
// Remove prefix in Url including workspace and other prefix | ||
return http.basePath.prepend(http.basePath.remove(appUrl), { | ||
withoutClientBasePath: true, | ||
}); | ||
}, [application, http.basePath, appId]); | ||
|
||
const isLeftClickEvent = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { | ||
return event.button === 0; | ||
}; | ||
|
||
const navigateToApp = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { | ||
/* Use href and onClick to support "open in new tab" and SPA navigation in the same link */ | ||
if ( | ||
isLeftClickEvent(event) && // ignore everything but left clicks | ||
!isModifiedOrPrevented(event) | ||
) { | ||
event.preventDefault(); | ||
application.navigateToUrl(targetUrl); | ||
} | ||
return; | ||
}; | ||
|
||
return ( | ||
<EuiHeaderSectionItemButton | ||
data-test-subj="rightNavigationButton" | ||
aria-label={title} | ||
onClick={navigateToApp} | ||
href={targetUrl} | ||
> | ||
<EuiIcon type={iconType} size="m" title={title} color="text" /> | ||
</EuiHeaderSectionItemButton> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ic/application/components/dashboard_listing/__snapshots__/dashboard_listing.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...ic/application/components/dashboard_top_nav/__snapshots__/dashboard_top_nav.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters