Files
supabase/apps/studio/data/content/content-upsert-mutation.ts
Ivan Vasilov 4add928dae chore: Update API types (#35879)
* Regenerate all types from develop branch.

* Fix all types in studio.

* Remove extra type.
2025-05-26 15:43:14 +02:00

70 lines
1.9 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import type { components } from 'data/api'
import { handleError, put } from 'data/fetchers'
import type { ResponseError } from 'types'
import type { Content } from './content-query'
import { contentKeys } from './keys'
export type UpsertContentPayload = Omit<components['schemas']['UpsertContentBody'], 'content'> & {
content: Partial<Content['content']>
}
export type UpsertContentVariables = {
projectRef: string
payload: UpsertContentPayload
}
export async function upsertContent(
{ projectRef, payload }: UpsertContentVariables,
signal?: AbortSignal
) {
const { data, error } = await put('/platform/projects/{ref}/content', {
params: { path: { ref: projectRef } },
body: payload,
headers: { Version: '2' },
signal,
})
if (error) handleError(error)
return data
}
export type UpsertContentData = Awaited<ReturnType<typeof upsertContent>>
export const useContentUpsertMutation = ({
onError,
onSuccess,
invalidateQueriesOnSuccess = true,
...options
}: Omit<
UseMutationOptions<UpsertContentData, ResponseError, UpsertContentVariables>,
'mutationFn'
> & {
invalidateQueriesOnSuccess?: boolean
} = {}) => {
const queryClient = useQueryClient()
return useMutation<UpsertContentData, ResponseError, UpsertContentVariables>(
(args) => upsertContent(args),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
if (invalidateQueriesOnSuccess) {
await queryClient.invalidateQueries(contentKeys.allContentLists(projectRef))
}
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to insert content: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}