Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

1.1 KB
Raw Blame
import { AlertmanagertypesGettableNotificationChannelDTO } from 'api/generated/services/sigNoz.schemas';

import { ChannelFormValues, ChannelKind } from './types';
import { webhookAuthOf } from './utils';

export interface ChannelFormState {
	kind: ChannelKind;
	values: ChannelFormValues;
}

/** Each union member types `kind` as its own enum, so a plain cast is unsafe. */
function toChannelKind(kind: string): ChannelKind {
	const known = (Object.values(ChannelKind) as string[]).includes(kind);

	if (!known) {
		throw new Error(`This build cannot render a channel of kind "${kind}".`);
	}

	return kind as ChannelKind;
}

/** The form edits the spec the API returns, so loading lifts it whole. */
export function toChannelFormState(
	channel: AlertmanagertypesGettableNotificationChannelDTO,
): ChannelFormState {
	const kind = toChannelKind(channel.config.kind);
	const values: ChannelFormValues = {
		...channel.config.spec,
		name: channel.displayName,
	};

	if (kind === ChannelKind.webhook) {
		values.webhookAuth = webhookAuthOf(values);
	}

	return { kind, values };
}