Skip to content

Commit

Permalink
Fixed pinning agent in NavigationService, fixed breadcrumb when there…
Browse files Browse the repository at this point in the history
… is no agent data and it is elevated to parent component withGuard of unselected agent
  • Loading branch information
jbiset committed Jun 25, 2024
1 parent f9e02d6 commit aab373f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 163 deletions.
26 changes: 0 additions & 26 deletions plugins/main/public/components/agents/stats/agent-stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
EuiPageBody,
EuiSpacer,
EuiText,
EuiLink,
} from '@elastic/eui';

import {
Expand All @@ -34,7 +33,6 @@ import { AgentStatTable } from './table';
import {
PromptNoActiveAgentWithoutSelect,
PromptAgentFeatureVersion,
PromptNoSelectedAgent,
} from '../prompts';
import {
UIErrorLog,
Expand All @@ -49,8 +47,6 @@ import {
import { getErrorOrchestrator } from '../../../react-services/common-services';
import { endpointSummary } from '../../../utils/applications';
import NavigationService from '../../../react-services/navigation-service';
import { getCore } from '../../../kibana-services';
import { RedirectAppLinks } from '../../../../../../src/plugins/opensearch_dashboards_react/public';

const tableColumns = [
{
Expand Down Expand Up @@ -119,28 +115,6 @@ export const MainAgentStats = compose(
text: 'Stats',
},
]),
withGuard(
props => !(props.agent && props.agent.id),
() => (
<>
<PromptNoSelectedAgent
body={
<>
You need to select an agent or return to
<RedirectAppLinks application={getCore().application}>
<EuiLink
aria-label='go to Endpoint summary'
href={`${endpointSummary.id}#/agents-preview`}
>
Endpoint summary
</EuiLink>
</RedirectAppLinks>
</>
}
/>
</>
),
),
withUserAuthorizationPrompt(({ agent }) => [
[
{ action: 'agent:read', resource: `agent:id:${agent.id}` },
Expand Down
32 changes: 1 addition & 31 deletions plugins/main/public/components/agents/syscollector/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@
*/

import React from 'react';
import {
withErrorBoundary,
withGlobalBreadcrumb,
withGuard,
} from '../../common/hocs';
import { withErrorBoundary, withGlobalBreadcrumb } from '../../common/hocs';
import { SyscollectorInventory } from './inventory';
import { compose } from 'redux';
import { endpointSummary } from '../../../utils/applications';
import NavigationService from '../../../react-services/navigation-service';
import { PromptNoSelectedAgent } from '../prompts';
import { getCore } from '../../../kibana-services';
import { EuiLink } from '@elastic/eui';
import { RedirectAppLinks } from '../../../../../../src/plugins/opensearch_dashboards_react/public';

export const MainSyscollector = compose(
withErrorBoundary,
Expand All @@ -41,26 +33,4 @@ export const MainSyscollector = compose(
},
];
}),
withGuard(
props => !(props.agent && props.agent.id),
() => (
<>
<PromptNoSelectedAgent
body={
<>
You need to select an agent or return to
<RedirectAppLinks application={getCore().application}>
<EuiLink
aria-label='go to Endpoint summary'
href={`${endpointSummary.id}#/agents-preview`}
>
Endpoint summary
</EuiLink>
</RedirectAppLinks>
</>
}
/>
</>
),
),
)(SyscollectorInventory);
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@ export const setBreadcrumbs = (breadcrumbs, router) => {
if (breadcrumbs === '' || breadcrumbs === undefined) {
return;
}
const breadcrumbsCustom = breadcrumbs?.map(breadcrumb =>
breadcrumb.agent
? {
className:
'euiLink euiLink--subdued osdBreadcrumbs wz-vertical-align-middle',
onClick: ev => {
ev.stopPropagation();
if (getWzCurrentAppID() === endpointSummary.id) {
NavigationService.getInstance().navigate(
`/agents?tab=welcome&agent=${breadcrumb.agent.id}`,
);
} else {
NavigationService.getInstance().navigateToApp(
endpointSummary.id,
{
path: `#/agents?tab=welcome&agent=${breadcrumb.agent.id}`,
},
);
}
const breadcrumbsCustom = breadcrumbs
?.map(breadcrumb =>
breadcrumb?.agent?.id
? {
className:
'euiLink euiLink--subdued osdBreadcrumbs wz-vertical-align-middle',
onClick: ev => {
ev.stopPropagation();
if (getWzCurrentAppID() === endpointSummary.id) {
NavigationService.getInstance().navigate(
`/agents?tab=welcome&agent=${breadcrumb.agent.id}`,
);
} else {
NavigationService.getInstance().navigateToApp(
endpointSummary.id,
{
path: `#/agents?tab=welcome&agent=${breadcrumb.agent.id}`,
},
);
}
},
truncate: true,
text: breadcrumb.agent.name,
}
: typeof breadcrumb.agent !== 'undefined'
? null
: {
...breadcrumb,
className: 'osdBreadcrumbs',
},
truncate: true,
text: breadcrumb.agent.name,
}
: {
...breadcrumb,
className: 'osdBreadcrumbs',
},
);
)
.filter(value => value);

getCore().chrome.setBreadcrumbs(breadcrumbsCustom);

Expand Down
28 changes: 1 addition & 27 deletions plugins/main/public/components/common/welcome/agents-welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
EuiToolTip,
EuiButtonIcon,
EuiPageBody,
EuiLink,
} from '@elastic/eui';
import {
FimEventsTable,
Expand All @@ -43,10 +42,7 @@ import { withErrorBoundary, withGlobalBreadcrumb, withGuard } from '../hocs';
import { compose } from 'redux';
import { API_NAME_AGENT_STATUS } from '../../../../common/constants';
import { WAZUH_MODULES } from '../../../../common/wazuh-modules';
import {
PromptAgentNeverConnected,
PromptNoSelectedAgent,
} from '../../agents/prompts';
import { PromptAgentNeverConnected } from '../../agents/prompts';
import { WzButton } from '../buttons';
import {
Applications,
Expand Down Expand Up @@ -83,28 +79,6 @@ export const AgentsWelcome = compose(
: []),
];
}),
withGuard(
props => !(props.agent && props.agent.id),
() => (
<>
<PromptNoSelectedAgent
body={
<>
You need to select an agent or return to
<RedirectAppLinks application={getCore().application}>
<EuiLink
aria-label='go to Endpoint summary'
href={`${endpointSummary.id}#/agents-preview`}
>
Endpoint summary
</EuiLink>
</RedirectAppLinks>
</>
}
/>
</>
),
),
withGuard(
props => props.agent.status === API_NAME_AGENT_STATUS.NEVER_CONNECTED,
PromptAgentNeverConnected,
Expand Down
70 changes: 54 additions & 16 deletions plugins/main/public/components/endpoints-summary/agent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import React, { useState, useEffect } from 'react';
import { EuiPage, EuiPageBody, EuiProgress } from '@elastic/eui';
import { EuiPage, EuiPageBody, EuiProgress, EuiLink } from '@elastic/eui';
import { AgentsWelcome } from '../../common/welcome/agents-welcome';
import { Agent } from '../types';
import { MainSyscollector } from '../../agents/syscollector/main';
import { MainAgentStats } from '../../agents/stats';
import WzManagementConfiguration from '../../../controllers/management/components/management/configuration/configuration-main.js';
import { withErrorBoundary, withRouteResolvers } from '../../common/hocs';
import {
withErrorBoundary,
withGuard,
withRouteResolvers,
} from '../../common/hocs';
import { compose } from 'redux';
import { PinnedAgentManager } from '../../wz-agent-selector/wz-agent-selector-service';
import { MainModuleAgent } from '../../common/modules/main-agent';
Expand All @@ -18,11 +22,48 @@ import {
import { useRouterSearch } from '../../common/hooks/use-router-search';
import { Redirect, Route, Switch } from '../../router-search';
import NavigationService from '../../../react-services/navigation-service';
import { connect } from 'react-redux';
import { PromptNoSelectedAgent } from '../../agents/prompts';
import { RedirectAppLinks } from '../../../../../../src/plugins/opensearch_dashboards_react/public';
import { getCore } from '../../../kibana-services';
import { endpointSummary } from '../../../utils/applications';

const mapStateToProps = state => ({
agent: state.appStateReducers?.currentAgentData,
});

export const AgentView = compose(
withErrorBoundary,
withRouteResolvers({ enableMenu, ip, nestedResolve, savedSearch }),
)(() => {
connect(mapStateToProps),
withGuard(
props => !(props.agent && props.agent.id),
() => (
<>
<PromptNoSelectedAgent
body={
<>
You need to select an agent or return to
<RedirectAppLinks application={getCore().application}>
<EuiLink
aria-label='go to Endpoint summary'
href={`${endpointSummary.id}#/agents-preview`}
onClick={() =>
NavigationService.getInstance().navigate(
`${endpointSummary.id}#/agents-preview`,
)
}
>
Endpoint summary
</EuiLink>
</RedirectAppLinks>
</>
}
/>
</>
),
),
)(({ agent: agentData }) => {
const { tab = 'welcome' } = useRouterSearch();
const navigationService = NavigationService.getInstance();

Expand All @@ -35,23 +76,20 @@ export const AgentView = compose(

const pinnedAgentManager = new PinnedAgentManager();

const [agent, setAgent] = useState<Agent>();
const [isLoadingAgent, setIsLoadingAgent] = useState(true);

const getAgent = async () => {
const syncAgent = async () => {
setIsLoadingAgent(true);
await pinnedAgentManager.syncPinnedAgentSources();
const isPinnedAgent = pinnedAgentManager.isPinnedAgent();
setAgent(isPinnedAgent ? pinnedAgentManager.getPinnedAgent() : null);
setIsLoadingAgent(false);
};

useEffect(() => {
getAgent();
syncAgent();
}, [tab]);

const switchTab = (tab: string) => {
navigationService.navigate(`/agents?tab=${tab}&agent=${agent?.id}`);
navigationService.navigate(`/agents?tab=${tab}&agent=${agentData?.id}`);
};

if (isLoadingAgent) {
Expand All @@ -67,21 +105,21 @@ export const AgentView = compose(
return (
<Switch>
<Route path='?tab=syscollector'>
<MainModuleAgent agent={agent} section={tab} />
<MainSyscollector agent={agent} />
<MainModuleAgent agent={agentData} section={tab} />
<MainSyscollector agent={agentData} />
</Route>
<Route path='?tab=stats'>
<MainModuleAgent agent={agent} section={tab} />
<MainAgentStats agent={agent} />
<MainModuleAgent agent={agentData} section={tab} />
<MainAgentStats agent={agentData} />
</Route>
<Route path='?tab=configuration'>
<MainModuleAgent agent={agent} section={tab} />
<WzManagementConfiguration agent={agent} />
<MainModuleAgent agent={agentData} section={tab} />
<WzManagementConfiguration agent={agentData} />
</Route>
<Route path='?tab=welcome'>
<AgentsWelcome
switchTab={switchTab}
agent={agent}
agent={agentData}
pinAgent={pinnedAgentManager.pinAgent}
unPinAgent={pinnedAgentManager.unPinAgent}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export class PinnedAgentManager {
const includesAgentViewURL = this.navigationService
.getPathname()
.includes(this.AGENT_VIEW_URL);
this.navigationService
.getParams()
.set(
includesAgentViewURL
? PinnedAgentManager.AGENT_ID_VIEW_KEY
: PinnedAgentManager.AGENT_ID_URL_VIEW_KEY,
String(agentData?.id),
);
this.navigationService.renewURL(this.navigationService.getParams());
const params = this.navigationService.getParams();

params.set(
includesAgentViewURL
? PinnedAgentManager.AGENT_ID_VIEW_KEY
: PinnedAgentManager.AGENT_ID_URL_VIEW_KEY,
String(agentData?.id),
);
this.navigationService.renewURL(params);
}

unPinAgent(): void {
Expand Down
Loading

0 comments on commit aab373f

Please sign in to comment.