Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.7 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'
import { components } from 'api-types'

import { lintKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { PROJECT_STATUS } from '@/lib/constants'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

type ProjectLintRulesVariables = {
  projectRef?: string
}
type LintDismissalResponse = components['schemas']['ListNotificationExceptionsResponse_Output']
export type LintException = LintDismissalResponse['exceptions'][0]

export async function getProjectLintRules(
  { projectRef }: ProjectLintRulesVariables,
  signal?: AbortSignal
) {
  if (!projectRef) throw new Error('Project ref is required')

  const { data, error } = await get('/platform/projects/{ref}/notifications/advisor/exceptions', {
    params: { path: { ref: projectRef } },
    signal,
  })

  if (error) handleError(error)

  return data
}

export type ProjectLintRulesData = Awaited<ReturnType<typeof getProjectLintRules>>
export type ProjectLintRulesError = ResponseError

export const useProjectLintRulesQuery = <TData = ProjectLintRulesData>(
  { projectRef }: ProjectLintRulesVariables,
  {
    enabled = true,
    ...options
  }: UseCustomQueryOptions<ProjectLintRulesData, ProjectLintRulesError, TData> = {}
) => {
  const { data: project } = useSelectedProjectQuery()
  const isActive = project?.status === PROJECT_STATUS.ACTIVE_HEALTHY

  return useQuery<ProjectLintRulesData, ProjectLintRulesError, TData>({
    queryKey: lintKeys.lintRules(projectRef),
    queryFn: ({ signal }) => getProjectLintRules({ projectRef }, signal),
    enabled: enabled && typeof projectRef !== 'undefined' && isActive,
    ...options,
  })
}