Skip to content

Commit f465bb8

Browse files
committed
Set the default language if the language is not found in the URL
1 parent 8ab6971 commit f465bb8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/contexts/AppContext.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { createContext, FC, useContext, useEffect, useState } from "react";
2-
import { useParams } from "react-router-dom";
2+
import { useNavigate, useParams } from "react-router-dom";
33

44
import { useLanguages } from "@hooks/useLanguages";
55
import { AppState, LanguageType, SnippetType } from "@types";
66
import { configureUserSelection } from "@utils/configureUserSelection";
7-
import { defaultState } from "@utils/consts";
7+
import { defaultLanguage, defaultState } from "@utils/consts";
8+
import { slugify } from "@utils/slugify";
89

910
const AppContext = createContext<AppState>(defaultState);
1011

1112
export const AppProvider: FC<{ children: React.ReactNode }> = ({
1213
children,
1314
}) => {
15+
const navigate = useNavigate();
1416
const { languageName, categoryName } = useParams();
1517

1618
const { fetchedLanguages } = useLanguages();
@@ -35,6 +37,16 @@ export const AppProvider: FC<{ children: React.ReactNode }> = ({
3537
// eslint-disable-next-line react-hooks/exhaustive-deps
3638
}, [fetchedLanguages]);
3739

40+
/**
41+
* Set the default language if the language is not found in the URL.
42+
*/
43+
useEffect(() => {
44+
if (languageName === undefined) {
45+
navigate(`/${slugify(defaultLanguage.name)}`, { replace: true });
46+
}
47+
// eslint-disable-next-line react-hooks/exhaustive-deps
48+
}, []);
49+
3850
if (language === null || category === null) {
3951
return <div>Loading...</div>;
4052
}

0 commit comments

Comments
 (0)