From 3f7a55a145a63174bca4396e4f79bdc397a830ba Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Tue, 25 Jul 2023 07:54:21 +0800 Subject: [PATCH] feat: do not register app when feature flag is off (#56) * feat: do not register app when feature flag is off Signed-off-by: SuZhou-Joe * feat: comply with the category name Signed-off-by: SuZhou-Joe * feat: opt according to PR Signed-off-by: SuZhou-Joe * feat: optimize the comment Signed-off-by: SuZhou-Joe --------- Signed-off-by: SuZhou-Joe --- src/core/utils/default_app_categories.ts | 4 +- .../saved_objects_management/public/plugin.ts | 102 +++++----- src/plugins/workspace/public/plugin.ts | 186 ++++++++++-------- 3 files changed, 163 insertions(+), 129 deletions(-) diff --git a/src/core/utils/default_app_categories.ts b/src/core/utils/default_app_categories.ts index 0b9e5d8b2817..68531107a5f1 100644 --- a/src/core/utils/default_app_categories.ts +++ b/src/core/utils/default_app_categories.ts @@ -35,8 +35,8 @@ import { AppCategory } from '../types'; export const DEFAULT_APP_CATEGORIES: Record = Object.freeze({ opensearchDashboards: { id: 'opensearchDashboards', - label: i18n.translate('core.ui.libraryNavList.label', { - defaultMessage: 'Library', + label: i18n.translate('core.ui.opensearchDashboardsNavList.label', { + defaultMessage: 'OpenSearch Dashboards', }), order: 1000, }, diff --git a/src/plugins/saved_objects_management/public/plugin.ts b/src/plugins/saved_objects_management/public/plugin.ts index 157e1ccf33a6..e3263f396512 100644 --- a/src/plugins/saved_objects_management/public/plugin.ts +++ b/src/plugins/saved_objects_management/public/plugin.ts @@ -105,53 +105,10 @@ export class SavedObjectsManagementPlugin private namespaceService = new SavedObjectsManagementNamespaceService(); private serviceRegistry = new SavedObjectsManagementServiceRegistry(); - public setup( - core: CoreSetup, - { home, management, uiActions }: SetupDependencies - ): SavedObjectsManagementPluginSetup { - const actionSetup = this.actionService.setup(); - const columnSetup = this.columnService.setup(); - const namespaceSetup = this.namespaceService.setup(); - - if (home) { - home.featureCatalogue.register({ - id: 'saved_objects', - title: i18n.translate('savedObjectsManagement.objects.savedObjectsTitle', { - defaultMessage: 'Saved Objects', - }), - description: i18n.translate('savedObjectsManagement.objects.savedObjectsDescription', { - defaultMessage: - 'Import, export, and manage your saved searches, visualizations, and dashboards.', - }), - icon: 'savedObjectsApp', - path: '/app/management/opensearch-dashboards/objects', - showOnHomePage: false, - category: FeatureCatalogueCategory.ADMIN, - }); - } - - const opensearchDashboardsSection = management.sections.section.opensearchDashboards; - opensearchDashboardsSection.registerApp({ - id: 'objects', - title: i18n.translate('savedObjectsManagement.managementSectionLabel', { - defaultMessage: 'Saved objects', - }), - order: 1, - mount: async (mountParams) => { - const { mountManagementSection } = await import('./management_section'); - return mountManagementSection({ - core, - serviceRegistry: this.serviceRegistry, - mountParams, - title: i18n.translate('savedObjectsManagement.managementSectionLabel', { - defaultMessage: 'Saved Objects', - }), - }); - }, - }); - - // sets up the context mappings and registers any triggers/actions for the plugin - bootstrap(uiActions); + private registerLibrarySubApp( + coreSetup: CoreSetup + ) { + const core = coreSetup; const mountWrapper = ({ title, allowedObjectTypes, @@ -208,6 +165,57 @@ export class SavedObjectsManagementPlugin allowedObjectTypes: ['query'], }), }); + } + + public setup( + core: CoreSetup, + { home, management, uiActions }: SetupDependencies + ): SavedObjectsManagementPluginSetup { + const actionSetup = this.actionService.setup(); + const columnSetup = this.columnService.setup(); + const namespaceSetup = this.namespaceService.setup(); + const isWorkspaceEnabled = core.uiSettings.get('workspace:enabled'); + + if (home) { + home.featureCatalogue.register({ + id: 'saved_objects', + title: i18n.translate('savedObjectsManagement.objects.savedObjectsTitle', { + defaultMessage: 'Saved Objects', + }), + description: i18n.translate('savedObjectsManagement.objects.savedObjectsDescription', { + defaultMessage: + 'Import, export, and manage your saved searches, visualizations, and dashboards.', + }), + icon: 'savedObjectsApp', + path: '/app/management/opensearch-dashboards/objects', + showOnHomePage: false, + category: FeatureCatalogueCategory.ADMIN, + }); + } + + const opensearchDashboardsSection = management.sections.section.opensearchDashboards; + opensearchDashboardsSection.registerApp({ + id: 'objects', + title: i18n.translate('savedObjectsManagement.managementSectionLabel', { + defaultMessage: 'Saved objects', + }), + order: 1, + mount: async (mountParams) => { + const { mountManagementSection } = await import('./management_section'); + return mountManagementSection({ + core, + serviceRegistry: this.serviceRegistry, + mountParams, + title: SAVED_OBJECT_MANAGEMENT_TITLE_WORDINGS, + }); + }, + }); + + // sets up the context mappings and registers any triggers/actions for the plugin + bootstrap(uiActions); + if (isWorkspaceEnabled) { + this.registerLibrarySubApp(core); + } // depends on `getStartServices`, should not be awaited registerServices(this.serviceRegistry, core.getStartServices); diff --git a/src/plugins/workspace/public/plugin.ts b/src/plugins/workspace/public/plugin.ts index e154796fe783..70f0cbfc4cc9 100644 --- a/src/plugins/workspace/public/plugin.ts +++ b/src/plugins/workspace/public/plugin.ts @@ -6,6 +6,7 @@ import { i18n } from '@osd/i18n'; import type { Subscription } from 'rxjs'; import { combineLatest } from 'rxjs'; +import { map } from 'rxjs/operators'; import { ApplicationStart, AppMountParameters, @@ -16,6 +17,7 @@ import { Plugin, WorkspaceAttribute, WorkspacesStart, + DEFAULT_APP_CATEGORIES, } from '../../../core/public'; import { PATHS, WORKSPACE_APP_ID, WORKSPACE_NAV_CATEGORY } from '../common/constants'; import { mountDropdownList } from './mount'; @@ -157,90 +159,92 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep if (workspaceEnabled) { const workspaceList$ = core.workspaces.client.workspaceList$; const currentWorkspace$ = core.workspaces.client.currentWorkspace$; - combineLatest([workspaceList$, chromeNavLinks$, currentWorkspace$]).subscribe( - ([workspaceList, chromeNavLinks, currentWorkspace]) => { - const filteredNavLinks = new Map(); - 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: 1000, - }; - filteredNavLinks.set(chromeNavLink.id, homeNavLink); - } else { - filteredNavLinks.set(chromeNavLink.id, chromeNavLink); - } - }); - workspaceList - .filter((value, index) => !currentWorkspace && index < 5) - .map((workspace) => - this.workspaceToChromeNavLink(workspace, core.workspaces, core.application) - ) - .forEach((workspaceNavLink) => - filteredNavLinks.set(workspaceNavLink.id, workspaceNavLink) - ); - // See more - const seeMoreId = WORKSPACE_APP_ID + PATHS.list; - const seeMoreUrl = WORKSPACE_APP_ID + PATHS.list; - const seeMoreNavLink: ChromeNavLink = { - id: seeMoreId, - title: i18n.translate('core.ui.workspaceNavList.seeMore', { - defaultMessage: 'SEE MORE', - }), - hidden: currentWorkspace !== null, - disabled: false, - baseUrl: seeMoreUrl, - href: seeMoreUrl, - category: WORKSPACE_NAV_CATEGORY, - }; - filteredNavLinks.set(seeMoreId, seeMoreNavLink); - // Admin - const adminId = 'admin'; - const adminUrl = '/app/admin'; - const adminNavLink: ChromeNavLink = { - id: adminId, - title: i18n.translate('core.ui.workspaceNavList.admin', { - defaultMessage: 'Admin', - }), - hidden: currentWorkspace !== null, - 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', + combineLatest([ + workspaceList$, + chromeNavLinks$.pipe(map(this.changeCategoryNameByWorkspaceFeatureFlag)), + currentWorkspace$, + ]).subscribe(([workspaceList, chromeNavLinks, currentWorkspace]) => { + const filteredNavLinks = new Map(); + 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: 1000, }; - filteredNavLinks.set(overviewId, overviewNavLink); + filteredNavLinks.set(chromeNavLink.id, homeNavLink); + } else { + filteredNavLinks.set(chromeNavLink.id, chromeNavLink); } - navLinksService.setFilteredNavLinks(filteredNavLinks); + }); + workspaceList + .filter((value, index) => !currentWorkspace && index < 5) + .map((workspace) => + this.workspaceToChromeNavLink(workspace, core.workspaces, core.application) + ) + .forEach((workspaceNavLink) => + filteredNavLinks.set(workspaceNavLink.id, workspaceNavLink) + ); + // See more + const seeMoreId = WORKSPACE_APP_ID + PATHS.list; + const seeMoreUrl = WORKSPACE_APP_ID + PATHS.list; + const seeMoreNavLink: ChromeNavLink = { + id: seeMoreId, + title: i18n.translate('core.ui.workspaceNavList.seeMore', { + defaultMessage: 'SEE MORE', + }), + hidden: currentWorkspace !== null, + disabled: false, + baseUrl: seeMoreUrl, + href: seeMoreUrl, + category: WORKSPACE_NAV_CATEGORY, + }; + filteredNavLinks.set(seeMoreId, seeMoreNavLink); + // Admin + const adminId = 'admin'; + const adminUrl = '/app/admin'; + const adminNavLink: ChromeNavLink = { + id: adminId, + title: i18n.translate('core.ui.workspaceNavList.admin', { + defaultMessage: 'Admin', + }), + hidden: currentWorkspace !== null, + 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: 1000, + }; + filteredNavLinks.set(overviewId, overviewNavLink); } - ); + navLinksService.setFilteredNavLinks(filteredNavLinks); + }); } else { chromeNavLinks$.subscribe((chromeNavLinks) => { const filteredNavLinks = new Map(); @@ -252,6 +256,28 @@ export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDep } } + /** + * The category "Opensearch Dashboards" needs to be renamed as "Library" + * when workspace feature flag is on, we need to do it here and generate + * a new item without polluting the original ChromeNavLink. + */ + private changeCategoryNameByWorkspaceFeatureFlag(chromeLinks: ChromeNavLink[]): ChromeNavLink[] { + return chromeLinks.map((item) => { + if (item.category?.id === DEFAULT_APP_CATEGORIES.opensearchDashboards.id) { + return { + ...item, + category: { + ...item.category, + label: i18n.translate('core.ui.libraryNavList.label', { + defaultMessage: 'Library', + }), + }, + }; + } + return item; + }); + } + public start(core: CoreStart) { // If workspace feature is disabled, it will not load the workspace plugin if (core.uiSettings.get('workspace:enabled') === false) {