index.tsx 4.6 KB

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