Skip to content

Commit 13878e4

Browse files
committed
fix(hooks): unify hooks files
1 parent 4ef5c03 commit 13878e4

File tree

4 files changed

+63
-94
lines changed

4 files changed

+63
-94
lines changed

static/app/views/automations/hooks/index.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

static/app/views/automations/hooks/index.tsx

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type {Automation} from 'sentry/types/workflowEngine/automations';
2-
import {useApiQuery} from 'sentry/utils/queryClient';
1+
import type {Automation, NewAutomation} from 'sentry/types/workflowEngine/automations';
2+
import {useApiQuery, useMutation, useQueryClient} from 'sentry/utils/queryClient';
3+
import useApi from 'sentry/utils/useApi';
34
import useOrganization from 'sentry/utils/useOrganization';
45

56
interface UseAutomationsQueryOptions {
@@ -14,3 +15,56 @@ export function useAutomationsQuery(_options: UseAutomationsQueryOptions = {}) {
1415
retry: false,
1516
});
1617
}
18+
19+
export const makeAutomationQueryKey = (
20+
orgSlug: string,
21+
automationId = ''
22+
): [url: string] => [`/organizations/${orgSlug}/workflows/${automationId}/`];
23+
24+
export function useCreateAutomation(automation: NewAutomation) {
25+
const org = useOrganization();
26+
27+
return useApiQuery<Automation[]>(
28+
[...makeAutomationQueryKey(org.slug), {method: 'POST', data: automation}],
29+
{
30+
staleTime: 0,
31+
retry: false,
32+
}
33+
);
34+
}
35+
36+
export function useAutomationQuery(automationId: string) {
37+
const org = useOrganization();
38+
39+
return useApiQuery<Automation>([...makeAutomationQueryKey(org.slug, automationId)], {
40+
staleTime: 0,
41+
retry: false,
42+
});
43+
}
44+
45+
export function useAutomationMutation(automation: Partial<Automation> & {id: string}) {
46+
const api = useApi({persistInFlight: true});
47+
const queryClient = useQueryClient();
48+
const org = useOrganization();
49+
const queryKey = makeAutomationQueryKey(org.slug, automation.id);
50+
return useMutation<Automation>({
51+
mutationFn: data =>
52+
api.requestPromise(queryKey[0], {
53+
method: 'PUT',
54+
data,
55+
}),
56+
onSuccess: _ => {
57+
queryClient.invalidateQueries({queryKey});
58+
// setApiQueryData<Project>(
59+
// queryClient,
60+
// makeDetailedProjectQueryKey({
61+
// orgSlug: organization.slug,
62+
// projectSlug: project.slug,
63+
// }),
64+
// existingData => (updatedProject ? updatedProject : existingData)
65+
// );
66+
// return onSuccess?.(updatedProject);
67+
},
68+
onError: _ => {},
69+
});
70+
}

static/app/views/automations/hooks/utils.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

static/app/views/automations/hooks/utils.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {useState} from 'react';
22

33
import type {ActionType} from 'sentry/types/workflowEngine/actions';
44
import type {Automation} from 'sentry/types/workflowEngine/automations';
5+
import {useDetectorQueriesByIds} from 'sentry/views/detectors/hooks';
56

67
export function useAutomationActions(automation: Automation): ActionType[] {
78
return [
@@ -47,3 +48,9 @@ export function useConnectedIds({storageKey, initialIds = []}: UseConnectedIdsPr
4748
}
4849

4950
export const NEW_AUTOMATION_CONNECTED_IDS_KEY = 'new-automation-connected-ids';
51+
export function useAutomationProjectIds(automation: Automation): string[] {
52+
const queries = useDetectorQueriesByIds(automation.detectorIds);
53+
return [
54+
...new Set(queries.map(query => query.data?.projectId).filter(x => x)),
55+
] as string[];
56+
}

0 commit comments

Comments
 (0)