CloudProviderSelector.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { UseFormReturn } from 'react-hook-form'
  2. import {
  3. FormControl,
  4. FormField,
  5. Select,
  6. SelectContent,
  7. SelectGroup,
  8. SelectItem,
  9. SelectTrigger,
  10. SelectValue,
  11. useWatch,
  12. } from 'ui'
  13. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  14. import { CreateProjectForm } from './ProjectCreation.schema'
  15. import { useCustomContent } from '@/hooks/custom-content/useCustomContent'
  16. import { PROVIDERS } from '@/lib/constants'
  17. const HA_SUPPORTED_PROVIDERS = ['AWS_K8S']
  18. interface CloudProviderSelectorProps {
  19. form: UseFormReturn<CreateProjectForm>
  20. }
  21. export const CloudProviderSelector = ({ form }: CloudProviderSelectorProps) => {
  22. const { infraCloudProviders: validCloudProviders } = useCustomContent(['infra:cloud_providers'])
  23. const highAvailability = useWatch({ control: form.control, name: 'highAvailability' })
  24. return (
  25. <FormField
  26. control={form.control}
  27. name="cloudProvider"
  28. render={({ field }) => (
  29. <FormItemLayout
  30. label="Cloud provider"
  31. layout="horizontal"
  32. description={
  33. highAvailability ? (
  34. <p className="text-warning">High availability is only supported on AWS (Revamped)</p>
  35. ) : (
  36. 'Select which cloud provider to spin up project from'
  37. )
  38. }
  39. >
  40. <Select
  41. onValueChange={(value) => field.onChange(value)}
  42. defaultValue={field.value}
  43. value={field.value}
  44. >
  45. <FormControl>
  46. <SelectTrigger>
  47. <SelectValue placeholder="Select a cloud provider" />
  48. </SelectTrigger>
  49. </FormControl>
  50. <SelectContent>
  51. <SelectGroup>
  52. {Object.values(PROVIDERS)
  53. .filter((provider) => validCloudProviders?.includes(provider.id) ?? true)
  54. .map((providerObj) => {
  55. const label = providerObj['name']
  56. const value = providerObj['id']
  57. const isDisabled = highAvailability && !HA_SUPPORTED_PROVIDERS.includes(value)
  58. return (
  59. <SelectItem key={value} value={value} disabled={isDisabled}>
  60. {label}
  61. </SelectItem>
  62. )
  63. })}
  64. </SelectGroup>
  65. </SelectContent>
  66. </Select>
  67. </FormItemLayout>
  68. )}
  69. />
  70. )
  71. }