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

[Backport 2.x] feat: Add rightNavigationButton component in chrome service for applications to register and add dev tool to top right navigation #6684

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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 = 10,
DevTool = 20,
}
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();
});
});
63 changes: 63 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,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>
);
};
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,
})
),
});
}

public stop() {}
Expand Down
Loading