rules.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {forwardRef} from 'react';
  2. import styled from '@emotion/styled';
  3. import Button from 'sentry/components/button';
  4. import ConfirmDelete from 'sentry/components/confirmDelete';
  5. import TextOverflow from 'sentry/components/textOverflow';
  6. import {IconDelete, IconEdit} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import space from 'sentry/styles/space';
  9. import {Rule} from './types';
  10. import {getRuleDescription} from './utils';
  11. type Props = {
  12. rules: Array<Rule>;
  13. disabled?: boolean;
  14. onDeleteRule?: (id: Rule['id']) => void;
  15. onEditRule?: (id: Rule['id']) => void;
  16. };
  17. const Rules = forwardRef(function RulesList(
  18. {rules, onEditRule, onDeleteRule, disabled}: Props,
  19. ref: React.Ref<HTMLUListElement>
  20. ) {
  21. return (
  22. <List ref={ref} isDisabled={disabled} data-test-id="advanced-data-scrubbing-rules">
  23. {rules.map(rule => {
  24. const {id} = rule;
  25. const ruleDescription = getRuleDescription(rule);
  26. return (
  27. <ListItem key={id}>
  28. <TextOverflow>{ruleDescription}</TextOverflow>
  29. {onEditRule && (
  30. <Button
  31. aria-label={t('Edit Rule')}
  32. size="sm"
  33. onClick={() => onEditRule(id)}
  34. icon={<IconEdit />}
  35. disabled={disabled}
  36. title={
  37. disabled ? t('You do not have permission to edit rules') : undefined
  38. }
  39. />
  40. )}
  41. {onDeleteRule && (
  42. <ConfirmDelete
  43. message={t('Are you sure you wish to delete this rule?')}
  44. priority="danger"
  45. onConfirm={() => onDeleteRule(id)}
  46. confirmInput={ruleDescription}
  47. disabled={disabled}
  48. stopPropagation
  49. >
  50. <Button
  51. aria-label={t('Delete Rule')}
  52. size="sm"
  53. icon={<IconDelete />}
  54. disabled={disabled}
  55. title={
  56. disabled ? t('You do not have permission to delete rules') : undefined
  57. }
  58. />
  59. </ConfirmDelete>
  60. )}
  61. </ListItem>
  62. );
  63. })}
  64. </List>
  65. );
  66. });
  67. export default Rules;
  68. const List = styled('ul')<{
  69. isDisabled?: boolean;
  70. }>`
  71. list-style: none;
  72. margin: 0;
  73. padding: 0;
  74. margin-bottom: 0 !important;
  75. ${p =>
  76. p.isDisabled &&
  77. `
  78. color: ${p.theme.gray200};
  79. background: ${p.theme.backgroundSecondary};
  80. `}
  81. `;
  82. const ListItem = styled('li')`
  83. display: grid;
  84. grid-template-columns: auto max-content max-content;
  85. grid-column-gap: ${space(1)};
  86. align-items: center;
  87. padding: ${space(1)} ${space(2)};
  88. border-bottom: 1px solid ${p => p.theme.border};
  89. &:hover {
  90. background-color: ${p => p.theme.backgroundSecondary};
  91. }
  92. &:last-child {
  93. border-bottom: 0;
  94. }
  95. `;