Skip to content

Commit 2bf7d08

Browse files
authored
Refactor localized countries hook to use commons-ui's useLocalizedCountries (#3137)
Signed-off-by: achour94 <[email protected]>
1 parent f0b6ab6 commit 2bf7d08

File tree

4 files changed

+4
-55
lines changed

4 files changed

+4
-55
lines changed

src/components/app.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
NotificationsUrlKeys,
2727
useNotificationsListener,
2828
useSnackMessage,
29+
getComputedLanguage,
2930
} from '@gridsuite/commons-ui';
3031
import PageNotFound from './page-not-found';
3132
import { FormattedMessage } from 'react-intl';
@@ -37,7 +38,6 @@ import {
3738
PARAM_THEME,
3839
PARAM_USE_NAME,
3940
} from '../utils/config-params';
40-
import { getComputedLanguage } from '../utils/language';
4141
import AppTopBar from './app-top-bar';
4242
import { StudyContainer } from './study-container';
4343
import { fetchDefaultParametersValues, fetchIdpSettings } from '../services/utils';

src/components/utils/localized-countries-hook.ts

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,12 @@
66
*/
77

88
import { PARAM_LANGUAGE } from '../../utils/config-params';
9-
import { useCallback, useEffect, useMemo, useState } from 'react';
10-
import { getComputedLanguage } from '../../utils/language';
11-
import localizedCountries from 'localized-countries';
12-
import countriesFr from 'localized-countries/data/fr.json';
13-
import countriesEn from 'localized-countries/data/en.json';
149
import { useParameterState } from 'components/dialogs/parameters/use-parameters-state';
10+
import { useLocalizedCountries as useLocalizedCountriesCUI } from '@gridsuite/commons-ui';
1511

1612
export const useLocalizedCountries = () => {
1713
const [languageLocal] = useParameterState(PARAM_LANGUAGE);
18-
const [localizedCountriesModule, setLocalizedCountriesModule] = useState<localizedCountries.LocalizedCountries>();
19-
20-
//TODO FM this is disgusting, can we make it better ?
21-
useEffect(() => {
22-
const lang = getComputedLanguage(languageLocal).substr(0, 2);
23-
let localizedCountriesResult: localizedCountries.LocalizedCountries;
24-
// vite does not support ESM dynamic imports on node_modules, so we have to imports the languages before and do this
25-
// https://github.com/vitejs/vite/issues/14102
26-
if (lang === 'fr') {
27-
localizedCountriesResult = localizedCountries(countriesFr);
28-
} else if (lang === 'en') {
29-
localizedCountriesResult = localizedCountries(countriesEn);
30-
} else {
31-
console.warn('Unsupported language "' + lang + '" for countries translation, we use english as default');
32-
localizedCountriesResult = localizedCountries(countriesEn);
33-
}
34-
setLocalizedCountriesModule(localizedCountriesResult);
35-
}, [languageLocal]);
36-
37-
const countryCodes = useMemo(
38-
() => (localizedCountriesModule ? Object.keys(localizedCountriesModule.object()) : []),
39-
[localizedCountriesModule]
40-
);
41-
42-
const translate = useCallback(
43-
(countryCode: string) => (localizedCountriesModule ? localizedCountriesModule.get(countryCode) : ''),
44-
[localizedCountriesModule]
45-
);
14+
const { translate, countryCodes } = useLocalizedCountriesCUI(languageLocal);
4615

4716
return { translate, countryCodes };
4817
};

src/redux/session-storage/local-storage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { DARK_THEME, GsLang, GsTheme, LANG_SYSTEM } from '@gridsuite/commons-ui';
9-
import { getComputedLanguage } from '../../utils/language';
8+
import { DARK_THEME, getComputedLanguage, GsLang, GsTheme, LANG_SYSTEM } from '@gridsuite/commons-ui';
109
import { APP_NAME } from '../../utils/config-params';
1110
import { StudyDisplayMode } from 'components/network-modification.type';
1211
import { UUID } from 'crypto';

src/utils/language.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)