ruleNodeList.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import * as React from 'react';
  2. import styled from '@emotion/styled';
  3. import FeatureBadge from 'sentry/components/featureBadge';
  4. import SelectControl from 'sentry/components/forms/selectControl';
  5. import {t} from 'sentry/locale';
  6. import space from 'sentry/styles/space';
  7. import {Organization, Project} from 'sentry/types';
  8. import {
  9. IssueAlertRuleAction,
  10. IssueAlertRuleActionTemplate,
  11. IssueAlertRuleCondition,
  12. IssueAlertRuleConditionTemplate,
  13. } from 'sentry/types/alerts';
  14. import {
  15. CHANGE_ALERT_CONDITION_IDS,
  16. COMPARISON_INTERVAL_CHOICES,
  17. COMPARISON_TYPE_CHOICE_VALUES,
  18. COMPARISON_TYPE_CHOICES,
  19. } from 'sentry/views/alerts/changeAlerts/constants';
  20. import {EVENT_FREQUENCY_PERCENT_CONDITION} from 'sentry/views/projectInstall/issueAlertOptions';
  21. import RuleNode 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: (value: string) => void;
  34. onDeleteRow: (ruleIndex: number) => void;
  35. onPropertyChange: (ruleIndex: number, prop: string, val: string) => void;
  36. onResetRow: (ruleIndex: number, name: string, value: string) => void;
  37. organization: Organization;
  38. /**
  39. * Placeholder for select control
  40. */
  41. placeholder: string;
  42. project: Project;
  43. selectType?: 'grouped';
  44. };
  45. class RuleNodeList extends React.Component<Props> {
  46. componentWillUnmount() {
  47. window.clearTimeout(this.propertyChangeTimeout);
  48. }
  49. propertyChangeTimeout: number | undefined = undefined;
  50. getNode = (
  51. id: string,
  52. itemIdx: number
  53. ):
  54. | IssueAlertRuleActionTemplate
  55. | IssueAlertRuleConditionTemplate
  56. | null
  57. | undefined => {
  58. const {nodes, items, organization, onPropertyChange} = this.props;
  59. const node = nodes ? nodes.find(n => n.id === id) : null;
  60. if (!node) {
  61. return null;
  62. }
  63. if (
  64. !organization.features.includes('change-alerts') ||
  65. !CHANGE_ALERT_CONDITION_IDS.includes(node.id)
  66. ) {
  67. return node;
  68. }
  69. const item = items[itemIdx] as IssueAlertRuleCondition;
  70. let changeAlertNode: IssueAlertRuleConditionTemplate = {
  71. ...node,
  72. label: node.label.replace('...', ' {comparisonType}'),
  73. formFields: {
  74. ...node.formFields,
  75. comparisonType: {
  76. type: 'choice',
  77. choices: COMPARISON_TYPE_CHOICES,
  78. // give an initial value from not among choices so selector starts with none selected
  79. initial: 'select',
  80. },
  81. },
  82. };
  83. // item.comparison type isn't backfilled and is missing for old alert rules
  84. // this is a problem when an old alert is being edited, need to initialize it
  85. if (!item.comparisonType && item.value && item.name) {
  86. item.comparisonType = item.comparisonInterval === undefined ? 'count' : 'percent';
  87. }
  88. if (item.comparisonType) {
  89. changeAlertNode = {
  90. ...changeAlertNode,
  91. label: changeAlertNode.label.replace(
  92. '{comparisonType}',
  93. COMPARISON_TYPE_CHOICE_VALUES[item.comparisonType]
  94. ),
  95. };
  96. if (item.comparisonType === 'percent') {
  97. if (!item.comparisonInterval) {
  98. // comparisonInterval value in IssueRuleEditor state
  99. // is undefined even if initial value is defined
  100. // can't directly call onPropertyChange, because
  101. // getNode is called during render
  102. window.clearTimeout(this.propertyChangeTimeout);
  103. this.propertyChangeTimeout = window.setTimeout(() =>
  104. onPropertyChange(itemIdx, 'comparisonInterval', '1w')
  105. );
  106. }
  107. changeAlertNode = {
  108. ...changeAlertNode,
  109. formFields: {
  110. ...changeAlertNode.formFields,
  111. comparisonInterval: {
  112. type: 'choice',
  113. choices: COMPARISON_INTERVAL_CHOICES,
  114. initial: '1w',
  115. },
  116. },
  117. };
  118. }
  119. }
  120. return changeAlertNode;
  121. };
  122. render() {
  123. const {
  124. onAddRow,
  125. onResetRow,
  126. onDeleteRow,
  127. onPropertyChange,
  128. nodes,
  129. placeholder,
  130. items,
  131. organization,
  132. project,
  133. disabled,
  134. error,
  135. selectType,
  136. } = this.props;
  137. const shouldUsePrompt = project.features?.includes?.('issue-alerts-targeting');
  138. const enabledNodes = nodes ? nodes.filter(({enabled}) => enabled) : [];
  139. const createSelectOptions = (actions: IssueAlertRuleActionTemplate[]) =>
  140. actions.map(node => {
  141. const isNew = node.id === EVENT_FREQUENCY_PERCENT_CONDITION;
  142. if (node.id.includes('NotifyEmailAction')) {
  143. return {
  144. value: node.id,
  145. label: t('Issue Owners, Team, or Member'),
  146. };
  147. }
  148. return {
  149. value: node.id,
  150. label: (
  151. <React.Fragment>
  152. {isNew && <StyledFeatureBadge type="new" noTooltip />}
  153. {shouldUsePrompt && node.prompt?.length > 0 ? node.prompt : node.label}
  154. </React.Fragment>
  155. ),
  156. };
  157. });
  158. let options: any = !selectType ? createSelectOptions(enabledNodes) : [];
  159. if (selectType === 'grouped') {
  160. const grouped = enabledNodes.reduce(
  161. (acc, curr) => {
  162. if (curr.actionType === 'ticket') {
  163. acc.ticket.push(curr);
  164. } else if (curr.id.includes('event_frequency')) {
  165. acc.frequency.push(curr);
  166. } else if (
  167. curr.id.includes('sentry.rules.conditions') &&
  168. !curr.id.includes('event_frequency')
  169. ) {
  170. acc.change.push(curr);
  171. } else if (curr.id.includes('sentry.integrations')) {
  172. acc.notifyIntegration.push(curr);
  173. } else if (curr.id.includes('notify_event')) {
  174. acc.notifyIntegration.push(curr);
  175. } else {
  176. acc.notify.push(curr);
  177. }
  178. return acc;
  179. },
  180. {
  181. notify: [] as IssueAlertRuleActionTemplate[],
  182. notifyIntegration: [] as IssueAlertRuleActionTemplate[],
  183. ticket: [] as IssueAlertRuleActionTemplate[],
  184. change: [] as IssueAlertRuleConditionTemplate[],
  185. frequency: [] as IssueAlertRuleConditionTemplate[],
  186. }
  187. );
  188. options = Object.entries(grouped)
  189. .filter(([_, values]) => values.length)
  190. .map(([key, values]) => {
  191. const label = {
  192. notify: t('Send notification to\u{2026}'),
  193. notifyIntegration: t('Notify integration\u{2026}'),
  194. ticket: t('Create new\u{2026}'),
  195. change: t('Issue state change'),
  196. frequency: t('Issue frequency'),
  197. };
  198. return {label: label[key], options: createSelectOptions(values)};
  199. });
  200. }
  201. return (
  202. <React.Fragment>
  203. <RuleNodes>
  204. {error}
  205. {items.map((item, idx) => (
  206. <RuleNode
  207. key={idx}
  208. index={idx}
  209. node={this.getNode(item.id, idx)}
  210. onDelete={onDeleteRow}
  211. onPropertyChange={onPropertyChange}
  212. onReset={onResetRow}
  213. data={item}
  214. organization={organization}
  215. project={project}
  216. disabled={disabled}
  217. />
  218. ))}
  219. </RuleNodes>
  220. <StyledSelectControl
  221. placeholder={placeholder}
  222. value={null}
  223. onChange={obj => onAddRow(obj ? obj.value : obj)}
  224. options={options}
  225. disabled={disabled}
  226. />
  227. </React.Fragment>
  228. );
  229. }
  230. }
  231. export default RuleNodeList;
  232. const StyledSelectControl = styled(SelectControl)`
  233. width: 100%;
  234. `;
  235. const RuleNodes = styled('div')`
  236. display: grid;
  237. margin-bottom: ${space(1)};
  238. gap: ${space(1)};
  239. @media (max-width: ${p => p.theme.breakpoints[1]}) {
  240. grid-auto-flow: row;
  241. }
  242. `;
  243. const StyledFeatureBadge = styled(FeatureBadge)`
  244. margin: 0 ${space(1)} 0 0;
  245. `;