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 & { type: 'notebook' visibility: 'project' content: Notebooks.Content } } export type NotebookData = Awaited> export const useNotebookQuery = ( { projectRef, id }: NotebookVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: contentKeys.resource(projectRef, id), queryFn: ({ signal }) => getNotebook({ projectRef, id }, signal), enabled: enabled && typeof projectRef !== 'undefined' && typeof id !== 'undefined', ...options, })