Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
db69e1e
Clean up Delete Confirmation Modal
CoenWarmer Nov 23, 2022
2aa7689
Break out Rule Detail tabs into its own component
CoenWarmer Nov 23, 2022
edbd05e
Break out loading error state into its own component
CoenWarmer Nov 23, 2022
c60c8e8
Break out HeaderActions into its own component
CoenWarmer Nov 23, 2022
0baf37d
Tweak loader styling
CoenWarmer Nov 23, 2022
8d0b1a0
Clean up Page Title component
CoenWarmer Nov 23, 2022
2f63b8a
Export all components from index.ts
CoenWarmer Nov 23, 2022
07afee9
Move util functions
CoenWarmer Nov 23, 2022
fa8ff7e
Add useIsRuleEditable hook
CoenWarmer Nov 23, 2022
ea77191
Create UseGetFilteredRuleTypes hook
CoenWarmer Nov 23, 2022
79b5a16
Create UseGetRuleTypeDefinitionFromRuleType hook
CoenWarmer Nov 23, 2022
33d0fb2
Update types
CoenWarmer Nov 23, 2022
2901648
Refactor Rule Details page
CoenWarmer Nov 23, 2022
56e627b
Add test
CoenWarmer Nov 23, 2022
56f7b1b
Update translations
CoenWarmer Nov 22, 2022
9b71597
Add all triggerActionsUI comps to mock, turn them into JSX components
CoenWarmer Nov 24, 2022
6297a60
Merge branch 'main' of github.com:elastic/kibana into chore/refactor-…
CoenWarmer Nov 24, 2022
865279b
Update isRuleEditable hook
CoenWarmer Nov 24, 2022
05aad22
Merge branch 'main' of github.com:elastic/kibana into chore/refactor-…
CoenWarmer Nov 24, 2022
cb00b48
Merge branch 'main' of github.com:elastic/kibana into chore/refactor-…
CoenWarmer Nov 24, 2022
5b7c967
Merge branch 'main' into chore/refactor-rules-detail-page
kibanamachine Nov 25, 2022
705678d
Merge branch 'main' into chore/refactor-rules-detail-page
kibanamachine Nov 28, 2022
721335f
Merge branch 'main' into chore/refactor-rules-detail-page
fkanout Dec 2, 2022
ee1d1d3
Merge branch 'main' into chore/refactor-rules-detail-page
kibanamachine Dec 5, 2022
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
@@ -0,0 +1,24 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useLoadRuleTypes } from '@kbn/triggers-actions-ui-plugin/public';
import { useGetFilteredRuleTypes } from './use_get_filtered_rule_types';

interface UseGetRuleTypeDefinitionFromRuleTypeProps {
ruleTypeId: string | undefined;
}

export function useGetRuleTypeDefinitionFromRuleType({
ruleTypeId,
}: UseGetRuleTypeDefinitionFromRuleTypeProps) {
const filteredRuleTypes = useGetFilteredRuleTypes();

const { ruleTypes } = useLoadRuleTypes({
filteredRuleTypes,
});

return ruleTypes.find(({ id }) => id === ruleTypeId);
}
137 changes: 137 additions & 0 deletions x-pack/plugins/observability/public/hooks/use_is_rule_editable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { renderHook } from '@testing-library/react-hooks';
import { Capabilities } from '@kbn/core-capabilities-common';
import { Rule, RuleAction, RuleType, RuleTypeModel } from '@kbn/triggers-actions-ui-plugin/public';
import { RecursiveReadonly } from '@kbn/utility-types';

import { useIsRuleEditable, UseIsRuleEditableProps } from './use_is_rule_editable';
import { TypeRegistry } from '@kbn/triggers-actions-ui-plugin/public/application/type_registry';

const mockConsumer = 'mock-consumerId';
const mockRuleTypeId = 'mock-ruleTypeId';

const ruleTypeRegistry = new TypeRegistry<RuleTypeModel>();
ruleTypeRegistry.register({
id: mockRuleTypeId,
requiresAppContext: false,
actions: [],
} as unknown as RuleTypeModel);

const renderUseIsRuleEditableHook = (props: UseIsRuleEditableProps) => {
return renderHook(() => useIsRuleEditable({ ...props }));
};

const capabilities = {
actions: {
execute: true,
},
} as unknown as RecursiveReadonly<Capabilities>;

const rule = {
consumer: mockConsumer,
actions: [],
ruleTypeId: mockRuleTypeId,
} as unknown as Rule;

const ruleType = {
authorizedConsumers: {
[mockConsumer]: {
all: true,
},
},
} as unknown as RuleType;

describe('useIsRuleEditable', () => {
it('should return false if there is no rule', () => {
const {
result: { current: isRuleEditable },
} = renderUseIsRuleEditableHook({
capabilities,
rule: undefined,
ruleType,
ruleTypeRegistry,
});

expect(isRuleEditable).toBe(false);
});

it('should return false if the authorized consumers object of the rule type does not contain the particular rule being passed', () => {
const {
result: { current: isRuleEditable },
} = renderUseIsRuleEditableHook({
capabilities,
rule,
ruleType: {
authorizedConsumers: {},
} as unknown as RuleType,
ruleTypeRegistry,
});
expect(isRuleEditable).toBe(false);
});

it('should return false if the authorized consumers object of the rule type has the id for the particular rule, but all is not set to true', () => {
const {
result: { current: isRuleEditable },
} = renderUseIsRuleEditableHook({
capabilities,
rule,
ruleType: {
authorizedConsumers: {
[mockConsumer]: {
all: false,
},
},
} as unknown as RuleType,
ruleTypeRegistry,
});
expect(isRuleEditable).toBe(false);
});

it('should return false if the rule has actions to perform but the execute capability is false ', () => {
const {
result: { current: isRuleEditable },
} = renderUseIsRuleEditableHook({
capabilities: {
actions: {
execute: false,
},
} as unknown as RecursiveReadonly<Capabilities>,
rule: {
...rule,
actions: ['123'] as unknown as RuleAction[],
},
ruleType,
ruleTypeRegistry,
});
expect(isRuleEditable).toBe(false);
});

it('should return false if the rule is not registered in the rule registry', () => {
const {
result: { current: isRuleEditable },
} = renderUseIsRuleEditableHook({
capabilities,
rule,
ruleType,
ruleTypeRegistry: new TypeRegistry<RuleTypeModel>(),
});
expect(isRuleEditable).toBe(false);
});

it('it should return true if all conditions are met', () => {
const {
result: { current: isRuleEditable },
} = renderUseIsRuleEditableHook({
capabilities,
rule,
ruleType,
ruleTypeRegistry,
});
expect(isRuleEditable).toBe(true);
});
});
50 changes: 50 additions & 0 deletions x-pack/plugins/observability/public/hooks/use_is_rule_editable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { Capabilities } from '@kbn/core-capabilities-common';
import { Rule, RuleType, RuleTypeModel } from '@kbn/triggers-actions-ui-plugin/public';
import { TypeRegistry } from '@kbn/triggers-actions-ui-plugin/public/application/type_registry';
import { RecursiveReadonly } from '@kbn/utility-types';

export interface UseIsRuleEditableProps {
capabilities: RecursiveReadonly<Capabilities>;
rule: Rule | undefined;
ruleType: RuleType<string, string> | undefined;
ruleTypeRegistry: TypeRegistry<RuleTypeModel<any>>;
}

export function useIsRuleEditable({
capabilities,
rule,
ruleType,
ruleTypeRegistry,
}: UseIsRuleEditableProps): boolean {
if (!rule) {
return false;
}

// If the authorized consumers object of the rule type does not contain the rule
// being passed, the rule is not editable
if (!ruleType?.authorizedConsumers[rule.consumer]?.all) {
return false;
}

// If there are no capabilities to execute actions and the rule has 1 or more
// actions to perform, the rule is not editable.
if (!capabilities.actions?.execute && rule.actions.length !== 0) {
return false;
}

try {
// If the rule has been registered in the ruleTypeRegistry and requiresAppContext
// is set to false, it means the rule is editable.
// Wrapped in try-catch as ruleTypeRegistry will throw an Error if the rule is not registered
return ruleTypeRegistry.get(rule.ruleTypeId).requiresAppContext === false;
} catch (e) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

import React from 'react';

import { EuiFlexGroup, EuiFlexItem, EuiLoadingSpinner } from '@elastic/eui';
import { EuiFlexGroup, EuiFlexItem, EuiLoadingSpinner, EuiSpacer } from '@elastic/eui';
import { EuiLoadingSpinnerSize } from '@elastic/eui/src/components/loading/loading_spinner';

interface Props {
Expand All @@ -18,7 +17,9 @@ export function CenterJustifiedSpinner({ size }: Props) {
return (
<EuiFlexGroup data-test-subj="centerJustifiedSpinner" justifyContent="center">
Copy link
Contributor Author

@CoenWarmer CoenWarmer Nov 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the loading state for this page look nicer (the spinner is no longer at the top edge of the page).

I do think we need to unify the page loaders for Overview, Rules, Rule Details, Alerts, Alert Detail into one component so there's less code to manage and more UI consistency, but that can happen in a different PR.

<EuiFlexItem grow={false}>
<EuiLoadingSpinner size={size || 'xl'} />
<EuiSpacer size="xxl" />
<EuiLoadingSpinner size={size || 'xxl'} />
<EuiSpacer size="xxl" />
</EuiFlexItem>
</EuiFlexGroup>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EuiConfirmModal } from '@elastic/eui';
import React, { useEffect, useState } from 'react';
import { HttpSetup } from '@kbn/core/public';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../../../utils/kibana_react';

interface DeleteConfirmationPropsModal {
apiDeleteCall: ({
ids,
http,
}: {
ids: string[];
http: HttpSetup;
}) => Promise<{ successes: string[]; errors: string[] }>;
idToDelete: string | undefined;
title: string;
onCancel: () => void;
onDeleted: () => void;
onDeleting: () => void;
onErrors: () => void;
}

export function DeleteConfirmationModal({
apiDeleteCall,
idToDelete,
title,
onCancel,
onDeleted,
onDeleting,
onErrors,
}: DeleteConfirmationPropsModal) {
const {
http,
notifications: { toasts },
} = useKibana().services;

const [deleteModalFlyoutVisible, setDeleteModalVisibility] = useState<boolean>(false);

useEffect(() => {
setDeleteModalVisibility(Boolean(idToDelete));
}, [idToDelete]);

if (!deleteModalFlyoutVisible) {
return null;
}

return (
<EuiConfirmModal
buttonColor="danger"
data-test-subj="deleteIdsConfirmation"
title={i18n.translate('xpack.observability.rules.deleteConfirmationModal.descriptionText', {
defaultMessage: "You can't recover {title} after deleting.",
values: { title },
})}
cancelButtonText={i18n.translate(
'xpack.observability.rules.deleteConfirmationModal.cancelButtonLabel',
{
defaultMessage: 'Cancel',
}
)}
confirmButtonText={i18n.translate(
'xpack.observability.rules.deleteConfirmationModal.deleteButtonLabel',
{
defaultMessage: 'Delete {title}',
values: { title },
}
)}
onCancel={() => {
setDeleteModalVisibility(false);
onCancel();
}}
onConfirm={async () => {
if (idToDelete) {
setDeleteModalVisibility(false);
onDeleting();
const { successes, errors } = await apiDeleteCall({ ids: [idToDelete], http });

const hasSucceeded = Boolean(successes.length);
const hasErrored = Boolean(errors.length);

if (hasSucceeded) {
toasts.addSuccess(
i18n.translate(
'xpack.observability.rules.deleteConfirmationModal.successNotification.descriptionText',
{
defaultMessage: 'Deleted {title}',
values: { title },
}
)
);
}

if (hasErrored) {
toasts.addDanger(
i18n.translate(
'xpack.observability.rules.deleteConfirmationModal.errorNotification.descriptionText',
{
defaultMessage: 'Failed to delete {title}',
values: { title },
}
)
);
onErrors();
}

onDeleted();
}
}}
>
{i18n.translate('xpack.observability.rules.deleteConfirmationModal.descriptionText', {
defaultMessage: "You can't recover {title} after deleting.",
values: { title },
})}
</EuiConfirmModal>
);
}
Loading