ruleNodeList.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/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/utils/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 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 enabledNodes = nodes ? nodes.filter(({enabled}) => enabled) : [];
  138. const createSelectOptions = (actions: IssueAlertRuleActionTemplate[]) =>
  139. actions.map(node => {
  140. const isNew = node.id === EVENT_FREQUENCY_PERCENT_CONDITION;
  141. if (node.id.includes('NotifyEmailAction')) {
  142. return {
  143. value: node.id,
  144. label: organization.features?.includes('alert-release-notification-workflow')
  145. ? t('Issue Owners, Team, Member, or Release Members')
  146. : t('Issue Owners, Team, or Member'),
  147. };
  148. }
  149. return {
  150. value: node.id,
  151. label: (
  152. <Fragment>
  153. {isNew && <StyledFeatureBadge type="new" noTooltip />}
  154. {node.prompt?.length ? node.prompt : node.label}
  155. </Fragment>
  156. ),
  157. };
  158. });
  159. let options: any = !selectType ? createSelectOptions(enabledNodes) : [];
  160. if (selectType === 'grouped') {
  161. const grouped = enabledNodes.reduce(
  162. (acc, curr) => {
  163. if (curr.actionType === 'ticket') {
  164. acc.ticket.push(curr);
  165. } else if (curr.id.includes('event_frequency')) {
  166. acc.frequency.push(curr);
  167. } else if (
  168. curr.id.includes('sentry.rules.conditions') &&
  169. !curr.id.includes('event_frequency')
  170. ) {
  171. acc.change.push(curr);
  172. } else if (curr.id.includes('sentry.integrations')) {
  173. acc.notifyIntegration.push(curr);
  174. } else if (curr.id.includes('notify_event')) {
  175. acc.notifyIntegration.push(curr);
  176. } else {
  177. acc.notify.push(curr);
  178. }
  179. return acc;
  180. },
  181. {
  182. notify: [] as IssueAlertRuleActionTemplate[],
  183. notifyIntegration: [] as IssueAlertRuleActionTemplate[],
  184. ticket: [] as IssueAlertRuleActionTemplate[],
  185. change: [] as IssueAlertRuleConditionTemplate[],
  186. frequency: [] as IssueAlertRuleConditionTemplate[],
  187. }
  188. );
  189. options = Object.entries(grouped)
  190. .filter(([_, values]) => values.length)
  191. .map(([key, values]) => {
  192. const label = {
  193. notify: t('Send notification to\u{2026}'),
  194. notifyIntegration: t('Notify integration\u{2026}'),
  195. ticket: t('Create new\u{2026}'),
  196. change: t('Issue state change'),
  197. frequency: t('Issue frequency'),
  198. };
  199. return {label: label[key], options: createSelectOptions(values)};
  200. });
  201. }
  202. return (
  203. <Fragment>
  204. <RuleNodes>
  205. {error}
  206. {items.map((item, idx) => (
  207. <RuleNode
  208. key={idx}
  209. index={idx}
  210. node={this.getNode(item.id, idx)}
  211. onDelete={onDeleteRow}
  212. onPropertyChange={onPropertyChange}
  213. onReset={onResetRow}
  214. data={item}
  215. organization={organization}
  216. project={project}
  217. disabled={disabled}
  218. />
  219. ))}
  220. </RuleNodes>
  221. <StyledSelectControl
  222. placeholder={placeholder}
  223. value={null}
  224. onChange={obj => onAddRow(obj ? obj.value : obj)}
  225. options={options}
  226. disabled={disabled}
  227. />
  228. </Fragment>
  229. );
  230. }
  231. }
  232. export default RuleNodeList;
  233. const StyledSelectControl = styled(SelectControl)`
  234. width: 100%;
  235. `;
  236. const RuleNodes = styled('div')`
  237. display: grid;
  238. margin-bottom: ${space(1)};
  239. gap: ${space(1)};
  240. @media (max-width: ${p => p.theme.breakpoints.medium}) {
  241. grid-auto-flow: row;
  242. }
  243. `;
  244. const StyledFeatureBadge = styled(FeatureBadge)`
  245. margin: 0 ${space(1)} 0 0;
  246. `;