Skip to content

Commit 975cfe6

Browse files
authored
Fix language selector state not updating on change (#61060)
1 parent 4b34c42 commit 975cfe6

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

airflow-core/src/airflow/ui/src/layouts/Nav/LanguageSelector.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,33 @@
1818
*/
1919
import { Field, VStack, Box, Text } from "@chakra-ui/react";
2020
import { Select, type SingleValue } from "chakra-react-select";
21-
import React from "react";
21+
import React, { useState, useCallback } from "react";
2222
import { useTranslation } from "react-i18next";
2323

2424
import { supportedLanguages } from "src/i18n/config";
2525

2626
const LanguageSelector: React.FC = () => {
2727
const { i18n, t: translate } = useTranslation();
28+
const [selectedLang, setSelectedLang] = useState(i18n.resolvedLanguage ?? i18n.language);
2829

2930
const options = supportedLanguages.map((lang) => ({
3031
label: lang.name,
3132
value: lang.code,
3233
}));
3334

34-
const handleLanguageChange = (selectedOption: SingleValue<{ label: string; value: string }>) => {
35-
if (selectedOption) {
36-
void i18n.changeLanguage(selectedOption.value);
37-
}
38-
};
35+
const handleLanguageChange = useCallback(
36+
(selectedOption: SingleValue<{ label: string; value: string }>) => {
37+
if (selectedOption) {
38+
void i18n.changeLanguage(selectedOption.value).then(() => {
39+
setSelectedLang(selectedOption.value);
40+
});
41+
}
42+
},
43+
[i18n],
44+
);
3945

40-
const currentLang = options.find((option) => option.value === i18n.language);
41-
const langDir = i18n.dir(i18n.language);
46+
const currentLang = options.find((option) => option.value === selectedLang);
47+
const langDir = i18n.dir(selectedLang);
4248

4349
return (
4450
<VStack align="stretch" gap={6}>
@@ -62,7 +68,7 @@ const LanguageSelector: React.FC = () => {
6268
</Field.Root>
6369
<Box borderRadius="md" boxShadow="sm" display="flex" flexDirection="column" gap={2} p={6}>
6470
<Text fontSize="lg" fontWeight="bold">
65-
{currentLang?.label} ({i18n.language})
71+
{currentLang?.label} ({selectedLang})
6672
</Text>
6773
<Text>{`${translate("direction")}: ${langDir.toUpperCase()}`}</Text>
6874
</Box>

0 commit comments

Comments
 (0)