Files
supabase/apps/studio/data/content/content-query.ts
T
CharisandJoshen Lim 0b97e37ccf feat: notebook content schema (#48813)
Related to FE-4109.

## Summary

- **API codegen workaround**: Platform API's `notebook` content type
hasn't shipped to the OpenAPI spec yet, so `pnpm api:codegen` can't be
run. Locally widened `ContentBase.type` to include `'notebook'` (marked
with TODO for removal once spec publishes).
- **Notebook schema & type system**: Introduced Zod schemas mirroring
RFC-defined notebook shape (`schema_version: 1, cells: Cell[]`).
Maintains wire/domain boundary (cell `sql` → `unchecked_sql` branded for
security). Agent-writable schema for `create_notebook` tool omits cell
IDs (backend-generated); future update operations will require them. All
TypeScript types are `z.infer`'d from schemas (no hand-written parallel
interfaces).
- **IsoDateTimeString moved**: Extracted ISO datetime validator from
`querySource.ts` to `lib/iso-datetime.ts` (data layer shouldn't import
from components layer). Needed by notebook `time_range` fields.

## Test plan

- [x] Unit tests: `notebook-schema.test.ts` (9 tests),
`iso-datetime.test.ts` (3 tests), `querySource.test.ts` updated and
passing (26 tests)
- [x] Typecheck: no new errors
- [x] Prettier: formatting clean

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Added support for validating and processing notebook content,
including markdown, database, log cells, time ranges, and chart
configurations.
  - Added compatibility for notebook content types in content handling.
- Added reliable ISO date-time validation for notebook data and related
features.

- **Tests**
- Expanded coverage for valid and invalid notebook structures, cell
requirements, time ranges, chart settings, and date-time values.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-08-07 13:40:18 +07:00

78 lines
2.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { components } from 'api-types'
import { remapSqlContentFields } from './content-remap'
import { contentKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { Dashboards, LogSqlSnippets, SqlSnippets, UseCustomQueryOptions } from '@/types'
// TODO — Charis 2026-08-06
// Temporary widening until we have API support for notebooks
export type ContentBase = Omit<
components['schemas']['GetUserContentResponse']['data'][number],
'type'
> & { type: components['schemas']['GetUserContentResponse']['data'][number]['type'] | 'notebook' }
export type Content = Omit<ContentBase, 'content' | 'type'> &
(
| {
type: 'sql'
content: SqlSnippets.Content
}
| {
type: 'report'
content: Dashboards.Content
}
| {
type: 'log_sql'
content: LogSqlSnippets.Content
}
)
export type ContentType = Content['type']
interface GetContentVariables {
projectRef?: string
type: ContentType
name?: string
limit?: number
}
export async function getContent(
{ projectRef, type, name, limit = 10 }: GetContentVariables,
signal?: AbortSignal,
headers?: HeadersInit
) {
if (typeof projectRef === 'undefined') {
throw new Error('projectRef is required for getContent')
}
const { data, error } = await get('/platform/projects/{ref}/content', {
params: { path: { ref: projectRef }, query: { type, name, limit: limit.toString() } },
headers,
signal,
})
if (error) handleError(error)
return {
cursor: data.cursor,
content: remapSqlContentFields(data.data as unknown as Content[]),
}
}
export type ContentData = Awaited<ReturnType<typeof getContent>>
export type ContentError = unknown
/** @deprecated Use useContentInfiniteQuery from content-infinite-query instead */
export const useContentQuery = <TData = ContentData>(
{ projectRef, type, name, limit }: GetContentVariables,
{ enabled = true, ...options }: UseCustomQueryOptions<ContentData, ContentError, TData> = {}
) =>
useQuery<ContentData, ContentError, TData>({
queryKey: contentKeys.list(projectRef, { type, name, limit }),
queryFn: ({ signal }) => getContent({ projectRef, type, name, limit }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})