@ -2,6 +2,7 @@ import { Listbox, Transition } from '@headlessui/react';
import { CheckIcon , ChevronDownIcon } from '@heroicons/react/solid' ;
import { CheckIcon , ChevronDownIcon } from '@heroicons/react/solid' ;
import { hasFlag } from 'country-flag-icons' ;
import { hasFlag } from 'country-flag-icons' ;
import 'country-flag-icons/3x2/flags.css' ;
import 'country-flag-icons/3x2/flags.css' ;
import { sortBy } from 'lodash' ;
import React , { useEffect , useMemo , useState } from 'react' ;
import React , { useEffect , useMemo , useState } from 'react' ;
import { defineMessages , useIntl } from 'react-intl' ;
import { defineMessages , useIntl } from 'react-intl' ;
import useSWR from 'swr' ;
import useSWR from 'swr' ;
@ -39,32 +40,21 @@ const RegionSelector: React.FC<RegionSelectorProps> = ({
[ ]
[ ]
) ;
) ;
const sortedRegions = useMemo (
const sortedRegions = useMemo ( ( ) = > {
( ) = >
regions ? . forEach ( ( region ) = > {
regions ? . sort ( ( region1 , region2 ) = > {
region . name =
const region1Name =
intl . formatDisplayName ( region . iso_3166_1 , {
intl . formatDisplayName ( region1 . iso_3166_1 , {
type : 'region' ,
type : 'region' ,
fallback : 'none' ,
fallback : 'none' ,
} ) ? ? region . english_name ;
} ) ? ? region1 . english_name ;
} ) ;
const region2Name =
intl . formatDisplayName ( region2 . iso_3166_1 , {
type : 'region' ,
fallback : 'none' ,
} ) ? ? region2 . english_name ;
return region1Name === region2Name
return sortBy ( regions , 'name' ) ;
? 0
} , [ intl , regions ] ) ;
: region1Name > region2Name
? 1
: - 1 ;
} ) ,
[ intl , regions ]
) ;
const defaultRegionNameFallback =
const regionName = ( regionCode : string ) = >
regions? . find ( ( region ) = > region . iso_3166_1 === currentSettings. region )
sortedRegions ? . find ( ( region ) = > region . iso_3166_1 === regionCode ) ? . name ? ?
? . english_name ? ? currentSettings . region ;
regionCode ;
useEffect ( ( ) = > {
useEffect ( ( ) = > {
if ( regions && value ) {
if ( regions && value ) {
@ -110,17 +100,11 @@ const RegionSelector: React.FC<RegionSelectorProps> = ({
) }
) }
< span className = "block truncate" >
< span className = "block truncate" >
{ selectedRegion && selectedRegion . iso_3166_1 !== 'all'
{ selectedRegion && selectedRegion . iso_3166_1 !== 'all'
? intl . formatDisplayName ( selectedRegion . iso_3166_1 , {
? regionName ( selectedRegion . iso_3166_1 )
type : 'region' ,
fallback : 'none' ,
} ) ? ? selectedRegion . english_name
: isUserSetting && selectedRegion ? . iso_3166_1 !== 'all'
: isUserSetting && selectedRegion ? . iso_3166_1 !== 'all'
? intl . formatMessage ( messages . regionServerDefault , {
? intl . formatMessage ( messages . regionServerDefault , {
region : currentSettings.region
region : currentSettings.region
? intl . formatDisplayName ( currentSettings . region , {
? regionName ( currentSettings . region )
type : 'region' ,
fallback : 'none' ,
} ) ? ? defaultRegionNameFallback
: intl . formatMessage ( messages . regionDefault ) ,
: intl . formatMessage ( messages . regionDefault ) ,
} )
} )
: intl . formatMessage ( messages . regionDefault ) }
: intl . formatMessage ( messages . regionDefault ) }
@ -168,13 +152,7 @@ const RegionSelector: React.FC<RegionSelectorProps> = ({
>
>
{ intl . formatMessage ( messages . regionServerDefault , {
{ intl . formatMessage ( messages . regionServerDefault , {
region : currentSettings.region
region : currentSettings.region
? intl . formatDisplayName (
? regionName ( currentSettings . region )
currentSettings . region ,
{
type : 'region' ,
fallback : 'none' ,
}
) ? ? defaultRegionNameFallback
: intl . formatMessage ( messages . regionDefault ) ,
: intl . formatMessage ( messages . regionDefault ) ,
} ) }
} ) }
< / span >
< / span >
@ -241,10 +219,7 @@ const RegionSelector: React.FC<RegionSelectorProps> = ({
selected ? 'font-semibold' : 'font-normal'
selected ? 'font-semibold' : 'font-normal'
} block truncate ` }
} block truncate ` }
>
>
{ intl . formatDisplayName ( region . iso_3166_1 , {
{ regionName ( region . iso_3166_1 ) }
type : 'region' ,
fallback : 'none' ,
} ) ? ? region . english_name }
< / span >
< / span >
{ selected && (
{ selected && (
< span
< span