ruleNodeList.tsx 8.8 KB

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