import type { PGTrigger } from '@supabase/pg-meta' import { useQuery } from '@tanstack/react-query' import { useParams } from 'common' import { useEffect } from 'react' import { UseFormReturn } from 'react-hook-form' import { Checkbox, FormControl, FormField, Input, Label, RadioGroupStacked, RadioGroupStackedItem, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, SidePanel, useWatch, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { WebhookFormValues } from './EditHookPanel.constants' import { AVAILABLE_WEBHOOK_TYPES, HOOK_EVENTS } from './Hooks.constants' import { HTTPHeaders } from './HTTPHeaders' import { HTTPParameters } from './HTTPParameters' import { HTTPRequestConfig } from './HTTPRequestConfig' import { ensureEdgeFunctionAuthorizationHeader } from '@/components/interfaces/Functions/httpHeaderAddActions' import { FormSection, FormSectionContent, FormSectionLabel, } from '@/components/ui/Forms/FormSection' import { getKeys, revealedApiKeysQueryOptions } from '@/data/api-keys/api-keys-query' import { useEdgeFunctionsQuery } from '@/data/edge-functions/edge-functions-query' import { useTableNamesQuery } from '@/data/tables/table-names-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { buildDatabaseEdgeFunctionUrl, isEdgeFunctionUrl } from '@/lib/api/edgeFunctions' import { useGrant } from '@/lib/grants' import { uuidv4 } from '@/lib/helpers' export interface FormContentsProps { form: UseFormReturn selectedHook?: PGTrigger } export const FormContents = ({ form, selectedHook }: FormContentsProps) => { const { ref } = useParams() const { data: project } = useSelectedProjectQuery() const restUrl = project?.restUrl // Denied access is surfaced next to the headers (`HTTPHeaders`), so the form stays usable here const apiKeysGrant = useGrant( 'GET /v1/projects/{ref}/api-keys', { projectRef: ref }, { variant: 'reveal' } ) const { data: keys = [] } = useQuery( revealedApiKeysQueryOptions({ projectRef: ref, grant: apiKeysGrant.grant }) ) const { data: functions = [], isSuccess: isSuccessEdgeFunctions } = useEdgeFunctionsQuery({ projectRef: ref, }) const { serviceKey } = getKeys(keys) const serviceRoleKey = serviceKey?.api_key const httpUrl = useWatch({ control: form.control, name: 'http_url' }) const httpHeaders = useWatch({ control: form.control, name: 'httpHeaders' }) const { data: tables = [] } = useTableNamesQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) // Handle auth header auto-add for edge functions useEffect(() => { if (!isSuccessEdgeFunctions) return const isEdgeFunctionSelected = isEdgeFunctionUrl(httpUrl, ref ?? '', restUrl) if (httpUrl && isEdgeFunctionSelected) { const fnSlug = httpUrl.split('/').at(-1) const fn = functions.find((x) => x.slug === fnSlug) const updatedHttpHeaders = ensureEdgeFunctionAuthorizationHeader({ headers: httpHeaders, serviceRoleKey, verifyJwt: fn?.verify_jwt, createRow: (name, value) => ({ id: uuidv4(), name, value }), }) if (updatedHttpHeaders !== httpHeaders) { form.setValue('httpHeaders', updatedHttpHeaders) } } }, [form, functions, httpHeaders, httpUrl, isSuccessEdgeFunctions, serviceRoleKey, ref, restUrl]) return (
General}> (

Do not use spaces/whitespaces

)} />
Select which table and events will trigger your webhook

} > Conditions to fire webhook } > ( )} /> (
{HOOK_EVENTS.map((event) => (
{ if (checked) { field.onChange([...field.value, event.value]) } else { field.onChange(field.value.filter((v) => v !== event.value)) } }} />

{event.description}

))}
)} />
Webhook configuration } > ( { if (functionType === 'http_request') { if (selectedHook !== undefined) { const [url] = selectedHook.function_args form.setValue('http_url', url, { shouldDirty: false }) } else { form.setValue('http_url', '', { shouldDirty: false }) } } else if (functionType === 'supabase_function') { // Default to first edge function in the list const fnSlug = functions[0]?.slug const defaultFunctionUrl = buildDatabaseEdgeFunctionUrl( fnSlug ?? '', ref ?? '', restUrl ) const currentUrl = form.getValues('http_url') if (!isEdgeFunctionUrl(currentUrl, ref ?? '', restUrl)) { form.setValue('http_url', defaultFunctionUrl, { shouldDirty: false }) } } field.onChange(functionType) }} > {AVAILABLE_WEBHOOK_TYPES.map((webhook) => (

{webhook.label}

{webhook.description}

))}
)} />
) }