ruleNodeList.tsx 8.9 KB

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