ruleNodeList.tsx 8.8 KB

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