mirror of
https://github.com/supabase/supabase.git
synced 2026-05-06 08:56:46 -04:00
0facd341a6
## Problem We used to have a `_Shadcn_` suffix for all the shadcn form components because we also had `formik` form components. This is not needed anymore. ## Solution - Remove the suffix - Update all usages
165 lines
5.8 KiB
TypeScript
165 lines
5.8 KiB
TypeScript
import { useParams } from 'common'
|
|
import Link from 'next/link'
|
|
import { UseFormReturn } from 'react-hook-form'
|
|
import {
|
|
Button,
|
|
FormControl,
|
|
FormField,
|
|
Input_Shadcn_,
|
|
Select_Shadcn_,
|
|
SelectContent_Shadcn_,
|
|
SelectItem_Shadcn_,
|
|
SelectTrigger_Shadcn_,
|
|
SelectValue_Shadcn_,
|
|
useWatch,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
|
|
import { WebhookFormValues } from './EditHookPanel.constants'
|
|
import {
|
|
FormSection,
|
|
FormSectionContent,
|
|
FormSectionLabel,
|
|
} from '@/components/ui/Forms/FormSection'
|
|
import { useEdgeFunctionsQuery } from '@/data/edge-functions/edge-functions-query'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
|
|
interface HTTPRequestConfigProps {
|
|
form: UseFormReturn<WebhookFormValues>
|
|
}
|
|
|
|
export const HTTPRequestConfig = ({ form }: HTTPRequestConfigProps) => {
|
|
const { ref } = useParams()
|
|
const { data: selectedProject } = useSelectedProjectQuery()
|
|
|
|
const { data: functions } = useEdgeFunctionsQuery({ projectRef: ref })
|
|
|
|
const edgeFunctions = functions ?? []
|
|
const functionType = useWatch({ control: form.control, name: 'function_type' })
|
|
|
|
return (
|
|
<FormSection
|
|
header={
|
|
<FormSectionLabel className="lg:!col-span-4">
|
|
{functionType === 'http_request'
|
|
? 'HTTP Request'
|
|
: functionType === 'supabase_function'
|
|
? 'Edge Function'
|
|
: ''}
|
|
</FormSectionLabel>
|
|
}
|
|
>
|
|
<FormSectionContent loading={false} className="lg:!col-span-8">
|
|
<FormField
|
|
control={form.control}
|
|
name="http_method"
|
|
render={({ field }) => (
|
|
<FormItemLayout label="Method" layout="vertical" className="gap-1">
|
|
<Select_Shadcn_ value={field.value} onValueChange={field.onChange}>
|
|
<FormControl>
|
|
<SelectTrigger_Shadcn_>
|
|
<SelectValue_Shadcn_ />
|
|
</SelectTrigger_Shadcn_>
|
|
</FormControl>
|
|
<SelectContent_Shadcn_>
|
|
<SelectItem_Shadcn_ value="GET">GET</SelectItem_Shadcn_>
|
|
<SelectItem_Shadcn_ value="POST">POST</SelectItem_Shadcn_>
|
|
</SelectContent_Shadcn_>
|
|
</Select_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
|
|
{functionType === 'http_request' ? (
|
|
<FormField
|
|
control={form.control}
|
|
name="http_url"
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
label="URL"
|
|
layout="vertical"
|
|
className="gap-1"
|
|
description="URL of the HTTP request. Must include HTTP/HTTPS"
|
|
>
|
|
<FormControl>
|
|
<Input_Shadcn_ {...field} placeholder="http://api.com/path/resource" />
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
) : functionType === 'supabase_function' && edgeFunctions.length === 0 ? (
|
|
<div className="space-y-1">
|
|
<p className="text-sm text-foreground-light">Select which edge function to trigger</p>
|
|
<div className="px-4 py-4 border rounded 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>
|
|
) : functionType === 'supabase_function' && edgeFunctions.length > 0 ? (
|
|
<FormField
|
|
control={form.control}
|
|
name="http_url"
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
label="Select which edge function to trigger"
|
|
layout="vertical"
|
|
className="gap-1"
|
|
>
|
|
<Select_Shadcn_ value={field.value} onValueChange={field.onChange}>
|
|
<FormControl>
|
|
<SelectTrigger_Shadcn_>
|
|
<SelectValue_Shadcn_ placeholder="Select an edge function" />
|
|
</SelectTrigger_Shadcn_>
|
|
</FormControl>
|
|
<SelectContent_Shadcn_>
|
|
{edgeFunctions.map((fn) => {
|
|
const restUrl = selectedProject?.restUrl
|
|
const restUrlTld = restUrl ? new URL(restUrl).hostname.split('.').pop() : 'co'
|
|
const functionUrl = `https://${ref}.supabase.${restUrlTld}/functions/v1/${fn.slug}`
|
|
|
|
return (
|
|
<SelectItem_Shadcn_ key={fn.id} value={functionUrl}>
|
|
{fn.name}
|
|
</SelectItem_Shadcn_>
|
|
)
|
|
})}
|
|
</SelectContent_Shadcn_>
|
|
</Select_Shadcn_>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
) : null}
|
|
|
|
<FormField
|
|
control={form.control}
|
|
name="timeout_ms"
|
|
render={({ field }) => (
|
|
<FormItemLayout
|
|
label="Timeout"
|
|
labelOptional="Between 1000ms to 10,000ms"
|
|
layout="vertical"
|
|
className="gap-1"
|
|
>
|
|
<FormControl>
|
|
<div className="relative">
|
|
<Input_Shadcn_
|
|
{...field}
|
|
type="number"
|
|
onChange={(e) => field.onChange(Number(e.target.value))}
|
|
className="pr-10"
|
|
/>
|
|
<span className="absolute right-3 top-1/2 -translate-y-1/2 text-foreground-light text-sm">
|
|
ms
|
|
</span>
|
|
</div>
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</FormSectionContent>
|
|
</FormSection>
|
|
)
|
|
}
|