HTTPHeaders.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useParams } from 'common'
  3. import { UseFormReturn } from 'react-hook-form'
  4. import { useWatch } from 'ui'
  5. import { KeyValueFieldArray } from 'ui-patterns/form/KeyValueFieldArray/KeyValueFieldArray'
  6. import { WebhookFormValues } from './EditHookPanel.constants'
  7. import { buildEdgeFunctionHeaderAddActions } from '@/components/interfaces/Functions/httpHeaderAddActions'
  8. import {
  9. FormSection,
  10. FormSectionContent,
  11. FormSectionLabel,
  12. } from '@/components/ui/Forms/FormSection'
  13. import { getKeys, useAPIKeysQuery } from '@/data/api-keys/api-keys-query'
  14. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  15. import { uuidv4 } from '@/lib/helpers'
  16. interface HTTPHeadersProps {
  17. form: UseFormReturn<WebhookFormValues>
  18. }
  19. export const HTTPHeaders = ({ form }: HTTPHeadersProps) => {
  20. const { ref } = useParams()
  21. const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
  22. const { data: apiKeys } = useAPIKeysQuery(
  23. { projectRef: ref, reveal: true },
  24. { enabled: canReadAPIKeys }
  25. )
  26. const { serviceKey, secretKey } = getKeys(apiKeys)
  27. const apiKey = secretKey?.api_key ?? serviceKey?.api_key ?? '[YOUR API KEY]'
  28. const functionType = useWatch({ control: form.control, name: 'function_type' })
  29. const addActions =
  30. functionType === 'briven_function'
  31. ? buildEdgeFunctionHeaderAddActions({
  32. apiKey,
  33. includeApiKeyHeader: serviceKey?.type === 'secret',
  34. createRow: (name: string, value: string) => ({ id: uuidv4(), name, value }),
  35. })
  36. : []
  37. return (
  38. <FormSection
  39. header={<FormSectionLabel className="lg:col-span-4!">HTTP Headers</FormSectionLabel>}
  40. >
  41. <FormSectionContent loading={false} className="lg:col-span-8!">
  42. <KeyValueFieldArray
  43. control={form.control}
  44. name="httpHeaders"
  45. keyFieldName="name"
  46. valueFieldName="value"
  47. createEmptyRow={() => ({ id: uuidv4(), name: '', value: '' })}
  48. keyPlaceholder="Header name"
  49. valuePlaceholder="Header value"
  50. addLabel="Add a new header"
  51. addActions={addActions}
  52. />
  53. </FormSectionContent>
  54. </FormSection>
  55. )
  56. }