mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 17:30:25 -04:00
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { components } from 'api-types'
|
|
import { toast } from 'sonner'
|
|
|
|
import { integrationKeys } from './keys'
|
|
import { handleError, patch } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
type GitHubConnectionUpdateVariables = {
|
|
connectionId: string | number
|
|
organizationId: number
|
|
connection: components['schemas']['UpdateGitHubConnectionBody']
|
|
}
|
|
|
|
export async function updateConnection(
|
|
{ connectionId, connection }: GitHubConnectionUpdateVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
const { data, error } = await patch('/platform/integrations/github/connections/{connection_id}', {
|
|
params: { path: { connection_id: String(connectionId) } },
|
|
signal,
|
|
body: connection,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type UpdateContentData = Awaited<ReturnType<typeof updateConnection>>
|
|
|
|
export const useGitHubConnectionUpdateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<UpdateContentData, ResponseError, GitHubConnectionUpdateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
return useMutation<UpdateContentData, ResponseError, GitHubConnectionUpdateVariables>({
|
|
mutationFn: (args) => updateConnection(args),
|
|
async onSuccess(data, variables, context) {
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({
|
|
queryKey: integrationKeys.githubConnectionsList(variables.organizationId),
|
|
}),
|
|
])
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to update GitHub connection: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|