GrafanaRules.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { css } from '@emotion/css';
  2. import React, { FC } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { LoadingPlaceholder, useStyles } from '@grafana/ui';
  5. import { useQueryParams } from 'app/core/hooks/useQueryParams';
  6. import { CombinedRuleNamespace } from 'app/types/unified-alerting';
  7. import { flattenGrafanaManagedRules } from '../../hooks/useCombinedRuleNamespaces';
  8. import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector';
  9. import { GRAFANA_RULES_SOURCE_NAME } from '../../utils/datasource';
  10. import { initialAsyncRequestState } from '../../utils/redux';
  11. import { RulesGroup } from './RulesGroup';
  12. interface Props {
  13. namespaces: CombinedRuleNamespace[];
  14. expandAll: boolean;
  15. }
  16. export const GrafanaRules: FC<Props> = ({ namespaces, expandAll }) => {
  17. const styles = useStyles(getStyles);
  18. const [queryParams] = useQueryParams();
  19. const { loading } = useUnifiedAlertingSelector(
  20. (state) => state.promRules[GRAFANA_RULES_SOURCE_NAME] || initialAsyncRequestState
  21. );
  22. const wantsGroupedView = queryParams['view'] === 'grouped';
  23. const namespacesFormat = wantsGroupedView ? namespaces : flattenGrafanaManagedRules(namespaces);
  24. return (
  25. <section className={styles.wrapper}>
  26. <div className={styles.sectionHeader}>
  27. <h5>Grafana</h5>
  28. {loading ? <LoadingPlaceholder className={styles.loader} text="Loading..." /> : <div />}
  29. </div>
  30. {namespacesFormat?.map((namespace) =>
  31. namespace.groups.map((group) => (
  32. <RulesGroup
  33. group={group}
  34. key={`${namespace.name}-${group.name}`}
  35. namespace={namespace}
  36. expandAll={expandAll}
  37. />
  38. ))
  39. )}
  40. {namespacesFormat?.length === 0 && <p>No rules found.</p>}
  41. </section>
  42. );
  43. };
  44. const getStyles = (theme: GrafanaTheme) => ({
  45. loader: css`
  46. margin-bottom: 0;
  47. `,
  48. sectionHeader: css`
  49. display: flex;
  50. justify-content: space-between;
  51. `,
  52. wrapper: css`
  53. margin-bottom: ${theme.spacing.xl};
  54. `,
  55. });