diff options
Diffstat (limited to 'src/components/hooks/useCountryNames.ts')
| -rw-r--r-- | src/components/hooks/useCountryNames.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/components/hooks/useCountryNames.ts b/src/components/hooks/useCountryNames.ts new file mode 100644 index 0000000..1ec9fc1 --- /dev/null +++ b/src/components/hooks/useCountryNames.ts @@ -0,0 +1,32 @@ +import { useEffect, useState } from 'react'; +import { httpGet } from '@/lib/fetch'; +import enUS from '../../../public/intl/country/en-US.json'; + +const countryNames = { + 'en-US': enUS, +}; + +export function useCountryNames(locale: string) { + const [list, setList] = useState(countryNames[locale] || enUS); + + async function loadData(locale: string) { + const { data } = await httpGet(`${process.env.basePath || ''}/intl/country/${locale}.json`); + + if (data) { + countryNames[locale] = data; + setList(countryNames[locale]); + } else { + setList(enUS); + } + } + + useEffect(() => { + if (!countryNames[locale]) { + loadData(locale); + } else { + setList(countryNames[locale]); + } + }, [locale]); + + return { countryNames: list }; +} |