ruleNodeList.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import FeatureBadge from 'sentry/components/featureBadge';
  4. import SelectControl from 'sentry/components/forms/selectControl';
  5. import {t} from 'sentry/locale';
  6. import space from 'sentry/styles/space';
  7. import {IssueOwnership, Organization, Project} from 'sentry/types';
  8. import {
  9. IssueAlertRuleAction,
  10. IssueAlertRuleActionTemplate,
  11. IssueAlertRuleCondition,
  12. IssueAlertRuleConditionTemplate,
  13. } from 'sentry/types/alerts';
  14. import {
  15. CHANGE_ALERT_CONDITION_IDS,
  16. COMPARISON_INTERVAL_CHOICES,
  17. COMPARISON_TYPE_CHOICE_VALUES,
  18. COMPARISON_TYPE_CHOICES,
  19. } from 'sentry/views/alerts/utils/constants';
  20. import {EVENT_FREQUENCY_PERCENT_CONDITION} from 'sentry/views/projectInstall/issueAlertOptions';
  21. import {AlertRuleComparisonType} from '../metric/types';
  22. import RuleNode from './ruleNode';
  23. type Props = {
  24. disabled: boolean;
  25. error: React.ReactNode;
  26. /**
  27. * actions/conditions that have been added to the rule
  28. */
  29. items: IssueAlertRuleAction[] | IssueAlertRuleCondition[];
  30. /**
  31. * All available actions or conditions
  32. */
  33. nodes: IssueAlertRuleActionTemplate[] | IssueAlertRuleConditionTemplate[] | null;
  34. onAddRow: (value: string) => void;
  35. onDeleteRow: (ruleIndex: number) => void;
  36. onPropertyChange: (ruleIndex: number, prop: string, val: string) => void;
  37. onResetRow: (ruleIndex: number, name: string, value: string) => void;
  38. organization: Organization;
  39. /**
  40. * Placeholder for select control
  41. */
  42. placeholder: string;
  43. project: Project;
  44. ownership?: null | IssueOwnership;
  45. selectType?: 'grouped';
  46. };
  47. class RuleNodeList extends Component<Props> {
  48. componentWillUnmount() {
  49. window.clearTimeout(this.propertyChangeTimeout);
  50. }
  51. propertyChangeTimeout: number | undefined = undefined;
  52. getNode = (
  53. id: string,
  54. itemIdx: number
  55. ):
  56. | IssueAlertRuleActionTemplate
  57. | IssueAlertRuleConditionTemplate
  58. | null
  59. | undefined => {
  60. const {nodes, items, organization, onPropertyChange} = this.props;
  61. const node = nodes ? nodes.find(n => n.id === id) : null;
  62. if (!node) {
  63. return null;
  64. }
  65. if (
  66. !organization.features.includes('change-alerts') ||
  67. !CHANGE_ALERT_CONDITION_IDS.includes(node.id)
  68. ) {
  69. return node;
  70. }
  71. const item = items[itemIdx] as IssueAlertRuleCondition;
  72. let changeAlertNode: IssueAlertRuleConditionTemplate = {
  73. ...node,
  74. label: node.label.replace('...', ' {comparisonType}'),
  75. formFields: {
  76. ...node.formFields,
  77. comparisonType: {
  78. type: 'choice',
  79. choices: COMPARISON_TYPE_CHOICES,
  80. // give an initial value from not among choices so selector starts with none selected
  81. initial: 'select',
  82. },
  83. },
  84. };
  85. // item.comparison type isn't backfilled and is missing for old alert rules
  86. // this is a problem when an old alert is being edited, need to initialize it
  87. if (!item.comparisonType && item.value && item.name) {
  88. item.comparisonType = item.comparisonInterval === undefined ? 'count' : 'percent';
  89. }
  90. if (item.comparisonType) {
  91. changeAlertNode = {
  92. ...changeAlertNode,
  93. label: changeAlertNode.label.replace(
  94. '{comparisonType}',
  95. COMPARISON_TYPE_CHOICE_VALUES[item.comparisonType]
  96. ),
  97. };
  98. if (item.comparisonType === AlertRuleComparisonType.PERCENT) {
  99. if (!item.comparisonInterval) {
  100. // comparisonInterval value in IssueRuleEditor state
  101. // is undefined even if initial value is defined
  102. // can't directly call onPropertyChange, because
  103. // getNode is called during render
  104. window.clearTimeout(this.propertyChangeTimeout);
  105. this.propertyChangeTimeout = window.setTimeout(() =>
  106. onPropertyChange(itemIdx, 'comparisonInterval', '1w')
  107. );
  108. }
  109. changeAlertNode = {
  110. ...changeAlertNode,
  111. formFields: {
  112. ...changeAlertNode.formFields,
  113. comparisonInterval: {
  114. type: 'choice',
  115. choices: COMPARISON_INTERVAL_CHOICES,
  116. initial: '1w',
  117. },
  118. },
  119. };
  120. }
  121. }
  122. return changeAlertNode;
  123. };
  124. render() {
  125. const {
  126. onAddRow,
  127. onResetRow,
  128. onDeleteRow,
  129. onPropertyChange,
  130. nodes,
  131. placeholder,
  132. items,
  133. organization,
  134. ownership,
  135. project,
  136. disabled,
  137. error,
  138. selectType,
  139. } = this.props;
  140. const enabledNodes = nodes ? nodes.filter(({enabled}) => enabled) : [];
  141. const createSelectOptions = (actions: IssueAlertRuleActionTemplate[]) =>
  142. actions.map(node => {
  143. const isNew = node.id === EVENT_FREQUENCY_PERCENT_CONDITION;
  144. if (node.id.includes('NotifyEmailAction')) {
  145. return {
  146. value: node.id,
  147. label: organization.features?.includes('alert-release-notification-workflow')
  148. ? t('Issue Owners, Team, Member, or Release Members')
  149. : t('Issue Owners, Team, or Member'),
  150. };
  151. }
  152. return {
  153. value: node.id,
  154. label: (
  155. <Fragment>
  156. {isNew && <StyledFeatureBadge type="new" noTooltip />}
  157. {node.prompt?.length ? node.prompt : node.label}
  158. </Fragment>
  159. ),
  160. };
  161. });
  162. let options: any = !selectType ? createSelectOptions(enabledNodes) : [];
  163. if (selectType === 'grouped') {
  164. const grouped = enabledNodes.reduce(
  165. (acc, curr) => {
  166. if (curr.actionType === 'ticket') {
  167. acc.ticket.push(curr);
  168. } else if (curr.id.includes('event_frequency')) {
  169. acc.frequency.push(curr);
  170. } else if (
  171. curr.id.includes('sentry.rules.conditions') &&
  172. !curr.id.includes('event_frequency')
  173. ) {
  174. acc.change.push(curr);
  175. } else if (curr.id.includes('sentry.integrations')) {
  176. acc.notifyIntegration.push(curr);
  177. } else if (curr.id.includes('notify_event')) {
  178. acc.notifyIntegration.push(curr);
  179. } else {
  180. acc.notify.push(curr);
  181. }
  182. return acc;
  183. },
  184. {
  185. notify: [] as IssueAlertRuleActionTemplate[],
  186. notifyIntegration: [] as IssueAlertRuleActionTemplate[],
  187. ticket: [] as IssueAlertRuleActionTemplate[],
  188. change: [] as IssueAlertRuleConditionTemplate[],
  189. frequency: [] as IssueAlertRuleConditionTemplate[],
  190. }
  191. );
  192. options = Object.entries(grouped)
  193. .filter(([_, values]) => values.length)
  194. .map(([key, values]) => {
  195. const label = {
  196. notify: t('Send notification to\u{2026}'),
  197. notifyIntegration: t('Notify integration\u{2026}'),
  198. ticket: t('Create new\u{2026}'),
  199. change: t('Issue state change'),
  200. frequency: t('Issue frequency'),
  201. };
  202. return {label: label[key], options: createSelectOptions(values)};
  203. });
  204. }
  205. return (
  206. <Fragment>
  207. <RuleNodes>
  208. {error}
  209. {items.map((item, idx) => (
  210. <RuleNode
  211. key={idx}
  212. index={idx}
  213. node={this.getNode(item.id, idx)}
  214. onDelete={onDeleteRow}
  215. onPropertyChange={onPropertyChange}
  216. onReset={onResetRow}
  217. data={item}
  218. organization={organization}
  219. project={project}
  220. disabled={disabled}
  221. ownership={ownership}
  222. />
  223. ))}
  224. </RuleNodes>
  225. <StyledSelectControl
  226. placeholder={placeholder}
  227. value={null}
  228. onChange={obj => onAddRow(obj ? obj.value : obj)}
  229. options={options}
  230. disabled={disabled}
  231. />
  232. </Fragment>
  233. );
  234. }
  235. }
  236. export default RuleNodeList;
  237. const StyledSelectControl = styled(SelectControl)`
  238. width: 100%;
  239. `;
  240. const RuleNodes = styled('div')`
  241. display: grid;
  242. margin-bottom: ${space(1)};
  243. gap: ${space(1)};
  244. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  245. grid-auto-flow: row;
  246. }
  247. `;
  248. const StyledFeatureBadge = styled(FeatureBadge)`
  249. margin: 0 ${space(1)} 0 0;
  250. `;