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

feat: Add rightNavigationButton component in chrome service for applications to register and add dev tool to top right navigation #6553

Merged
merged 17 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Extract the button component for datasource picker to avoid duplicate code ([#6559](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6559))
- [Workspace] Add workspaces filter to saved objects page. ([#6458](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6458))
- [Multiple Datasource] Support multi data source in Region map ([#6654](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6654))
- Add `rightNavigationButton` component in chrome service for applications to register and add dev tool to top right navigation. ([#6553](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6553))

### 🐛 Bug Fixes

Expand Down
6 changes: 6 additions & 0 deletions src/core/public/chrome/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@
export const OPENSEARCH_DASHBOARDS_ASK_OPENSEARCH_LINK = 'https://forum.opensearch.org/';
export const GITHUB_CREATE_ISSUE_LINK =
'https://github.com/opensearch-project/OpenSearch-Dashboards/issues/new/choose';

export enum RightNavigationOrder {
// order of dev tool should be after advance settings
Settings = 1,
DevTool = 2,
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: the value could be 10, 20 and leave some gaps between them so that we can add more between settings and devtool without change the value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. Updated.

}
3 changes: 2 additions & 1 deletion src/core/public/chrome/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export {
ChromeHelpExtensionMenuDocumentationLink,
ChromeHelpExtensionMenuGitHubLink,
} from './ui/header/header_help_menu';
export { NavType } from './ui';
export { NavType, RightNavigationButton, RightNavigationButtonProps } from './ui';
export { ChromeNavLink, ChromeNavLinks, ChromeNavLinkUpdateableFields } from './nav_links';
export { ChromeRecentlyAccessed, ChromeRecentlyAccessedHistoryItem } from './recently_accessed';
export { ChromeNavControl, ChromeNavControls } from './nav_controls';
export { ChromeDocTitle } from './doc_title';
export { RightNavigationOrder } from './constants';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/core/public/chrome/ui/header/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export {
ChromeHelpExtensionMenuDocumentationLink,
ChromeHelpExtensionMenuGitHubLink,
} from './header_help_menu';
export { RightNavigationButton, RightNavigationButtonProps } from './right_navigation_button';
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();
raintygao marked this conversation as resolved.
Show resolved Hide resolved
});
});
59 changes: 59 additions & 0 deletions src/core/public/chrome/ui/header/right_navigation_button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 navigateToApp = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
/* Use href and onClick to support "open in new tab" and SPA navigation in the same link */
if (
event.button === 0 && // ignore everything but left clicks
Copy link
Member

Choose a reason for hiding this comment

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

Nit: consider to extract to a function for better readability

const isLeftClickEvent = (event) => event.button === 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

Copy link
Contributor Author

@raintygao raintygao Apr 29, 2024

Choose a reason for hiding this comment

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

Seems your guys approvals were dismissed because of commits to address comments. Could you guys help add 2.x label and approve again?

!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>
);
};
2 changes: 2 additions & 0 deletions src/core/public/chrome/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ export {
ChromeHelpExtensionMenuDocumentationLink,
ChromeHelpExtensionMenuGitHubLink,
NavType,
RightNavigationButton,
RightNavigationButtonProps,
} from './header';
6 changes: 6 additions & 0 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import {
ChromeRecentlyAccessed,
ChromeRecentlyAccessedHistoryItem,
NavType,
RightNavigationOrder,
RightNavigationButton,
RightNavigationButtonProps,
} from './chrome';
import { FatalErrorsSetup, FatalErrorsStart, FatalErrorInfo } from './fatal_errors';
import { HttpSetup, HttpStart } from './http';
Expand Down Expand Up @@ -360,6 +363,9 @@ export {
UiSettingsState,
NavType,
Branding,
RightNavigationOrder,
RightNavigationButton,
RightNavigationButtonProps,
};

export { __osdBootstrap__ } from './osd_bootstrap';
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/plugins/dev_tools/opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"server": false,
"ui": true,
"optionalPlugins": ["dataSource", "managementOverview", "dataSourceManagement"],
"requiredPlugins": ["urlForwarding"]
"requiredPlugins": ["urlForwarding"],
"requiredBundles": ["opensearchDashboardsReact"]
}
31 changes: 26 additions & 5 deletions src/plugins/dev_tools/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@
* under the License.
*/

import React from 'react';
import { BehaviorSubject } from 'rxjs';
import { Plugin, CoreSetup, AppMountParameters } from 'src/core/public';
import { Plugin, CoreSetup, AppMountParameters, CoreStart } from 'src/core/public';
import { AppUpdater } from 'opensearch-dashboards/public';
import { i18n } from '@osd/i18n';
import { sortBy } from 'lodash';
import { DataSourceManagementPluginSetup } from 'src/plugins/data_source_management/public';
import { DataSourcePluginSetup } from 'src/plugins/data_source/public';
import { AppNavLinkStatus, DEFAULT_APP_CATEGORIES } from '../../../core/public';
import {
AppNavLinkStatus,
DEFAULT_APP_CATEGORIES,
RightNavigationOrder,
RightNavigationButton,
} from '../../../core/public';
import { UrlForwardingSetup } from '../../url_forwarding/public';
import { CreateDevToolArgs, DevToolApp, createDevToolApp } from './dev_tool';

import './index.scss';
import { ManagementOverViewPluginSetup } from '../../management_overview/public';
import { toMountPoint } from '../../opensearch_dashboards_react/public';

export interface DevToolsSetupDependencies {
urlForwarding: UrlForwardingSetup;
Expand Down Expand Up @@ -75,12 +82,14 @@ export class DevToolsPlugin implements Plugin<DevToolsSetup> {
defaultMessage: 'Dev Tools',
});

private id = 'dev_tools';

public setup(coreSetup: CoreSetup, deps: DevToolsSetupDependencies) {
const { application: applicationSetup, getStartServices } = coreSetup;
const { urlForwarding, managementOverview } = deps;

applicationSetup.register({
id: 'dev_tools',
id: this.id,
title: this.title,
updater$: this.appStateUpdater,
icon: '/ui/logos/opensearch_mark.svg',
Expand All @@ -99,7 +108,7 @@ export class DevToolsPlugin implements Plugin<DevToolsSetup> {
});

managementOverview?.register({
id: 'dev_tools',
id: this.id,
title: this.title,
description: i18n.translate('devTools.devToolsDescription', {
defaultMessage:
Expand All @@ -125,10 +134,22 @@ export class DevToolsPlugin implements Plugin<DevToolsSetup> {
};
}

public start() {
public start(core: CoreStart) {
if (this.getSortedDevTools().length === 0) {
this.appStateUpdater.next(() => ({ navLinkStatus: AppNavLinkStatus.hidden }));
}
core.chrome.navControls.registerRight({
order: RightNavigationOrder.DevTool,
mount: toMountPoint(
React.createElement(RightNavigationButton, {
appId: this.id,
iconType: 'consoleApp',
title: this.title,
application: core.application,
http: core.http,
Comment on lines +148 to +149
Copy link
Member

Choose a reason for hiding this comment

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

Nit: perhaps CoreStart will be better for backward-capability.

})
),
});
}

public stop() {}
Expand Down
Loading