-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Add notification policies UI and Storybook form story #255599
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
Merged
kdelemme
merged 29 commits into
elastic:alerting_v2
from
kdelemme:alertingv2/notification-policies-ui
Mar 5, 2026
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
3707793
Basic list notification policies UI
kdelemme e7ccfd5
Add react query hook
kdelemme 10fb46d
Add basic form
kdelemme 91f3116
Improve UI with panel header
kdelemme 75b70c6
Move to form/
kdelemme eb51143
Handle multiple destination workflow
kdelemme 73d6185
wrap destination badges
kdelemme ac8c997
remove exported type
kdelemme 72558e8
Handle edit action
kdelemme 4ee282f
Add delete action
kdelemme 1104634
Add storybook
kdelemme ef4a20e
Changes from node scripts/lint_ts_projects --fix
kibanamachine d0df360
Add unit tests
kdelemme 88cdd01
Remove uncontrolled input warning
kdelemme f411657
Address comments
kdelemme 309a813
Add validation for destinations
kdelemme 83de86f
Add form page
kdelemme 0a5fdf6
Move flyout into folder
kdelemme 4552f20
Merge branch 'alerting_v2' into alertingv2/notification-policies-ui
kdelemme 621c553
fix integration test setup
kdelemme 2c32656
Merge branch 'alerting_v2' into alertingv2/notification-policies-ui
kdelemme d3ac599
Changes from node scripts/lint_ts_projects --fix
kibanamachine 329137e
Handle use params correctly in tests
kdelemme e5f4b41
Add group and throttle
kdelemme 6d4080b
remove throttle use update
kdelemme c9d9037
cherry pick dispatcher integration test
kdelemme a170ea3
Extract common form logic
kdelemme baba259
Merge branch 'alerting_v2' into alertingv2/notification-policies-ui
kdelemme cb77d23
Remove requried for description
kdelemme 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
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
8 changes: 8 additions & 0 deletions
8
x-pack/platform/plugins/shared/alerting_v2/.storybook/main.js
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,8 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| module.exports = require('@kbn/storybook').defaultConfig; |
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
55 changes: 55 additions & 0 deletions
55
...ns/shared/alerting_v2/public/components/notification_policy/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,55 @@ | ||
| /* | ||
| * 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, useGeneratedHtmlId } from '@elastic/eui'; | ||
| import { i18n } from '@kbn/i18n'; | ||
| import { FormattedMessage } from '@kbn/i18n-react'; | ||
| import React from 'react'; | ||
|
|
||
| interface DeleteNotificationPolicyConfirmModalProps { | ||
| policyName: string; | ||
| onCancel: () => void; | ||
| onConfirm: () => void; | ||
| isLoading?: boolean; | ||
| } | ||
|
|
||
| export const DeleteNotificationPolicyConfirmModal = ({ | ||
| policyName, | ||
| onCancel, | ||
| onConfirm, | ||
| isLoading = false, | ||
| }: DeleteNotificationPolicyConfirmModalProps) => { | ||
| const titleId = useGeneratedHtmlId(); | ||
| return ( | ||
| <EuiConfirmModal | ||
| id="deleteNotificationPolicyConfirmModal" | ||
| aria-labelledby={titleId} | ||
| titleProps={{ id: titleId }} | ||
| title={i18n.translate('xpack.alertingV2.notificationPolicy.deleteModal.title', { | ||
| defaultMessage: 'Delete notification policy', | ||
| })} | ||
| onCancel={onCancel} | ||
| onConfirm={onConfirm} | ||
| cancelButtonText={i18n.translate( | ||
| 'xpack.alertingV2.notificationPolicy.deleteModal.cancelButton', | ||
| { defaultMessage: 'Cancel' } | ||
| )} | ||
| confirmButtonText={i18n.translate( | ||
| 'xpack.alertingV2.notificationPolicy.deleteModal.confirmButton', | ||
| { defaultMessage: 'Delete' } | ||
| )} | ||
| buttonColor="danger" | ||
| isLoading={isLoading} | ||
| > | ||
| <FormattedMessage | ||
| id="xpack.alertingV2.notificationPolicy.deleteModal.body" | ||
| defaultMessage="Are you sure you want to delete {policyName}? This action cannot be undone." | ||
| values={{ policyName: <strong>{policyName}</strong> }} | ||
| /> | ||
| </EuiConfirmModal> | ||
| ); | ||
| }; |
41 changes: 41 additions & 0 deletions
41
...atform/plugins/shared/alerting_v2/public/components/notification_policy/form/constants.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,41 @@ | ||
| /* | ||
| * 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 { i18n } from '@kbn/i18n'; | ||
| import type { NotificationPolicyFormState } from './types'; | ||
|
|
||
| export const WORKFLOW_OPTIONS = [ | ||
| { value: 'workflow-1', label: 'Slack notification workflow' }, | ||
| { value: 'workflow-2', label: 'PagerDuty escalation workflow' }, | ||
| { value: 'workflow-3', label: 'Email digest workflow' }, | ||
| ]; | ||
|
|
||
| export const FREQUENCY_OPTIONS = [ | ||
| { | ||
| value: 'immediate', | ||
| text: i18n.translate('xpack.alertingV2.notificationPolicy.form.frequency.immediate', { | ||
| defaultMessage: 'Immediate', | ||
| }), | ||
| }, | ||
| { | ||
| value: 'throttle', | ||
| text: i18n.translate('xpack.alertingV2.notificationPolicy.form.frequency.throttle', { | ||
| defaultMessage: 'Throttle', | ||
| }), | ||
| }, | ||
| ]; | ||
|
|
||
| export const THROTTLE_INTERVAL_PATTERN = /^[1-9][0-9]*[dhms]$/; | ||
|
|
||
| export const DEFAULT_FORM_STATE: NotificationPolicyFormState = { | ||
| name: '', | ||
| description: '', | ||
| matcher: '', | ||
| groupBy: [], | ||
| frequency: { type: 'immediate' }, | ||
| destinations: [{ type: 'workflow', id: 'workflow-1' }], | ||
| }; | ||
51 changes: 51 additions & 0 deletions
51
...tform/plugins/shared/alerting_v2/public/components/notification_policy/form/form_utils.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,51 @@ | ||
| /* | ||
| * 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 type { | ||
| CreateNotificationPolicyData, | ||
| NotificationPolicyResponse, | ||
| UpdateNotificationPolicyBody, | ||
| } from '@kbn/alerting-v2-schemas'; | ||
| import type { NotificationPolicyFormState } from './types'; | ||
|
|
||
| export const toFormState = (response: NotificationPolicyResponse): NotificationPolicyFormState => { | ||
| return { | ||
| name: response.name, | ||
| description: response.description, | ||
| matcher: response.matcher ?? '', | ||
| groupBy: response.group_by ?? [], | ||
| frequency: response.throttle | ||
| ? { type: 'throttle', interval: response.throttle.interval } | ||
| : { type: 'immediate' }, | ||
| destinations: response.destinations.map((d) => ({ type: d.type, id: d.id })), | ||
| }; | ||
| }; | ||
|
|
||
| export const toCreatePayload = ( | ||
| state: NotificationPolicyFormState | ||
| ): CreateNotificationPolicyData => { | ||
| return { | ||
| name: state.name, | ||
| description: state.description, | ||
| ...(state.matcher ? { matcher: state.matcher } : {}), | ||
| ...(state.groupBy.length > 0 ? { group_by: state.groupBy } : {}), | ||
| ...(state.frequency.type === 'throttle' | ||
| ? { throttle: { interval: state.frequency.interval } } | ||
| : {}), | ||
| destinations: state.destinations.map((d) => ({ type: d.type, id: d.id })), | ||
| }; | ||
| }; | ||
|
|
||
| export const toUpdatePayload = ( | ||
| state: NotificationPolicyFormState, | ||
| version: string | ||
| ): UpdateNotificationPolicyBody => { | ||
| return { | ||
| ...toCreatePayload(state), | ||
| version, | ||
| }; | ||
| }; |
10 changes: 10 additions & 0 deletions
10
...k/platform/plugins/shared/alerting_v2/public/components/notification_policy/form/index.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,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; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| export { NotificationPolicyFormFlyout } from '../form_flyout/notification_policy_form_flyout'; | ||
| export { toFormState, toCreatePayload, toUpdatePayload } from './form_utils'; | ||
| export { useNotificationPolicyForm } from './use_notification_policy_form'; |
66 changes: 66 additions & 0 deletions
66
...erting_v2/public/components/notification_policy/form/notification_policy_form.stories.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,66 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import type { Meta, StoryObj } from '@storybook/react'; | ||
| import { FormProvider, useForm } from 'react-hook-form'; | ||
| import { DEFAULT_FORM_STATE } from './constants'; | ||
| import { NotificationPolicyForm } from './notification_policy_form'; | ||
| import type { NotificationPolicyFormState } from './types'; | ||
|
|
||
| interface NotificationPolicyFormStoryProps { | ||
| defaultValues: NotificationPolicyFormState; | ||
| } | ||
|
|
||
| const NotificationPolicyFormStory = ({ defaultValues }: NotificationPolicyFormStoryProps) => { | ||
| const methods = useForm<NotificationPolicyFormState>({ | ||
| mode: 'onBlur', | ||
| defaultValues, | ||
| }); | ||
|
|
||
| return ( | ||
| <FormProvider {...methods}> | ||
| <NotificationPolicyForm /> | ||
| </FormProvider> | ||
| ); | ||
| }; | ||
|
|
||
| const meta: Meta<typeof NotificationPolicyFormStory> = { | ||
| title: 'Alerting V2/Notification Policy/Form', | ||
| component: NotificationPolicyFormStory, | ||
| parameters: { | ||
| layout: 'padded', | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof NotificationPolicyFormStory>; | ||
|
|
||
| export const CreateMode: Story = { | ||
| args: { | ||
| defaultValues: { | ||
| ...DEFAULT_FORM_STATE, | ||
| groupBy: [...DEFAULT_FORM_STATE.groupBy], | ||
| frequency: { ...DEFAULT_FORM_STATE.frequency }, | ||
| destinations: DEFAULT_FORM_STATE.destinations.map((destination) => ({ ...destination })), | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const EditMode: Story = { | ||
| args: { | ||
| defaultValues: { | ||
| name: 'Critical production alerts', | ||
| description: 'Routes critical production alerts to escalation workflows', | ||
| matcher: 'data.severity : "critical" and data.env : "prod"', | ||
| groupBy: ['host.name', 'service.name'], | ||
| frequency: { type: 'throttle', interval: '5m' }, | ||
| destinations: [{ type: 'workflow', id: 'workflow-2' }], | ||
| }, | ||
| }, | ||
| }; |
79 changes: 79 additions & 0 deletions
79
.../alerting_v2/public/components/notification_policy/form/notification_policy_form.test.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,79 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { I18nProvider } from '@kbn/i18n-react'; | ||
| import { FormProvider, useForm } from 'react-hook-form'; | ||
| import { DEFAULT_FORM_STATE } from './constants'; | ||
| import { NotificationPolicyForm } from './notification_policy_form'; | ||
| import type { NotificationPolicyFormState } from './types'; | ||
|
|
||
| const renderForm = (defaultValues: NotificationPolicyFormState = DEFAULT_FORM_STATE) => { | ||
| const TestComponent = () => { | ||
| const methods = useForm<NotificationPolicyFormState>({ | ||
| mode: 'onBlur', | ||
| defaultValues, | ||
| }); | ||
|
|
||
| return ( | ||
| <I18nProvider> | ||
| <FormProvider {...methods}> | ||
| <NotificationPolicyForm /> | ||
| </FormProvider> | ||
| </I18nProvider> | ||
| ); | ||
| }; | ||
|
|
||
| return render(<TestComponent />); | ||
| }; | ||
|
|
||
| const TEST_SUBJ = { | ||
| nameInput: 'nameInput', | ||
| descriptionInput: 'descriptionInput', | ||
| frequencySelect: 'frequencySelect', | ||
| throttleIntervalInput: 'throttleIntervalInput', | ||
| } as const; | ||
|
|
||
| describe('NotificationPolicyForm', () => { | ||
| it('shows required errors for name on blur', async () => { | ||
| const user = userEvent.setup(); | ||
| renderForm(); | ||
|
|
||
| await user.click(screen.getByTestId(TEST_SUBJ.nameInput)); | ||
| await user.tab(); | ||
| expect(await screen.findByText('Name is required.')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows throttle interval input only when throttle frequency is selected', async () => { | ||
| const user = userEvent.setup(); | ||
| renderForm(); | ||
|
|
||
| expect(screen.queryByTestId(TEST_SUBJ.throttleIntervalInput)).not.toBeInTheDocument(); | ||
|
|
||
| await user.selectOptions(screen.getByTestId(TEST_SUBJ.frequencySelect), 'throttle'); | ||
|
|
||
| expect(screen.getByTestId(TEST_SUBJ.throttleIntervalInput)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('validates throttle interval format when in throttle mode', async () => { | ||
| const user = userEvent.setup(); | ||
| renderForm(); | ||
|
|
||
| await user.selectOptions(screen.getByTestId(TEST_SUBJ.frequencySelect), 'throttle'); | ||
|
|
||
| const intervalInput = screen.getByTestId(TEST_SUBJ.throttleIntervalInput); | ||
| await user.clear(intervalInput); | ||
| await user.type(intervalInput, '10x'); | ||
| await user.tab(); | ||
|
|
||
| expect( | ||
| await screen.findByText('Invalid throttle interval. Must be in the format of 1h, 5m, 30s') | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
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.
💬 dummies for now