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

Refactor workspace context menu register #207

Merged
merged 10 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
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,
};
40 changes: 39 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;
wanglam marked this conversation as resolved.
Show resolved Hide resolved
}

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;
},
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

is it ok to set collapsibleNavHeaderRender multiple times to override previous one, or we should add a check here to let it only set it once?

Copy link
Owner Author

Choose a reason for hiding this comment

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

You mean if different plugins called registerCollapsibleNavHeader with different render, previous one will be override?
Actually I have the same concern, but it should be fine as this will only be used by workspace for now. If in the future, this is called by different plugins, we may add the check or register the render to an array, I don't know which is expected, so just leave it like this for now.

}

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,7 @@ export class ChromeService {
branding={injectedMetadata.getBranding()}
logos={logos}
survey={injectedMetadata.getSurvey()}
workspaces={workspaces}
collapsibleNavHeaderRender={collapsibleNavHeaderRender}
/>
),

Expand Down Expand Up @@ -328,6 +352,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
Loading