From c08a0c013562699d2453f0785dba17d9991ea878 Mon Sep 17 00:00:00 2001 From: yubonluo Date: Tue, 22 Oct 2024 16:07:01 +0800 Subject: [PATCH 1/4] Fix non-workspace admin update observability:defaultDashboard Signed-off-by: yubonluo --- public/components/overview/home.tsx | 46 ++++++++++++++++++----------- public/framework/core_refs.ts | 2 ++ public/plugin.tsx | 1 + 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/public/components/overview/home.tsx b/public/components/overview/home.tsx index aa50a01e7..588fe62e1 100644 --- a/public/components/overview/home.tsx +++ b/public/components/overview/home.tsx @@ -11,7 +11,7 @@ import { EuiButtonEmpty, EuiToolTip, } from '@elastic/eui'; -import React, { ReactNode, useEffect, useState } from 'react'; +import React, { ReactNode, useEffect, useMemo, useState } from 'react'; import { HashRouter, Route, Switch } from 'react-router-dom'; import { useObservable } from 'react-use'; import { EMPTY } from 'rxjs'; @@ -52,6 +52,14 @@ export const Home = () => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [showGetStarted, setShowGetStarted] = useState(null); // Initial null state + // When workspace enabled, only workspace owner can update observability:defaultDashboard. + const isWorkspaceOwner = useMemo(() => { + const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; + const isDashboardAdmin = + coreRefs.application?.capabilities?.dashboards?.isDashboardAdmin !== false; + return isCurrentWorkspaceOwner || isDashboardAdmin; + }, [coreRefs.workspaces, coreRefs.application?.capabilities]); + ObsDashboardStateManager.showFlyout$.next(() => () => setIsFlyoutVisible(true)); const loadHomePage = () => { @@ -95,14 +103,16 @@ export const Home = () => { }); ObsDashboardStateManager.isDashboardSelected$.next(true); } else { - setObservabilityDashboardsId(null); - ObsDashboardStateManager.dashboardState$.next({ - startDate: '', - endDate: '', - dashboardTitle: '', - dashboardId: '', - }); - ObsDashboardStateManager.isDashboardSelected$.next(false); + if (isWorkspaceOwner) { + setObservabilityDashboardsId(null); + ObsDashboardStateManager.dashboardState$.next({ + startDate: '', + endDate: '', + dashboardTitle: '', + dashboardId: '', + }); + ObsDashboardStateManager.isDashboardSelected$.next(false); + } } }) .catch((error) => { @@ -316,14 +326,16 @@ export const Home = () => { }); ObsDashboardStateManager.isDashboardSelected$.next(true); } else { - setObservabilityDashboardsId(null); - ObsDashboardStateManager.dashboardState$.next({ - startDate: '', - endDate: '', - dashboardTitle: '', - dashboardId: '', - }); - ObsDashboardStateManager.isDashboardSelected$.next(false); + if (isWorkspaceOwner) { + setObservabilityDashboardsId(null); + ObsDashboardStateManager.dashboardState$.next({ + startDate: '', + endDate: '', + dashboardTitle: '', + dashboardId: '', + }); + ObsDashboardStateManager.isDashboardSelected$.next(false); + } } }) .catch((error) => { diff --git a/public/framework/core_refs.ts b/public/framework/core_refs.ts index f125f0f64..d9d104992 100644 --- a/public/framework/core_refs.ts +++ b/public/framework/core_refs.ts @@ -12,6 +12,7 @@ import { IToasts, OverlayStart, SavedObjectsClientContract, + WorkspacesStart, } from '../../../../src/core/public'; import { DashboardStart } from '../../../../src/plugins/dashboard/public'; import { DataSourcePluginStart } from '../../../../src/plugins/data_source/public'; @@ -38,6 +39,7 @@ class CoreRefs { public dataSource?: DataSourcePluginStart; public navigation?: NavigationPublicPluginStart; public contentManagement?: ContentManagementPluginStart; + public workspaces?: WorkspacesStart; private constructor() { // ... } diff --git a/public/plugin.tsx b/public/plugin.tsx index 3b0a0e253..fc491e644 100644 --- a/public/plugin.tsx +++ b/public/plugin.tsx @@ -459,6 +459,7 @@ export class ObservabilityPlugin coreRefs.dataSource = startDeps.dataSource; coreRefs.navigation = startDeps.navigation; coreRefs.contentManagement = startDeps.contentManagement; + coreRefs.workspaces = core.workspaces; // redirect trace URL based on new navigation if (window.location.pathname.includes(observabilityTracesID)) { From 6c6bc274752db3f7e9b3eb5a1d0702837f6dcc7c Mon Sep 17 00:00:00 2001 From: yubonluo Date: Tue, 22 Oct 2024 17:05:18 +0800 Subject: [PATCH 2/4] optimize the code Signed-off-by: yubonluo --- public/components/overview/home.tsx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/public/components/overview/home.tsx b/public/components/overview/home.tsx index 588fe62e1..d8f5f6a6c 100644 --- a/public/components/overview/home.tsx +++ b/public/components/overview/home.tsx @@ -52,12 +52,16 @@ export const Home = () => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [showGetStarted, setShowGetStarted] = useState(null); // Initial null state - // When workspace enabled, only workspace owner can update observability:defaultDashboard. - const isWorkspaceOwner = useMemo(() => { - const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; - const isDashboardAdmin = - coreRefs.application?.capabilities?.dashboards?.isDashboardAdmin !== false; - return isCurrentWorkspaceOwner || isDashboardAdmin; + // When workspace enabled, only workspace owner/OSD admin can update observability:defaultDashboard. + const canUpdateUiSetting = useMemo(() => { + if (coreRefs.application?.capabilities?.workspaces?.enabled) { + const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; + const isDashboardAdmin = + coreRefs.application?.capabilities?.dashboards?.isDashboardAdmin !== false; + return isCurrentWorkspaceOwner || isDashboardAdmin; + } else { + return true; + } }, [coreRefs.workspaces, coreRefs.application?.capabilities]); ObsDashboardStateManager.showFlyout$.next(() => () => setIsFlyoutVisible(true)); @@ -103,7 +107,7 @@ export const Home = () => { }); ObsDashboardStateManager.isDashboardSelected$.next(true); } else { - if (isWorkspaceOwner) { + if (canUpdateUiSetting) { setObservabilityDashboardsId(null); ObsDashboardStateManager.dashboardState$.next({ startDate: '', @@ -326,7 +330,7 @@ export const Home = () => { }); ObsDashboardStateManager.isDashboardSelected$.next(true); } else { - if (isWorkspaceOwner) { + if (canUpdateUiSetting) { setObservabilityDashboardsId(null); ObsDashboardStateManager.dashboardState$.next({ startDate: '', From 05893cfeba873df495274d41d79ed95e149d10df Mon Sep 17 00:00:00 2001 From: yubonluo Date: Tue, 22 Oct 2024 17:56:53 +0800 Subject: [PATCH 3/4] optimize the code Signed-off-by: yubonluo --- public/components/overview/home.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/public/components/overview/home.tsx b/public/components/overview/home.tsx index d8f5f6a6c..c58c06c00 100644 --- a/public/components/overview/home.tsx +++ b/public/components/overview/home.tsx @@ -52,17 +52,17 @@ export const Home = () => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [showGetStarted, setShowGetStarted] = useState(null); // Initial null state + const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; + const isDashboardAdmin = + coreRefs.application?.capabilities?.dashboards?.isDashboardAdmin !== false; // When workspace enabled, only workspace owner/OSD admin can update observability:defaultDashboard. const canUpdateUiSetting = useMemo(() => { - if (coreRefs.application?.capabilities?.workspaces?.enabled) { - const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; - const isDashboardAdmin = - coreRefs.application?.capabilities?.dashboards?.isDashboardAdmin !== false; + const capabilities = coreRefs.application?.capabilities; + if (capabilities?.workspaces?.enabled) { return isCurrentWorkspaceOwner || isDashboardAdmin; - } else { - return true; } - }, [coreRefs.workspaces, coreRefs.application?.capabilities]); + return true; + }, [isCurrentWorkspaceOwner, isDashboardAdmin]); ObsDashboardStateManager.showFlyout$.next(() => () => setIsFlyoutVisible(true)); From e3c487237b186397f88568865e50e45a6f87d120 Mon Sep 17 00:00:00 2001 From: yubonluo Date: Tue, 22 Oct 2024 18:08:26 +0800 Subject: [PATCH 4/4] optimize the code Signed-off-by: yubonluo --- public/components/overview/home.tsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/components/overview/home.tsx b/public/components/overview/home.tsx index c58c06c00..66256500a 100644 --- a/public/components/overview/home.tsx +++ b/public/components/overview/home.tsx @@ -52,17 +52,17 @@ export const Home = () => { const [isPopoverOpen, setIsPopoverOpen] = useState(false); const [showGetStarted, setShowGetStarted] = useState(null); // Initial null state - const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; - const isDashboardAdmin = - coreRefs.application?.capabilities?.dashboards?.isDashboardAdmin !== false; - // When workspace enabled, only workspace owner/OSD admin can update observability:defaultDashboard. const canUpdateUiSetting = useMemo(() => { const capabilities = coreRefs.application?.capabilities; + + // When workspace enabled, only workspace owner/OSD admin can update observability:defaultDashboard. if (capabilities?.workspaces?.enabled) { + const isCurrentWorkspaceOwner = coreRefs.workspaces?.currentWorkspace$.getValue()?.owner; + const isDashboardAdmin = capabilities?.dashboards?.isDashboardAdmin !== false; return isCurrentWorkspaceOwner || isDashboardAdmin; } return true; - }, [isCurrentWorkspaceOwner, isDashboardAdmin]); + }, [coreRefs.workspaces?.currentWorkspace$, coreRefs.application?.capabilities]); ObsDashboardStateManager.showFlyout$.next(() => () => setIsFlyoutVisible(true));