thresholdControl.tsx 6.9 KB

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