Skip to content

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

move useSingleLog to react-query

Alaister Young6c92c1c52e06
diff --git a/studio/hooks/analytics/useSingleLog.tsx b/studio/hooks/analytics/useSingleLog.tsx
index 1d468d18c6c..d1f8757c134 100644
--- a/studio/hooks/analytics/useSingleLog.tsx
+++ b/studio/hooks/analytics/useSingleLog.tsx
@@ -7,9 +7,9 @@ import {
LOGS_TABLES,
QueryType,
} from 'components/interfaces/Settings/Logs'
-import useSWR from 'swr'
import { API_URL } from 'lib/constants'
import { get } from 'lib/common/fetch'
+import { useQuery } from '@tanstack/react-query'
interface Data {
logData: LogData | undefined
@@ -20,38 +20,43 @@ interface Handlers {
refresh: () => void
}
function useSingleLog(
- project: string,
+ projectRef: string,
queryType?: QueryType,
paramsToMerge?: Partial<LogsEndpointParams>,
id?: string | null
): [Data, Handlers] {
const table = queryType ? LOGS_TABLES[queryType] : undefined
const sql = id && table ? genSingleLogQuery(table, id) : ''
- const params: LogsEndpointParams = { ...paramsToMerge, project, sql }
- const endpointUrl = `${API_URL}/projects/${project}/analytics/endpoints/logs.all?${genQueryParams(
+ const params: LogsEndpointParams = { ...paramsToMerge, project: projectRef, sql }
+ const endpointUrl = `${API_URL}/projects/${projectRef}/analytics/endpoints/logs.all?${genQueryParams(
params as any
)}`
const {
data,
- error: swrError,
- isValidating,
- mutate,
- } = useSWR<Logs>(id && table ? endpointUrl : null, get, {
- revalidateOnFocus: false,
- revalidateIfStale: false,
- revalidateOnReconnect: false,
- dedupingInterval: 5000,
- })
+ error: rcError,
+ isLoading,
+ isRefetching,
+ refetch,
+ } = useQuery(
+ ['projects', projectRef, 'log', id],
+ ({ signal }) => get(endpointUrl, { signal }) as Promise<Logs>,
+ {
+ enabled: Boolean(id && table),
+ refetchOnWindowFocus: false,
+ refetchOnMount: false,
+ refetchOnReconnect: false,
+ }
+ )
- let error: null | string | object = swrError ? swrError.message : null
+ let error: null | string | object = rcError ? (rcError as any).message : null
return [
{
logData: data?.result ? data.result[0] : undefined,
- isLoading: isValidating,
+ isLoading: isLoading || isRefetching,
error,
},
{
- refresh: () => mutate(),
+ refresh: () => refetch(),
},
]
}