AnnotationsSettings.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import React, { useState } from 'react';
  2. import { AnnotationQuery, getDataSourceRef } from '@grafana/data';
  3. import { getDataSourceSrv } from '@grafana/runtime';
  4. import { DashboardModel } from '../../state/DashboardModel';
  5. import { AnnotationSettingsEdit, AnnotationSettingsList, newAnnotationName } from '../AnnotationSettings';
  6. import { DashboardSettingsHeader } from './DashboardSettingsHeader';
  7. interface Props {
  8. dashboard: DashboardModel;
  9. }
  10. export const AnnotationsSettings: React.FC<Props> = ({ dashboard }) => {
  11. const [editIdx, setEditIdx] = useState<number | null>(null);
  12. const onGoBack = () => {
  13. setEditIdx(null);
  14. };
  15. const onNew = () => {
  16. const newAnnotation: AnnotationQuery = {
  17. name: newAnnotationName,
  18. enable: true,
  19. datasource: getDataSourceRef(getDataSourceSrv().getInstanceSettings(null)!),
  20. iconColor: 'red',
  21. };
  22. dashboard.annotations.list = [...dashboard.annotations.list, { ...newAnnotation }];
  23. setEditIdx(dashboard.annotations.list.length - 1);
  24. };
  25. const onEdit = (idx: number) => {
  26. setEditIdx(idx);
  27. };
  28. const isEditing = editIdx !== null;
  29. return (
  30. <>
  31. <DashboardSettingsHeader title="Annotations" onGoBack={onGoBack} isEditing={isEditing} />
  32. {!isEditing && <AnnotationSettingsList dashboard={dashboard} onNew={onNew} onEdit={onEdit} />}
  33. {isEditing && <AnnotationSettingsEdit dashboard={dashboard} editIdx={editIdx!} />}
  34. </>
  35. );
  36. };