Files
supabase/apps/studio/data/content/content-upsert-v2-mutation.ts
Joshen Lim 17e0c5dbcf Improve snippet organization in SQL Editor (#27881)
* Init changes for sql fodlers

* Added upsert logic in sql-editor-v2 valtio, hooked up with templates and quickstarts

* Do up logic for creating new snippets by typing in /new or by clicking new query button

* Do up logic for updating and deleting snippets

* Do up logic for favourites and shared snippets

* Do up logic for favourites and shared snippets

* Fix

* Fix saving indicator, add empty states for favorites and shared snippets

* Implement sorting

* Some minor QOL improvements

* Minor fix on empty state for private snippets

* Add delete folder mutation

* Implement create and update folder

* Fix reinstate with AI renaming for new snippets under folder

* Support controlled multi select behaviour in private snippets

* Undo changes to tree-view-multi-select

* Support bulk deletes

* Support moving queries + rendering queries in folders

* Support deleting folders and creating a new folder when moving a query

* Fix bug where renaming query removes content

* Add initial loading state in sql editor nav + handle fallback if cannot retrieve content by id

* Fix some spelling

* Fix TS issue in sql folders mutation keys

* Fix toggling favorite

* Lint

* Revert fallback behaviour in ]id] for now

* Fix favorites and shared snippets not showing

* Fix moving currently opened snippet leads to loading

* Support bulk moving

* Improve multi select logic a little

* Nit lint

* Reinstate AI retitling for untitled snippets when running query

* Remove hardcode in useAFlag

* Support creating new snippet in a folder directly

* Fix sharing snippets that are within a folder

* Fix sharing snippets within a folder

* Fix favorite

* Add loading state when fetching folder contents

* Fix favoriting snippets in folders
2024-07-17 12:23:19 +08:00

74 lines
2.1 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import toast from 'react-hot-toast'
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 UpsertContentPayloadV2 = Omit<
components['schemas']['UpsertContentBodyV2'],
'content'
> & {
content: Content['content']
}
export type UpsertContentVariables = {
projectRef: string
payload: UpsertContentPayloadV2
}
export async function upsertContent(
{ projectRef, payload }: UpsertContentVariables,
signal?: AbortSignal
) {
const { data, error } = await put('/platform/projects/{ref}/content', {
// @ts-ignore API codegen is wrong, any is also cause of API codegen being unable to handle versioning
params: { path: { ref: projectRef } },
body: payload as any,
headers: { Version: '2' },
signal,
})
if (error) handleError(error)
return data
}
export type UpsertContentData = Awaited<ReturnType<typeof upsertContent>>
export const useContentUpsertV2Mutation = ({
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.list(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,
}
)
}