mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 02:45:07 +03:00
## Context This is pre-requisite work for adding support to managing custom reports from the Assistant. Planning to break this into a number of PRs, briefly - Adding read support for custom reports - Adding write support for custom reports - Adding run support for custom reports - Should be able to infer data from the results then This PR starts with adding support for listing and reading custom reports from the Assistant ## Other changes involved - Updates setting up of the home page report to have better title and description - Swaps the variant of the ToggleGroup in the SQL block for custom reports as the default variant blends into the background color of the PopoverContent ## To test - [ ] Assistant should be able to list custom reports + read its contents <img width="428" height="755" alt="image" src="https://github.com/user-attachments/assets/6a15b660-c0ee-4a06-984c-87eff3943eec" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added AI-assisted tools to list reports and retrieve report details, including chart counts, layouts, configurations, and SQL-backed chart information. - Added clearer empty-state messaging when no snippets are available. - **Improvements** - New homepage reports now use the name “Homepage Report” and include a descriptive project-home summary. - Updated query controls with refreshed visual styling. - Improved content requests to support additional request context. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
73 lines
2.1 KiB
TypeScript
73 lines
2.1 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'
|
|
|
|
export type ContentBase = components['schemas']['GetUserContentResponse']['data'][number]
|
|
|
|
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,
|
|
})
|