Files
supabase/apps/studio/hooks/analytics/useSingleLog.tsx
Ivan Vasilov 436bdb10ae chore: Move the studio app to apps/studio (#18915)
* Move all studio files from /studio to /apps/studio.

* Move studio specific prettier ignores.

* Fix the ui references from studio.

* Fix the css imports.

* Fix all package.json issues.

* Fix the prettier setup for the studio app.

* Add .turbo folder to prettierignore.

* Fix the github workflows.
2023-11-15 12:38:55 +01:00

61 lines
1.5 KiB
TypeScript

import {
genQueryParams,
genSingleLogQuery,
LogData,
Logs,
LogsEndpointParams,
LOGS_TABLES,
QueryType,
} from 'components/interfaces/Settings/Logs'
import { API_URL } from 'lib/constants'
import { get } from 'lib/common/fetch'
import { useQuery } from '@tanstack/react-query'
interface SingleLogHook {
logData: LogData | undefined
error: string | Object | null
isLoading: boolean
refresh: () => void
}
function useSingleLog(
projectRef: string,
queryType?: QueryType,
paramsToMerge?: Partial<LogsEndpointParams>,
id?: string | null
): SingleLogHook {
const table = queryType ? LOGS_TABLES[queryType] : undefined
const sql = id && table ? genSingleLogQuery(table, id) : ''
const params: LogsEndpointParams = { ...paramsToMerge, project: projectRef, sql }
const endpointUrl = `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${genQueryParams(
params as any
)}`
const enabled = Boolean(id && table)
const {
data,
error: rcError,
isLoading,
isRefetching,
refetch,
} = useQuery(
['projects', projectRef, 'log', id],
({ signal }) => get(endpointUrl, { signal }) as Promise<Logs>,
{
enabled,
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false,
}
)
let error: null | string | object = rcError ? (rcError as any).message : null
return {
logData: data?.result ? data.result[0] : undefined,
isLoading: (enabled && isLoading) || isRefetching,
error,
refresh: () => refetch(),
}
}
export default useSingleLog