import { css } from '@emotion/css'; import React, { FC, useEffect } from 'react'; import { GrafanaTheme, SelectableValue } from '@grafana/data'; import { Button, FormAPI, HorizontalGroup, stylesFactory, useTheme, Spinner } from '@grafana/ui'; import config from 'app/core/config'; import { NotificationChannelType, NotificationChannelDTO, NotificationChannelSecureFields } from '../../../types'; import { BasicSettings } from './BasicSettings'; import { ChannelSettings } from './ChannelSettings'; import { NotificationSettings } from './NotificationSettings'; interface Props extends Pick, 'control' | 'errors' | 'register' | 'watch' | 'getValues'> { selectableChannels: Array>; selectedChannel?: NotificationChannelType; imageRendererAvailable: boolean; secureFields: NotificationChannelSecureFields; resetSecureField: (key: string) => void; onTestChannel: (data: NotificationChannelDTO) => void; } export interface NotificationSettingsProps extends Pick, 'control' | 'errors' | 'register'> { currentFormValues: NotificationChannelDTO; } export const NotificationChannelForm: FC = ({ control, errors, selectedChannel, selectableChannels, register, watch, getValues, imageRendererAvailable, onTestChannel, resetSecureField, secureFields, }) => { const styles = getStyles(useTheme()); useEffect(() => { /* Find fields that have dependencies on other fields and removes duplicates. Needs to be prefixed with settings. */ const fieldsToWatch = new Set( selectedChannel?.options .filter((o) => o.showWhen.field) .map((option) => { return `settings.${option.showWhen.field}`; }) ) || []; watch(['type', 'sendReminder', 'uploadImage', ...fieldsToWatch]); }, [selectedChannel?.options, watch]); const currentFormValues = getValues(); if (!selectedChannel) { return ; } return (
{/* If there are no non-required fields, don't render this section*/} {selectedChannel.options.filter((o) => !o.required).length > 0 && (
)}
); }; const getStyles = stylesFactory((theme: GrafanaTheme) => { return { formContainer: css``, formItem: css` flex-grow: 1; padding-top: ${theme.spacing.md}; `, formButtons: css` padding-top: ${theme.spacing.xl}; `, }; });