mirror of
https://github.com/supabase/supabase.git
synced 2026-09-06 09:59:03 +08:00
## 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
41 lines
1.4 KiB
TypeScript
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,
|
|
})
|