1
- import { useCallback , useEffect , useState } from 'react' ;
1
+ import { useCallback , useState } from 'react' ;
2
2
3
3
import type { AutofixData , GroupWithAutofix } from 'sentry/components/events/autofix/types' ;
4
4
import type { Event } from 'sentry/types' ;
5
- import { useApiQuery } from 'sentry/utils/queryClient' ;
5
+ import {
6
+ type ApiQueryKey ,
7
+ setApiQueryData ,
8
+ useApiQuery ,
9
+ useQueryClient ,
10
+ } from 'sentry/utils/queryClient' ;
6
11
import useApi from 'sentry/utils/useApi' ;
7
12
13
+ type AutofixResponse = {
14
+ autofix : AutofixData | null ;
15
+ } ;
16
+
8
17
const POLL_INTERVAL = 2500 ;
9
18
19
+ const makeAutofixQueryKey = ( groupId : string ) : ApiQueryKey => [
20
+ `/issues/${ groupId } /ai-autofix/` ,
21
+ ] ;
22
+
23
+ const isPolling = ( autofixData ?: AutofixData | null ) =>
24
+ autofixData ?. status === 'PROCESSING' ;
25
+
10
26
export const useAiAutofix = ( group : GroupWithAutofix , event : Event ) => {
11
27
const api = useApi ( ) ;
28
+ const queryClient = useQueryClient ( ) ;
12
29
13
- const [ overwriteData , setOverwriteData ] = useState < AutofixData | 'reset' | null > ( null ) ;
14
- const autofixData =
15
- ( overwriteData === 'reset' ? null : overwriteData ?? group . metadata ?. autofix ) ?? null ;
16
- const isPolling = autofixData ?. status === 'PROCESSING' ;
30
+ const [ isReset , setIsReset ] = useState < boolean > ( false ) ;
31
+ const initialAutofixData = group . metadata ?. autofix ?? null ;
17
32
18
33
const {
19
34
data : apiData ,
20
35
isError,
21
- refetch : dataRefetch ,
22
36
error,
23
- } = useApiQuery < { autofix : AutofixData | null } > ( [ `/issues/ ${ group . id } /ai-autofix/` ] , {
37
+ } = useApiQuery < AutofixResponse > ( makeAutofixQueryKey ( group . id ) , {
24
38
staleTime : Infinity ,
25
39
retry : false ,
26
- enabled : ! autofixData ?. status || autofixData . status === 'PROCESSING' ,
40
+ initialData : [ { autofix : initialAutofixData } , undefined , undefined ] ,
27
41
refetchInterval : data => {
28
- if ( data ?. [ 0 ] ?. autofix ?. status === 'PROCESSING' ) {
42
+ if ( isPolling ( data ?. [ 0 ] ?. autofix ) ) {
29
43
return POLL_INTERVAL ;
30
44
}
31
45
return false ;
32
46
} ,
33
47
} ) ;
34
48
35
- useEffect ( ( ) => {
36
- if ( overwriteData !== 'reset' && apiData ?. autofix ) {
37
- setOverwriteData ( apiData . autofix ) ;
38
- }
39
- } , [ apiData ?. autofix , overwriteData ] ) ;
40
-
41
49
const triggerAutofix = useCallback (
42
50
async ( instruction : string ) => {
43
- setOverwriteData ( {
44
- status : 'PROCESSING' ,
45
- steps : [
46
- {
47
- id : '1' ,
48
- index : 0 ,
49
- status : 'PROCESSING' ,
50
- title : 'Starting Autofix...' ,
51
- progress : [ ] ,
52
- } ,
53
- ] ,
54
- created_at : new Date ( ) . toISOString ( ) ,
51
+ setIsReset ( false ) ;
52
+ setApiQueryData < AutofixResponse > ( queryClient , makeAutofixQueryKey ( group . id ) , {
53
+ autofix : {
54
+ status : 'PROCESSING' ,
55
+ steps : [
56
+ {
57
+ id : '1' ,
58
+ index : 0 ,
59
+ status : 'PROCESSING' ,
60
+ title : 'Starting Autofix...' ,
61
+ progress : [ ] ,
62
+ } ,
63
+ ] ,
64
+ created_at : new Date ( ) . toISOString ( ) ,
65
+ } ,
55
66
} ) ;
56
67
57
68
try {
@@ -65,21 +76,21 @@ export const useAiAutofix = (group: GroupWithAutofix, event: Event) => {
65
76
} catch ( e ) {
66
77
// Don't need to do anything, error should be in the metadata
67
78
}
68
-
69
- dataRefetch ( ) ;
70
79
} ,
71
- [ api , group . id , event . id , dataRefetch ]
80
+ [ queryClient , group . id , api , event . id ]
72
81
) ;
73
82
74
83
const reset = useCallback ( ( ) => {
75
- setOverwriteData ( 'reset' ) ;
84
+ setIsReset ( true ) ;
76
85
} , [ ] ) ;
77
86
87
+ const autofixData = isReset ? null : apiData ?. autofix ?? null ;
88
+
78
89
return {
79
90
autofixData,
80
91
error,
81
92
isError,
82
- isPolling,
93
+ isPolling : isPolling ( autofixData ) ,
83
94
triggerAutofix,
84
95
reset,
85
96
} ;
0 commit comments