AlertInstancesTable.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { css } from '@emotion/css';
  2. import React, { FC, useMemo } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { Alert } from 'app/types/unified-alerting';
  5. import { alertInstanceKey } from '../../utils/rules';
  6. import { AlertLabels } from '../AlertLabels';
  7. import { DynamicTable, DynamicTableColumnProps, DynamicTableItemProps } from '../DynamicTable';
  8. import { AlertInstanceDetails } from './AlertInstanceDetails';
  9. import { AlertStateTag } from './AlertStateTag';
  10. interface Props {
  11. instances: Alert[];
  12. }
  13. type AlertTableColumnProps = DynamicTableColumnProps<Alert>;
  14. type AlertTableItemProps = DynamicTableItemProps<Alert>;
  15. export const AlertInstancesTable: FC<Props> = ({ instances }) => {
  16. const items = useMemo(
  17. (): AlertTableItemProps[] =>
  18. instances.map((instance) => ({
  19. data: instance,
  20. id: alertInstanceKey(instance),
  21. })),
  22. [instances]
  23. );
  24. return (
  25. <DynamicTable
  26. cols={columns}
  27. isExpandable={true}
  28. items={items}
  29. renderExpandedContent={({ data }) => <AlertInstanceDetails instance={data} />}
  30. />
  31. );
  32. };
  33. export const getStyles = (theme: GrafanaTheme2) => ({
  34. colExpand: css`
  35. width: 36px;
  36. `,
  37. colState: css`
  38. width: 110px;
  39. `,
  40. labelsCell: css`
  41. padding-top: ${theme.spacing(0.5)} !important;
  42. padding-bottom: ${theme.spacing(0.5)} !important;
  43. `,
  44. createdCell: css`
  45. white-space: nowrap;
  46. `,
  47. table: css`
  48. td {
  49. vertical-align: top;
  50. padding-top: ${theme.spacing(1)};
  51. padding-bottom: ${theme.spacing(1)};
  52. }
  53. `,
  54. });
  55. const columns: AlertTableColumnProps[] = [
  56. {
  57. id: 'state',
  58. label: 'State',
  59. // eslint-disable-next-line react/display-name
  60. renderCell: ({ data: { state } }) => <AlertStateTag state={state} />,
  61. size: '80px',
  62. },
  63. {
  64. id: 'labels',
  65. label: 'Labels',
  66. // eslint-disable-next-line react/display-name
  67. renderCell: ({ data: { labels } }) => <AlertLabels labels={labels} />,
  68. },
  69. {
  70. id: 'created',
  71. label: 'Created',
  72. // eslint-disable-next-line react/display-name
  73. renderCell: ({ data: { activeAt } }) => (
  74. <>{activeAt.startsWith('0001') ? '-' : activeAt.slice(0, 19).replace('T', ' ')}</>
  75. ),
  76. size: '150px',
  77. },
  78. ];