Skip to content

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

feat(studio): add test connection button for project log drains (#46720)

Jordi Enric88440f087b1b
diff --git a/apps/studio/components/interfaces/LogDrains/LogDrains.tsx b/apps/studio/components/interfaces/LogDrains/LogDrains.tsx
index a4bb37db5f2..280ef715b2f 100644
--- a/apps/studio/components/interfaces/LogDrains/LogDrains.tsx
+++ b/apps/studio/components/interfaces/LogDrains/LogDrains.tsx
@@ -7,6 +7,7 @@ import { LogDrainsEmpty } from './LogDrainsEmpty'
import { LogDrainsList } from './LogDrainsList'
import { useDeleteLogDrainMutation } from '@/data/log-drains/delete-log-drain-mutation'
import { LogDrainData, useLogDrainsQuery } from '@/data/log-drains/log-drains-query'
+import { useTestLogDrainMutation } from '@/data/log-drains/test-log-drain-mutation'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
import { useTrack } from '@/lib/telemetry/track'
@@ -35,6 +36,12 @@ export function LogDrains({
},
})
+ const { mutate: testLogDrain } = useTestLogDrainMutation({
+ onSuccess: () => {
+ toast.success('Log drain connection test succeeded')
+ },
+ })
+
if (isLoadingEntitlement) {
return (
<div>
@@ -61,6 +68,11 @@ export function LogDrains({
track('log_drain_removed', { destination: drain.type })
}
}}
+ onTestDrain={(drain) => {
+ if (ref) {
+ testLogDrain({ token: drain.token, projectRef: ref })
+ }
+ }}
/>
)
}
diff --git a/apps/studio/data/log-drains/log-drains.test.tsx b/apps/studio/data/log-drains/log-drains.test.tsx
new file mode 100644
index 00000000000..765d527a0f5
--- /dev/null
+++ b/apps/studio/data/log-drains/log-drains.test.tsx
@@ -0,0 +1,33 @@
+import { waitFor } from '@testing-library/react'
+import { http, HttpResponse } from 'msw'
+import { describe, expect, it } from 'vitest'
+
+import { useTestLogDrainMutation } from './test-log-drain-mutation'
+import { API_URL } from '@/lib/constants'
+import { customRenderHook } from '@/tests/lib/custom-render'
+import { mswServer } from '@/tests/lib/msw'
+
+const REF = 'my-project'
+const BASE = `${API_URL}/platform/projects/:ref/analytics/log-drains`
+
+describe('project log drain hooks', () => {
+ it('tests a log drain connection by token at the project-scoped path', async () => {
+ let receivedRef: string | undefined
+ let receivedToken: string | undefined
+ mswServer.use(
+ http.post(`${BASE}/:token/test`, ({ params }) => {
+ receivedRef = params.ref as string
+ receivedToken = params.token as string
+ return HttpResponse.json({})
+ })
+ )
+
+ const { result } = customRenderHook(() => useTestLogDrainMutation())
+
+ result.current.mutate({ projectRef: REF, token: 'tok-1' })
+
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
+ expect(receivedRef).toBe(REF)
+ expect(receivedToken).toBe('tok-1')
+ })
+})
diff --git a/apps/studio/data/log-drains/test-log-drain-mutation.ts b/apps/studio/data/log-drains/test-log-drain-mutation.ts
new file mode 100644
index 00000000000..3e2f3655778
--- /dev/null
+++ b/apps/studio/data/log-drains/test-log-drain-mutation.ts
@@ -0,0 +1,45 @@
+import { useMutation } from '@tanstack/react-query'
+import { toast } from 'sonner'
+
+import { handleError, post } from '@/data/fetchers'
+import type { ResponseError, UseCustomMutationOptions } from '@/types'
+
+export type LogDrainTestVariables = {
+ projectRef: string
+ token: string
+}
+
+export async function testLogDrain({ projectRef, token }: LogDrainTestVariables) {
+ const { data, error } = await post('/platform/projects/{ref}/analytics/log-drains/{token}/test', {
+ params: { path: { ref: projectRef, token } },
+ })
+
+ if (error) handleError(error)
+ return data
+}
+
+type LogDrainTestData = Awaited<ReturnType<typeof testLogDrain>>
+
+export const useTestLogDrainMutation = ({
+ onSuccess,
+ onError,
+ ...options
+}: Omit<
+ UseCustomMutationOptions<LogDrainTestData, ResponseError, LogDrainTestVariables>,
+ 'mutationFn'
+> = {}) => {
+ return useMutation<LogDrainTestData, ResponseError, LogDrainTestVariables>({
+ mutationFn: (vars) => testLogDrain(vars),
+ async onSuccess(data, variables, context) {
+ await onSuccess?.(data, variables, context)
+ },
+ async onError(data, variables, context) {
+ if (onError === undefined) {
+ toast.error(`Failed to test log drain: ${data.message}`)
+ } else {
+ onError(data, variables, context)
+ }
+ },
+ ...options,
+ })
+}