Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] Only show agent dashboard links if there is more than one non-server agent and if the dashboards exist #164469

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion x-pack/plugins/fleet/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"taskManager",
"guidedOnboarding",
"files",
"uiActions"
"uiActions",
"dashboard"
],
"optionalPlugins": [
"features",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,37 @@
* 2.0.
*/

import React from 'react';
import React, { useEffect } from 'react';
import { EuiFlexGroup, EuiFlexItem, EuiButtonEmpty } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

import { DASHBOARD_LOCATORS_IDS } from '../../../../../../../common/constants';

import { useDashboardLocator } from '../../../../hooks';
import { useDashboardLocator, useStartServices } from '../../../../hooks';

const useDashboardExists = (dashboardId: string) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as this hook is only used here I kept it in the same file, happy to move it to its own file?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can stay in this file and can be moved if we need to reuse it elsewhere

const [dashboardExists, setDashboardExists] = React.useState<boolean>(false);
const [loading, setLoading] = React.useState<boolean>(true);
const { dashboard: dashboardPlugin } = useStartServices();

useEffect(() => {
const fetchDashboard = async () => {
try {
const findDashboardsService = await dashboardPlugin.findDashboardsService();
const [dashboard] = await findDashboardsService.findByIds([dashboardId]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use the dashboard service!

setLoading(false);
setDashboardExists(dashboard?.status === 'success');
} catch (e) {
setLoading(false);
setDashboardExists(false);
}
};

fetchDashboard();
}, [dashboardId, dashboardPlugin]);

return { dashboardExists, loading };
};

export const DashboardsButtons: React.FunctionComponent = () => {
const dashboardLocator = useDashboardLocator();
Expand All @@ -20,6 +44,14 @@ export const DashboardsButtons: React.FunctionComponent = () => {
return dashboardLocator?.getRedirectUrl({ dashboardId }) || '';
};

const { dashboardExists, loading: dashboardLoading } = useDashboardExists(
DASHBOARD_LOCATORS_IDS.ELASTIC_AGENT_OVERVIEW
);

if (dashboardLoading || !dashboardExists) {
return null;
}

return (
<>
<EuiFlexGroup gutterSize="s" justifyContent="flexStart">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
import { FormattedMessage } from '@kbn/i18n-react';
import styled from 'styled-components';

import { useIsFirstTimeAgentUserQuery } from '../../../../../integrations/sections/epm/screens/detail/hooks';

import type { Agent, AgentPolicy } from '../../../../types';
import { SearchBar } from '../../../../components';
import { AGENTS_INDEX, AGENTS_PREFIX } from '../../../../constants';
Expand Down Expand Up @@ -94,6 +96,8 @@ export const SearchAndFilterBar: React.FunctionComponent<SearchAndFilterBarProps
}) => {
const { euiTheme } = useEuiTheme();
const { isFleetServerStandalone } = useFleetServerStandalone();
const { isFirstTimeAgentUser, isLoading: isFirstTimeAgentUserLoading } =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isFirstTimeAgentUser checks if the user has at least one non-fleet-server agent

useIsFirstTimeAgentUserQuery();
const showAddFleetServerBtn = !isFleetServerStandalone;

// Policies state for filtering
Expand Down Expand Up @@ -126,7 +130,9 @@ export const SearchAndFilterBar: React.FunctionComponent<SearchAndFilterBarProps
<EuiFlexGroup direction="column">
{/* Top Buttons and Links */}
<EuiFlexGroup>
<EuiFlexItem>{totalAgents > 0 && <DashboardsButtons />}</EuiFlexItem>
<EuiFlexItem>
{!isFirstTimeAgentUserLoading && !isFirstTimeAgentUser && <DashboardsButtons />}
</EuiFlexItem>
<EuiFlexGroup gutterSize="s" justifyContent="flexEnd">
<EuiFlexItem grow={false}>
<AgentActivityButton
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/fleet/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import type { SendRequestResponse } from '@kbn/es-ui-shared-plugin/public';
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type { GuidedOnboardingPluginStart } from '@kbn/guided-onboarding-plugin/public';

import type { DashboardStart } from '@kbn/dashboard-plugin/public';

import { PLUGIN_ID, INTEGRATIONS_PLUGIN_ID, setupRouteService, appRoutesService } from '../common';
import { calculateAuthz, calculatePackagePrivilegesFromCapabilities } from '../common/authz';
import { parseExperimentalConfigValue } from '../common/experimental_features';
Expand Down Expand Up @@ -128,6 +130,7 @@ export interface FleetStartDeps {
export interface FleetStartServices extends CoreStart, Exclude<FleetStartDeps, 'cloud'> {
storage: Storage;
share: SharePluginStart;
dashboard: DashboardStart;
cloud?: CloudSetup & CloudStart;
discover?: DiscoverStart;
spaces?: SpacesPluginStart;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,6 @@
"@kbn/core-http-router-server-mocks",
"@kbn/core-application-browser",
"@kbn/core-saved-objects-base-server-internal",
"@kbn/dashboard-plugin",
]
}