import React, { useState } from 'react'; import { useAsync } from 'react-use'; import { AnnotationQuery, DataSourceInstanceSettings, getDataSourceRef } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { DataSourcePicker, getDataSourceSrv, locationService } from '@grafana/runtime'; import { Button, Checkbox, Field, FieldSet, HorizontalGroup, Input, Stack } from '@grafana/ui'; import { ColorValueEditor } from 'app/core/components/OptionsUI/color'; import StandardAnnotationQueryEditor from 'app/features/annotations/components/StandardAnnotationQueryEditor'; import { DashboardModel } from '../../state/DashboardModel'; import { AngularEditorLoader } from './AngularEditorLoader'; type Props = { editIdx: number; dashboard: DashboardModel; }; export const newAnnotationName = 'New annotation'; export const AnnotationSettingsEdit = ({ editIdx, dashboard }: Props) => { const [annotation, setAnnotation] = useState(dashboard.annotations.list[editIdx]); const { value: ds } = useAsync(() => { return getDataSourceSrv().get(annotation.datasource); }, [annotation.datasource]); const onUpdate = (annotation: AnnotationQuery) => { const list = [...dashboard.annotations.list]; list.splice(editIdx, 1, annotation); setAnnotation(annotation); dashboard.annotations.list = list; }; const onNameChange = (ev: React.FocusEvent) => { onUpdate({ ...annotation, name: ev.currentTarget.value, }); }; const onDataSourceChange = (ds: DataSourceInstanceSettings) => { onUpdate({ ...annotation, datasource: getDataSourceRef(ds), }); }; const onChange = (ev: React.FocusEvent) => { const target = ev.currentTarget; onUpdate({ ...annotation, [target.name]: target.type === 'checkbox' ? target.checked : target.value, }); }; const onColorChange = (color: string) => { onUpdate({ ...annotation, iconColor: color, }); }; const onApply = goBackToList; const onPreview = () => { locationService.partial({ editview: null, editIndex: null }); }; const onDelete = () => { const annotations = dashboard.annotations.list; dashboard.annotations.list = [...annotations.slice(0, editIdx), ...annotations.slice(editIdx + 1)]; goBackToList(); }; const isNewAnnotation = annotation.name === newAnnotationName; return (

Query

{ds?.annotations && ( )} {ds && !ds.annotations && }
); }; AnnotationSettingsEdit.displayName = 'AnnotationSettingsEdit'; function goBackToList() { locationService.partial({ editIndex: null }); }