Skip to content

Commit

Permalink
[8.14] [MGMTEX] Fix rules order in create rule modal (elastic#181743) (
Browse files Browse the repository at this point in the history
…elastic#181830)

# Backport

This will backport the following commits from `main` to `8.14`:
- [[MGMTEX] Fix rules order in create rule modal
(elastic#181743)](elastic#181743)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Julian
Gernun","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-04-26T12:00:28Z","message":"[MGMTEX]
Fix rules order in create rule modal (elastic#181743)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/181527\r\n\r\nFix how rules in
create rule modal are sorted. Rules not enabled will be\r\nshown at the
end of the list and as disabled.\r\n\r\nWe did not implement \"Close
modal when outside of modal is clicked\" as\r\nmentioned
in\r\nhttps://github.com/elastic/issues/181527#issuecomment-2078821162\r\n\r\n<details>\r\n
<summary>Recording</summary>\r\n\r\n\r\nhttps://github.com/elastic/kibana/assets/17549662/9f6f8519-4f90-4b66-b790-c0144427e776\r\n\r\n\r\n</details>","sha":"8623c91cc4baeadab01a636daa1ca7ad49d50ca2","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","Feature:Alerting/RulesManagement","v8.14.0","v8.15.0"],"title":"[MGMTEX]
Fix rules order in create rule
modal","number":181743,"url":"https://github.com/elastic/kibana/pull/181743","mergeCommit":{"message":"[MGMTEX]
Fix rules order in create rule modal (elastic#181743)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/181527\r\n\r\nFix how rules in
create rule modal are sorted. Rules not enabled will be\r\nshown at the
end of the list and as disabled.\r\n\r\nWe did not implement \"Close
modal when outside of modal is clicked\" as\r\nmentioned
in\r\nhttps://github.com/elastic/issues/181527#issuecomment-2078821162\r\n\r\n<details>\r\n
<summary>Recording</summary>\r\n\r\n\r\nhttps://github.com/elastic/kibana/assets/17549662/9f6f8519-4f90-4b66-b790-c0144427e776\r\n\r\n\r\n</details>","sha":"8623c91cc4baeadab01a636daa1ca7ad49d50ca2"}},"sourceBranch":"main","suggestedTargetBranches":["8.14"],"targetPullRequestStates":[{"branch":"8.14","label":"v8.14.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/181743","number":181743,"mergeCommit":{"message":"[MGMTEX]
Fix rules order in create rule modal (elastic#181743)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/181527\r\n\r\nFix how rules in
create rule modal are sorted. Rules not enabled will be\r\nshown at the
end of the list and as disabled.\r\n\r\nWe did not implement \"Close
modal when outside of modal is clicked\" as\r\nmentioned
in\r\nhttps://github.com/elastic/issues/181527#issuecomment-2078821162\r\n\r\n<details>\r\n
<summary>Recording</summary>\r\n\r\n\r\nhttps://github.com/elastic/kibana/assets/17549662/9f6f8519-4f90-4b66-b790-c0144427e776\r\n\r\n\r\n</details>","sha":"8623c91cc4baeadab01a636daa1ca7ad49d50ca2"}}]}]
BACKPORT-->

Co-authored-by: Julian Gernun <[email protected]>
  • Loading branch information
kibanamachine and jcger authored Apr 26, 2024
1 parent 4754f74 commit 087133a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import React from 'react';
import { render, screen, within } from '@testing-library/react';
import { RuleTypeList } from './rule_type_list';
import { RuleTypeWithDescription } from '../types';

const ruleTypes: RuleTypeWithDescription[] = [
{
id: '1',
name: 'B - Rule Type 1',
enabledInLicense: true,
description: 'Description 1',
actionVariables: {
params: [],
},
authorizedConsumers: {},
actionGroups: [],
producer: 'stackAlerts',
minimumLicenseRequired: 'basic',
recoveryActionGroup: {
id: '1',
name: 'default',
},
defaultActionGroupId: '1',
},
{
id: '2',
name: 'Rule Type 2',
enabledInLicense: false,
description: 'Description 2',
actionVariables: {
params: [],
},
authorizedConsumers: {},
actionGroups: [],
producer: 'stackAlerts',
minimumLicenseRequired: 'platinum',
recoveryActionGroup: {
id: '2',
name: 'default',
},
defaultActionGroupId: '2',
},
{
id: '3',
name: 'A - Rule Type 3',
enabledInLicense: true,
description: 'Description 3',
actionVariables: {
params: [],
},
authorizedConsumers: {},
actionGroups: [],
producer: 'stackAlerts',
minimumLicenseRequired: 'basic',
recoveryActionGroup: {
id: '3',
name: 'default',
},
defaultActionGroupId: '3',
},
];

describe('RuleTypeList', () => {
it('should sort by enabled in license first and then alphabetically', async () => {
render(
<RuleTypeList
ruleTypes={ruleTypes}
onSelectRuleType={jest.fn()}
onFilterByProducer={jest.fn()}
selectedProducer={null}
ruleTypeCountsByProducer={{
total: 3,
'Stack Alerts': 3,
}}
onClearFilters={jest.fn()}
showCategories={false}
/>
);

const ruleListEl = await screen.findAllByTestId('-SelectOption', { exact: false });
const firstRuleInList = within(ruleListEl[0]).getByRole('button', { name: 'A - Rule Type 3' });
expect(firstRuleInList).not.toBeDisabled();
const secondRuleInList = within(ruleListEl[1]).getByRole('button', { name: 'B - Rule Type 1' });
expect(secondRuleInList).not.toBeDisabled();
const thirdRuleInList = within(ruleListEl[2]).getByRole('button', { name: 'Rule Type 2' });
expect(thirdRuleInList).toBeDisabled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ const producerToDisplayName = (producer: string) => {
return Reflect.get(PRODUCER_DISPLAY_NAMES, producer) ?? producer;
};

/**
* Sorts an array of objects (ruleTypes) based on two criteria:
* 1. First, sorts by the 'enabledInLicense' property.
* - If 'enabledInLicense' is the same for both rules (a and b),
* it sorts them based on the 'name' property using locale-sensitive string comparison.
* 2. If 'enabledInLicense' is different for a and b,
* places the object with 'enabledInLicense' set to true before the one with 'enabledInLicense' set to false.
*/
const sortRuleTypes = (a: RuleTypeWithDescription, b: RuleTypeWithDescription) => {
if (a.enabledInLicense === b.enabledInLicense) {
return a.name.localeCompare(b.name);
}
return a.enabledInLicense ? -1 : 1;
};

export const RuleTypeList: React.FC<RuleTypeListProps> = ({
ruleTypes,
onSelectRuleType,
Expand All @@ -47,7 +62,7 @@ export const RuleTypeList: React.FC<RuleTypeListProps> = ({
onClearFilters,
showCategories = true,
}) => {
const ruleTypesList = [...ruleTypes].sort((a, b) => a.name.localeCompare(b.name));
const ruleTypesList = [...ruleTypes].sort(sortRuleTypes);
const { euiTheme } = useEuiTheme();

const facetList = useMemo(
Expand Down Expand Up @@ -154,6 +169,7 @@ export const RuleTypeList: React.FC<RuleTypeListProps> = ({
}
style={{ marginRight: '8px', flexGrow: 0 }}
data-test-subj={`${rule.id}-SelectOption`}
isDisabled={rule.enabledInLicense === false}
/>
<EuiSpacer size="s" />
</React.Fragment>
Expand Down

0 comments on commit 087133a

Please sign in to comment.