ruleNodeList.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 {IssueOwnership, Organization, Project} from 'sentry/types';
  7. import type {
  8. IssueAlertConfiguration,
  9. IssueAlertGenericConditionConfig,
  10. IssueAlertRuleAction,
  11. IssueAlertRuleActionTemplate,
  12. IssueAlertRuleCondition,
  13. IssueAlertRuleConditionTemplate,
  14. } from 'sentry/types/alerts';
  15. import {IssueAlertActionType, IssueAlertConditionType} from 'sentry/types/alerts';
  16. import {
  17. CHANGE_ALERT_CONDITION_IDS,
  18. COMPARISON_INTERVAL_CHOICES,
  19. COMPARISON_TYPE_CHOICE_VALUES,
  20. COMPARISON_TYPE_CHOICES,
  21. } from 'sentry/views/alerts/utils/constants';
  22. import {AlertRuleComparisonType} from '../metric/types';
  23. import RuleNode from './ruleNode';
  24. type Props = {
  25. disabled: boolean;
  26. error: React.ReactNode;
  27. /**
  28. * actions/conditions that have been added to the rule
  29. */
  30. items: IssueAlertRuleAction[] | IssueAlertRuleCondition[];
  31. /**
  32. * All available actions or conditions
  33. */
  34. nodes: IssueAlertConfiguration[keyof IssueAlertConfiguration] | null;
  35. onAddRow: (
  36. value: IssueAlertRuleActionTemplate | IssueAlertRuleConditionTemplate
  37. ) => void;
  38. onDeleteRow: (ruleIndex: number) => void;
  39. onPropertyChange: (ruleIndex: number, prop: string, val: string) => void;
  40. onResetRow: (ruleIndex: number, name: string, value: string) => void;
  41. organization: Organization;
  42. /**
  43. * Placeholder for select control
  44. */
  45. placeholder: string;
  46. project: Project;
  47. incompatibleBanner?: number | null;
  48. incompatibleRules?: number[] | null;
  49. ownership?: null | IssueOwnership;
  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. ownership,
  225. project,
  226. disabled,
  227. error,
  228. selectType,
  229. incompatibleRules,
  230. incompatibleBanner,
  231. } = this.props;
  232. const enabledNodes = nodes ? nodes.filter(({enabled}) => enabled) : [];
  233. const options =
  234. selectType === 'grouped'
  235. ? groupSelectOptions(enabledNodes)
  236. : createSelectOptions(enabledNodes);
  237. return (
  238. <Fragment>
  239. <RuleNodes>
  240. {error}
  241. {items.map(
  242. (item: IssueAlertRuleAction | IssueAlertRuleCondition, idx: number) => (
  243. <RuleNode
  244. key={idx}
  245. index={idx}
  246. node={this.getNode(item, idx)}
  247. onDelete={onDeleteRow}
  248. onPropertyChange={onPropertyChange}
  249. onReset={onResetRow}
  250. data={item}
  251. organization={organization}
  252. project={project}
  253. disabled={disabled}
  254. ownership={ownership}
  255. incompatibleRule={incompatibleRules?.includes(idx)}
  256. incompatibleBanner={incompatibleBanner === idx}
  257. />
  258. )
  259. )}
  260. </RuleNodes>
  261. <StyledSelectControl
  262. placeholder={placeholder}
  263. value={null}
  264. onChange={obj => {
  265. onAddRow(obj.value);
  266. }}
  267. options={options}
  268. disabled={disabled}
  269. />
  270. </Fragment>
  271. );
  272. }
  273. }
  274. export default RuleNodeList;
  275. const StyledSelectControl = styled(SelectControl)`
  276. width: 100%;
  277. `;
  278. const RuleNodes = styled('div')`
  279. display: grid;
  280. margin-bottom: ${space(1)};
  281. gap: ${space(1)};
  282. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  283. grid-auto-flow: row;
  284. }
  285. `;