ruleNodeList.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. IssueAlertRuleAction,
  9. IssueAlertRuleActionTemplate,
  10. IssueAlertRuleCondition,
  11. IssueAlertRuleConditionTemplate,
  12. } from 'sentry/types/alerts';
  13. import {
  14. CHANGE_ALERT_CONDITION_IDS,
  15. COMPARISON_INTERVAL_CHOICES,
  16. COMPARISON_TYPE_CHOICE_VALUES,
  17. COMPARISON_TYPE_CHOICES,
  18. } from 'sentry/views/alerts/utils/constants';
  19. import {REAPPEARED_EVENT_CONDITION} from 'sentry/views/projectInstall/issueAlertOptions';
  20. import {AlertRuleComparisonType} from '../metric/types';
  21. import RuleNode, {hasStreamlineTargeting} from './ruleNode';
  22. type Props = {
  23. disabled: boolean;
  24. error: React.ReactNode;
  25. /**
  26. * actions/conditions that have been added to the rule
  27. */
  28. items: IssueAlertRuleAction[] | IssueAlertRuleCondition[];
  29. /**
  30. * All available actions or conditions
  31. */
  32. nodes: IssueAlertRuleActionTemplate[] | IssueAlertRuleConditionTemplate[] | null;
  33. onAddRow: (
  34. value: IssueAlertRuleActionTemplate | IssueAlertRuleConditionTemplate
  35. ) => void;
  36. onDeleteRow: (ruleIndex: number) => void;
  37. onPropertyChange: (ruleIndex: number, prop: string, val: string) => void;
  38. onResetRow: (ruleIndex: number, name: string, value: string) => void;
  39. organization: Organization;
  40. /**
  41. * Placeholder for select control
  42. */
  43. placeholder: string;
  44. project: Project;
  45. incompatibleBanner?: number | null;
  46. incompatibleRules?: number[] | null;
  47. ownership?: null | IssueOwnership;
  48. selectType?: 'grouped';
  49. };
  50. const createSelectOptions = (
  51. actions: IssueAlertRuleActionTemplate[],
  52. organization: Organization
  53. ): Array<{
  54. label: React.ReactNode;
  55. value: IssueAlertRuleActionTemplate;
  56. }> => {
  57. return actions.map(node => {
  58. if (node.id.includes('NotifyEmailAction')) {
  59. let label = t('Issue Owners, Team, or Member');
  60. if (hasStreamlineTargeting(organization)) {
  61. label = t('Suggested Assignees, Team, or Member');
  62. }
  63. return {
  64. value: node,
  65. label,
  66. };
  67. }
  68. if (
  69. node.id === REAPPEARED_EVENT_CONDITION &&
  70. organization.features.includes('escalating-issues')
  71. ) {
  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. ): IssueAlertRuleActionTemplate | IssueAlertRuleConditionTemplate | null => {
  149. const {nodes, items, organization, onPropertyChange} = this.props;
  150. const node = nodes?.find(n => {
  151. return (
  152. n.id === template.id &&
  153. // Match more than just the id for sentryApp actions, they share the same id
  154. n.sentryAppInstallationUuid === template.sentryAppInstallationUuid
  155. );
  156. });
  157. if (!node) {
  158. return null;
  159. }
  160. if (
  161. !organization.features.includes('change-alerts') ||
  162. !CHANGE_ALERT_CONDITION_IDS.includes(node.id)
  163. ) {
  164. return node;
  165. }
  166. const item = items[itemIdx] as IssueAlertRuleCondition;
  167. let changeAlertNode: IssueAlertRuleConditionTemplate = {
  168. ...node,
  169. label: node.label.replace('...', ' {comparisonType}'),
  170. formFields: {
  171. ...node.formFields,
  172. comparisonType: {
  173. type: 'choice',
  174. choices: COMPARISON_TYPE_CHOICES,
  175. // give an initial value from not among choices so selector starts with none selected
  176. initial: 'select',
  177. },
  178. },
  179. };
  180. // item.comparison type isn't backfilled and is missing for old alert rules
  181. // this is a problem when an old alert is being edited, need to initialize it
  182. if (!item.comparisonType && item.value && item.name) {
  183. item.comparisonType = item.comparisonInterval === undefined ? 'count' : 'percent';
  184. }
  185. if (item.comparisonType) {
  186. changeAlertNode = {
  187. ...changeAlertNode,
  188. label: changeAlertNode.label.replace(
  189. '{comparisonType}',
  190. COMPARISON_TYPE_CHOICE_VALUES[item.comparisonType]
  191. ),
  192. };
  193. if (item.comparisonType === AlertRuleComparisonType.PERCENT) {
  194. if (!item.comparisonInterval) {
  195. // comparisonInterval value in IssueRuleEditor state
  196. // is undefined even if initial value is defined
  197. // can't directly call onPropertyChange, because
  198. // getNode is called during render
  199. window.clearTimeout(this.propertyChangeTimeout);
  200. this.propertyChangeTimeout = window.setTimeout(() =>
  201. onPropertyChange(itemIdx, 'comparisonInterval', '1w')
  202. );
  203. }
  204. changeAlertNode = {
  205. ...changeAlertNode,
  206. formFields: {
  207. ...changeAlertNode.formFields,
  208. comparisonInterval: {
  209. type: 'choice',
  210. choices: COMPARISON_INTERVAL_CHOICES,
  211. initial: '1w',
  212. },
  213. },
  214. };
  215. }
  216. }
  217. return changeAlertNode;
  218. };
  219. render() {
  220. const {
  221. onAddRow,
  222. onResetRow,
  223. onDeleteRow,
  224. onPropertyChange,
  225. nodes,
  226. placeholder,
  227. items,
  228. organization,
  229. ownership,
  230. project,
  231. disabled,
  232. error,
  233. selectType,
  234. incompatibleRules,
  235. incompatibleBanner,
  236. } = this.props;
  237. const enabledNodes = nodes ? nodes.filter(({enabled}) => enabled) : [];
  238. const options =
  239. selectType === 'grouped'
  240. ? groupSelectOptions(enabledNodes, organization)
  241. : createSelectOptions(enabledNodes, organization);
  242. return (
  243. <Fragment>
  244. <RuleNodes>
  245. {error}
  246. {items.map(
  247. (item: IssueAlertRuleAction | IssueAlertRuleCondition, idx: number) => (
  248. <RuleNode
  249. key={idx}
  250. index={idx}
  251. node={this.getNode(item, idx)}
  252. onDelete={onDeleteRow}
  253. onPropertyChange={onPropertyChange}
  254. onReset={onResetRow}
  255. data={item}
  256. organization={organization}
  257. project={project}
  258. disabled={disabled}
  259. ownership={ownership}
  260. incompatibleRule={incompatibleRules?.includes(idx)}
  261. incompatibleBanner={incompatibleBanner === idx}
  262. />
  263. )
  264. )}
  265. </RuleNodes>
  266. <StyledSelectControl
  267. placeholder={placeholder}
  268. value={null}
  269. onChange={obj => {
  270. onAddRow(obj.value);
  271. }}
  272. options={options}
  273. disabled={disabled}
  274. />
  275. </Fragment>
  276. );
  277. }
  278. }
  279. export default RuleNodeList;
  280. const StyledSelectControl = styled(SelectControl)`
  281. width: 100%;
  282. `;
  283. const RuleNodes = styled('div')`
  284. display: grid;
  285. margin-bottom: ${space(1)};
  286. gap: ${space(1)};
  287. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  288. grid-auto-flow: row;
  289. }
  290. `;