ruleNodeList.tsx 8.2 KB

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