aboutsummaryrefslogtreecommitdiff
path: root/src/components/hooks/useRegionNames.ts
blob: 57dcc416cc551399425b81109bc3bc0d0db93607 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import regions from '../../../public/iso-3166-2.json';
import { useCountryNames } from './useCountryNames';

export function useRegionNames(locale: string) {
  const { countryNames } = useCountryNames(locale);

  const getRegionName = (regionCode: string, countryCode?: string) => {
    if (!countryCode) {
      return regions[regionCode];
    }

    if (!regionCode) {
      return null;
    }

    const region = regionCode?.includes('-') ? regionCode : `${countryCode}-${regionCode}`;

    return regions[region] ? `${regions[region]}, ${countryNames[countryCode]}` : region;
  };

  return { regionNames: regions, getRegionName };
}