index.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {Component, Fragment} from 'react';
  2. import Panel from 'sentry/components/panels/panel';
  3. import PanelBody from 'sentry/components/panels/panelBody';
  4. import type {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 type {
  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. isMigration?: boolean;
  41. };
  42. /**
  43. * A list of forms to add, edit, and delete triggers.
  44. */
  45. class Triggers extends Component<Props> {
  46. handleDeleteTrigger = (index: number) => {
  47. const {triggers, onChange} = this.props;
  48. const updatedTriggers = removeAtArrayIndex(triggers, index);
  49. onChange(updatedTriggers);
  50. };
  51. handleChangeTrigger = (
  52. triggerIndex: number,
  53. trigger: Trigger,
  54. changeObj: Partial<Trigger>
  55. ) => {
  56. const {triggers, onChange} = this.props;
  57. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, trigger);
  58. onChange(updatedTriggers, triggerIndex, changeObj);
  59. };
  60. handleAddAction = (triggerIndex: number, action: Action) => {
  61. const {onChange, triggers} = this.props;
  62. const trigger = triggers[triggerIndex];
  63. const actions = [...trigger.actions, action];
  64. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  65. ...trigger,
  66. actions,
  67. });
  68. onChange(updatedTriggers, triggerIndex, {actions});
  69. };
  70. handleChangeActions = (
  71. triggerIndex: number,
  72. triggers: Trigger[],
  73. actions: Action[]
  74. ): void => {
  75. const {onChange} = this.props;
  76. const trigger = triggers[triggerIndex];
  77. const updatedTriggers = replaceAtArrayIndex(triggers, triggerIndex, {
  78. ...trigger,
  79. actions,
  80. });
  81. onChange(updatedTriggers, triggerIndex, {actions});
  82. };
  83. render() {
  84. const {
  85. availableActions,
  86. currentProject,
  87. errors,
  88. organization,
  89. projects,
  90. triggers,
  91. disabled,
  92. aggregate,
  93. thresholdType,
  94. thresholdPeriod,
  95. comparisonType,
  96. resolveThreshold,
  97. isMigration,
  98. onThresholdTypeChange,
  99. onResolveThresholdChange,
  100. onThresholdPeriodChange,
  101. } = this.props;
  102. // Note we only support 2 triggers max
  103. return (
  104. <Fragment>
  105. <Panel>
  106. <PanelBody>
  107. <TriggerForm
  108. disabled={disabled}
  109. errors={errors}
  110. organization={organization}
  111. projects={projects}
  112. triggers={triggers}
  113. aggregate={aggregate}
  114. resolveThreshold={resolveThreshold}
  115. thresholdType={thresholdType}
  116. thresholdPeriod={thresholdPeriod}
  117. comparisonType={comparisonType}
  118. onChange={this.handleChangeTrigger}
  119. onThresholdTypeChange={onThresholdTypeChange}
  120. onResolveThresholdChange={onResolveThresholdChange}
  121. onThresholdPeriodChange={onThresholdPeriodChange}
  122. />
  123. </PanelBody>
  124. </Panel>
  125. {isMigration ? null : (
  126. <ActionsPanel
  127. disabled={disabled}
  128. loading={availableActions === null}
  129. error={false}
  130. availableActions={availableActions}
  131. currentProject={currentProject}
  132. organization={organization}
  133. projects={projects}
  134. triggers={triggers}
  135. onChange={this.handleChangeActions}
  136. onAdd={this.handleAddAction}
  137. />
  138. )}
  139. </Fragment>
  140. );
  141. }
  142. }
  143. export default Triggers;