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 & { content: Partial } 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> export const useContentUpsertMutation = ({ onError, onSuccess, invalidateQueriesOnSuccess = true, ...options }: Omit< UseMutationOptions, 'mutationFn' > & { invalidateQueriesOnSuccess?: boolean } = {}) => { const queryClient = useQueryClient() return useMutation( (args) => upsertContent(args), { async onSuccess(data, variables, context) { const { projectRef } = variables if (invalidateQueriesOnSuccess) { await Promise.all([ queryClient.invalidateQueries(contentKeys.allContentLists(projectRef)), queryClient.invalidateQueries(contentKeys.infiniteList(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, } ) }