ruleNodeList.tsx 6.3 KB

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