ruleNodeList.tsx 8.6 KB

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