Skip to content

Commit

Permalink
[Security Solution] Implement refactoring remark from PR elastic#201731
Browse files Browse the repository at this point in the history
… (elastic#204022)

## Summary

In the PR elastic#201731 for ticket elastic#180660 @banderror advised to refactor code
in that PR to better separate the concerns (business logic from
components). This is the implementation of that review
[remark](https://github.com/elastic/kibana/pull/201731/files#r1860492191).

Recording:


https://github.com/user-attachments/assets/471a0986-bcdb-4611-ab1a-bdcbe5151f47

---------

Co-authored-by: Elastic Machine <[email protected]>
Co-authored-by: Nikita Indik <[email protected]>
  • Loading branch information
3 people authored and CAWilson94 committed Jan 10, 2025
1 parent f0239c4 commit 2cdc119
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ export const AddPrebuiltRulesHeaderButtons = () => {
const {
state: {
selectedRules,
loadingRules,
isRefetching,
isUpgradingSecurityPackages,
isInstallingAllRules,
isAnyRuleInstalling,
hasRulesToInstall,
},
actions: { installAllRules, installSelectedRules },
Expand All @@ -39,8 +38,7 @@ export const AddPrebuiltRulesHeaderButtons = () => {
const numberOfSelectedRules = selectedRules.length ?? 0;
const shouldDisplayInstallSelectedRulesButton = numberOfSelectedRules > 0;

const isRuleInstalling = loadingRules.length > 0 || isInstallingAllRules;
const isRequestInProgress = isRuleInstalling || isRefetching || isUpgradingSecurityPackages;
const isRequestInProgress = isAnyRuleInstalling || isRefetching || isUpgradingSecurityPackages;

const [isOverflowPopoverOpen, setOverflowPopover] = useBoolean(false);

Expand Down Expand Up @@ -81,7 +79,7 @@ export const AddPrebuiltRulesHeaderButtons = () => {
data-test-subj="installSelectedRulesButton"
>
{i18n.INSTALL_SELECTED_RULES(numberOfSelectedRules)}
{isRuleInstalling && <EuiLoadingSpinner size="s" />}
{isAnyRuleInstalling && <EuiLoadingSpinner size="s" />}
</EuiButton>
</EuiFlexItem>
<EuiFlexItem grow={false}>
Expand Down Expand Up @@ -116,7 +114,7 @@ export const AddPrebuiltRulesHeaderButtons = () => {
aria-label={i18n.INSTALL_ALL_ARIA_LABEL}
>
{i18n.INSTALL_ALL}
{isRuleInstalling && <EuiLoadingSpinner size="s" />}
{isAnyRuleInstalling && <EuiLoadingSpinner size="s" />}
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import React, { useCallback, useMemo } from 'react';
import useBoolean from 'react-use/lib/useBoolean';
import type { Rule } from '../../../../rule_management/logic';
import type { RuleSignatureId } from '../../../../../../common/api/detection_engine';
import { type AddPrebuiltRulesTableActions } from './add_prebuilt_rules_table_context';
import {
useAddPrebuiltRulesTableContext,
type AddPrebuiltRulesTableActions,
} from './add_prebuilt_rules_table_context';
import * as i18n from './translations';

export interface PrebuiltRulesInstallButtonProps {
Expand All @@ -28,7 +31,6 @@ export interface PrebuiltRulesInstallButtonProps {
installOneRule: AddPrebuiltRulesTableActions['installOneRule'];
loadingRules: RuleSignatureId[];
isDisabled: boolean;
isInstallingAllRules: boolean;
}

export const PrebuiltRulesInstallButton = ({
Expand All @@ -37,8 +39,10 @@ export const PrebuiltRulesInstallButton = ({
installOneRule,
loadingRules,
isDisabled,
isInstallingAllRules,
}: PrebuiltRulesInstallButtonProps) => {
const {
state: { isInstallingAllRules },
} = useAddPrebuiltRulesTableContext();
const isRuleInstalling = loadingRules.includes(ruleId) || isInstallingAllRules;
const isInstallButtonDisabled = isRuleInstalling || isDisabled;
const [isPopoverOpen, setPopover] = useBoolean(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ export interface AddPrebuiltRulesTableState {
* package in background
*/
isUpgradingSecurityPackages: boolean;

/**
* Is true when performing Install All Rules mutation
*/
isInstallingAllRules: boolean;
/**
* Is true when any rule is currently being installed
*/
isAnyRuleInstalling: boolean;
/**
* List of rule IDs that are currently being upgraded
*/
Expand Down Expand Up @@ -145,6 +148,8 @@ export const AddPrebuiltRulesTableContextProvider = ({
}),
});

const isAnyRuleInstalling = loadingRules.length > 0 || isInstallingAllRules;

const { mutateAsync: installAllRulesRequest } = usePerformInstallAllRules();
const { mutateAsync: installSpecificRulesRequest } = usePerformInstallSpecificRules();

Expand Down Expand Up @@ -281,6 +286,7 @@ export const AddPrebuiltRulesTableContextProvider = ({
isRefetching,
isUpgradingSecurityPackages,
isInstallingAllRules,
isAnyRuleInstalling,
selectedRules,
lastUpdated: dataUpdatedAt,
},
Expand All @@ -297,6 +303,7 @@ export const AddPrebuiltRulesTableContextProvider = ({
isRefetching,
isUpgradingSecurityPackages,
isInstallingAllRules,
isAnyRuleInstalling,
selectedRules,
dataUpdatedAt,
actions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ const INTEGRATIONS_COLUMN: TableColumn = {
const createInstallButtonColumn = (
installOneRule: AddPrebuiltRulesTableActions['installOneRule'],
loadingRules: RuleSignatureId[],
isDisabled: boolean,
isInstallingAllRules: boolean
isDisabled: boolean
): TableColumn => ({
field: 'rule_id',
name: <RulesTableEmptyColumnName name={i18n.INSTALL_RULE_BUTTON} />,
Expand All @@ -122,7 +121,6 @@ const createInstallButtonColumn = (
installOneRule={installOneRule}
loadingRules={loadingRules}
isDisabled={isDisabled}
isInstallingAllRules={isInstallingAllRules}
/>
),
width: '10%',
Expand Down Expand Up @@ -166,23 +164,9 @@ export const useAddPrebuiltRulesTableColumns = (): TableColumn[] => {
width: '12%',
},
...(hasCRUDPermissions
? [
createInstallButtonColumn(
installOneRule,
loadingRules,
isDisabled,
isInstallingAllRules
),
]
? [createInstallButtonColumn(installOneRule, loadingRules, isDisabled)]
: []),
],
[
hasCRUDPermissions,
installOneRule,
loadingRules,
isDisabled,
showRelatedIntegrations,
isInstallingAllRules,
]
[hasCRUDPermissions, installOneRule, loadingRules, isDisabled, showRelatedIntegrations]
);
};

0 comments on commit 2cdc119

Please sign in to comment.