import { useParams } from 'common/hooks' import { Check, ChevronsUpDown } from 'lucide-react' import Link from 'next/link' import { useEffect, useId, useMemo, useState } from 'react' import { UseFormReturn } from 'react-hook-form' import { Button, cn, Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, FormControl, FormField, FormItem, FormLabel, FormMessage, InputGroup, InputGroupAddon, InputGroupInput, InputGroupText, Popover, PopoverContent, PopoverTrigger, ScrollArea, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, SheetSection, } from 'ui' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { CreateCronJobForm } from './CreateCronJobSheet/CreateCronJobSheet.constants' import { useEdgeFunctionsQuery } from '@/data/edge-functions/edge-functions-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' 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 [open, setOpen] = useState(false) const listboxId = useId() const edgeFunctions = useMemo( () => functions?.map((fn) => ({ ...fn, url: buildFunctionUrl(fn.slug, selectedProject?.ref || '', selectedProject?.restUrl), })) ?? [], [functions, selectedProject] ) // Only set a default value if the field is empty useEffect(() => { if (isSuccess && edgeFunctions.length > 0 && !form.getValues('values.edgeFunctionName')) { const fn = edgeFunctions[0] form.setValue('values.edgeFunctionName', fn.url) } }, [edgeFunctions, form, isSuccess, selectedProject?.ref, selectedProject?.restUrl]) return ( ( Method )} /> {edgeFunctions.length === 0 ? (

Select which edge function to trigger

{isLoading ? ( ) : (

No edge functions created yet

)}
) : edgeFunctions.length > 0 ? ( { const selectedFunction = edgeFunctions.find((fn) => fn.url === field.value) return ( Edge Function No edge function found. 7 ? 'h-[210px]' : ''}> {edgeFunctions.map((fn) => { return ( { field.onChange(fn.url === field.value ? '' : fn.url) setOpen(false) }} > {fn.name} ) })} ) }} /> ) : null} ( ms )} />
) }