-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathuse-update-ai-copilot-config.tsx
More file actions
95 lines (88 loc) · 3.08 KB
/
Copy pathuse-update-ai-copilot-config.tsx
File metadata and controls
95 lines (88 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { useMutation, useQueryClient } from '@tanstack/react-query'
import posthog from 'posthog-js'
import { type AICopilotConfigResponse, devopsCopilot, mutations } from '@qovery/shared/devops-copilot/data-access'
import { toast } from '@qovery/shared/ui'
export interface UseUpdateAICopilotConfigProps {
organizationId: string
instructions?: string
}
export function useUpdateAICopilotConfig({ organizationId, instructions }: UseUpdateAICopilotConfigProps) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({
enabled,
readOnly,
userEmail,
tokenMode,
}: {
enabled: boolean
readOnly: boolean
userEmail?: string
tokenMode?: 'jwt' | 'role'
}) =>
mutations.updateOrgConfig({
organizationId,
enabled,
readOnly,
instructions: instructions || '',
userEmail,
tokenMode,
}),
onMutate: async ({ readOnly, tokenMode }) => {
await queryClient.cancelQueries({
queryKey: devopsCopilot.config({ organizationId }).queryKey,
})
const previousConfig = queryClient.getQueryData(devopsCopilot.config({ organizationId }).queryKey)
queryClient.setQueryData(
devopsCopilot.config({ organizationId }).queryKey,
(old: AICopilotConfigResponse | undefined) =>
old
? {
...old,
org_config: {
...old.org_config,
read_only: readOnly,
...(tokenMode !== undefined && { token_mode: tokenMode }),
},
}
: old
)
return { previousConfig }
},
onSuccess: (_data, variables) => {
if (variables.tokenMode !== undefined) {
toast('success', 'Access source updated successfully')
posthog.capture('ai-copilot-token-mode-changed', {
organization_id: organizationId,
token_mode: variables.tokenMode,
})
} else if (variables.enabled) {
const modeName = variables.readOnly ? 'Read-Only' : 'Read-Write'
toast('success', `AI Copilot enabled with ${modeName} mode`)
posthog.capture('ai-copilot-enabled', {
organization_id: organizationId,
mode: modeName,
})
} else {
toast('success', 'AI Copilot disabled successfully')
posthog.capture('ai-copilot-disabled', {
organization_id: organizationId,
})
}
},
onError: (err, _variables, context) => {
if (context?.previousConfig) {
queryClient.setQueryData(devopsCopilot.config({ organizationId }).queryKey, context.previousConfig)
}
const axiosErr = err as { response?: { data?: { error?: string } } }
const message = axiosErr?.response?.data?.error ?? (err instanceof Error ? err.message : 'Please try again later')
toast('error', 'Failed to update AI Copilot configuration', message)
},
onSettled: () => {
queryClient.invalidateQueries({
queryKey: devopsCopilot.config({ organizationId }).queryKey,
})
},
})
}
export default useUpdateAICopilotConfig