umami/src/components/hooks/useCountryNames.ts

35 lines
775 B
TypeScript
Raw Normal View History

2020-09-30 23:27:27 +00:00
import { useState, useEffect } from 'react';
2023-06-16 03:15:31 +00:00
import { httpGet } from 'next-basics';
2024-01-29 02:33:40 +00:00
import enUS from '../../../public/intl/country/en-US.json';
2020-09-30 23:27:27 +00:00
const countryNames = {
'en-US': enUS,
};
2023-11-14 05:36:52 +00:00
export function useCountryNames(locale: string) {
2020-09-30 23:27:27 +00:00
const [list, setList] = useState(countryNames[locale] || enUS);
2023-11-14 05:36:52 +00:00
async function loadData(locale: string) {
2023-09-29 12:29:22 +00:00
const { data } = await httpGet(`${process.env.basePath}/intl/country/${locale}.json`);
2020-09-30 23:27:27 +00:00
if (data) {
2020-09-30 23:27:27 +00:00
countryNames[locale] = data;
setList(countryNames[locale]);
} else {
setList(enUS);
}
}
useEffect(() => {
if (!countryNames[locale]) {
loadData(locale);
} else {
setList(countryNames[locale]);
}
}, [locale]);
return list;
}
2023-05-18 06:20:06 +00:00
export default useCountryNames;