ruleNodeList.tsx 8.5 KB

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