-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Refactor Rule Detail Page #146192
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
Closed
CoenWarmer
wants to merge
24
commits into
elastic:main
from
CoenWarmer:chore/refactor-rules-detail-page
Closed
Refactor Rule Detail Page #146192
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 2aa7689
Break out Rule Detail tabs into its own component
CoenWarmer edbd05e
Break out loading error state into its own component
CoenWarmer c60c8e8
Break out HeaderActions into its own component
CoenWarmer 0baf37d
Tweak loader styling
CoenWarmer 8d0b1a0
Clean up Page Title component
CoenWarmer 2f63b8a
Export all components from index.ts
CoenWarmer 07afee9
Move util functions
CoenWarmer fa8ff7e
Add useIsRuleEditable hook
CoenWarmer ea77191
Create UseGetFilteredRuleTypes hook
CoenWarmer 79b5a16
Create UseGetRuleTypeDefinitionFromRuleType hook
CoenWarmer 33d0fb2
Update types
CoenWarmer 2901648
Refactor Rule Details page
CoenWarmer 56e627b
Add test
CoenWarmer 56f7b1b
Update translations
CoenWarmer 9b71597
Add all triggerActionsUI comps to mock, turn them into JSX components
CoenWarmer 6297a60
Merge branch 'main' of github.com:elastic/kibana into chore/refactor-…
CoenWarmer 865279b
Update isRuleEditable hook
CoenWarmer 05aad22
Merge branch 'main' of github.com:elastic/kibana into chore/refactor-…
CoenWarmer cb00b48
Merge branch 'main' of github.com:elastic/kibana into chore/refactor-…
CoenWarmer 5b7c967
Merge branch 'main' into chore/refactor-rules-detail-page
kibanamachine 705678d
Merge branch 'main' into chore/refactor-rules-detail-page
kibanamachine 721335f
Merge branch 'main' into chore/refactor-rules-detail-page
fkanout ee1d1d3
Merge branch 'main' into chore/refactor-rules-detail-page
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/observability/public/hooks/use_get_rule_type_definition_from_rule_type.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
137
x-pack/plugins/observability/public/hooks/use_is_rule_editable.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
50
x-pack/plugins/observability/public/hooks/use_is_rule_editable.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.../plugins/observability/public/pages/rule_details/components/delete_confirmation_modal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.