umami/src/components/hooks/useCountryNames.ts

35 lines
766 B
TypeScript
Raw Normal View History

2020-10-01 01:27:27 +02:00
import { useState, useEffect } from 'react';
2023-06-16 05:15:31 +02:00
import { httpGet } from 'next-basics';
2023-08-22 04:32:17 +02:00
import enUS from 'public/intl/country/en-US.json';
2020-10-01 01:27:27 +02:00
const countryNames = {
'en-US': enUS,
};
2023-11-14 06:36:52 +01:00
export function useCountryNames(locale: string) {
2020-10-01 01:27:27 +02:00
const [list, setList] = useState(countryNames[locale] || enUS);
2023-11-14 06:36:52 +01:00
async function loadData(locale: string) {
2023-09-29 14:29:22 +02:00
const { data } = await httpGet(`${process.env.basePath}/intl/country/${locale}.json`);
2020-10-01 01:27:27 +02:00
if (data) {
2020-10-01 01:27:27 +02: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 08:20:06 +02:00
export default useCountryNames;