Files
supabase/apps/studio/data/content/content-insert-mutation.ts
Alaister Young f6d9617413 chore: paginate user content (#30675)
* chore: paginate user content

* progress

* loading states

* add load more buttons to private snippets

* working pagination

* fix some types

* always show snippet counts

* support new api parameters

* favorite snippets

* progress

* searching

* paginate root folder

* fix renaming snippets

* fix ts

* removed unused prop

* Shift sharing/unsharing query logic outside of valtio to leverage on RQ only

* Fix invalidation on an unsaved snippet

* Clean up

* Fix

* Clean up

* Update API type

* Update API

* fix duplicate snippets error after moving a snippet

* add currently selected snippet

* Fix unsharing a snippet that has yet to be opened

* i'm dumb

* fix sharing a snippet

* fix sharing and unsharing

* show favorite or shared snippet in list even if it's in another page

* Fix wrong import for debounce

* Fix false positive toast error when creating custom report

* Update API type

* Change create new snippet CTA to link back to /new with skip flag

* Fix saving logs explorer query

* Bump page number

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2024-12-19 16:08:14 +08:00

77 lines
2.1 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import type { components } from 'data/api'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import type { Content } from './content-query'
import { contentKeys } from './keys'
export type InsertContentPayload = Omit<
components['schemas']['CreateContentBodyDto'],
'content'
> & {
content: Content['content']
}
export type InsertContentVariables = {
projectRef: string
payload: InsertContentPayload
}
export type InsertContentResponse = components['schemas']['UserContentObject']
export async function insertContent(
{ projectRef, payload }: InsertContentVariables,
signal?: AbortSignal
) {
const { data, error } = await post('/platform/projects/{ref}/content', {
params: { path: { ref: projectRef } },
body: {
id: payload.id,
name: payload.name,
description: payload.description,
owner_id: payload.owner_id,
type: payload.type,
visibility: payload.visibility,
content: payload.content as any,
folder_id: payload.folder_id,
},
signal,
})
if (error) handleError(error)
return data
}
export type InsertContentData = Awaited<ReturnType<typeof insertContent>>
export const useContentInsertMutation = ({
onError,
onSuccess,
...options
}: Omit<
UseMutationOptions<InsertContentData, ResponseError, InsertContentVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<InsertContentData, ResponseError, InsertContentVariables>(
(args) => insertContent(args),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
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,
}
)
}