import { css, cx } from '@emotion/css'; import produce from 'immer'; import React, { useCallback } from 'react'; import { useFieldArray, useFormContext } from 'react-hook-form'; import { useToggle } from 'react-use'; import { GrafanaTheme2 } from '@grafana/data'; import { Stack } from '@grafana/experimental'; import { Button, Field, Input, InputControl, Label, TextArea, useStyles2 } from '@grafana/ui'; import { RuleFormValues } from '../../types/rule-form'; import { Annotation } from '../../utils/constants'; import { AnnotationKeyInput } from './AnnotationKeyInput'; import { DashboardPicker } from './DashboardPicker'; const AnnotationsField = () => { const styles = useStyles2(getStyles); const [showPanelSelector, setShowPanelSelector] = useToggle(false); const { control, register, watch, formState: { errors }, setValue, } = useFormContext(); const annotations = watch('annotations'); const existingKeys = useCallback( (index: number): string[] => annotations.filter((_, idx: number) => idx !== index).map(({ key }) => key), [annotations] ); const { fields, append, remove } = useFieldArray({ control, name: 'annotations' }); const selectedDashboardUid = annotations.find((annotation) => annotation.key === Annotation.dashboardUID)?.value; const selectedPanelId = annotations.find((annotation) => annotation.key === Annotation.panelID)?.value; const setSelectedDashboardAndPanelId = (dashboardUid: string, panelId: string) => { const updatedAnnotations = produce(annotations, (draft) => { const dashboardAnnotation = draft.find((a) => a.key === Annotation.dashboardUID); const panelAnnotation = draft.find((a) => a.key === Annotation.panelID); if (dashboardAnnotation) { dashboardAnnotation.value = dashboardUid; } else { draft.push({ key: Annotation.dashboardUID, value: dashboardUid }); } if (panelAnnotation) { panelAnnotation.value = panelId; } else { draft.push({ key: Annotation.panelID, value: panelId }); } }); setValue('annotations', updatedAnnotations); setShowPanelSelector(false); }; return ( <>
{fields.map((annotationField, index) => { const isUrl = annotations[index]?.key?.toLocaleLowerCase().endsWith('url'); const ValueInputComponent = isUrl ? Input : TextArea; return (
( )} control={control} rules={{ required: { value: !!annotations[index]?.value, message: 'Required.' } }} />
); })} {showPanelSelector && ( setShowPanelSelector(false)} /> )}
); }; const getStyles = (theme: GrafanaTheme2) => ({ annotationValueInput: css` width: 394px; `, textarea: css` height: 76px; `, addAnnotationsButton: css` flex-grow: 0; align-self: flex-start; margin-left: 148px; `, flexColumn: css` display: flex; flex-direction: column; `, field: css` margin-bottom: ${theme.spacing(0.5)}; `, flexRow: css` display: flex; flex-direction: row; justify-content: flex-start; `, flexRowItemMargin: css` margin-left: ${theme.spacing(0.5)}; `, }); export default AnnotationsField;