1
- import { useCallback , useState } from 'react' ;
1
+ import { useContext } from 'react' ;
2
2
import styled from '@emotion/styled' ;
3
3
4
4
import { GroupPriorityBadge } from 'sentry/components/badge/groupPriority' ;
5
5
import { Flex } from 'sentry/components/container/flex' ;
6
- import { CompactSelect , type SelectOption } from 'sentry/components/core/compactSelect' ;
6
+ import { CompactSelect } from 'sentry/components/core/compactSelect' ;
7
7
import { FieldWrapper } from 'sentry/components/forms/fieldGroup/fieldWrapper' ;
8
8
import NumberField from 'sentry/components/forms/fields/numberField' ;
9
+ import FormContext from 'sentry/components/forms/formContext' ;
9
10
import InteractionStateLayer from 'sentry/components/interactionStateLayer' ;
11
+ import { useFormField } from 'sentry/components/workflowEngine/form/hooks' ;
10
12
import { IconArrow , IconChevron } from 'sentry/icons' ;
11
13
import { t } from 'sentry/locale' ;
12
14
import { space } from 'sentry/styles/space' ;
13
15
import { PriorityLevel } from 'sentry/types/group' ;
14
16
15
- interface PriorityControlGridProps {
16
- name : string ;
17
- onPriorityChange ?: ( value : PriorityLevel ) => void ;
18
- onThresholdChange ?: ( level : PriorityLevel , threshold : number ) => void ;
19
- priority ?: PriorityLevel ;
20
- thresholds ?: PriorityThresholds ;
17
+ function ThresholdPriority ( ) {
18
+ const lowThresholdDirection = useFormField < string > ( 'conditionGroup.conditions.0.type' ) ! ;
19
+ const lowThreshold = useFormField < string > ( 'conditionGroup.conditions.0.comparison' ) ! ;
20
+ return (
21
+ < div >
22
+ { lowThresholdDirection === ''
23
+ ? t ( 'Above' )
24
+ : lowThresholdDirection === 'above'
25
+ ? t ( 'Above' )
26
+ : t ( 'Below' ) } { ' ' }
27
+ { lowThreshold === '' ? '0s' : lowThreshold + 's' }
28
+ </ div >
29
+ ) ;
21
30
}
22
31
23
- interface PriorityThresholds {
24
- high ?: number ;
25
- medium ?: number ;
32
+ function ChangePriority ( ) {
33
+ const lowThresholdDirection = useFormField < string > ( 'conditionGroup.conditions.0.type' ) ! ;
34
+ const lowThreshold = useFormField < string > ( 'conditionGroup.conditions.0.comparison' ) ! ;
35
+ return (
36
+ < div >
37
+ { lowThreshold === '' ? '0' : lowThreshold } %{ ' ' }
38
+ { lowThresholdDirection === ''
39
+ ? t ( 'higher' )
40
+ : lowThresholdDirection === 'higher'
41
+ ? t ( 'higher' )
42
+ : t ( 'lower' ) }
43
+ </ div >
44
+ ) ;
26
45
}
27
46
28
- export default function PriorityControl ( {
29
- name,
30
- priority : initialPriority ,
31
- onPriorityChange,
32
- thresholds : initialThresholds ,
33
- onThresholdChange,
34
- } : PriorityControlGridProps ) {
35
- const [ priority , setPriority ] = useState < PriorityLevel > (
36
- initialPriority ?? PriorityLevel . LOW
37
- ) ;
38
- const [ thresholds , setThresholds ] = useState < PriorityThresholds > (
39
- initialThresholds ?? { }
40
- ) ;
41
- const setCreatedPriority = useCallback (
42
- ( level : PriorityLevel ) => {
43
- setPriority ( level ) ;
44
- onPriorityChange ?.( level ) ;
45
- } ,
46
- [ setPriority , onPriorityChange ]
47
- ) ;
48
- const setMediumThreshold = useCallback (
49
- ( threshold : number ) => {
50
- setThresholds ( v => ( { ...v , [ PriorityLevel . MEDIUM ] : threshold } ) ) ;
51
- onThresholdChange ?.( PriorityLevel . MEDIUM , threshold ) ;
52
- } ,
53
- [ setThresholds , onThresholdChange ]
54
- ) ;
55
- const setHighThreshold = useCallback (
56
- ( threshold : number ) => {
57
- setThresholds ( v => ( { ...v , [ PriorityLevel . HIGH ] : threshold } ) ) ;
58
- onThresholdChange ?.( PriorityLevel . HIGH , threshold ) ;
59
- } ,
60
- [ setThresholds , onThresholdChange ]
61
- ) ;
47
+ export default function PriorityControl ( ) {
48
+ // 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 ;
62
53
63
54
return (
64
55
< Grid >
65
56
< PrioritizeRow
66
- left = { < span style = { { textAlign : 'right' } } > { t ( 'Issue created' ) } </ span > }
67
- right = { < PrioritySelect value = { priority } onChange = { setCreatedPriority } /> }
57
+ left = {
58
+ < Flex align = "center" column >
59
+ { ! detectorKind || detectorKind === 'threshold' ? (
60
+ < ThresholdPriority />
61
+ ) : (
62
+ < ChangePriority />
63
+ ) }
64
+ < SecondaryLabel > ({ t ( 'issue created' ) } )</ SecondaryLabel >
65
+ </ Flex >
66
+ }
67
+ right = { < PrioritySelect /> }
68
68
/>
69
- { priorityIsConfigurable ( priority , PriorityLevel . MEDIUM ) && (
69
+ { priorityIsConfigurable ( conditionResult , PriorityLevel . MEDIUM ) && (
70
70
< PrioritizeRow
71
71
left = {
72
72
< NumberField
@@ -77,17 +77,14 @@ export default function PriorityControl({
77
77
size = "sm"
78
78
suffix = "s"
79
79
placeholder = "0"
80
- // empty string required to keep this as a controlled input
81
- value = { thresholds [ PriorityLevel . MEDIUM ] ?? '' }
82
- onChange = { threshold => setMediumThreshold ( Number ( threshold ) ) }
83
- name = { `${ name } -medium` }
80
+ name = { `conditionGroup.conditions.1.comparison` }
84
81
data-test-id = "priority-control-medium"
85
82
/>
86
83
}
87
84
right = { < GroupPriorityBadge showLabel priority = { PriorityLevel . MEDIUM } /> }
88
85
/>
89
86
) }
90
- { priorityIsConfigurable ( priority , PriorityLevel . HIGH ) && (
87
+ { priorityIsConfigurable ( conditionResult , PriorityLevel . HIGH ) && (
91
88
< PrioritizeRow
92
89
left = {
93
90
< NumberField
@@ -98,10 +95,7 @@ export default function PriorityControl({
98
95
size = "sm"
99
96
suffix = "s"
100
97
placeholder = "0"
101
- // empty string required to keep this as a controlled input
102
- value = { thresholds [ PriorityLevel . HIGH ] ?? '' }
103
- onChange = { threshold => setHighThreshold ( Number ( threshold ) ) }
104
- name = { `${ name } -high` }
98
+ name = { `conditionGroup.conditions.2.comparison` }
105
99
data-test-id = "priority-control-high"
106
100
/>
107
101
}
@@ -143,29 +137,19 @@ function PrioritizeRow({left, right}: {left: React.ReactNode; right: React.React
143
137
144
138
const priorities = [ PriorityLevel . LOW , PriorityLevel . MEDIUM , PriorityLevel . HIGH ] ;
145
139
146
- function PrioritySelect ( {
147
- value : initialValue ,
148
- onChange = ( ) => { } ,
149
- } : {
150
- onChange ?: ( value : PriorityLevel ) => void ;
151
- value ?: PriorityLevel ;
152
- } ) {
153
- const [ value , setValue ] = useState < PriorityLevel > ( initialValue ?? PriorityLevel . HIGH ) ;
154
- const handleChange = useCallback (
155
- ( select : SelectOption < PriorityLevel > ) => {
156
- onChange ( select . value ) ;
157
- setValue ( select . value ) ;
158
- } ,
159
- [ onChange , setValue ]
160
- ) ;
140
+ function PrioritySelect ( ) {
141
+ const formContext = useContext ( FormContext ) ;
142
+ const conditionResult =
143
+ useFormField < PriorityLevel > ( 'conditionGroup.conditions.0.conditionResult' ) ||
144
+ PriorityLevel . LOW ;
161
145
162
146
return (
163
147
< CompactSelect
164
148
size = "xs"
165
149
trigger = { ( props , isOpen ) => {
166
150
return (
167
151
< EmptyButton { ...props } >
168
- < GroupPriorityBadge showLabel priority = { value } >
152
+ < GroupPriorityBadge showLabel priority = { conditionResult } >
169
153
< InteractionStateLayer isPressed = { isOpen } />
170
154
< IconChevron direction = { isOpen ? 'up' : 'down' } size = "xs" />
171
155
</ GroupPriorityBadge >
@@ -177,8 +161,10 @@ function PrioritySelect({
177
161
value : priority ,
178
162
textValue : priority ,
179
163
} ) ) }
180
- value = { value }
181
- onChange = { handleChange }
164
+ value = { conditionResult }
165
+ onChange = { ( { value} ) => {
166
+ formContext . form ?. setValue ( 'conditionGroup.conditions.0.conditionResult' , value ) ;
167
+ } }
182
168
/>
183
169
) ;
184
170
}
@@ -212,3 +198,8 @@ const Cell = styled(Flex)`
212
198
width: 5rem;
213
199
}
214
200
` ;
201
+
202
+ const SecondaryLabel = styled ( 'div' ) `
203
+ font-size: ${ p => p . theme . fontSizeSmall } ;
204
+ color: ${ p => p . theme . subText } ;
205
+ ` ;
0 commit comments