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

Rule details flyout on create rule page #236

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import {
CriteriaWithPagination,
EuiText,
} from '@elastic/eui';
import React, { useMemo } from 'react';

import React, { useMemo, useState } from 'react';
import { DetectionRulesTable } from './DetectionRulesTable';
import { RuleItem, RuleItemInfo } from './types/interfaces';
import { RuleViewerFlyout } from '../../../../../Rules/components/RuleViewerFlyout/RuleViewerFlyout';
import { RuleTableItem } from '../../../../../Rules/utils/helpers';
import { RuleItemInfoBase } from '../../../../../Rules/models/types';

export interface CreateDetectorRulesState {
allRules: RuleItemInfo[];
Expand All @@ -35,6 +39,8 @@ export const DetectionRules: React.FC<DetectionRulesProps> = ({
onRuleToggle,
onAllRulesToggle,
}) => {
const [flyoutData, setFlyoutData] = useState<RuleTableItem | null>(null);

let enabledRulesCount = 0;
rulesState.allRules.forEach((ruleItem) => {
if (ruleItem.enabled) {
Expand All @@ -60,8 +66,28 @@ export const DetectionRules: React.FC<DetectionRulesProps> = ({
onPageChange(nextValues.page);
};

const onRuleDetails = (ruleItem: RuleItem) => {
setFlyoutData(() => ({
title: ruleItem.name,
level: ruleItem.severity,
category: ruleItem.logType,
description: ruleItem.description,
source: ruleItem.library,
ruleInfo: rulesState.allRules.find((r) => r._id === ruleItem.id) as RuleItemInfoBase,
ruleId: ruleItem.id,
}));
};

return (
<EuiPanel style={{ paddingLeft: '0px', paddingRight: '0px' }}>
{flyoutData ? (
<RuleViewerFlyout
hideFlyout={() => setFlyoutData(() => null)}
history={null as any}
ruleTableItem={flyoutData}
ruleService={null as any}
/>
) : null}
<EuiAccordion
buttonContent={
<div data-test-subj="detection-rules-btn">
Expand All @@ -85,6 +111,7 @@ export const DetectionRules: React.FC<DetectionRulesProps> = ({
onAllRulesToggled={onAllRulesToggle}
onRuleActivationToggle={onRuleToggle}
onTableChange={onTableChange}
onRuleDetails={onRuleDetails}
/>
</EuiAccordion>
</EuiPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface DetectionRulesTableProps {
onRuleActivationToggle: (changedItem: RuleItem, isActive: boolean) => void;
onTableChange?: (nextValues: CriteriaWithPagination<RuleItem>) => void;
loading?: boolean;
onRuleDetails?: (ruleItem: RuleItem) => void;
}

const rulePriorityBySeverity: { [severity: string]: number } = {
Expand All @@ -34,6 +35,7 @@ export const DetectionRulesTable: React.FC<DetectionRulesTableProps> = ({
onRuleActivationToggle,
onTableChange,
loading = false,
onRuleDetails,
}) => {
//Filter table by rule type
const search: Search = {
Expand Down Expand Up @@ -77,7 +79,12 @@ export const DetectionRulesTable: React.FC<DetectionRulesTableProps> = ({
return (
<div style={{ padding: 10 }}>
<EuiInMemoryTable
columns={getRulesColumns(allRulesEnabled, onAllRulesToggled, onRuleActivationToggle)}
columns={getRulesColumns(
allRulesEnabled,
onAllRulesToggled,
onRuleActivationToggle,
onRuleDetails
)}
items={ruleItems}
itemId={(item: RuleItem) => `${item.name}`}
search={search}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@ export type ActiveToggleOnChangeEvent = React.BaseSyntheticEvent<
export const getRulesColumns = (
allEnabled: boolean,
onAllRulesToggled?: (enabled: boolean) => void,
onActivationToggle?: (item: RuleItem, active: boolean) => void
onActivationToggle?: (item: RuleItem, active: boolean) => void,
onRuleDetails?: (item: RuleItem) => void
): EuiBasicTableColumn<RuleItem>[] => {
const columns: EuiBasicTableColumn<RuleItem>[] = [
{
field: 'name',
name: 'Rule name',
render: (ruleName: string, item: RuleItem): ReactNode => (
<EuiLink style={{ marginLeft: 10 }}>{ruleName}</EuiLink>
),
render: (ruleName: string, item: RuleItem): ReactNode => {
const onRuleNameClicker = () => {
if (onRuleDetails) {
onRuleDetails(item);
}
};
return (
<EuiLink style={{ marginLeft: 10 }} onClick={onRuleNameClicker}>
{ruleName}
</EuiLink>
);
},
},
{
field: 'severity',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { RuleTableItem } from '../../utils/helpers';
import { DeleteRuleModal } from '../DeleteModal/DeleteModal';
import { RuleContentViewer } from '../RuleContentViewer/RuleContentViewer';
import { RuleViewerFlyoutHeaderActions } from './RuleViewFlyoutHeaderActions';
import { RuleService, NotificationsStart } from '../../../../services';
import { RuleService } from '../../../../services';
import { NotificationsStart } from 'opensearch-dashboards/public';

export interface RuleViewerFlyoutProps {
history?: RouteComponentProps['history'];
Expand Down Expand Up @@ -87,7 +88,9 @@ export const RuleViewerFlyout: React.FC<RuleViewerFlyoutProps> = ({
closeDeleteModal();
hideFlyout(true);
} else {
errorNotificationToast(notifications, 'delete', 'rule', deleteRuleRes.error);
if (notifications) {
errorNotificationToast(notifications, 'delete', 'rule', deleteRuleRes.error);
}
}
};

Expand Down