Skip to content

Commit

Permalink
Refactor workspace context menu register (opensearch-project#207)
Browse files Browse the repository at this point in the history
1. remove unnecessary workspace menu register
2. expose interface from chrome service to allow customize left nav header

---------

Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl authored Oct 10, 2023
1 parent 5a26f70 commit b935e36
Show file tree
Hide file tree
Showing 24 changed files with 409 additions and 3,491 deletions.
8 changes: 8 additions & 0 deletions src/core/public/chrome/chrome_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import type { PublicMethodsOf } from '@osd/utility-types';
import { ChromeBadge, ChromeBreadcrumb, ChromeService, InternalChromeStart } from './';
import { getLogosMock } from '../../common/mocks';

const createSetupContractMock = () => {
return {
registerCollapsibleNavHeader: jest.fn(),
};
};

const createStartContractMock = () => {
const startContract: DeeplyMockedKeys<InternalChromeStart> = {
getHeaderComponent: jest.fn(),
Expand Down Expand Up @@ -97,6 +103,7 @@ const createStartContractMock = () => {
type ChromeServiceContract = PublicMethodsOf<ChromeService>;
const createMock = () => {
const mocked: jest.Mocked<ChromeServiceContract> = {
setup: jest.fn(),
start: jest.fn(),
stop: jest.fn(),
};
Expand All @@ -107,4 +114,5 @@ const createMock = () => {
export const chromeServiceMock = {
create: createMock,
createStartContract: createStartContractMock,
createSetupContract: createSetupContractMock,
};
42 changes: 41 additions & 1 deletion src/core/public/chrome/chrome_service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ interface StartDeps {
workspaces: WorkspacesStart;
}

type CollapsibleNavHeaderRender = (context: {
basePath: HttpStart['basePath'];
getUrlForApp: InternalApplicationStart['getUrlForApp'];
workspaces: WorkspacesStart;
}) => JSX.Element | null;

/** @internal */
export class ChromeService {
private isVisible$!: Observable<boolean>;
Expand All @@ -108,6 +114,7 @@ export class ChromeService {
private readonly navLinks = new NavLinksService();
private readonly recentlyAccessed = new RecentlyAccessedService();
private readonly docTitle = new DocTitleService();
private collapsibleNavHeaderRender?: CollapsibleNavHeaderRender;

constructor(private readonly params: ConstructorParams) {}

Expand Down Expand Up @@ -143,6 +150,14 @@ export class ChromeService {
);
}

public setup() {
return {
registerCollapsibleNavHeader: (render: CollapsibleNavHeaderRender) => {
this.collapsibleNavHeaderRender = render;
},
};
}

public async start({
application,
docLinks,
Expand Down Expand Up @@ -181,6 +196,15 @@ export class ChromeService {
localStorage.setItem(IS_LOCKED_KEY, `${isLocked}`);
};

const collapsibleNavHeaderRender = () =>
this.collapsibleNavHeaderRender
? this.collapsibleNavHeaderRender({
basePath: http.basePath,
workspaces,
getUrlForApp: application.getUrlForApp,
})
: null;

const getIsNavDrawerLocked$ = isNavDrawerLocked$.pipe(takeUntil(this.stop$));

const logos = getLogos(injectedMetadata.getBranding(), http.basePath.serverBasePath);
Expand Down Expand Up @@ -264,7 +288,9 @@ export class ChromeService {
branding={injectedMetadata.getBranding()}
logos={logos}
survey={injectedMetadata.getSurvey()}
workspaces={workspaces}
collapsibleNavHeaderRender={
this.collapsibleNavHeaderRender ? collapsibleNavHeaderRender : undefined
}
/>
),

Expand Down Expand Up @@ -328,6 +354,20 @@ export class ChromeService {
}
}

/**
* ChromeSetup allows plugins to customize the global chrome header UI rendering
* before the header UI is mounted.
*
* @example
* Customize the Collapsible Nav's (left nav menu) header section:
* ```ts
* core.chrome.registerCollapsibleNavHeader(() => <CustomNavHeader />)
* ```
*/
export interface ChromeSetup {
registerCollapsibleNavHeader: (render: CollapsibleNavHeaderRender) => void;
}

/**
* ChromeStart allows plugins to customize the global chrome header UI and
* enrich the UX with additional information about the current location of the
Expand Down
1 change: 1 addition & 0 deletions src/core/public/chrome/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export {
ChromeBreadcrumb,
ChromeService,
ChromeStart,
ChromeSetup,
InternalChromeStart,
ChromeHelpExtension,
} from './chrome_service';
Expand Down
Loading

0 comments on commit b935e36

Please sign in to comment.