Files
supabase/apps/studio/components/interfaces/Integrations/CronJobs/EdgeFunctionSection.tsx
Gildas Garcia 9240478816 chore: migrate Input usages to Shadcn component in integrations screens/components (#45591)
## Screenshots

### New cron job edge function timeout
Before:
<img width="1157" height="259" alt="image"
src="https://github.com/user-attachments/assets/b5e056e7-6216-45a6-9cc6-15e56621c62a"
/>


After:
<img width="1162" height="258" alt="image"
src="https://github.com/user-attachments/assets/bfb12a20-8a11-47f1-b7e6-c1ebc2fc187e"
/>

### New cron job http request timeout
Before:
<img width="1161" height="237" alt="image"
src="https://github.com/user-attachments/assets/ad1dc7ef-e9ec-4219-8f84-f20025aa1c68"
/>

After:
<img width="1160" height="231" alt="image"
src="https://github.com/user-attachments/assets/eb4d0df2-db20-4e04-a78d-fa36656a2987"
/>

### New queue, partition configuration
Before:
<img width="786" height="677" alt="image"
src="https://github.com/user-attachments/assets/34b3f1fc-b1e8-434f-bfc7-8a5686bd1c29"
/>

After:
<img width="778" height="668" alt="image"
src="https://github.com/user-attachments/assets/f7423240-b810-47d6-af1d-9d5647c78843"
/>

### Queue: send message dialog
Before:
<img width="522" height="411" alt="image"
src="https://github.com/user-attachments/assets/f9cf5993-c7e4-4bd0-9718-0c9e85e41378"
/>

After:
<img width="532" height="414" alt="image"
src="https://github.com/user-attachments/assets/d965bfcc-c074-44a1-8a8f-ecdd4e766221"
/>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Style**
* Enhanced input field presentation for timeout, delay, and interval
configurations with inline unit labels (milliseconds, seconds, messages)
for improved clarity and consistency across integration settings.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-05 17:56:59 +02:00

210 lines
7.7 KiB
TypeScript

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_Shadcn_,
CommandEmpty_Shadcn_,
CommandGroup_Shadcn_,
CommandInput_Shadcn_,
CommandItem_Shadcn_,
CommandList_Shadcn_,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
InputGroup,
InputGroupAddon,
InputGroupInput,
InputGroupText,
Popover_Shadcn_,
PopoverContent_Shadcn_,
PopoverTrigger_Shadcn_,
ScrollArea,
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'
import { useEdgeFunctionsQuery } from '@/data/edge-functions/edge-functions-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
interface HTTPRequestFieldsProps {
form: UseFormReturn<CreateCronJobForm>
}
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 (
<SheetSection className="flex flex-col gap-6">
<FormField
control={form.control}
name="values.method"
render={({ field }) => (
<FormItem>
<FormLabel>Method</FormLabel>
<Select_Shadcn_ onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger_Shadcn_>
<SelectValue_Shadcn_ placeholder="Select a method for the API call" />
</SelectTrigger_Shadcn_>
</FormControl>
<SelectContent_Shadcn_>
<SelectItem_Shadcn_ value="GET">GET</SelectItem_Shadcn_>
<SelectItem_Shadcn_ value="POST">POST</SelectItem_Shadcn_>
</SelectContent_Shadcn_>
</Select_Shadcn_>
<FormMessage />
</FormItem>
)}
/>
{edgeFunctions.length === 0 ? (
<div className="space-y-1">
<p className="text-sm text-foreground-light">Select which edge function to trigger</p>
{isLoading ? (
<Button type="default" className="justify-start" block size="small" loading>
Loading edge functions...
</Button>
) : (
<div className="px-4 py-4 border rounded-sm bg-surface-300 border-strong flex items-center justify-between space-x-4">
<p className="text-sm">No edge functions created yet</p>
<Button asChild>
<Link href={`/project/${ref}/functions`}>Create an edge function</Link>
</Button>
</div>
)}
</div>
) : edgeFunctions.length > 0 ? (
<FormField
control={form.control}
name="values.edgeFunctionName"
render={({ field }) => {
const selectedFunction = edgeFunctions.find((fn) => fn.url === field.value)
return (
<FormItem>
<FormLabel>Edge Function</FormLabel>
<Popover_Shadcn_ open={open} onOpenChange={setOpen}>
<PopoverTrigger_Shadcn_ asChild>
<FormControl>
<Button
type="default"
role="combobox"
aria-expanded={open}
aria-controls={listboxId}
className={cn(
'w-full justify-between',
!field.value && 'text-muted-foreground'
)}
size="small"
iconRight={
<ChevronsUpDown
className="ml-2 h-4 w-4 shrink-0 opacity-50"
strokeWidth={1}
/>
}
>
{selectedFunction
? selectedFunction.name
: 'Select which edge function to trigger'}
</Button>
</FormControl>
</PopoverTrigger_Shadcn_>
<PopoverContent_Shadcn_ id={listboxId} className="p-0" sameWidthAsTrigger>
<Command_Shadcn_>
<CommandInput_Shadcn_ placeholder="Search edge functions..." />
<CommandList_Shadcn_>
<CommandEmpty_Shadcn_>No edge function found.</CommandEmpty_Shadcn_>
<CommandGroup_Shadcn_>
<ScrollArea className={edgeFunctions.length > 7 ? 'h-[210px]' : ''}>
{edgeFunctions.map((fn) => {
return (
<CommandItem_Shadcn_
value={fn.name}
key={fn.id}
onSelect={() => {
field.onChange(fn.url === field.value ? '' : fn.url)
setOpen(false)
}}
>
<Check
className={cn(
'mr-2 h-4 w-4',
fn.url === field.value ? 'opacity-100' : 'opacity-0'
)}
/>
{fn.name}
</CommandItem_Shadcn_>
)
})}
</ScrollArea>
</CommandGroup_Shadcn_>
</CommandList_Shadcn_>
</Command_Shadcn_>
</PopoverContent_Shadcn_>
</Popover_Shadcn_>
<FormMessage />
</FormItem>
)
}}
/>
) : null}
<FormField
control={form.control}
name="values.timeoutMs"
render={({ field: { ref, ...rest } }) => (
<FormItemLayout label="Timeout" layout="vertical" className="gap-1">
<InputGroup>
<InputGroupInput {...rest} type="number" placeholder="1000" />
<InputGroupAddon align="inline-end">
<InputGroupText> ms</InputGroupText>
</InputGroupAddon>
</InputGroup>
</FormItemLayout>
)}
/>
</SheetSection>
)
}