Skip to content

Commit 793fde0

Browse files
authored
fix: validate language codes in URL path extraction (#634)
1 parent 5a8b32b commit 793fde0

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

fundamentals/today-i-learned/src/components/shared/layout/LayoutNavigation.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@ import { useLocation } from "react-router-dom";
22
import { OneNavigationReact } from "@shared/components";
33
import { NavigationBar } from "./NavigationBar";
44

5+
const SUPPORTED_LANGUAGES = ["ko", "en", "ja", "zh-hans"];
6+
7+
const extractLanguageCode = (path: string) => {
8+
const firstSegment = path.split("/")[1];
9+
const isValidLanguage = SUPPORTED_LANGUAGES.includes(firstSegment);
10+
11+
if (isValidLanguage) {
12+
return firstSegment;
13+
}
14+
15+
return null;
16+
};
17+
518
export const LayoutNavigation: React.FC = () => {
619
const location = useLocation();
7-
const isKorean =
8-
!location.pathname.startsWith("/en") &&
9-
!location.pathname.startsWith("/ja") &&
10-
!location.pathname.startsWith("/zh-hans");
20+
const lang = extractLanguageCode(location.pathname) ?? "ko";
21+
const isKorean = lang === "ko";
1122

1223
return (
1324
<>
1425
<NavigationBar />
1526
<OneNavigationReact
16-
lang={location.pathname.split("/")[1] || "ko"}
27+
lang={lang}
1728
isKorean={isKorean}
1829
pathname={location.pathname}
1930
/>

0 commit comments

Comments
 (0)