HTTPRequestConfig.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { useParams } from 'common'
  2. import Link from 'next/link'
  3. import { UseFormReturn } from 'react-hook-form'
  4. import {
  5. Button,
  6. FormControl,
  7. FormField,
  8. Input,
  9. Select,
  10. SelectContent,
  11. SelectItem,
  12. SelectTrigger,
  13. SelectValue,
  14. useWatch,
  15. } from 'ui'
  16. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  17. import { WebhookFormValues } from './EditHookPanel.constants'
  18. import {
  19. FormSection,
  20. FormSectionContent,
  21. FormSectionLabel,
  22. } from '@/components/ui/Forms/FormSection'
  23. import { useEdgeFunctionsQuery } from '@/data/edge-functions/edge-functions-query'
  24. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  25. interface HTTPRequestConfigProps {
  26. form: UseFormReturn<WebhookFormValues>
  27. }
  28. export const HTTPRequestConfig = ({ form }: HTTPRequestConfigProps) => {
  29. const { ref } = useParams()
  30. const { data: selectedProject } = useSelectedProjectQuery()
  31. const { data: functions } = useEdgeFunctionsQuery({ projectRef: ref })
  32. const edgeFunctions = functions ?? []
  33. const functionType = useWatch({ control: form.control, name: 'function_type' })
  34. return (
  35. <FormSection
  36. header={
  37. <FormSectionLabel className="lg:col-span-4!">
  38. {functionType === 'http_request'
  39. ? 'HTTP Request'
  40. : functionType === 'briven_function'
  41. ? 'Edge Function'
  42. : ''}
  43. </FormSectionLabel>
  44. }
  45. >
  46. <FormSectionContent loading={false} className="lg:col-span-8!">
  47. <FormField
  48. control={form.control}
  49. name="http_method"
  50. render={({ field }) => (
  51. <FormItemLayout label="Method" layout="vertical" className="gap-1">
  52. <Select value={field.value} onValueChange={field.onChange}>
  53. <FormControl>
  54. <SelectTrigger>
  55. <SelectValue />
  56. </SelectTrigger>
  57. </FormControl>
  58. <SelectContent>
  59. <SelectItem value="GET">GET</SelectItem>
  60. <SelectItem value="POST">POST</SelectItem>
  61. </SelectContent>
  62. </Select>
  63. </FormItemLayout>
  64. )}
  65. />
  66. {functionType === 'http_request' ? (
  67. <FormField
  68. control={form.control}
  69. name="http_url"
  70. render={({ field }) => (
  71. <FormItemLayout
  72. label="URL"
  73. layout="vertical"
  74. className="gap-1"
  75. description="URL of the HTTP request. Must include HTTP/HTTPS"
  76. >
  77. <FormControl>
  78. <Input {...field} placeholder="http://api.com/path/resource" />
  79. </FormControl>
  80. </FormItemLayout>
  81. )}
  82. />
  83. ) : functionType === 'briven_function' && edgeFunctions.length === 0 ? (
  84. <div className="space-y-1">
  85. <p className="text-sm text-foreground-light">Select which edge function to trigger</p>
  86. <div className="px-4 py-4 border rounded-sm bg-surface-300 border-strong flex items-center justify-between space-x-4">
  87. <p className="text-sm">No edge functions created yet</p>
  88. <Button asChild>
  89. <Link href={`/project/${ref}/functions`}>Create an edge function</Link>
  90. </Button>
  91. </div>
  92. </div>
  93. ) : functionType === 'briven_function' && edgeFunctions.length > 0 ? (
  94. <FormField
  95. control={form.control}
  96. name="http_url"
  97. render={({ field }) => (
  98. <FormItemLayout
  99. label="Select which edge function to trigger"
  100. layout="vertical"
  101. className="gap-1"
  102. >
  103. <Select value={field.value} onValueChange={field.onChange}>
  104. <FormControl>
  105. <SelectTrigger>
  106. <SelectValue placeholder="Select an edge function" />
  107. </SelectTrigger>
  108. </FormControl>
  109. <SelectContent>
  110. {edgeFunctions.map((fn) => {
  111. const restUrl = selectedProject?.restUrl
  112. const restUrlTld = restUrl ? new URL(restUrl).hostname.split('.').pop() : 'co'
  113. const functionUrl = `https://${ref}.briven.${restUrlTld}/functions/v1/${fn.slug}`
  114. return (
  115. <SelectItem key={fn.id} value={functionUrl}>
  116. {fn.name}
  117. </SelectItem>
  118. )
  119. })}
  120. </SelectContent>
  121. </Select>
  122. </FormItemLayout>
  123. )}
  124. />
  125. ) : null}
  126. <FormField
  127. control={form.control}
  128. name="timeout_ms"
  129. render={({ field }) => (
  130. <FormItemLayout
  131. label="Timeout"
  132. labelOptional="Between 1000ms to 10,000ms"
  133. layout="vertical"
  134. className="gap-1"
  135. >
  136. <FormControl>
  137. <div className="relative">
  138. <Input
  139. {...field}
  140. type="number"
  141. onChange={(e) => field.onChange(Number(e.target.value))}
  142. className="pr-10"
  143. />
  144. <span className="absolute right-3 top-1/2 -translate-y-1/2 text-foreground-light text-sm">
  145. ms
  146. </span>
  147. </div>
  148. </FormControl>
  149. </FormItemLayout>
  150. )}
  151. />
  152. </FormSectionContent>
  153. </FormSection>
  154. )
  155. }