index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {Component, Fragment} from 'react';
  2. import Panel from 'sentry/components/panels/panel';
  3. import PanelBody from 'sentry/components/panels/panelBody';
  4. import {Organization, Project} from 'sentry/types';
  5. import {removeAtArrayIndex} from 'sentry/utils/removeAtArrayIndex';
  6. import {replaceAtArrayIndex} from 'sentry/utils/replaceAtArrayIndex';
  7. import ActionsPanel from 'sentry/views/alerts/rules/metric/triggers/actionsPanel';
  8. import TriggerForm from 'sentry/views/alerts/rules/metric/triggers/form';
  9. import {
  10. Action,
  11. AlertRuleComparisonType,
  12. AlertRuleThresholdType,
  13. MetricActionTemplate,
  14. Trigger,
  15. UnsavedMetricRule,
  16. } from '../types';
  17. type Props = {
  18. aggregate: UnsavedMetricRule['aggregate'];
  19. availableActions: MetricActionTemplate[] | null;
  20. comparisonType: AlertRuleComparisonType;
  21. currentProject: string;
  22. disabled: boolean;
  23. errors: Map<number, {[fieldName: string]: string}>;
  24. onChange: (
  25. triggers: Trigger[],
  26. triggerIndex?: number,
  27. changeObj?: Partial<Trigger>
  28. ) => void;
  29. onResolveThresholdChange: (
  30. resolveThreshold: UnsavedMetricRule['resolveThreshold']
  31. ) => void;
  32. onThresholdPeriodChange: (value: number) => void;
  33. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  34. organization: Organization;
  35. projects: Project[];
  36. resolveThreshold: UnsavedMetricRule['resolveThreshold'];
  37. thresholdPeriod: UnsavedMetricRule['thresholdPeriod'];
  38. thresholdType: UnsavedMetricRule['thresholdType'];
  39. triggers: Trigger[];
  40. };
  41. /**
  42. * A list of forms to add, edit, and delete triggers.
  43. */
  44. class Triggers extends Component<Props> {
  45. handleDeleteTrigger = (index: number) => {
  46. const {triggers, onChange} = this.props;
  47. const updatedTriggers = removeAtArrayIndex(triggers, index);
  48. onChange(updatedTriggers);
  49. };
  50. handleChangeTrigger = (
  51. triggerIndex: number,
  52. trigger: Trigger,
  53. changeObj: Partial<Trigger>
  54. ) => {
  55. const {triggers, onChange} = this.props;
  56. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, trigger);
  57. onChange(updatedTriggers, triggerIndex, changeObj);
  58. };
  59. handleAddAction = (triggerIndex: number, action: Action) => {
  60. const {onChange, triggers} = this.props;
  61. const trigger = triggers[triggerIndex];
  62. const actions = [...trigger.actions, action];
  63. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  64. ...trigger,
  65. actions,
  66. });
  67. onChange(updatedTriggers, triggerIndex, {actions});
  68. };
  69. handleChangeActions = (
  70. triggerIndex: number,
  71. triggers: Trigger[],
  72. actions: Action[]
  73. ): void => {
  74. const {onChange} = this.props;
  75. const trigger = triggers[triggerIndex];
  76. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  77. ...trigger,
  78. actions,
  79. });
  80. onChange(updatedTriggers, triggerIndex, {actions});
  81. };
  82. render() {
  83. const {
  84. availableActions,
  85. currentProject,
  86. errors,
  87. organization,
  88. projects,
  89. triggers,
  90. disabled,
  91. aggregate,
  92. thresholdType,
  93. thresholdPeriod,
  94. comparisonType,
  95. resolveThreshold,
  96. onThresholdTypeChange,
  97. onResolveThresholdChange,
  98. onThresholdPeriodChange,
  99. } = this.props;
  100. // Note we only support 2 triggers max
  101. return (
  102. <Fragment>
  103. <Panel>
  104. <PanelBody>
  105. <TriggerForm
  106. disabled={disabled}
  107. errors={errors}
  108. organization={organization}
  109. projects={projects}
  110. triggers={triggers}
  111. aggregate={aggregate}
  112. resolveThreshold={resolveThreshold}
  113. thresholdType={thresholdType}
  114. thresholdPeriod={thresholdPeriod}
  115. comparisonType={comparisonType}
  116. onChange={this.handleChangeTrigger}
  117. onThresholdTypeChange={onThresholdTypeChange}
  118. onResolveThresholdChange={onResolveThresholdChange}
  119. onThresholdPeriodChange={onThresholdPeriodChange}
  120. />
  121. </PanelBody>
  122. </Panel>
  123. <ActionsPanel
  124. disabled={disabled}
  125. loading={availableActions === null}
  126. error={false}
  127. availableActions={availableActions}
  128. currentProject={currentProject}
  129. organization={organization}
  130. projects={projects}
  131. triggers={triggers}
  132. onChange={this.handleChangeActions}
  133. onAdd={this.handleAddAction}
  134. />
  135. </Fragment>
  136. );
  137. }
  138. }
  139. export default Triggers;