Skip to content

Commit

Permalink
Improve Route Handling for Localized and Non-Localized Routes (#1063)
Browse files Browse the repository at this point in the history
* Add helper function that removes locale prefixes from route names

* Implments helper removeLocaleFromRouteName

* Fix currentRoutePathIncludes

* Calls removeLoacaleFromRouteName from isCurrentRoutePathSubpageOf

* Uses result from removeLocaleFromRouteName accordingly

* Removed additional whitesapce

* Removed additional locale

---------

Co-authored-by: mohtelsayed <mtelsaye@LAPTOP-1P1MHBAT>
  • Loading branch information
Mohtelsayed and mohtelsayed authored Dec 13, 2024
1 parent dc153dd commit 6871265
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions frontend/utils/routeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,31 @@ export function isTopLevelRouteActive(routePath: string): boolean {
return currentSegments[0] === targetSegments[0];
}

/**
* Helper: Removes locale prefixes from route names that follow the pattern
* "locale___restOfTheRoute". For ex. "en___organizations" -> "organizations".
*/
function removeLocaleFromRouteName(routeName: string): string {
const LOCALES = ['en', 'fr', 'de'];
const parts = routeName.split('___');
if (parts.length > 1 && LOCALES.includes(parts[0])) {
return parts.slice(1).join('___');
}
return routeName;
}

export function isCurrentRoutePathSubpageOf(path: string, routeName: string) {
const baseName = removeLocaleFromRouteName(routeName);

// The first split is to remove the localization path.
const segments = routeName.split("___")[0].split(path + "-");
const segments = baseName.split(`${path}-`);
const subpage = segments.length > 1 ? segments[1] : "";

// Check that this subpage is valid and not one of the excluded routes
return subpage !== "search" && subpage !== "create" && subpage.length > 0;
}

export function currentRoutePathIncludes(
path: string,
routeName: string
): boolean {
return routeName.includes(path);
export function currentRoutePathIncludes(path: string, routeName: string): boolean {
const baseName = removeLocaleFromRouteName(routeName);
return baseName.includes(path);
}

0 comments on commit 6871265

Please sign in to comment.