HttpHeaderFieldsSection.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { PermissionAction } from '@supabase/shared-types/out/constants'
  2. import { useParams } from 'common'
  3. import { useFormContext } from 'react-hook-form'
  4. import { FormLabel, SheetSection } from 'ui'
  5. import { KeyValueFieldArray } from 'ui-patterns/form/KeyValueFieldArray/KeyValueFieldArray'
  6. import { CreateCronJobForm } from './CreateCronJobSheet/CreateCronJobSheet.constants'
  7. import { buildEdgeFunctionHeaderAddActions } from '@/components/interfaces/Functions/httpHeaderAddActions'
  8. import { getKeys, useAPIKeysQuery } from '@/data/api-keys/api-keys-query'
  9. import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions'
  10. interface HTTPHeaderFieldsSectionProps {
  11. variant: 'edge_function' | 'http_request'
  12. }
  13. export const HTTPHeaderFieldsSection = ({ variant }: HTTPHeaderFieldsSectionProps) => {
  14. const form = useFormContext<CreateCronJobForm>()
  15. const { ref } = useParams()
  16. const { can: canReadAPIKeys } = useAsyncCheckPermissions(PermissionAction.SECRETS_READ, '*')
  17. const { data: apiKeys } = useAPIKeysQuery(
  18. { projectRef: ref, reveal: true },
  19. { enabled: canReadAPIKeys }
  20. )
  21. const { serviceKey, secretKey } = getKeys(apiKeys)
  22. const apiKey = secretKey?.api_key ?? serviceKey?.api_key ?? '[YOUR API KEY]'
  23. const addActions =
  24. variant === 'edge_function'
  25. ? buildEdgeFunctionHeaderAddActions({
  26. apiKey,
  27. includeApiKeyHeader: serviceKey?.type === 'secret',
  28. createRow: (name: string, value: string) => ({ name, value }),
  29. })
  30. : []
  31. return (
  32. <SheetSection>
  33. <FormLabel>HTTP Headers</FormLabel>
  34. <KeyValueFieldArray
  35. control={form.control}
  36. name="values.httpHeaders"
  37. keyFieldName="name"
  38. valueFieldName="value"
  39. createEmptyRow={() => ({ name: '', value: '' })}
  40. keyPlaceholder="Header name"
  41. valuePlaceholder="Header value"
  42. addLabel="Add a new header"
  43. addActions={addActions}
  44. />
  45. </SheetSection>
  46. )
  47. }