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