Files
supabase/apps/studio/data/content/notebooks/notebook-query.ts
Joshen Lim 2893c783d5 Hook up APIs for Notebooks CRUD (#49254)
## Context

API changes are ready so hooking up the endpoints for full CRUD UX E2E
- Can create notebooks
- Can load notebooks
- Can delete notebooks
- Can update notebooks
2026-08-20 22:23:20 +08:00

41 lines
1.4 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { getContentById } from '../content-id-query'
import { contentKeys } from '../keys'
import type { Notebooks, UseCustomQueryOptions } from '@/types'
import { ResponseError } from '@/types'
export type NotebookVariables = { projectRef?: string; id?: string }
export type NotebookError = ResponseError
export async function getNotebook(
{ projectRef, id }: NotebookVariables,
signal?: AbortSignal,
headers?: HeadersInit
) {
const data = await getContentById({ projectRef, id }, signal, headers)
if (data.type !== 'notebook') {
throw new Error(`Content ${id} is not a notebook (got type: ${data.type})`)
}
return data as Omit<typeof data, 'content' | 'type' | 'visibility'> & {
type: 'notebook'
visibility: 'project'
content: Notebooks.Content
}
}
export type NotebookData = Awaited<ReturnType<typeof getNotebook>>
export const useNotebookQuery = <TData = NotebookData>(
{ projectRef, id }: NotebookVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<NotebookData, NotebookError, TData> = {}
) =>
useQuery<NotebookData, NotebookError, TData>({
queryKey: contentKeys.resource(projectRef, id),
queryFn: ({ signal }) => getNotebook({ projectRef, id }, signal),
enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined',
...options,
})