DiffGroup.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { css } from '@emotion/css';
  2. import { last } from 'lodash';
  3. import React from 'react';
  4. import { GrafanaTheme } from '@grafana/data';
  5. import { useStyles } from '@grafana/ui';
  6. import { DiffTitle } from './DiffTitle';
  7. import { DiffValues } from './DiffValues';
  8. import { Diff, getDiffText } from './utils';
  9. type DiffGroupProps = {
  10. diffs: Diff[];
  11. title: string;
  12. };
  13. export const DiffGroup: React.FC<DiffGroupProps> = ({ diffs, title }) => {
  14. const styles = useStyles(getStyles);
  15. if (diffs.length === 1) {
  16. return (
  17. <div className={styles.container} data-testid="diffGroup">
  18. <DiffTitle title={title} diff={diffs[0]} />
  19. </div>
  20. );
  21. }
  22. return (
  23. <div className={styles.container} data-testid="diffGroup">
  24. <DiffTitle title={title} />
  25. <ul className={styles.list}>
  26. {diffs.map((diff: Diff, idx: number) => {
  27. return (
  28. <li className={styles.listItem} key={`${last(diff.path)}__${idx}`}>
  29. <span>{getDiffText(diff)}</span> <DiffValues diff={diff} />
  30. </li>
  31. );
  32. })}
  33. </ul>
  34. </div>
  35. );
  36. };
  37. const getStyles = (theme: GrafanaTheme) => ({
  38. container: css`
  39. background-color: ${theme.colors.bg2};
  40. font-size: ${theme.typography.size.md};
  41. margin-bottom: ${theme.spacing.md};
  42. padding: ${theme.spacing.md};
  43. `,
  44. list: css`
  45. margin-left: ${theme.spacing.xl};
  46. `,
  47. listItem: css`
  48. margin-bottom: ${theme.spacing.sm};
  49. `,
  50. });