import Link from 'next/link' import { UseFormReturn } from 'react-hook-form' import { useParams } from 'common/hooks' import { useEdgeFunctionsQuery } from 'data/edge-functions/edge-functions-query' import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject' import { useEffect, useMemo } from 'react' import { Button, FormControl_Shadcn_, FormField_Shadcn_, FormItem_Shadcn_, FormLabel_Shadcn_, FormMessage_Shadcn_, Input, Select_Shadcn_, SelectContent_Shadcn_, SelectItem_Shadcn_, SelectTrigger_Shadcn_, SelectValue_Shadcn_, SheetSection, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { CreateCronJobForm } from './CreateCronJobSheet/CreateCronJobSheet.constants' interface HTTPRequestFieldsProps { form: UseFormReturn } const buildFunctionUrl = (slug: string, projectRef: string, restUrl?: string) => { const restUrlTld = restUrl ? new URL(restUrl).hostname.split('.').pop() : 'co' const functionUrl = `https://${projectRef}.supabase.${restUrlTld}/functions/v1/${slug}` return functionUrl } export const EdgeFunctionSection = ({ form }: HTTPRequestFieldsProps) => { const { ref } = useParams() const { data: selectedProject } = useSelectedProjectQuery() const { data: functions, isSuccess, isPending: isLoading, } = useEdgeFunctionsQuery({ projectRef: ref }) const edgeFunctions = useMemo(() => functions ?? [], [functions]) // Only set a default value if the field is empty useEffect(() => { if (isSuccess && edgeFunctions.length > 0 && !form.getValues('values.edgeFunctionName')) { const fn = edgeFunctions[0] const functionUrl = buildFunctionUrl( fn.slug, selectedProject?.ref || '', selectedProject?.restUrl ) form.setValue('values.edgeFunctionName', functionUrl) } }, [edgeFunctions, form, isSuccess, selectedProject?.ref, selectedProject?.restUrl]) return ( ( Method GET POST )} /> {edgeFunctions.length === 0 ? (

Select which edge function to trigger

No edge functions created yet

) : edgeFunctions.length > 0 ? ( { return ( Edge Function {edgeFunctions.map((fn) => { const functionUrl = buildFunctionUrl( fn.slug, selectedProject?.ref || '', selectedProject?.restUrl ) return ( {fn.name} ) })} ) }} /> ) : null} ( ms

} />
)} />
) }