AwsRegionSelector.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { Check, ChevronsUpDown } from 'lucide-react'
  2. import { useId, useState } from 'react'
  3. import {
  4. Button,
  5. cn,
  6. Command,
  7. CommandEmpty,
  8. CommandGroup,
  9. CommandInput,
  10. CommandItem,
  11. CommandList,
  12. FormControl,
  13. Popover,
  14. PopoverContent,
  15. PopoverTrigger,
  16. ScrollArea,
  17. } from 'ui'
  18. // copied from https://docs.aws.amazon.com/general/latest/gr/cognito_identity.html
  19. export const AWS_IDP_REGIONS = [
  20. 'af-south-1',
  21. 'ap-northeast-1',
  22. 'ap-northeast-2',
  23. 'ap-northeast-3',
  24. 'ap-south-1 ',
  25. 'ap-south-2 ',
  26. 'ap-southeast-1',
  27. 'ap-southeast-2',
  28. 'ap-southeast-3',
  29. 'ap-southeast-4',
  30. 'ca-central-1',
  31. 'eu-central-1',
  32. 'eu-central-2',
  33. 'eu-north-1',
  34. 'eu-south-1',
  35. 'eu-south-2',
  36. 'eu-west-1',
  37. 'eu-west-2',
  38. 'eu-west-3',
  39. 'il-central-1',
  40. 'me-central-1',
  41. 'me-south-1',
  42. 'sa-east-1',
  43. 'us-east-1',
  44. 'us-east-2',
  45. 'us-gov-west-1',
  46. 'us-west-1',
  47. 'us-west-2',
  48. ]
  49. export const AwsRegionSelector = ({
  50. value,
  51. onChange,
  52. }: {
  53. value: string
  54. onChange: (value: string) => void
  55. }) => {
  56. const [open, setOpen] = useState(false)
  57. const listboxId = useId()
  58. return (
  59. <Popover open={open} onOpenChange={setOpen}>
  60. <PopoverTrigger asChild>
  61. <FormControl>
  62. <Button
  63. type="default"
  64. role="combobox"
  65. aria-expanded={open}
  66. aria-controls={listboxId}
  67. className={cn('w-full justify-between', !value && 'text-muted-foreground')}
  68. size="small"
  69. iconRight={
  70. <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" strokeWidth={1} />
  71. }
  72. >
  73. {value ?? 'Select a region'}
  74. </Button>
  75. </FormControl>
  76. </PopoverTrigger>
  77. <PopoverContent id={listboxId} className="p-0" sameWidthAsTrigger>
  78. <Command>
  79. <CommandInput placeholder="Search AWS regions..." />
  80. <CommandList>
  81. <CommandEmpty>No regions found.</CommandEmpty>
  82. <CommandGroup>
  83. <ScrollArea className="h-72">
  84. {AWS_IDP_REGIONS.map((option) => (
  85. <CommandItem
  86. value={option}
  87. key={option}
  88. onSelect={(currentValue) => {
  89. onChange(currentValue === value ? '' : currentValue)
  90. setOpen(false)
  91. }}
  92. >
  93. <Check
  94. className={cn('mr-2 h-4 w-4', option === value ? 'opacity-100' : 'opacity-0')}
  95. />
  96. {option}
  97. </CommandItem>
  98. ))}
  99. </ScrollArea>
  100. </CommandGroup>
  101. </CommandList>
  102. </Command>
  103. </PopoverContent>
  104. </Popover>
  105. )
  106. }