@@ -15,11 +15,7 @@ import type {
1515} from 'sentry/types/workflowEngine/dataConditions' ;
1616import { apiOptions } from 'sentry/utils/api/apiOptions' ;
1717import { getApiUrl } from 'sentry/utils/api/getApiUrl' ;
18- import type {
19- ApiQueryKey ,
20- UseApiQueryOptions ,
21- UseMutationOptions ,
22- } from 'sentry/utils/queryClient' ;
18+ import type { ApiQueryKey , UseMutationOptions } from 'sentry/utils/queryClient' ;
2319import {
2420 setApiQueryData ,
2521 useApiQuery ,
@@ -30,73 +26,48 @@ import type {RequestError} from 'sentry/utils/requestError/requestError';
3026import { useApi } from 'sentry/utils/useApi' ;
3127import { useOrganization } from 'sentry/utils/useOrganization' ;
3228
33- export const makeAutomationsQueryKey = ( {
34- orgSlug,
35- query,
36- sortBy,
37- priorityDetector,
38- ids,
39- limit,
40- cursor,
41- projects,
42- detector,
43- } : {
44- orgSlug : string ;
45- cursor ?: string ;
46- detector ?: string [ ] ;
47- ids ?: string [ ] ;
48- limit ?: number ;
49- priorityDetector ?: string ;
50- projects ?: number [ ] ;
51- query ?: string ;
52- sortBy ?: string ;
53- } ) : ApiQueryKey => [
54- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
55- path : { organizationIdOrSlug : orgSlug } ,
56- } ) ,
57- {
58- query : {
29+ export const automationsApiOptions = (
30+ organization : Organization ,
31+ options ?: {
32+ cursor ?: string ;
33+ detector ?: string [ ] ;
34+ ids ?: string [ ] ;
35+ limit ?: number ;
36+ priorityDetector ?: string ;
37+ projects ?: number [ ] ;
38+ query ?: string ;
39+ sortBy ?: string ;
40+ }
41+ ) => {
42+ const query = options
43+ ? {
44+ query : options . query ,
45+ sortBy : options . sortBy ,
46+ priorityDetector : options . priorityDetector ,
47+ id : options . ids ,
48+ per_page : options . limit ,
49+ cursor : options . cursor ,
50+ project : options . projects ,
51+ detector : options . detector ,
52+ }
53+ : undefined ;
54+
55+ return queryOptions ( {
56+ ...apiOptions . as < Automation [ ] > ( ) ( '/organizations/$organizationIdOrSlug/workflows/' , {
57+ path : { organizationIdOrSlug : organization . slug } ,
5958 query,
60- sortBy,
61- priorityDetector,
62- id : ids ,
63- per_page : limit ,
64- cursor,
65- project : projects ,
66- detector,
67- } ,
68- } ,
69- ] ;
59+ staleTime : 0 ,
60+ } ) ,
61+ retry : false ,
62+ } ) ;
63+ } ;
7064
7165const makeAutomationQueryKey = ( orgSlug : string , automationId : string ) : ApiQueryKey => [
7266 getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/$workflowId/' , {
7367 path : { organizationIdOrSlug : orgSlug , workflowId : automationId } ,
7468 } ) ,
7569] ;
7670
77- interface UseAutomationsQueryOptions {
78- cursor ?: string ;
79- detector ?: string [ ] ;
80- ids ?: string [ ] ;
81- limit ?: number ;
82- priorityDetector ?: string ;
83- projects ?: number [ ] ;
84- query ?: string ;
85- sortBy ?: string ;
86- }
87- export function useAutomationsQuery (
88- options : UseAutomationsQueryOptions = { } ,
89- useApiQueryOptions : Partial < UseApiQueryOptions < Automation [ ] > > = { }
90- ) {
91- const { slug : orgSlug } = useOrganization ( ) ;
92-
93- return useApiQuery < Automation [ ] > ( makeAutomationsQueryKey ( { orgSlug, ...options } ) , {
94- staleTime : 0 ,
95- retry : false ,
96- ...useApiQueryOptions ,
97- } ) ;
98- }
99-
10071export function useAutomationQuery ( automationId : string ) {
10172 const { slug} = useOrganization ( ) ;
10273
@@ -186,11 +157,7 @@ export function useCreateAutomation() {
186157 ) ,
187158 onSuccess : _ => {
188159 queryClient . invalidateQueries ( {
189- queryKey : [
190- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
191- path : { organizationIdOrSlug : org . slug } ,
192- } ) ,
193- ] ,
160+ queryKey : automationsApiOptions ( org ) . queryKey ,
194161 } ) ;
195162 } ,
196163 onError : _ => {
@@ -216,11 +183,7 @@ export function useDeleteAutomationMutation() {
216183 ) ,
217184 onSuccess : ( ) => {
218185 queryClient . invalidateQueries ( {
219- queryKey : [
220- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
221- path : { organizationIdOrSlug : org . slug } ,
222- } ) ,
223- ] ,
186+ queryKey : automationsApiOptions ( org ) . queryKey ,
224187 } ) ;
225188 addSuccessMessage ( t ( 'Alert deleted' ) ) ;
226189 } ,
@@ -258,11 +221,7 @@ export function useDeleteAutomationsMutation() {
258221 } ,
259222 onSuccess : ( ) => {
260223 queryClient . invalidateQueries ( {
261- queryKey : [
262- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
263- path : { organizationIdOrSlug : org . slug } ,
264- } ) ,
265- ] ,
224+ queryKey : automationsApiOptions ( org ) . queryKey ,
266225 } ) ;
267226 addSuccessMessage ( t ( 'Alerts deleted' ) ) ;
268227 } ,
@@ -305,11 +264,7 @@ export function useUpdateAutomation() {
305264 ) ;
306265 // Invalidate list query
307266 queryClient . invalidateQueries ( {
308- queryKey : [
309- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
310- path : { organizationIdOrSlug : org . slug } ,
311- } ) ,
312- ] ,
267+ queryKey : automationsApiOptions ( org ) . queryKey ,
313268 } ) ;
314269 } ,
315270 onError : _ => {
@@ -347,11 +302,7 @@ export function useUpdateAutomationsMutation() {
347302 } ,
348303 onSuccess : ( _ , variables ) => {
349304 queryClient . invalidateQueries ( {
350- queryKey : [
351- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
352- path : { organizationIdOrSlug : org . slug } ,
353- } ) ,
354- ] ,
305+ queryKey : automationsApiOptions ( org ) . queryKey ,
355306 } ) ;
356307 addSuccessMessage ( variables . enabled ? t ( 'Alerts enabled' ) : t ( 'Alerts disabled' ) ) ;
357308 } ,
@@ -388,11 +339,7 @@ export function useSendTestNotification(
388339 ...options ,
389340 onSuccess : ( data , variables , onMutateResult , context ) => {
390341 queryClient . invalidateQueries ( {
391- queryKey : [
392- getApiUrl ( '/organizations/$organizationIdOrSlug/workflows/' , {
393- path : { organizationIdOrSlug : org . slug } ,
394- } ) ,
395- ] ,
342+ queryKey : automationsApiOptions ( org ) . queryKey ,
396343 } ) ;
397344 addSuccessMessage (
398345 tn ( 'Notification fired!' , 'Notifications sent!' , variables . length )
0 commit comments