Skip to content

Commit

Permalink
Clea up route utils - remove unused code/logs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtavis committed Dec 13, 2024
1 parent 6871265 commit cd7e3c9
Showing 1 changed file with 9 additions and 39 deletions.
48 changes: 9 additions & 39 deletions frontend/utils/routeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function normalizePath(path: string): string {

/**
* Remove the leading locale segment from the given route segments if present.
* For example, ['en', 'organizations'] -> ['organizations'].
*/
function removeLocaleSegment(segments: string[]): string[] {
if (segments.length > 0 && localCodes.includes(segments[0])) {
Expand All @@ -20,33 +19,6 @@ function removeLocaleSegment(segments: string[]): string[] {
return segments;
}

export function isRouteActive(routePath: string): boolean {
const route = useRoute();

const currentPath = normalizePath(route.path);
const targetPath = normalizePath(routePath);

let currentSegments = currentPath.split("/");
let targetSegments = targetPath.split("/");

// Remove locale segments from both current and target paths.
currentSegments = removeLocaleSegment(currentSegments);
targetSegments = removeLocaleSegment(targetSegments);

// If current route is shorter than the target, it cannot match.
if (currentSegments.length < targetSegments.length) {
return false;
}

// Check if the target segments match the end of the current segments.
return targetSegments.every(
(segment, index) =>
currentSegments[
currentSegments.length - targetSegments.length + index
] === segment
);
}

export function isTopLevelRouteActive(routePath: string): boolean {
const route = useRoute();

Expand All @@ -60,21 +32,16 @@ export function isTopLevelRouteActive(routePath: string): boolean {
currentSegments = removeLocaleSegment(currentSegments);
targetSegments = removeLocaleSegment(targetSegments);

console.log(`currentSegments: ${currentSegments}`);
console.log(`targetSegments: ${targetSegments}`);

return currentSegments[0] === targetSegments[0];
}

/**
* Helper: Removes locale prefixes from route names that follow the pattern
* "locale___restOfTheRoute". For ex. "en___organizations" -> "organizations".
* Removes locale prefixes from route names that follow the pattern "ISO2___restOfRoute".
*/
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('___');
const parts = routeName.split("___");
if (parts.length > 1 && localCodes.includes(parts[0])) {
return parts.slice(1).join("___");
}
return routeName;
}
Expand All @@ -86,11 +53,14 @@ export function isCurrentRoutePathSubpageOf(path: string, routeName: string) {
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
// 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 {
export function currentRoutePathIncludes(
path: string,
routeName: string
): boolean {
const baseName = removeLocaleFromRouteName(routeName);
return baseName.includes(path);
}

0 comments on commit cd7e3c9

Please sign in to comment.