import React, { FC, useEffect } from 'react'; import { css } from 'emotion'; import { GrafanaTheme, SelectableValue } from '@grafana/data'; import { Button, FormAPI, HorizontalGroup, stylesFactory, useTheme, Spinner } from '@grafana/ui'; import { NotificationChannelType, NotificationChannelDTO, NotificationChannelSecureFields } from '../../../types'; import { NotificationSettings } from './NotificationSettings'; import { BasicSettings } from './BasicSettings'; import { ChannelSettings } from './ChannelSettings'; import config from 'app/core/config'; interface Props extends Omit, 'formState'> { selectableChannels: Array>; selectedChannel?: NotificationChannelType; imageRendererAvailable: boolean; secureFields: NotificationChannelSecureFields; resetSecureField: (key: string) => void; onTestChannel: (data: NotificationChannelDTO) => void; } export interface NotificationSettingsProps extends Omit, 'formState' | 'watch' | 'getValues'> { currentFormValues: NotificationChannelDTO; } export const NotificationChannelForm: FC = ({ control, errors, selectedChannel, selectableChannels, register, watch, getValues, imageRendererAvailable, onTestChannel, resetSecureField, secureFields, }) => { const styles = getStyles(useTheme()); /* Finds 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}`; }) ) || []; useEffect(() => { watch(['type', 'sendReminder', 'uploadImage', ...fieldsToWatch]); }, [fieldsToWatch]); 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}; `, }; });