Skip to content

Commit

Permalink
Register navGroupUpdater in workspace plugin
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Jul 1, 2024
1 parent b3d667e commit d1821b5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/plugins/workspace/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,38 @@ describe('Workspace plugin', () => {
workspacePlugin.start(startMock);
expect(startMock.chrome.setBreadcrumbs).not.toHaveBeenCalled();
});

it('#start should call navGroupUpdater$.next after currentWorkspace set', async () => {
const workspacePlugin = new WorkspacePlugin();
const setupMock = getSetupMock();
const coreStart = coreMock.createStart();
await workspacePlugin.setup(setupMock, {});

expect(setupMock.chrome.navGroup.registerNavGroupUpdater).toHaveBeenCalled();
const navGroupUpdater$ = setupMock.chrome.navGroup.registerNavGroupUpdater.mock.calls[0][0];

expect(navGroupUpdater$).toBeTruthy();
jest.spyOn(navGroupUpdater$, 'next');

expect(navGroupUpdater$.next).not.toHaveBeenCalled();
workspacePlugin.start(coreStart);

waitFor(() => {
expect(navGroupUpdater$.next).toHaveBeenCalled();
});
});

it('#stop should call unregisterNavGroupUpdater', async () => {
const workspacePlugin = new WorkspacePlugin();
const setupMock = getSetupMock();
const unregisterNavGroupUpdater = jest.fn();
setupMock.chrome.navGroup.registerNavGroupUpdater.mockReturnValueOnce(
unregisterNavGroupUpdater
);
await workspacePlugin.setup(setupMock, {});

workspacePlugin.stop();

expect(unregisterNavGroupUpdater).toHaveBeenCalled();
});
});
26 changes: 25 additions & 1 deletion src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
PublicAppInfo,
ChromeBreadcrumb,
WorkspaceAvailability,
ChromeNavGroupUpdater,
NavGroupStatus,
} from '../../../core/public';
import {
WORKSPACE_FATAL_ERROR_APP_ID,
Expand All @@ -33,7 +35,11 @@ import { SavedObjectsManagementPluginSetup } from '../../../plugins/saved_object
import { ManagementSetup } from '../../../plugins/management/public';
import { WorkspaceMenu } from './components/workspace_menu/workspace_menu';
import { getWorkspaceColumn } from './components/workspace_column';
import { filterWorkspaceConfigurableApps, isAppAccessibleInWorkspace } from './utils';
import {
filterWorkspaceConfigurableApps,
isAppAccessibleInWorkspace,
isNavGroupInFeatureConfigs,
} from './utils';

type WorkspaceAppType = (
params: AppMountParameters,
Expand All @@ -53,7 +59,10 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
private currentWorkspaceIdSubscription?: Subscription;
private managementCurrentWorkspaceIdSubscription?: Subscription;
private appUpdater$ = new BehaviorSubject<AppUpdater>(() => undefined);
private navGroupUpdater$ = new BehaviorSubject<ChromeNavGroupUpdater>(() => undefined);
private workspaceConfigurableApps$ = new BehaviorSubject<PublicAppInfo[]>([]);
private unregisterNavGroupUpdater?: () => void;

private _changeSavedObjectCurrentWorkspace() {
if (this.coreStart) {
return this.coreStart.workspaces.currentWorkspaceId$.subscribe((currentWorkspaceId) => {
Expand Down Expand Up @@ -89,6 +98,17 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
*/
return { status: AppStatus.inaccessible };
});

this.navGroupUpdater$.next((navGroup) => {
if (
currentWorkspace.features &&
!isNavGroupInFeatureConfigs(navGroup.id, currentWorkspace.features)
) {
return {
status: NavGroupStatus.Hidden,
};
}
});
}
});
};
Expand Down Expand Up @@ -166,6 +186,9 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
const workspaceClient = new WorkspaceClient(core.http, core.workspaces);
await workspaceClient.init();
core.application.registerAppUpdater(this.appUpdater$);
this.unregisterNavGroupUpdater = core.chrome.navGroup.registerNavGroupUpdater(
this.navGroupUpdater$
);

// Hide advance settings and dataSource menus and disable in setup
if (management) {
Expand Down Expand Up @@ -331,5 +354,6 @@ export class WorkspacePlugin implements Plugin<{}, {}, WorkspacePluginSetupDeps>
this.currentWorkspaceIdSubscription?.unsubscribe();
this.managementCurrentWorkspaceIdSubscription?.unsubscribe();
this.breadcrumbsSubscription?.unsubscribe();
this.unregisterNavGroupUpdater?.();
}
}
17 changes: 17 additions & 0 deletions src/plugins/workspace/public/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
filterWorkspaceConfigurableApps,
isAppAccessibleInWorkspace,
isFeatureIdInsideUseCase,
isNavGroupInFeatureConfigs,
} from './utils';
import { WorkspaceAvailability } from '../../../core/public';

Expand Down Expand Up @@ -276,3 +277,19 @@ describe('workspace utils: isFeatureIdInsideUseCase', () => {
expect(isFeatureIdInsideUseCase('discover', 'use-case-invalid')).toBe(false);
});
});

describe('workspace utils: isNavGroupInFeatureConfigs', () => {
it('should return false if nav group not in feature configs', () => {
expect(
isNavGroupInFeatureConfigs('dataAdministration', [
'use-case-observability',
'use-case-search',
])
).toBe(false);
});
it('should return true if nav group in feature configs', () => {
expect(
isNavGroupInFeatureConfigs('observability', ['use-case-observability', 'use-case-search'])
).toBe(true);
});
});
3 changes: 3 additions & 0 deletions src/plugins/workspace/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export const isFeatureIdInsideUseCase = (featureId: string, featureConfig: strin
return false;
};

export const isNavGroupInFeatureConfigs = (navGroupId: string, featureConfigs: string[]) =>
featureConfigs.includes(getUseCaseFeatureConfig(navGroupId));

/**
* Checks if a given feature matches the provided feature configuration.
*
Expand Down

0 comments on commit d1821b5

Please sign in to comment.