AnnotationPicker.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { css } from '@emotion/css';
  2. import React, { useEffect, useState } from 'react';
  3. import { AnnotationQuery, EventBus, GrafanaTheme2 } from '@grafana/data';
  4. import { InlineField, InlineFieldRow, InlineSwitch, useStyles2 } from '@grafana/ui';
  5. import { LoadingIndicator } from '@grafana/ui/src/components/PanelChrome/LoadingIndicator';
  6. import { AnnotationQueryFinished, AnnotationQueryStarted } from '../../../../types/events';
  7. import { getDashboardQueryRunner } from '../../../query/state/DashboardQueryRunner/DashboardQueryRunner';
  8. export interface AnnotationPickerProps {
  9. events: EventBus;
  10. annotation: AnnotationQuery;
  11. onEnabledChanged: (annotation: AnnotationQuery) => void;
  12. }
  13. export const AnnotationPicker = ({ annotation, events, onEnabledChanged }: AnnotationPickerProps): JSX.Element => {
  14. const [loading, setLoading] = useState(false);
  15. const styles = useStyles2(getStyles);
  16. const onCancel = () => getDashboardQueryRunner().cancel(annotation);
  17. useEffect(() => {
  18. const started = events.getStream(AnnotationQueryStarted).subscribe({
  19. next: (event) => {
  20. if (event.payload === annotation) {
  21. setLoading(true);
  22. }
  23. },
  24. });
  25. const stopped = events.getStream(AnnotationQueryFinished).subscribe({
  26. next: (event) => {
  27. if (event.payload === annotation) {
  28. setLoading(false);
  29. }
  30. },
  31. });
  32. return () => {
  33. started.unsubscribe();
  34. stopped.unsubscribe();
  35. };
  36. });
  37. return (
  38. <div key={annotation.name} className={styles.annotation}>
  39. <InlineFieldRow>
  40. <InlineField label={annotation.name} disabled={loading}>
  41. <InlineSwitch value={annotation.enable} onChange={() => onEnabledChanged(annotation)} disabled={loading} />
  42. </InlineField>
  43. <div className={styles.indicator}>
  44. <LoadingIndicator loading={loading} onCancel={onCancel} />
  45. </div>
  46. </InlineFieldRow>
  47. </div>
  48. );
  49. };
  50. function getStyles(theme: GrafanaTheme2) {
  51. return {
  52. annotation: css`
  53. display: inline-block;
  54. margin-right: ${theme.spacing(1)};
  55. .fa-caret-down {
  56. font-size: 75%;
  57. padding-left: ${theme.spacing(1)};
  58. }
  59. .gf-form-inline .gf-form {
  60. margin-bottom: 0;
  61. }
  62. `,
  63. indicator: css`
  64. align-self: center;
  65. padding: 0 ${theme.spacing(0.5)};
  66. `,
  67. };
  68. }