Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.4 KB
Raw Blame
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,
  })