thresholds.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import {Fragment} from 'react';
  2. import cloneDeep from 'lodash/cloneDeep';
  3. import {t, tct} from 'sentry/locale';
  4. import {defined} from 'sentry/utils';
  5. import {
  6. HighlightedText,
  7. Thresholds,
  8. } from 'sentry/views/dashboards/widgetBuilder/buildSteps/thresholdsStep/thresholdsStep';
  9. import {SectionHeader} from 'sentry/views/dashboards/widgetBuilder/components/common/sectionHeader';
  10. import {useWidgetBuilderContext} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
  11. import {BuilderStateAction} from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
  12. function ThresholdsSection({dataType, dataUnit}: {dataType?: string; dataUnit?: string}) {
  13. const {state, dispatch} = useWidgetBuilderContext();
  14. return (
  15. <Fragment>
  16. <SectionHeader
  17. title={t('Thresholds')}
  18. tooltipText={tct(
  19. 'Set thresholds to identify problematic widgets. For example: setting the max values, [thresholdValues] will display a green indicator for results in the range [greenRange], a yellow indicator for results in the range [yellowRange] and a red indicator for results above [redValue].',
  20. {
  21. thresholdValues: <HighlightedText>(green: 100, yellow: 200)</HighlightedText>,
  22. greenRange: <HighlightedText>[0 - 100]</HighlightedText>,
  23. yellowRange: <HighlightedText>(100 - 200]</HighlightedText>,
  24. redValue: <HighlightedText>200</HighlightedText>,
  25. }
  26. )}
  27. optional
  28. />
  29. <Thresholds
  30. thresholdsConfig={state.thresholds ?? null}
  31. onThresholdChange={(maxKey, value) => {
  32. let newThresholds = cloneDeep(state.thresholds);
  33. if (!defined(newThresholds) && value) {
  34. newThresholds = {max_values: {}, unit: null};
  35. }
  36. if (newThresholds) {
  37. if (value) {
  38. newThresholds.max_values[maxKey] = parseInt(value, 10);
  39. } else {
  40. delete newThresholds.max_values[maxKey];
  41. }
  42. }
  43. // Check if the value cleared all of the max values
  44. if (
  45. newThresholds &&
  46. Object.values(newThresholds.max_values).every(
  47. nextMaxValue => !defined(nextMaxValue)
  48. )
  49. ) {
  50. newThresholds = undefined;
  51. }
  52. dispatch({
  53. type: BuilderStateAction.SET_THRESHOLDS,
  54. payload: newThresholds,
  55. });
  56. }}
  57. onUnitChange={unit => {
  58. dispatch({
  59. type: BuilderStateAction.SET_THRESHOLDS,
  60. payload: {
  61. max_values: state.thresholds?.max_values ?? {},
  62. unit,
  63. },
  64. });
  65. }}
  66. dataType={dataType}
  67. dataUnit={dataUnit}
  68. // TODO: Add errors
  69. errors={{}}
  70. />
  71. </Fragment>
  72. );
  73. }
  74. export default ThresholdsSection;