Files
supabase/apps/studio/hooks/analytics/useSingleLog.tsx
T
Jordi EnricandIvan Vasilov b6647d445c store selected logs to url (#29802)
* add selected logs to url

* Add mocks for nuqs package.

* fix layout issues n remove magic numbers for height

* fix chart height issue

* fix bug where logs not in first page wouldnt load correctly

* fix spacing between charts and header

* fix postgtres_logs issues, improve size in log detail

* fix borders

* fix default renderer

* fix detail width

* rm maxheight references

* fix tests

* fix typeerrs

* fix stories typerr

* improve colelctions empty state

* move state UP for logs and warehouse tables

* cleanup

* rm params

* rm params

* rm params

* fix log explorer selection

* fix tests types

* cleanup divs

* fix selected log delay

* use isEq for comparison in rows

* fix selection stuck open

* fix btnlink combo

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2024-10-25 12:40:14 +02:00

72 lines
1.9 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { LOGS_TABLES, genQueryParams } from 'components/interfaces/Settings/Logs/Logs.constants'
import type {
LogData,
Logs,
LogsEndpointParams,
QueryType,
} from 'components/interfaces/Settings/Logs/Logs.types'
import { genSingleLogQuery } from 'components/interfaces/Settings/Logs/Logs.utils'
import { get } from 'lib/common/fetch'
import { API_URL } from 'lib/constants'
interface SingleLogHook {
data: LogData | undefined
error: string | Object | null
isLoading: boolean
refresh: () => void
}
type SingleLogParams = {
id?: string
projectRef: string
queryType?: QueryType
paramsToMerge?: Partial<LogsEndpointParams>
}
function useSingleLog({
projectRef,
id,
queryType,
paramsToMerge,
}: SingleLogParams): 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 isWarehouseQuery = queryType === 'warehouse'
// Warehouse queries are handled differently
const enabled = Boolean(id && table && !isWarehouseQuery)
const {
data,
error: rcError,
isLoading,
isRefetching,
refetch,
} = useQuery(
['projects', projectRef, 'single-log', id, queryType],
({ 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
const result = data?.result ? data.result[0] : undefined
return {
data: result,
isLoading: (enabled && isLoading) || isRefetching,
error,
refresh: () => refetch(),
}
}
export default useSingleLog