Skip to content

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

1.5 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'

import { actionKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

type ActionLogsVariables = {
  projectRef?: string
  runId?: string
}

export async function getActionRunLogs(
  { projectRef, runId }: ActionLogsVariables,
  signal?: AbortSignal
) {
  if (!projectRef) throw new Error('projectRef is required')
  if (!runId) throw new Error('runId is required')

  const { data, error } = await get(`/v1/projects/{ref}/actions/{run_id}/logs`, {
    params: { path: { ref: projectRef, run_id: runId } },
    parseAs: 'text',
    headers: { Accept: 'text/plain' },
    signal,
  })
  if (error) handleError(error)

  return data
    .split('\n')
    .flatMap((line) => line.split('\r'))
    .join('\n')
    .trim()
}

export type ActionLogsData = Awaited<ReturnType<typeof getActionRunLogs>>
export type WorkflowRunLogsError = ResponseError

export const useActionRunLogsQuery = <TData = ActionLogsData>(
  { projectRef, runId }: ActionLogsVariables,
  {
    enabled = true,
    ...options
  }: UseCustomQueryOptions<ActionLogsData, WorkflowRunLogsError, TData> = {}
) =>
  useQuery<ActionLogsData, WorkflowRunLogsError, TData>({
    queryKey: actionKeys.logs(projectRef, runId),
    queryFn: ({ signal }) => getActionRunLogs({ projectRef, runId }, signal),
    enabled: enabled && typeof projectRef !== 'undefined' && typeof runId !== 'undefined',
    staleTime: 0,
    ...options,
  })