Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
Expand Up @@ -34,4 +34,10 @@ export interface ActionType {
subFeature?: SubFeature;
isDeprecated: boolean;
allowMultipleSystemActions?: boolean;
description?: string;
isExperimental?: boolean;
}

export type ConnectorUserAuthStatus = 'connected' | 'not_connected' | 'not_applicable';

export type ConnectorAuthStatusMap = Record<string, { userAuthStatus: ConnectorUserAuthStatus }>;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { AddMessageVariables } from './src/add_message_variables';

export * from './src/common/formatters';
export * from './src/common/hooks';
export * from './src/common/utils';
export { AlertsSearchBar } from './src/alerts_search_bar';
export type { AlertsSearchBarProps } from './src/alerts_search_bar/types';

Expand Down
2 changes: 2 additions & 0 deletions src/platform/packages/shared/kbn-alerts-ui-shared/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependsOn:
- '@kbn/triggers-actions-ui-types'
- '@kbn/alerting-types'
- '@kbn/actions-types'
- '@kbn/connector-specs'
- '@kbn/data-views-plugin'
- '@kbn/unified-search-plugin'
- '@kbn/es-query'
Expand All @@ -41,6 +42,7 @@ dependsOn:
- '@kbn/core-notifications-browser-mocks'
- '@kbn/shared-ux-table-persist'
- '@kbn/presentation-publishing'
- '@kbn/response-ops-form-generator'
- '@kbn/response-ops-rules-apis'
- '@kbn/controls-constants'
- '@kbn/controls-schemas'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('transformConnectorTypesResponse', () => {
sub_feature: 'endpointSecurity',
is_deprecated: false,
allow_multiple_system_actions: true,
description: 'Card subtitle from list API',
is_experimental: true,
},
{
id: 'actionType2Id',
Expand Down Expand Up @@ -51,6 +53,8 @@ describe('transformConnectorTypesResponse', () => {
subFeature: 'endpointSecurity',
isDeprecated: false,
allowMultipleSystemActions: true,
description: 'Card subtitle from list API',
isExperimental: true,
},
{
id: 'actionType2Id',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const transformConnectorType: RewriteRequestCase<ActionType> = ({
sub_feature: subFeature,
is_deprecated: isDeprecated,
allow_multiple_system_actions: allowMultipleSystemActions,
description,
is_experimental: isExperimental,
...res
}: AsApiContract<ActionType>) => ({
enabledInConfig,
Expand All @@ -28,6 +30,8 @@ const transformConnectorType: RewriteRequestCase<ActionType> = ({
subFeature,
isDeprecated,
allowMultipleSystemActions,
description,
isExperimental,
...res,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './use_load_alerting_framework_health';
export * from './use_get_rule_types_permissions';
export * from './use_load_ui_health';
export * from './use_fetch_unified_alerts_fields';
export * from './use_action_type_model';
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { useMemo } from 'react';
import { useQuery } from '@kbn/react-query';
import { ACTION_TYPE_SOURCES } from '@kbn/actions-types';
import type { ActionType } from '@kbn/actions-types';
import { fromConnectorSpecSchema } from '@kbn/connector-specs';
import type { HttpSetup, IUiSettingsClient } from '@kbn/core/public';
import type { ActionTypeModel, ActionTypeRegistryContract } from '../types';
import {
fetchConnectorSpec,
transformSpecToActionTypeModel,
} from '../utils/action_type_model_utils';

const CONNECTOR_SPEC_QUERY_KEY = 'connectorSpec';

const normalizeError = (error: unknown): Error | null => {
if (error == null) {
return null;
}
if (error instanceof Error) {
return error;
}
return new Error(String(error));
};

export interface UseActionTypeModelResult {
/** The action type model, either from registry or derived from spec */
actionTypeModel: ActionTypeModel | null;
/** Whether the spec is currently being fetched */
isLoading: boolean;
/** Error if fetching the spec failed */
error: Error | null;
/** Whether the model was derived from a spec (vs from registry) */
isFromSpec: boolean;
/** Re-runs the connector spec query (no-op when the model is from the registry) */
refetch: () => void;
}

/**
* Hook to get an ActionTypeModel for a given ActionType.
*
* For stack connectors (registered in the actionTypeRegistry), returns the model synchronously.
* For spec-based connectors, fetches the spec from the API and transforms it into an ActionTypeModel.
*
* Uses React Query for caching - connector specs are fresh for 5 minutes, then refetched on
* the next mount; window focus does not trigger refetches.
*/
export function useActionTypeModel({
actionTypeRegistry,
actionType,
http,
uiSettings,
}: {
actionTypeRegistry: ActionTypeRegistryContract;
actionType: ActionType | null;
http: HttpSetup;
uiSettings?: IUiSettingsClient;
}): UseActionTypeModelResult {
const registeredModel = useMemo(() => {
if (actionType == null) {
return null;
}
if (actionTypeRegistry.has(actionType.id)) {
return actionTypeRegistry.get(actionType.id);
}
return null;
}, [actionType, actionTypeRegistry]);

const shouldFetchSpec = actionType != null && actionType.source === ACTION_TYPE_SOURCES.spec;

const {
data: specData,
isLoading,
error,
refetch,
} = useQuery({
queryKey: [CONNECTOR_SPEC_QUERY_KEY, actionType?.id],
queryFn: async ({ signal }) => {
const spec = await fetchConnectorSpec(http, actionType!.id, signal);
// Validate eagerly — fail fast before caching. The schema is re-parsed
// lazily inside actionConnectorFields when the form component mounts.
if (!fromConnectorSpecSchema(spec.schema)) {
throw new Error(`Failed to parse connector spec schema for "${actionType!.id}"`);
}
return spec;
},
enabled: shouldFetchSpec,
staleTime: 5 * 60 * 1000, // Spec responses align with config that only changes after restart; re-fetch after 5 min covers long-lived tabs
refetchOnWindowFocus: false,
});

const specBasedModel = useMemo(() => {
if (!specData) {
return null;
}
return transformSpecToActionTypeModel(specData, uiSettings);
}, [specData, uiSettings]);

return useMemo(
() => ({
actionTypeModel: shouldFetchSpec ? specBasedModel : registeredModel,
isLoading: shouldFetchSpec && isLoading,
error: normalizeError(error),
isFromSpec: registeredModel == null && specBasedModel != null,
refetch: () => {
void refetch();
},
}),
[registeredModel, specBasedModel, shouldFetchSpec, isLoading, error, refetch]
);
}
Loading