Skip to content

Commit

Permalink
always route the user to the complete path ensuring that all sub lang…
Browse files Browse the repository at this point in the history
…uages and all snippets slugified strings are included in the uri
  • Loading branch information
barrymun committed Feb 7, 2025
1 parent be5c643 commit dc3a56b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const AppRouter = () => {
<Route element={<App />}>
<Route path="/" element={<SnippetList />} />
<Route path="/:languageName" element={<SnippetList />} />
<Route
path="/:languageName/:subLanguageName"
element={<SnippetList />}
/>
<Route
path="/:languageName/:subLanguageName/:categoryName"
element={<SnippetList />}
Expand Down
19 changes: 16 additions & 3 deletions src/contexts/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { useNavigate, useParams } from "react-router-dom";
import { useLanguages } from "@hooks/useLanguages";
import { AppState, LanguageType, SnippetType } from "@types";
import { configureUserSelection } from "@utils/configureUserSelection";
import { defaultLanguage, defaultState } from "@utils/consts";
import {
defaultCategoryName,
defaultLanguage,
defaultSlugifiedSubLanguageName,
defaultState,
} from "@utils/consts";
import { slugify } from "@utils/slugify";

const AppContext = createContext<AppState>(defaultState);
Expand Down Expand Up @@ -46,8 +51,16 @@ export const AppProvider: FC<{ children: React.ReactNode }> = ({
* Set the default language if the language is not found in the URL.
*/
useEffect(() => {
if (languageName === undefined) {
navigate(`/${slugify(defaultLanguage.name)}`, { replace: true });
const resolvedLanguage = languageName || defaultLanguage.name;
const resolvedSubLanguage =
subLanguageName || defaultSlugifiedSubLanguageName;
const resolvedCategory = categoryName || defaultCategoryName;

if (!languageName || !subLanguageName || !categoryName) {
navigate(
`/${slugify(resolvedLanguage)}/${slugify(resolvedSubLanguage)}/${slugify(resolvedCategory)}`,
{ replace: true }
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down

0 comments on commit dc3a56b

Please sign in to comment.