diff --git a/x-pack/plugins/observability/public/pages/rules/rules_tab.tsx b/x-pack/plugins/observability/public/pages/rules/rules_tab.tsx index a2939bb4876e9..ae896b66d990f 100644 --- a/x-pack/plugins/observability/public/pages/rules/rules_tab.tsx +++ b/x-pack/plugins/observability/public/pages/rules/rules_tab.tsx @@ -9,6 +9,7 @@ import React, { useState } from 'react'; import { useHistory } from 'react-router-dom'; import { createKbnUrlStateStorage } from '@kbn/kibana-utils-plugin/public'; import { RuleStatus } from '@kbn/triggers-actions-ui-plugin/public'; +import { AlertConsumers } from '@kbn/rule-data-utils'; import { useKibana } from '../../utils/kibana_react'; import { useGetFilteredRuleTypes } from '../../hooks/use_get_filtered_rule_types'; import { observabilityAlertFeatureIds } from '../../../common/constants'; @@ -97,6 +98,7 @@ export function RulesTab({ setRefresh, stateRefresh }: RulesTabProps) { onSearchFilterChange={handleSearchFilterChange} onStatusFilterChange={handleStatusFilterChange} onTypeFilterChange={handleTypeFilterChange} + initialSelectedConsumer={AlertConsumers.LOGS} /> ); } diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_add.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_add.tsx index 7f6a45c7d9992..07264709dd544 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_add.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_add.tsx @@ -315,6 +315,7 @@ const RuleAdd = ({ onChangeMetaData={onChangeMetaData} setConsumer={setSelectedConsumer} useRuleProducer={useRuleProducer} + initialSelectedConsumer={initialSelectedConsumer} /> > { validConsumers?: RuleCreationValidConsumer[]; onChangeMetaData: (metadata: MetaData) => void; useRuleProducer?: boolean; + initialSelectedConsumer?: RuleCreationValidConsumer | null; } const EMPTY_ARRAY: string[] = []; @@ -183,6 +184,7 @@ export const RuleForm = ({ validConsumers, onChangeMetaData, useRuleProducer, + initialSelectedConsumer, }: RuleFormProps) => { const { notifications: { toasts }, @@ -818,6 +820,7 @@ export const RuleForm = ({ onChange={setConsumer} errors={errors} selectedConsumer={selectedConsumer} + initialSelectedConsumer={initialSelectedConsumer} /> diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_form_consumer_selection.test.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_form_consumer_selection.test.tsx index 324e9a290e831..bec46c682919b 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_form_consumer_selection.test.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rule_form/rule_form_consumer_selection.test.tsx @@ -41,6 +41,71 @@ describe('RuleFormConsumerSelectionModal', () => { expect(screen.getByText('Stack Rules')).toBeInTheDocument(); }); + it('should be able to initialize to the prop initialSelectedConsumer', () => { + render( + + ); + expect(mockOnChange).toHaveBeenLastCalledWith('logs'); + }); + + it('should NOT initialize if initialSelectedConsumer is equal to null', () => { + render( + + ); + expect(mockOnChange).not.toBeCalled(); + }); + + it('should initialize to the first valid consumers if initialSelectedConsumer is not valid', () => { + render( + + ); + expect(mockOnChange).toHaveBeenLastCalledWith('logs'); + }); + + it('should initialize to stackAlerts if the initialSelectedConsumer is not a valid and consumers has stackAlerts', () => { + render( + + ); + expect(mockOnChange).toHaveBeenLastCalledWith('stackAlerts'); + }); + + it('should initialize to stackAlerts if the initialSelectedConsumer is undefined and consumers has stackAlerts', () => { + render( + + ); + expect(mockOnChange).toHaveBeenLastCalledWith('stackAlerts'); + }); + it('should be able to select infrastructure and call onChange', () => { render( void; errors: IErrorObject; - selectedConsumer: RuleCreationValidConsumer | null | undefined; + selectedConsumer?: RuleCreationValidConsumer | null; + /* FUTURE ENGINEER + * if this prop is set to null then we wont initialize the value and the user will have to set it + * if this prop is set to a valid consumers then we will set it up to what was passed + * if this prop is not valid or undefined but the valid consumers has stackAlerts then we will default it to stackAlerts + */ + initialSelectedConsumer?: RuleCreationValidConsumer | null; } const SINGLE_SELECTION = { asPlainText: true }; export const RuleFormConsumerSelection = (props: RuleFormConsumerSelectionProps) => { - const { consumers, errors, onChange, selectedConsumer } = props; + const { consumers, errors, onChange, selectedConsumer, initialSelectedConsumer } = props; const isInvalid = errors?.consumer?.length > 0; const handleOnChange = useCallback( (selected: Array>) => { @@ -124,13 +130,18 @@ export const RuleFormConsumerSelection = (props: RuleFormConsumerSelectionProps) }, [consumers]); useEffect(() => { - // At initialization, select Stack Alerts, or the first value + // At initialization, select initialSelectedConsumer or the first value if (!validatedSelectedConsumer) { - if (consumers.includes(STACK_ALERTS_FEATURE_ID)) { + if (initialSelectedConsumer === null) { + return; + } else if (initialSelectedConsumer && consumers.includes(initialSelectedConsumer)) { + onChange(initialSelectedConsumer); + return; + } else if (consumers.includes(STACK_ALERTS_FEATURE_ID)) { onChange(STACK_ALERTS_FEATURE_ID); return; } - onChange(consumers[0] as RuleCreationValidConsumer); + onChange(consumers[0]); } // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.tsx b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.tsx index fe6ba4b9ab91a..61d9fb7133f65 100644 --- a/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.tsx +++ b/x-pack/plugins/triggers_actions_ui/public/application/sections/rules_list/components/rules_list.tsx @@ -41,6 +41,7 @@ import { RuleLastRunOutcomeValues, } from '@kbn/alerting-plugin/common'; import { + RuleCreationValidConsumer, ruleDetailsRoute as commonRuleDetailsRoute, STACK_ALERTS_FEATURE_ID, } from '@kbn/rule-data-utils'; @@ -136,6 +137,7 @@ export interface RulesListProps { onTypeFilterChange?: (type: string[]) => void; onRefresh?: (refresh: Date) => void; setHeaderActions?: (components?: React.ReactNode[]) => void; + initialSelectedConsumer?: RuleCreationValidConsumer | null; } export const percentileFields = { @@ -176,6 +178,7 @@ export const RulesList = ({ onTypeFilterChange, onRefresh, setHeaderActions, + initialSelectedConsumer = STACK_ALERTS_FEATURE_ID, }: RulesListProps) => { const history = useHistory(); const kibanaServices = useKibana().services; @@ -1007,7 +1010,7 @@ export const RulesList = ({ ruleTypeRegistry={ruleTypeRegistry} ruleTypeIndex={ruleTypesState.data} onSave={refreshRules} - initialSelectedConsumer={STACK_ALERTS_FEATURE_ID} + initialSelectedConsumer={initialSelectedConsumer} /> )}