ruleNodeList.tsx 8.7 KB

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