AnnotationSettingsEdit.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import React, { useState } from 'react';
  2. import { useAsync } from 'react-use';
  3. import { AnnotationQuery, DataSourceInstanceSettings, getDataSourceRef } from '@grafana/data';
  4. import { selectors } from '@grafana/e2e-selectors';
  5. import { DataSourcePicker, getDataSourceSrv } from '@grafana/runtime';
  6. import { Checkbox, CollapsableSection, ColorValueEditor, Field, HorizontalGroup, Input } from '@grafana/ui';
  7. import StandardAnnotationQueryEditor from 'app/features/annotations/components/StandardAnnotationQueryEditor';
  8. import { DashboardModel } from '../../state/DashboardModel';
  9. import { AngularEditorLoader } from './AngularEditorLoader';
  10. type Props = {
  11. editIdx: number;
  12. dashboard: DashboardModel;
  13. };
  14. export const newAnnotationName = 'New annotation';
  15. export const AnnotationSettingsEdit: React.FC<Props> = ({ editIdx, dashboard }) => {
  16. const [annotation, setAnnotation] = useState(dashboard.annotations.list[editIdx]);
  17. const { value: ds } = useAsync(() => {
  18. return getDataSourceSrv().get(annotation.datasource);
  19. }, [annotation.datasource]);
  20. const onUpdate = (annotation: AnnotationQuery) => {
  21. const list = [...dashboard.annotations.list];
  22. list.splice(editIdx, 1, annotation);
  23. setAnnotation(annotation);
  24. dashboard.annotations.list = list;
  25. };
  26. const onNameChange = (ev: React.FocusEvent<HTMLInputElement>) => {
  27. onUpdate({
  28. ...annotation,
  29. name: ev.currentTarget.value,
  30. });
  31. };
  32. const onDataSourceChange = (ds: DataSourceInstanceSettings) => {
  33. onUpdate({
  34. ...annotation,
  35. datasource: getDataSourceRef(ds),
  36. });
  37. };
  38. const onChange = (ev: React.FocusEvent<HTMLInputElement>) => {
  39. const target = ev.currentTarget;
  40. onUpdate({
  41. ...annotation,
  42. [target.name]: target.type === 'checkbox' ? target.checked : target.value,
  43. });
  44. };
  45. const onColorChange = (color: string) => {
  46. onUpdate({
  47. ...annotation,
  48. iconColor: color,
  49. });
  50. };
  51. const isNewAnnotation = annotation.name === newAnnotationName;
  52. return (
  53. <div>
  54. <Field label="Name">
  55. <Input
  56. aria-label={selectors.pages.Dashboard.Settings.Annotations.Settings.name}
  57. name="name"
  58. id="name"
  59. autoFocus={isNewAnnotation}
  60. value={annotation.name}
  61. onChange={onNameChange}
  62. width={50}
  63. />
  64. </Field>
  65. <Field label="Data source" htmlFor="data-source-picker">
  66. <DataSourcePicker
  67. width={50}
  68. annotations
  69. variables
  70. current={annotation.datasource}
  71. onChange={onDataSourceChange}
  72. />
  73. </Field>
  74. <Field label="Enabled" description="When enabled the annotation query is issued every dashboard refresh">
  75. <Checkbox name="enable" id="enable" value={annotation.enable} onChange={onChange} />
  76. </Field>
  77. <Field
  78. label="Hidden"
  79. description="Annotation queries can be toggled on or off at the top of the dashboard. With this option checked this toggle will be hidden."
  80. >
  81. <Checkbox name="hide" id="hide" value={annotation.hide} onChange={onChange} />
  82. </Field>
  83. <Field label="Color" description="Color to use for the annotation event markers">
  84. <HorizontalGroup>
  85. <ColorValueEditor value={annotation?.iconColor} onChange={onColorChange} />
  86. </HorizontalGroup>
  87. </Field>
  88. <CollapsableSection isOpen={true} label="Query">
  89. {ds?.annotations && (
  90. <StandardAnnotationQueryEditor datasource={ds} annotation={annotation} onChange={onUpdate} />
  91. )}
  92. {ds && !ds.annotations && <AngularEditorLoader datasource={ds} annotation={annotation} onChange={onUpdate} />}
  93. </CollapsableSection>
  94. </div>
  95. );
  96. };
  97. AnnotationSettingsEdit.displayName = 'AnnotationSettingsEdit';