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

[Response Ops] [Rule Form] Add Show Request and Add Action screens to flyout #206154

Merged
merged 23 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3cdd3d0
Genericize request code block and add show request to flyout
Zacqary Jan 9, 2025
4851aa2
Hide close button when show request is open
Zacqary Jan 9, 2025
514200a
Decouple actions modal from body
Zacqary Jan 9, 2025
0a414f8
Add in-flyout actio rule form screen context and in-flyout connectors…
Zacqary Jan 10, 2025
4b1adac
Remove horizontal rule from horizontal step display
Zacqary Jan 10, 2025
1f73a1f
Add filter dropdown for smaller containers
Zacqary Jan 10, 2025
7a07955
Add EuiSpacer above code block
Zacqary Jan 10, 2025
3980df1
Use screen context for show request modal
Zacqary Jan 10, 2025
8521048
Fix missing ruleflyout showrequest changes
Zacqary Jan 10, 2025
4575262
Fix missing deps
Zacqary Jan 10, 2025
e8743f6
Remove stylelint disable
Zacqary Jan 10, 2025
3bfcfdb
Update tests
Zacqary Jan 10, 2025
6e0d535
Add actions connectors body test
Zacqary Jan 10, 2025
59091f7
Update all test suites to pass
Zacqary Jan 10, 2025
e4a0a24
Fix stylelint
Zacqary Jan 10, 2025
fd0a82b
Restore RulePage to create rule form
Zacqary Jan 10, 2025
fb3027e
Remove waitFor in flyout tests
Zacqary Jan 15, 2025
8d58b28
Use EUI breakpoint variables for container breakpoints
Zacqary Jan 15, 2025
f906fd1
Revert "Use EUI breakpoint variables for container breakpoints"
Zacqary Jan 17, 2025
e20a68b
Merge branch 'main' into 195211-demodalize-flyout
elasticmachine Jan 17, 2025
4f38926
Remove unused selectconnector hook
Zacqary Jan 21, 2025
97a8bd2
Merge branch '195211-demodalize-flyout' of https://github.com/Zacqary…
Zacqary Jan 21, 2025
acf8def
Merge remote-tracking branch 'upstream/main' into 195211-demodalize-f…
Zacqary Jan 21, 2025
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 @@ -10,3 +10,4 @@
export * from './use_rule_form_dispatch';
export * from './use_rule_form_state';
export * from './use_rule_form_steps';
export * from './use_rule_form_screen_context';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { useContext } from 'react';
import { RuleFormScreenContext } from '../rule_form_screen_context';

export const useRuleFormScreenContext = () => {
return useContext(RuleFormScreenContext);
};
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,15 @@ const useCommonRuleFormSteps = ({
? {
title: RULE_FORM_PAGE_RULE_ACTIONS_TITLE,
status: actionsStatus,
children: (
<>
<RuleActions />
<EuiSpacer />
<EuiHorizontalRule margin="none" />
</>
),
children: <RuleActions />,
}
: null,
[RuleFormStepId.DETAILS]: {
title: shortTitles
? RULE_FORM_PAGE_RULE_DETAILS_TITLE_SHORT
: RULE_FORM_PAGE_RULE_DETAILS_TITLE,
status: ruleDetailsStatus,
children: (
<>
<RuleDetails />
<EuiSpacer />
<EuiHorizontalRule margin="none" />
</>
),
children: <RuleDetails />,
},
}),
[ruleDefinitionStatus, canReadConnectors, actionsStatus, ruleDetailsStatus, shortTitles]
Expand Down Expand Up @@ -210,7 +198,7 @@ export const useRuleFormSteps: () => RuleFormVerticalSteps = () => {

const mappedSteps = useMemo(() => {
return stepOrder
.map((stepId) => {
.map((stepId, index) => {
const step = steps[stepId];
return step
? {
Expand All @@ -227,6 +215,12 @@ export const useRuleFormSteps: () => RuleFormVerticalSteps = () => {
stepId={stepId}
>
{step.children}
{index > 0 && (
<>
<EuiSpacer />
<EuiHorizontalRule margin="none" />
</>
)}
</ReportOnBlur>
),
}
Expand All @@ -246,8 +240,10 @@ interface RuleFormHorizontalSteps {
hasNextStep: boolean;
hasPreviousStep: boolean;
}
export const useRuleFormHorizontalSteps: () => RuleFormHorizontalSteps = () => {
const [currentStep, setCurrentStep] = useState<RuleFormStepId>(STEP_ORDER[0]);
export const useRuleFormHorizontalSteps: (
initialStep?: RuleFormStepId
) => RuleFormHorizontalSteps = (initialStep = STEP_ORDER[0]) => {
const [currentStep, setCurrentStep] = useState<RuleFormStepId>(initialStep);
const [touchedSteps, setTouchedSteps] = useState<Record<RuleFormStepId, boolean>>(
STEP_ORDER.reduce(
(result, stepId) => ({ ...result, [stepId]: false }),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export * from './request_code_block';
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { omit, pick } from 'lodash';
import React, { useMemo } from 'react';
import { EuiCodeBlock } from '@elastic/eui';
import {
CreateRuleBody,
UPDATE_FIELDS_WITH_ACTIONS,
UpdateRuleBody,
transformCreateRuleBody,
transformUpdateRuleBody,
} from '../common/apis';
import { BASE_ALERTING_API_PATH } from '../constants';
import { useRuleFormState } from '../hooks';
import { SHOW_REQUEST_MODAL_ERROR } from '../translations';
import { RuleFormData } from '../types';

const stringifyBodyRequest = ({
formData,
isEdit,
}: {
formData: RuleFormData;
isEdit: boolean;
}): string => {
try {
const request = isEdit
? transformUpdateRuleBody(pick(formData, UPDATE_FIELDS_WITH_ACTIONS) as UpdateRuleBody)
: transformCreateRuleBody(omit(formData, 'id') as CreateRuleBody);
return JSON.stringify(request, null, 2);
} catch {
return SHOW_REQUEST_MODAL_ERROR;
}
};

interface RequestCodeBlockProps {
isEdit: boolean;
'data-test-subj'?: string;
}
export const RequestCodeBlock = (props: RequestCodeBlockProps) => {
const { isEdit, 'data-test-subj': dataTestSubj } = props;
const { formData, id, multiConsumerSelection } = useRuleFormState();

const formattedRequest = useMemo(() => {
return stringifyBodyRequest({
formData: {
...formData,
...(multiConsumerSelection ? { consumer: multiConsumerSelection } : {}),
},
isEdit,
});
}, [formData, isEdit, multiConsumerSelection]);

return (
<EuiCodeBlock language="json" isCopyable data-test-subj={dataTestSubj}>
{`${isEdit ? 'PUT' : 'POST'} kbn:${BASE_ALERTING_API_PATH}/rule${
isEdit ? `/${id}` : ''
}\n${formattedRequest}`}
</EuiCodeBlock>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const http = httpServiceMock.createStartContract();
jest.mock('../hooks', () => ({
useRuleFormState: jest.fn(),
useRuleFormDispatch: jest.fn(),
useRuleFormScreenContext: jest.fn(),
}));

jest.mock('./rule_actions_system_actions_item', () => ({
Expand Down Expand Up @@ -94,7 +95,8 @@ const mockValidate = jest.fn().mockResolvedValue({
errors: {},
});

const { useRuleFormState, useRuleFormDispatch } = jest.requireMock('../hooks');
const { useRuleFormState, useRuleFormDispatch, useRuleFormScreenContext } =
jest.requireMock('../hooks');
const { useLoadConnectors, useLoadConnectorTypes, useLoadRuleTypeAadTemplateField } =
jest.requireMock('../common/hooks');

Expand All @@ -109,6 +111,7 @@ const mockActions = [getAction('1'), getAction('2')];
const mockSystemActions = [getSystemAction('3')];

const mockOnChange = jest.fn();
const mockSetIsConnectorsScreenVisible = jest.fn();

describe('ruleActions', () => {
beforeEach(() => {
Expand Down Expand Up @@ -167,6 +170,9 @@ describe('ruleActions', () => {
aadTemplateFields: [],
});
useRuleFormDispatch.mockReturnValue(mockOnChange);
useRuleFormScreenContext.mockReturnValue({
setIsConnectorsScreenVisible: mockSetIsConnectorsScreenVisible,
});
});

afterEach(() => {
Expand Down Expand Up @@ -216,29 +222,7 @@ describe('ruleActions', () => {
render(<RuleActions />);

await userEvent.click(screen.getByTestId('ruleActionsAddActionButton'));
expect(screen.getByText('RuleActionsConnectorsModal')).toBeInTheDocument();
});

test('should call onSelectConnector with the correct parameters', async () => {
render(<RuleActions />);

await userEvent.click(screen.getByTestId('ruleActionsAddActionButton'));
expect(screen.getByText('RuleActionsConnectorsModal')).toBeInTheDocument();

await userEvent.click(screen.getByText('select connector'));
expect(mockOnChange).toHaveBeenCalledWith({
payload: {
actionTypeId: 'actionType-1',
frequency: { notifyWhen: 'onActionGroupChange', summary: false, throttle: null },
group: 'test',
id: 'connector-1',
params: { key: 'value' },
uuid: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
},
type: 'addAction',
});

expect(screen.queryByText('RuleActionsConnectorsModal')).not.toBeInTheDocument();
expect(mockSetIsConnectorsScreenVisible).toHaveBeenCalledWith(true);
});

test('should use the rule producer ID if it is not a multi-consumer rule', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,17 @@

import { EuiButton, EuiFlexGroup, EuiFlexItem, EuiImage, EuiSpacer, EuiText } from '@elastic/eui';
import { RuleSystemAction } from '@kbn/alerting-types';
import { ActionConnector } from '@kbn/alerts-ui-shared';
import React, { useCallback, useMemo, useState } from 'react';
import useEffectOnce from 'react-use/lib/useEffectOnce';
import { v4 as uuidv4 } from 'uuid';
import { RuleAction, RuleFormParamsErrors } from '../common/types';
import { DEFAULT_FREQUENCY, MULTI_CONSUMER_RULE_TYPE_IDS } from '../constants';
import { useRuleFormDispatch, useRuleFormState } from '../hooks';
import { RuleAction } from '../common/types';
import { MULTI_CONSUMER_RULE_TYPE_IDS } from '../constants';
import { useRuleFormState, useRuleFormScreenContext } from '../hooks';
import {
ADD_ACTION_DESCRIPTION_TEXT,
ADD_ACTION_HEADER,
ADD_ACTION_OPTIONAL_TEXT,
ADD_ACTION_TEXT,
} from '../translations';
import { getDefaultParams } from '../utils';
import { RuleActionsConnectorsModal } from './rule_actions_connectors_modal';
import { RuleActionsItem } from './rule_actions_item';
import { RuleActionsSystemActionsItem } from './rule_actions_system_actions_item';

Expand All @@ -40,69 +36,19 @@ const useRuleActionsIllustration = () => {
};

export const RuleActions = () => {
const [isConnectorModalOpen, setIsConnectorModalOpen] = useState<boolean>(false);
const ruleActionsIllustration = useRuleActionsIllustration();
const { setIsConnectorsScreenVisible } = useRuleFormScreenContext();

const {
formData: { actions, consumer },
plugins: { actionTypeRegistry },
multiConsumerSelection,
selectedRuleType,
connectorTypes,
} = useRuleFormState();

const dispatch = useRuleFormDispatch();

const onModalOpen = useCallback(() => {
setIsConnectorModalOpen(true);
}, []);

const onModalClose = useCallback(() => {
setIsConnectorModalOpen(false);
}, []);

const onSelectConnector = useCallback(
async (connector: ActionConnector) => {
const { id, actionTypeId } = connector;
const uuid = uuidv4();
const group = selectedRuleType.defaultActionGroupId;
const actionTypeModel = actionTypeRegistry.get(actionTypeId);

const params =
getDefaultParams({
group,
ruleType: selectedRuleType,
actionTypeModel,
}) || {};

dispatch({
type: 'addAction',
payload: {
id,
actionTypeId,
uuid,
params,
group,
frequency: DEFAULT_FREQUENCY,
},
});

const res: { errors: RuleFormParamsErrors } = await actionTypeRegistry
.get(actionTypeId)
?.validateParams(params);

dispatch({
type: 'setActionParamsError',
payload: {
uuid,
errors: res.errors,
},
});

onModalClose();
},
[dispatch, onModalClose, selectedRuleType, actionTypeRegistry]
);
setIsConnectorsScreenVisible(true);
}, [setIsConnectorsScreenVisible]);

const producerId = useMemo(() => {
if (MULTI_CONSUMER_RULE_TYPE_IDS.includes(selectedRuleType.id)) {
Expand Down Expand Up @@ -184,9 +130,6 @@ export const RuleActions = () => {
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
{isConnectorModalOpen && (
<RuleActionsConnectorsModal onClose={onModalClose} onSelectConnector={onSelectConnector} />
)}
</>
);
};
Loading
Loading