Skip to content

Commit

Permalink
tweak comments
Browse files Browse the repository at this point in the history
Signed-off-by: Yulong Ruan <ruanyl@amazon.com>
  • Loading branch information
ruanyl committed Mar 8, 2024
1 parent ba53768 commit f29b6da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
27 changes: 15 additions & 12 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
return getWorkspaceIdFromUrl(window.location.href);
}

private filterByWorkspace(workspace: WorkspaceObject | null, allNavLinks: NavLinkWrapper[]) {
if (!workspace) return allNavLinks;
const features = workspace.features ?? ['*'];
return allNavLinks.filter(featureMatchesConfig(features));
/**
* Filter the nav links based on the feature configuration of workspace
*/
private filterByWorkspace(allNavLinks: NavLinkWrapper[], workspace: WorkspaceObject | null) {
if (!workspace || !workspace.features) return allNavLinks;

const featureFilter = featureMatchesConfig(workspace.features);
return allNavLinks.filter((linkWrapper) => featureFilter(linkWrapper.properties));
}

/**
* Filter nav links by currentWorkspace$, once the current workspace change, the nav links(left nav bar)
* Filter nav links by the current workspace, once the current workspace change, the nav links(left nav bar)
* should also be updated according to the configured features of the current workspace
*/
private filterNavLinks(core: CoreStart) {
Expand All @@ -57,13 +61,12 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
linkUpdaters = linkUpdaters.filter((updater) => updater !== filterLinksByWorkspace);

/**
* Whenever workspace changed, this function: `linkFilter` will filter out those links that should not
* Whenever workspace changed, this function will filter out those links that should not
* be displayed. For example, some workspace may not have Observability features configured, in such case,
* the nav links to Observability features should not be displayed in left nav bar
*
* the nav links of Observability features should not be displayed in left nav bar
*/
filterLinksByWorkspace = (navLinks) => {
const filteredNavLinks = this.filterByWorkspace(currentWorkspace, [...navLinks.values()]);
const filteredNavLinks = this.filterByWorkspace([...navLinks.values()], currentWorkspace);
const newNavLinks = new Map<string, NavLinkWrapper>();
filteredNavLinks.forEach((chromeNavLink) => {
newNavLinks.set(chromeNavLink.id, chromeNavLink);
Expand Down Expand Up @@ -165,9 +168,9 @@ export class WorkspacePlugin implements Plugin<{}, {}> {
this.coreStart = core;

this.currentWorkspaceIdSubscription = this._changeSavedObjectCurrentWorkspace();
if (core) {
this.filterNavLinks(core);
}

// When starts, filter the nav links based on the current workspace
this.filterNavLinks(core);
return {};
}

Expand Down
15 changes: 8 additions & 7 deletions src/plugins/workspace/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import { AppCategory } from '../../../core/public';

/**
* Given a list of feature config, check if a feature matches config
* Checks if a given feature matches the provided feature configuration.
*
* Rules:
* 1. `*` matches any feature
* 2. config starts with `@` matches category, for example, @management matches any feature of `management` category
* 3. to match a specific feature, just use the feature id, such as `discover`
* 4. to exclude feature or category, use `!@management` or `!discover`
* 5. the order of featureConfig array matters, from left to right, the later config override the previous config,
* for example, ['!@management', '*'] matches any feature because '*' overrides the previous setting: '!@management'
* 1. `*` matches any feature.
* 2. Config starts with `@` matches category, for example, @management matches any feature of `management` category,
* 3. To match a specific feature, use the feature id, such as `discover`,
* 4. To exclude a feature or category, prepend with `!`, e.g., `!discover` or `!@management`.
* 5. The order of featureConfig array matters. From left to right, later configs override the previous ones.
* For example, ['!@management', '*'] matches any feature because '*' overrides the previous setting: '!@management'.
*/
export const featureMatchesConfig = (featureConfigs: string[]) => ({
id,
Expand Down

0 comments on commit f29b6da

Please sign in to comment.