Files
supabase/apps/studio/data/content/content-query.ts
Joshen Lim 0129c94cf1 ReportsV2 P2: Support adding SQL snippets to Reports (#32851)
* midway

* Render, save, and save configuration of SQL blocks

* Fix re ordering of blocks not showing save UI

* Remove console log

* Smol fix

* Add queryHeight param to queryBlock

* Reinstate telemetry for run query clicked from assistant

* Add

* Add resizing

* Change compactType

* Fix switching reports doesnt update

* ONly show date picker if theres a chart thats using daily stats or infra monitoring

* Fix client crash

* Smol update
2025-01-20 15:49:32 +08:00

69 lines
1.8 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { get } from 'data/fetchers'
import type { Dashboards, LogSqlSnippets, SqlSnippets } from 'types'
import { contentKeys } from './keys'
export type ContentBase = components['schemas']['GetUserContentObject']
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
}
export async function getContent(
{ projectRef, type, name }: GetContentVariables,
signal?: AbortSignal
) {
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 } },
signal,
})
if (error) {
throw error
}
return {
cursor: data.cursor,
content: data.data as unknown as Content[],
}
}
export type ContentData = Awaited<ReturnType<typeof getContent>>
export type ContentError = unknown
export const useContentQuery = <TData = ContentData>(
{ projectRef, type, name }: GetContentVariables,
{ enabled = true, ...options }: UseQueryOptions<ContentData, ContentError, TData> = {}
) =>
useQuery<ContentData, ContentError, TData>(
contentKeys.list(projectRef, { type, name }),
({ signal }) => getContent({ projectRef, type, name }, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)