index.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {Component, Fragment} from 'react';
  2. import {Panel, PanelBody} from 'sentry/components/panels';
  3. import {Organization, Project} from 'sentry/types';
  4. import {removeAtArrayIndex} from 'sentry/utils/removeAtArrayIndex';
  5. import {replaceAtArrayIndex} from 'sentry/utils/replaceAtArrayIndex';
  6. import ActionsPanel from 'sentry/views/alerts/rules/metric/triggers/actionsPanel';
  7. import TriggerForm from 'sentry/views/alerts/rules/metric/triggers/form';
  8. import {
  9. Action,
  10. AlertRuleComparisonType,
  11. AlertRuleThresholdType,
  12. MetricActionTemplate,
  13. Trigger,
  14. UnsavedMetricRule,
  15. } from '../types';
  16. type Props = {
  17. aggregate: UnsavedMetricRule['aggregate'];
  18. availableActions: MetricActionTemplate[] | null;
  19. comparisonType: AlertRuleComparisonType;
  20. currentProject: string;
  21. disabled: boolean;
  22. errors: Map<number, {[fieldName: string]: string}>;
  23. hasAlertWizardV3: boolean;
  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. hasAlertWizardV3,
  97. onThresholdTypeChange,
  98. onResolveThresholdChange,
  99. onThresholdPeriodChange,
  100. } = this.props;
  101. // Note we only support 2 triggers max
  102. return (
  103. <Fragment>
  104. <Panel>
  105. <PanelBody>
  106. <TriggerForm
  107. disabled={disabled}
  108. errors={errors}
  109. organization={organization}
  110. projects={projects}
  111. triggers={triggers}
  112. aggregate={aggregate}
  113. resolveThreshold={resolveThreshold}
  114. thresholdType={thresholdType}
  115. thresholdPeriod={thresholdPeriod}
  116. comparisonType={comparisonType}
  117. hasAlertWizardV3={hasAlertWizardV3}
  118. onChange={this.handleChangeTrigger}
  119. onThresholdTypeChange={onThresholdTypeChange}
  120. onResolveThresholdChange={onResolveThresholdChange}
  121. onThresholdPeriodChange={onThresholdPeriodChange}
  122. />
  123. </PanelBody>
  124. </Panel>
  125. <ActionsPanel
  126. disabled={disabled}
  127. loading={availableActions === null}
  128. error={false}
  129. availableActions={availableActions}
  130. currentProject={currentProject}
  131. organization={organization}
  132. hasAlertWizardV3={hasAlertWizardV3}
  133. projects={projects}
  134. triggers={triggers}
  135. onChange={this.handleChangeActions}
  136. onAdd={this.handleAddAction}
  137. />
  138. </Fragment>
  139. );
  140. }
  141. }
  142. export default Triggers;