ruleNodeList.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import type React from 'react';
  2. import {Component, Fragment, type ReactNode} from 'react';
  3. import styled from '@emotion/styled';
  4. import SelectControl from 'sentry/components/forms/controls/selectControl';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  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 type {Organization} from 'sentry/types/organization';
  17. import type {Project} from 'sentry/types/project';
  18. import {
  19. CHANGE_ALERT_CONDITION_IDS,
  20. COMPARISON_INTERVAL_CHOICES,
  21. COMPARISON_TYPE_CHOICE_VALUES,
  22. COMPARISON_TYPE_CHOICES,
  23. } from 'sentry/views/alerts/utils/constants';
  24. import {AlertRuleComparisonType} from '../metric/types';
  25. import RuleNode from './ruleNode';
  26. type Props = {
  27. disabled: boolean;
  28. error: React.ReactNode;
  29. /**
  30. * actions/conditions that have been added to the rule
  31. */
  32. items: IssueAlertRuleAction[] | IssueAlertRuleCondition[];
  33. /**
  34. * All available actions or conditions
  35. */
  36. nodes: IssueAlertConfiguration[keyof IssueAlertConfiguration] | null;
  37. onAddRow: (
  38. value: IssueAlertRuleActionTemplate | IssueAlertRuleConditionTemplate
  39. ) => void;
  40. onDeleteRow: (ruleIndex: number) => void;
  41. onPropertyChange: (ruleIndex: number, prop: string, val: string) => void;
  42. onResetRow: (ruleIndex: number, name: string, value: string) => void;
  43. organization: Organization;
  44. /**
  45. * Placeholder for select control
  46. */
  47. placeholder: string;
  48. project: Project;
  49. additionalAction?: {
  50. label: ReactNode;
  51. onClick: () => void;
  52. option: {
  53. label: ReactNode;
  54. value: IssueAlertRuleActionTemplate;
  55. };
  56. };
  57. incompatibleBanner?: number | null;
  58. incompatibleRules?: number[] | null;
  59. selectType?: 'grouped';
  60. };
  61. const createSelectOptions = (
  62. actions: IssueAlertRuleActionTemplate[]
  63. ): Array<{
  64. label: React.ReactNode;
  65. value: IssueAlertRuleActionTemplate;
  66. }> => {
  67. return actions.map(node => {
  68. if (node.id === IssueAlertActionType.NOTIFY_EMAIL) {
  69. const label = t('Suggested Assignees, Team, or Member');
  70. return {
  71. value: node,
  72. label,
  73. };
  74. }
  75. if (node.id === IssueAlertConditionType.REAPPEARED_EVENT) {
  76. const label = t('The issue changes state from archived to escalating');
  77. return {
  78. value: node,
  79. label,
  80. };
  81. }
  82. return {
  83. value: node,
  84. label: node.prompt ?? node.label,
  85. };
  86. });
  87. };
  88. const groupLabels = {
  89. notify: t('Send notification to\u{2026}'),
  90. notifyIntegration: t('Notify integration\u{2026}'),
  91. ticket: t('Create new\u{2026}'),
  92. change: t('Issue state change'),
  93. frequency: t('Issue frequency'),
  94. };
  95. /**
  96. * Group options by category
  97. */
  98. const groupSelectOptions = (actions: IssueAlertRuleActionTemplate[]) => {
  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),
  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. ): IssueAlertConfiguration[keyof IssueAlertConfiguration][number] | null => {
  150. const {nodes, items, organization, onPropertyChange} = this.props;
  151. const node = nodes?.find(n => {
  152. if ('sentryAppInstallationUuid' in n) {
  153. // Match more than just the id for sentryApp actions, they share the same id
  154. return (
  155. n.id === template.id &&
  156. n.sentryAppInstallationUuid === template.sentryAppInstallationUuid
  157. );
  158. }
  159. return n.id === template.id;
  160. });
  161. if (!node) {
  162. return null;
  163. }
  164. if (
  165. !organization.features.includes('change-alerts') ||
  166. !CHANGE_ALERT_CONDITION_IDS.includes(node.id)
  167. ) {
  168. return node;
  169. }
  170. const item = items[itemIdx];
  171. let changeAlertNode: IssueAlertGenericConditionConfig = {
  172. ...(node as IssueAlertGenericConditionConfig),
  173. label: node.label.replace('...', ' {comparisonType}'),
  174. formFields: {
  175. ...(node.formFields as IssueAlertGenericConditionConfig['formFields']),
  176. comparisonType: {
  177. type: 'choice',
  178. choices: COMPARISON_TYPE_CHOICES,
  179. // give an initial value from not among choices so selector starts with none selected
  180. initial: 'select',
  181. },
  182. },
  183. };
  184. // item.comparison type isn't backfilled and is missing for old alert rules
  185. // this is a problem when an old alert is being edited, need to initialize it
  186. if (!item.comparisonType && item.value && item.name) {
  187. item.comparisonType = item.comparisonInterval === undefined ? 'count' : 'percent';
  188. }
  189. if (item.comparisonType) {
  190. changeAlertNode = {
  191. ...changeAlertNode,
  192. label: changeAlertNode.label.replace(
  193. '{comparisonType}',
  194. COMPARISON_TYPE_CHOICE_VALUES[item.comparisonType]
  195. ),
  196. };
  197. if (item.comparisonType === AlertRuleComparisonType.PERCENT) {
  198. if (!item.comparisonInterval) {
  199. // comparisonInterval value in IssueRuleEditor state
  200. // is undefined even if initial value is defined
  201. // can't directly call onPropertyChange, because
  202. // getNode is called during render
  203. window.clearTimeout(this.propertyChangeTimeout);
  204. this.propertyChangeTimeout = window.setTimeout(() =>
  205. onPropertyChange(itemIdx, 'comparisonInterval', '1w')
  206. );
  207. }
  208. changeAlertNode = {
  209. ...changeAlertNode,
  210. formFields: {
  211. ...changeAlertNode.formFields,
  212. comparisonInterval: {
  213. type: 'choice',
  214. choices: COMPARISON_INTERVAL_CHOICES,
  215. initial: '1w',
  216. },
  217. },
  218. };
  219. }
  220. }
  221. return changeAlertNode;
  222. };
  223. render() {
  224. const {
  225. onAddRow,
  226. onResetRow,
  227. onDeleteRow,
  228. onPropertyChange,
  229. additionalAction,
  230. nodes,
  231. placeholder,
  232. items,
  233. organization,
  234. project,
  235. disabled,
  236. error,
  237. selectType,
  238. incompatibleRules,
  239. incompatibleBanner,
  240. } = this.props;
  241. const enabledNodes = nodes ? nodes.filter(({enabled}) => enabled) : [];
  242. let options;
  243. if (selectType === 'grouped') {
  244. options = groupSelectOptions(enabledNodes);
  245. if (additionalAction) {
  246. const optionToModify = options.find(
  247. option => option.label === additionalAction.label
  248. );
  249. if (optionToModify) {
  250. optionToModify.options.push(additionalAction.option);
  251. }
  252. }
  253. } else {
  254. options = createSelectOptions(enabledNodes);
  255. }
  256. return (
  257. <Fragment>
  258. <RuleNodes>
  259. {error}
  260. {items.map(
  261. (item: IssueAlertRuleAction | IssueAlertRuleCondition, idx: number) => (
  262. <RuleNode
  263. key={idx}
  264. index={idx}
  265. node={this.getNode(item, idx)}
  266. onDelete={onDeleteRow}
  267. onPropertyChange={onPropertyChange}
  268. onReset={onResetRow}
  269. data={item}
  270. organization={organization}
  271. project={project}
  272. disabled={disabled}
  273. incompatibleRule={incompatibleRules?.includes(idx)}
  274. incompatibleBanner={incompatibleBanner === idx}
  275. />
  276. )
  277. )}
  278. </RuleNodes>
  279. <StyledSelectControl
  280. placeholder={placeholder}
  281. value={null}
  282. onChange={obj => {
  283. additionalAction && obj === additionalAction.option
  284. ? additionalAction.onClick()
  285. : onAddRow(obj.value);
  286. }}
  287. options={options}
  288. disabled={disabled}
  289. />
  290. </Fragment>
  291. );
  292. }
  293. }
  294. export default RuleNodeList;
  295. const StyledSelectControl = styled(SelectControl)`
  296. width: 100%;
  297. `;
  298. const RuleNodes = styled('div')`
  299. display: grid;
  300. margin-bottom: ${space(1)};
  301. gap: ${space(1)};
  302. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  303. grid-auto-flow: row;
  304. }
  305. `;