thresholdControl.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import Feature from 'sentry/components/acl/feature';
  4. import SelectControl from 'sentry/components/forms/controls/selectControl';
  5. import Input from 'sentry/components/input';
  6. import NumberDragControl from 'sentry/components/numberDragControl';
  7. import {Tooltip} from 'sentry/components/tooltip';
  8. import {t, tct, tn} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {ThresholdControlValue} from 'sentry/views/alerts/rules/metric/types';
  11. import {
  12. AlertRuleComparisonType,
  13. AlertRuleThresholdType,
  14. } from 'sentry/views/alerts/rules/metric/types';
  15. type Props = ThresholdControlValue & {
  16. comparisonType: AlertRuleComparisonType;
  17. disableThresholdType: boolean;
  18. disabled: boolean;
  19. onChange: (value: ThresholdControlValue, e: React.FormEvent) => void;
  20. onThresholdPeriodChange: (value: number) => void;
  21. onThresholdTypeChange: (thresholdType: AlertRuleThresholdType) => void;
  22. placeholder: string;
  23. thresholdPeriod: number | null;
  24. type: string;
  25. hideControl?: boolean;
  26. };
  27. type State = {
  28. currentValue: string | null;
  29. };
  30. class ThresholdControl extends Component<Props, State> {
  31. state: State = {
  32. currentValue: null,
  33. };
  34. handleThresholdChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  35. const {value} = e.target;
  36. // Only allow number and partial number inputs
  37. if (!/^[0-9]*\.?[0-9]*$/.test(value)) {
  38. return;
  39. }
  40. const {onChange, thresholdType} = this.props;
  41. // Empty input
  42. if (value === '') {
  43. this.setState({currentValue: null});
  44. onChange({thresholdType, threshold: ''}, e);
  45. return;
  46. }
  47. // Only call onChange if the new number is valid, and not partially typed
  48. // (eg writing out the decimal '5.')
  49. if (/\.+0*$/.test(value)) {
  50. this.setState({currentValue: value});
  51. return;
  52. }
  53. const numberValue = Number(value);
  54. this.setState({currentValue: null});
  55. onChange({thresholdType, threshold: numberValue}, e);
  56. };
  57. /**
  58. * Coerce the currentValue to a number and trigger the onChange.
  59. */
  60. handleThresholdBlur = (e: React.FocusEvent<HTMLInputElement>) => {
  61. if (this.state.currentValue === null) {
  62. return;
  63. }
  64. const {onChange, thresholdType} = this.props;
  65. onChange({thresholdType, threshold: Number(this.state.currentValue)}, e);
  66. this.setState({currentValue: null});
  67. };
  68. handleTypeChange = ({value}) => {
  69. const {onThresholdTypeChange} = this.props;
  70. onThresholdTypeChange(value);
  71. };
  72. handleDragChange = (delta: number, e: React.MouseEvent) => {
  73. const {onChange, thresholdType, threshold} = this.props;
  74. const currentValue = threshold || 0;
  75. onChange({thresholdType, threshold: currentValue + delta}, e);
  76. };
  77. handleThresholdPeriodChange = ({value}) => {
  78. this.props.onThresholdPeriodChange(value);
  79. };
  80. render() {
  81. const {currentValue} = this.state;
  82. const {
  83. thresholdPeriod,
  84. thresholdType,
  85. comparisonType,
  86. hideControl,
  87. threshold,
  88. placeholder,
  89. type,
  90. onChange: _,
  91. onThresholdTypeChange: __,
  92. disabled,
  93. disableThresholdType,
  94. } = this.props;
  95. return (
  96. <Wrapper>
  97. <Container comparisonType={comparisonType}>
  98. <SelectContainer>
  99. <SelectControl
  100. isDisabled={disabled || disableThresholdType}
  101. name={`${type}ThresholdType`}
  102. value={thresholdType}
  103. options={[
  104. {
  105. value: AlertRuleThresholdType.BELOW,
  106. label:
  107. comparisonType === AlertRuleComparisonType.COUNT
  108. ? hideControl
  109. ? t('When below Critical or Warning')
  110. : t('Below')
  111. : hideControl
  112. ? t('When lower than Critical or Warning')
  113. : t('Lower than'),
  114. },
  115. {
  116. value: AlertRuleThresholdType.ABOVE,
  117. label:
  118. comparisonType === AlertRuleComparisonType.COUNT
  119. ? hideControl
  120. ? t('When above Critical or Warning')
  121. : t('Above')
  122. : hideControl
  123. ? t('When higher than Critical or Warning')
  124. : t('Higher than'),
  125. },
  126. ]}
  127. components={disableThresholdType ? {DropdownIndicator: null} : undefined}
  128. styles={
  129. disableThresholdType
  130. ? {
  131. control: provided => ({
  132. ...provided,
  133. cursor: 'not-allowed',
  134. pointerEvents: 'auto',
  135. }),
  136. }
  137. : undefined
  138. }
  139. onChange={this.handleTypeChange}
  140. />
  141. </SelectContainer>
  142. {!hideControl && (
  143. <ThresholdContainer comparisonType={comparisonType}>
  144. <ThresholdInput>
  145. <Input
  146. size="md"
  147. disabled={disabled}
  148. name={`${type}Threshold`}
  149. data-test-id={`${type}-threshold`}
  150. placeholder={placeholder}
  151. value={currentValue ?? threshold ?? ''}
  152. onChange={this.handleThresholdChange}
  153. onBlur={this.handleThresholdBlur}
  154. // Disable lastpass autocomplete
  155. data-lpignore="true"
  156. />
  157. <DragContainer>
  158. <Tooltip
  159. title={tct(
  160. 'Drag to adjust threshold[break]You can hold shift to fine tune',
  161. {
  162. break: <br />,
  163. }
  164. )}
  165. >
  166. <NumberDragControl
  167. step={5}
  168. axis="y"
  169. onChange={this.handleDragChange}
  170. />
  171. </Tooltip>
  172. </DragContainer>
  173. </ThresholdInput>
  174. {comparisonType === AlertRuleComparisonType.CHANGE && (
  175. <PercentWrapper>%</PercentWrapper>
  176. )}
  177. </ThresholdContainer>
  178. )}
  179. </Container>
  180. {!hideControl && (
  181. <Feature features="metric-alert-threshold-period">
  182. <SelectContainer>
  183. <SelectControl
  184. isDisabled={disabled}
  185. name="thresholdPeriod"
  186. value={thresholdPeriod}
  187. options={[1, 2, 5, 10, 20].map(value => ({
  188. value,
  189. label: tn('For %s minute', 'For %s minutes', value),
  190. }))}
  191. onChange={this.handleThresholdPeriodChange}
  192. />
  193. </SelectContainer>
  194. </Feature>
  195. )}
  196. </Wrapper>
  197. );
  198. }
  199. }
  200. const Wrapper = styled('div')`
  201. display: flex;
  202. align-items: center;
  203. gap: ${space(1)};
  204. `;
  205. const Container = styled('div')<{comparisonType: AlertRuleComparisonType}>`
  206. flex: 2;
  207. display: flex;
  208. align-items: center;
  209. flex-direction: ${p =>
  210. p.comparisonType === AlertRuleComparisonType.COUNT ? 'row' : 'row-reverse'};
  211. gap: ${space(1)};
  212. `;
  213. const SelectContainer = styled('div')`
  214. flex: 1;
  215. `;
  216. const ThresholdContainer = styled('div')<{comparisonType: AlertRuleComparisonType}>`
  217. flex: 1;
  218. display: flex;
  219. flex-direction: row;
  220. align-items: center;
  221. `;
  222. const ThresholdInput = styled('div')`
  223. position: relative;
  224. display: flex;
  225. flex-direction: row;
  226. align-items: center;
  227. `;
  228. const PercentWrapper = styled('div')`
  229. margin-left: ${space(1)};
  230. `;
  231. const DragContainer = styled('div')`
  232. position: absolute;
  233. top: 4px;
  234. right: 12px;
  235. `;
  236. export default ThresholdControl;