Files
supabase/apps/studio/hooks/analytics/useSingleLog.tsx
T
Alaister YoungandKevin Grüneberg 70da0f1d1d chore: cleanup packages (#27770)
* chore: cleanup packages

- Avoid circular imports
- Export API-types as types
- pg-format without depending on Node internal Buffer (not browser-compatible)
- Avoid importing from barrel files in ui dir

* chore: avoid barrel file imports in studio (#27771)

* chore: avoid barrel file imports

- Removes some unused imports
- Avoids barrel file import for faster builds + less memory

* add eslint rule

* type fixes

* delete layouts barrel

* delete components/grid barrel file

* delete components/grid/utils barrel file

* delete components/grid/components/common barrel file

* delete components/grid/components/editor barrel file

* delete components/grid/components/formatter barrel file

* delete components/grid/components/grid barrel file

* delete components/grid/components/header/filter barrel file

* remote components/grid/store barrel file

* remove components/interfaces/Auth/Policies barrel file

* delete components/interfaces/Settings/Logs barrel file

* delete components/ui/CodeEditor barrel file

* delete components/ui/Forms barrel file

* delete components/ui/Shimmers barrel file

* delete data/analytics barrel file

* delete hooks barrel file

* cleanup lib/common/fetch barrel file

* final * barral files cleanup

* global react-data-grid styles

* remove console.log

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>

* fix build

---------

Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
2024-07-04 14:48:10 +08:00

63 lines
1.8 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 {
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 isWarehouseQuery = queryType === 'warehouse'
// Warehouse queries are handled differently
const enabled = Boolean(id && table && !isWarehouseQuery)
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