Files
supabase/apps/studio/data/content/content-query.ts
Joshen Lim 1afaae6447 Chore/reports read replicas filter (#23711)
* Scaffold for API reports

* Add support for filtering database reports by replicas

* midway adding read replica support for custom reports

* Refactor reports page to use react query and deprecate project content mobxstore

* Reports add chart labels if metric is read replica specific

* Give an easier way to remove charts from reports

* make reports layout non-blocking

* Update apps/studio/components/interfaces/Reports/Reports.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-05-07 16:44:23 +08:00

75 lines
1.9 KiB
TypeScript

import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
import { useCallback } from 'react'
import type { Dashboards, LogSqlSnippets, Owner, SqlSnippets } from 'types'
import { contentKeys } from './keys'
type ContentBase = {
id?: string
name: string
description?: string
visibility: 'user' | 'project' | 'org' | 'public'
owner_id?: number // user id
last_updated_by?: number // user id
inserted_at?: string // '2021-08-26T08:24:52.040695+00:00'
owner?: Owner
project_id?: number
updated_at?: string // '2021-08-26T08:24:52.040695+00:00'
updated_by?: Owner
}
export type Content = ContentBase &
(
| {
type: 'sql'
content: SqlSnippets.Content
}
| {
type: 'report'
content: Dashboards.Content
}
| {
type: 'log_sql'
content: LogSqlSnippets.Content
}
)
export async function getContent(
projectRef: string | undefined,
signal?: AbortSignal
): Promise<{
content: Content[]
}> {
if (typeof projectRef === 'undefined') {
throw new Error('projectRef is required for getContent')
}
let response = await get(`${API_URL}/projects/${projectRef}/content`, { signal })
if (response.error) {
throw response.error
}
if (!response) {
throw new Error('Content not found')
}
return {
content: response.data,
}
}
export type ContentData = Awaited<ReturnType<typeof getContent>>
export type ContentError = unknown
export const useContentQuery = <TData = ContentData>(
projectRef: string | undefined,
{ enabled = true, ...options }: UseQueryOptions<ContentData, ContentError, TData> = {}
) =>
useQuery<ContentData, ContentError, TData>(
contentKeys.list(projectRef),
({ signal }) => getContent(projectRef, signal),
{ enabled: enabled && typeof projectRef !== 'undefined', ...options }
)