@@ -8,48 +8,76 @@ import {FieldWrapper} from 'sentry/components/forms/fieldGroup/fieldWrapper';
8
8
import NumberField from 'sentry/components/forms/fields/numberField' ;
9
9
import FormContext from 'sentry/components/forms/formContext' ;
10
10
import InteractionStateLayer from 'sentry/components/interactionStateLayer' ;
11
- import { useFormField } from 'sentry/components/workflowEngine/form/useFormField' ;
12
11
import { IconArrow , IconChevron } from 'sentry/icons' ;
13
12
import { t } from 'sentry/locale' ;
14
13
import { space } from 'sentry/styles/space' ;
15
14
import { PriorityLevel } from 'sentry/types/group' ;
15
+ import {
16
+ DataConditionType ,
17
+ DetectorPriorityLevel ,
18
+ } from 'sentry/types/workflowEngine/dataConditions' ;
19
+ import {
20
+ METRIC_DETECTOR_FORM_FIELDS ,
21
+ useMetricDetectorFormField ,
22
+ } from 'sentry/views/detectors/components/forms/metricFormData' ;
23
+
24
+ const priorities = [
25
+ DetectorPriorityLevel . LOW ,
26
+ DetectorPriorityLevel . MEDIUM ,
27
+ DetectorPriorityLevel . HIGH ,
28
+ ] as const ;
29
+
30
+ const DETECTOR_PRIORITY_LEVEL_TO_PRIORITY_LEVEL : Record <
31
+ ( typeof priorities ) [ number ] ,
32
+ PriorityLevel
33
+ > = {
34
+ [ DetectorPriorityLevel . LOW ] : PriorityLevel . LOW ,
35
+ [ DetectorPriorityLevel . MEDIUM ] : PriorityLevel . MEDIUM ,
36
+ [ DetectorPriorityLevel . HIGH ] : PriorityLevel . HIGH ,
37
+ } ;
16
38
17
39
function ThresholdPriority ( ) {
18
- const lowThresholdDirection = useFormField < string > ( 'conditionGroup.conditions.0.type' ) ! ;
19
- const lowThreshold = useFormField < string > ( 'conditionGroup.conditions.0.comparison' ) ! ;
40
+ const conditionType = useMetricDetectorFormField (
41
+ METRIC_DETECTOR_FORM_FIELDS . conditionType
42
+ ) ;
43
+ const conditionValue = useMetricDetectorFormField (
44
+ METRIC_DETECTOR_FORM_FIELDS . conditionValue
45
+ ) ;
20
46
return (
21
47
< div >
22
- { lowThresholdDirection === ''
23
- ? t ( 'Above' )
24
- : lowThresholdDirection === 'above'
25
- ? t ( 'Above' )
26
- : t ( 'Below' ) } { ' ' }
27
- { lowThreshold === '' ? '0s' : lowThreshold + 's' }
48
+ { conditionType === DataConditionType . GREATER ? t ( 'Above' ) : t ( 'Below' ) } { ' ' }
49
+ { conditionValue === '' ? '0s' : `${ conditionValue } s` }
28
50
</ div >
29
51
) ;
30
52
}
31
53
32
54
function ChangePriority ( ) {
33
- const lowThresholdDirection = useFormField < string > ( 'conditionGroup.conditions.0.type' ) ! ;
34
- const lowThreshold = useFormField < string > ( 'conditionGroup.conditions.0.comparison' ) ! ;
55
+ const conditionType = useMetricDetectorFormField (
56
+ METRIC_DETECTOR_FORM_FIELDS . conditionType
57
+ ) ;
58
+ const conditionValue = useMetricDetectorFormField (
59
+ METRIC_DETECTOR_FORM_FIELDS . conditionValue
60
+ ) ;
35
61
return (
36
62
< div >
37
- { lowThreshold === '' ? '0' : lowThreshold } %{ ' ' }
38
- { lowThresholdDirection === ''
39
- ? t ( 'higher' )
40
- : lowThresholdDirection === 'higher'
41
- ? t ( 'higher' )
42
- : t ( 'lower' ) }
63
+ { conditionValue === '' ? '0' : conditionValue } %{ ' ' }
64
+ { conditionType === DataConditionType . GREATER ? t ( 'higher' ) : t ( 'lower' ) }
43
65
</ div >
44
66
) ;
45
67
}
46
68
47
- export default function PriorityControl ( ) {
69
+ interface PriorityControlProps {
70
+ minimumPriority ?: DetectorPriorityLevel ;
71
+ }
72
+
73
+ export default function PriorityControl ( {
74
+ minimumPriority = DetectorPriorityLevel . LOW ,
75
+ } : PriorityControlProps ) {
48
76
// TODO: kind type not yet available from detector types
49
- const detectorKind = useFormField < string > ( ' kind' ) ! ;
50
- const conditionResult =
51
- useFormField < PriorityLevel > ( 'conditionGroup.conditions.0.conditionResult' ) ||
52
- PriorityLevel . LOW ;
77
+ const detectorKind = useMetricDetectorFormField ( METRIC_DETECTOR_FORM_FIELDS . kind ) ;
78
+ const initialPriorityLevel = useMetricDetectorFormField (
79
+ METRIC_DETECTOR_FORM_FIELDS . initialPriorityLevel
80
+ ) ;
53
81
54
82
return (
55
83
< Grid >
@@ -64,9 +92,9 @@ export default function PriorityControl() {
64
92
< SecondaryLabel > ({ t ( 'issue created' ) } )</ SecondaryLabel >
65
93
</ Flex >
66
94
}
67
- right = { < PrioritySelect /> }
95
+ right = { < InitialPrioritySelect minimumPriority = { minimumPriority } /> }
68
96
/>
69
- { priorityIsConfigurable ( conditionResult , PriorityLevel . MEDIUM ) && (
97
+ { priorityIsConfigurable ( initialPriorityLevel , DetectorPriorityLevel . MEDIUM ) && (
70
98
< PrioritizeRow
71
99
left = {
72
100
< NumberField
@@ -77,14 +105,15 @@ export default function PriorityControl() {
77
105
size = "sm"
78
106
suffix = "s"
79
107
placeholder = "0"
80
- name = { `conditionGroup.conditions.1.comparison` }
108
+ name = { METRIC_DETECTOR_FORM_FIELDS . mediumThreshold }
81
109
data-test-id = "priority-control-medium"
110
+ required
82
111
/>
83
112
}
84
113
right = { < GroupPriorityBadge showLabel priority = { PriorityLevel . MEDIUM } /> }
85
114
/>
86
115
) }
87
- { priorityIsConfigurable ( conditionResult , PriorityLevel . HIGH ) && (
116
+ { priorityIsConfigurable ( initialPriorityLevel , DetectorPriorityLevel . HIGH ) && (
88
117
< PrioritizeRow
89
118
left = {
90
119
< NumberField
@@ -95,8 +124,9 @@ export default function PriorityControl() {
95
124
size = "sm"
96
125
suffix = "s"
97
126
placeholder = "0"
98
- name = { `conditionGroup.conditions.2.comparison` }
127
+ name = { METRIC_DETECTOR_FORM_FIELDS . highThreshold }
99
128
data-test-id = "priority-control-high"
129
+ required
100
130
/>
101
131
}
102
132
right = { < GroupPriorityBadge showLabel priority = { PriorityLevel . HIGH } /> }
@@ -107,18 +137,10 @@ export default function PriorityControl() {
107
137
}
108
138
109
139
function priorityIsConfigurable (
110
- createdPriority : PriorityLevel ,
111
- targetPriority : PriorityLevel
140
+ initialPriorityLevel : DetectorPriorityLevel ,
141
+ targetPriority : DetectorPriorityLevel
112
142
) : boolean {
113
- if ( createdPriority === PriorityLevel . LOW ) {
114
- return (
115
- targetPriority === PriorityLevel . MEDIUM || targetPriority === PriorityLevel . HIGH
116
- ) ;
117
- }
118
- if ( createdPriority === PriorityLevel . MEDIUM ) {
119
- return targetPriority === PriorityLevel . HIGH ;
120
- }
121
- return false ;
143
+ return targetPriority > initialPriorityLevel ;
122
144
}
123
145
124
146
function PrioritizeRow ( { left, right} : { left : React . ReactNode ; right : React . ReactNode } ) {
@@ -135,35 +157,50 @@ function PrioritizeRow({left, right}: {left: React.ReactNode; right: React.React
135
157
) ;
136
158
}
137
159
138
- const priorities = [ PriorityLevel . LOW , PriorityLevel . MEDIUM , PriorityLevel . HIGH ] ;
139
-
140
- function PrioritySelect ( ) {
160
+ function InitialPrioritySelect ( {
161
+ minimumPriority,
162
+ } : {
163
+ minimumPriority : DetectorPriorityLevel ;
164
+ } ) {
141
165
const formContext = useContext ( FormContext ) ;
142
- const conditionResult =
143
- useFormField < PriorityLevel > ( 'conditionGroup.conditions.0.conditionResult' ) ||
144
- PriorityLevel . LOW ;
166
+ const initialPriorityLevel = useMetricDetectorFormField (
167
+ METRIC_DETECTOR_FORM_FIELDS . initialPriorityLevel
168
+ ) ;
145
169
146
170
return (
147
171
< CompactSelect
148
172
size = "xs"
149
173
trigger = { ( props , isOpen ) => {
150
174
return (
151
- < EmptyButton { ...props } >
152
- < GroupPriorityBadge showLabel priority = { conditionResult } >
175
+ < EmptyButton type = "button" { ...props } >
176
+ < GroupPriorityBadge
177
+ showLabel
178
+ priority = { DETECTOR_PRIORITY_LEVEL_TO_PRIORITY_LEVEL [ initialPriorityLevel ] }
179
+ >
153
180
< InteractionStateLayer isPressed = { isOpen } />
154
181
< IconChevron direction = { isOpen ? 'up' : 'down' } size = "xs" />
155
182
</ GroupPriorityBadge >
156
183
</ EmptyButton >
157
184
) ;
158
185
} }
159
- options = { priorities . map ( priority => ( {
160
- label : < GroupPriorityBadge showLabel priority = { priority } /> ,
161
- value : priority ,
162
- textValue : priority ,
163
- } ) ) }
164
- value = { conditionResult }
186
+ options = { priorities
187
+ . filter ( priority => priority >= minimumPriority )
188
+ . map ( priority => ( {
189
+ label : (
190
+ < GroupPriorityBadge
191
+ showLabel
192
+ priority = { DETECTOR_PRIORITY_LEVEL_TO_PRIORITY_LEVEL [ priority ] }
193
+ />
194
+ ) ,
195
+ value : priority ,
196
+ textValue : DETECTOR_PRIORITY_LEVEL_TO_PRIORITY_LEVEL [ priority ] ,
197
+ } ) ) }
198
+ value = { initialPriorityLevel }
165
199
onChange = { ( { value} ) => {
166
- formContext . form ?. setValue ( 'conditionGroup.conditions.0.conditionResult' , value ) ;
200
+ formContext . form ?. setValue (
201
+ METRIC_DETECTOR_FORM_FIELDS . initialPriorityLevel ,
202
+ value
203
+ ) ;
167
204
} }
168
205
/>
169
206
) ;
0 commit comments