Skip to content

Commit

Permalink
Refactor: Assign default value for filtered nav links (opensearch-pro…
Browse files Browse the repository at this point in the history
…ject#64)

* remove default filtered nav link value set

Signed-off-by: yuye-aws <yuyezhu@amazon.com>

* default value for nav link

Signed-off-by: yuye-aws <yuyezhu@amazon.com>

* refactor currentworkspace logic

Signed-off-by: yuye-aws <yuyezhu@amazon.com>

---------

Signed-off-by: yuye-aws <yuyezhu@amazon.com>
  • Loading branch information
yuye-aws authored and SuZhou-Joe committed Aug 31, 2023
1 parent 9d0af66 commit fbd1155
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 67 deletions.
7 changes: 6 additions & 1 deletion src/core/public/chrome/nav_links/nav_links_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,12 @@ export class NavLinksService {
},

getFilteredNavLinks$: () => {
return this.filteredNavLinks$.pipe(map(sortChromeNavLinks), takeUntil(this.stop$));
return combineLatest([navLinks$, this.filteredNavLinks$]).pipe(
map(([navLinks, filteredNavLinks]) =>
filteredNavLinks.size ? sortChromeNavLinks(filteredNavLinks) : sortNavLinks(navLinks)
),
takeUntil(this.stop$)
);
},

get(id: string) {
Expand Down
121 changes: 55 additions & 66 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,35 +153,58 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep
return allNavLinks.filter((item) => features.includes(item.id));
}

private filterNavLinks(core: CoreStart, workspaceEnabled: boolean) {
private filterNavLinks(core: CoreStart) {
const navLinksService = core.chrome.navLinks;
const chromeNavLinks$ = navLinksService.getNavLinks$();
if (workspaceEnabled) {
const workspaceList$ = core.workspaces.client.workspaceList$;
const currentWorkspace$ = core.workspaces.client.currentWorkspace$;
combineLatest([
workspaceList$,
chromeNavLinks$.pipe(map(this.changeCategoryNameByWorkspaceFeatureFlag)),
currentWorkspace$,
]).subscribe(([workspaceList, chromeNavLinks, currentWorkspace]) => {
const filteredNavLinks = new Map<string, ChromeNavLink>();
chromeNavLinks = this.filterByWorkspace(currentWorkspace, chromeNavLinks);
chromeNavLinks.forEach((chromeNavLink) => {
if (chromeNavLink.id === 'home') {
// set hidden, icon and order for home nav link
const homeNavLink: ChromeNavLink = {
...chromeNavLink,
hidden: currentWorkspace !== null,
euiIconType: 'logoOpenSearch',
order: 0,
};
filteredNavLinks.set(chromeNavLink.id, homeNavLink);
} else {
filteredNavLinks.set(chromeNavLink.id, chromeNavLink);
}
});
const workspaceList$ = core.workspaces.client.workspaceList$;
const currentWorkspace$ = core.workspaces.client.currentWorkspace$;
combineLatest([
workspaceList$,
chromeNavLinks$.pipe(map(this.changeCategoryNameByWorkspaceFeatureFlag)),
currentWorkspace$,
]).subscribe(([workspaceList, chromeNavLinks, currentWorkspace]) => {
const filteredNavLinks = new Map<string, ChromeNavLink>();
chromeNavLinks = this.filterByWorkspace(currentWorkspace, chromeNavLinks);
chromeNavLinks.forEach((chromeNavLink) => {
if (chromeNavLink.id === 'home') {
// set hidden, icon and order for home nav link
const homeNavLink: ChromeNavLink = {
...chromeNavLink,
hidden: currentWorkspace !== null,
euiIconType: 'logoOpenSearch',
order: 0,
};
filteredNavLinks.set(chromeNavLink.id, homeNavLink);
} else {
filteredNavLinks.set(chromeNavLink.id, chromeNavLink);
}
});
if (currentWorkspace) {
// Overview only inside workspace
const overviewId = WORKSPACE_APP_ID + PATHS.update;
const overviewUrl = core.workspaces.formatUrlWithWorkspaceId(
core.application.getUrlForApp(WORKSPACE_APP_ID, {
path: PATHS.update,
absolute: true,
}),
currentWorkspace.id
);
const overviewNavLink: ChromeNavLink = {
id: overviewId,
title: i18n.translate('core.ui.workspaceNavList.overview', {
defaultMessage: 'Overview',
}),
hidden: false,
disabled: false,
baseUrl: overviewUrl,
href: overviewUrl,
euiIconType: 'grid',
order: 0,
};
filteredNavLinks.set(overviewId, overviewNavLink);
} else {
workspaceList
.filter((value, index) => !currentWorkspace && index < 5)
.filter((workspace, index) => index < 5)
.map((workspace) =>
this.workspaceToChromeNavLink(workspace, core.workspaces, core.application)
)
Expand All @@ -196,7 +219,7 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep
title: i18n.translate('core.ui.workspaceNavList.seeMore', {
defaultMessage: 'SEE MORE',
}),
hidden: currentWorkspace !== null,
hidden: false,
disabled: false,
baseUrl: seeMoreUrl,
href: seeMoreUrl,
Expand All @@ -211,49 +234,17 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep
title: i18n.translate('core.ui.workspaceNavList.admin', {
defaultMessage: 'Admin',
}),
hidden: currentWorkspace !== null,
hidden: false,
disabled: true,
baseUrl: adminUrl,
href: adminUrl,
euiIconType: 'managementApp',
order: 9000,
};
filteredNavLinks.set(adminId, adminNavLink);
// Overview only inside workspace
if (currentWorkspace) {
const overviewId = WORKSPACE_APP_ID + PATHS.update;
const overviewUrl = core.workspaces.formatUrlWithWorkspaceId(
core.application.getUrlForApp(WORKSPACE_APP_ID, {
path: PATHS.update,
absolute: true,
}),
currentWorkspace.id
);
const overviewNavLink: ChromeNavLink = {
id: overviewId,
title: i18n.translate('core.ui.workspaceNavList.overview', {
defaultMessage: 'Overview',
}),
hidden: false,
disabled: false,
baseUrl: overviewUrl,
href: overviewUrl,
euiIconType: 'grid',
order: 0,
};
filteredNavLinks.set(overviewId, overviewNavLink);
}
navLinksService.setFilteredNavLinks(filteredNavLinks);
});
} else {
chromeNavLinks$.subscribe((chromeNavLinks) => {
const filteredNavLinks = new Map<string, ChromeNavLink>();
chromeNavLinks.forEach((chromeNavLink) =>
filteredNavLinks.set(chromeNavLink.id, chromeNavLink)
);
navLinksService.setFilteredNavLinks(filteredNavLinks);
});
}
}
navLinksService.setFilteredNavLinks(filteredNavLinks);
});
}

/**
Expand Down Expand Up @@ -281,8 +272,6 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep
public start(core: CoreStart) {
// If workspace feature is disabled, it will not load the workspace plugin
if (core.uiSettings.get('workspace:enabled') === false) {
// set default value for filtered nav links
this.filterNavLinks(core, false);
return {};
}

Expand All @@ -295,7 +284,7 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep
});
this.currentWorkspaceSubscription = this._changeSavedObjectCurrentWorkspace();
if (core) {
this.filterNavLinks(core, true);
this.filterNavLinks(core);
}
return {};
}
Expand Down

0 comments on commit fbd1155

Please sign in to comment.