Skip to content

Commit

Permalink
adjusted isRouteActive to better handle conditions and routing in rou…
Browse files Browse the repository at this point in the history
…teUtils.ts by implementing helper functions and locals for each language for issue #963
  • Loading branch information
aadityanairaaq committed Dec 11, 2024
1 parent 1dd19c6 commit f532c75
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions frontend/utils/routeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@

// Define the supported locales. Adjust as needed to match your project.
const LOCALES = ['en', 'fr', 'de'];

/**
* Normalize a given path by removing leading and trailing slashes.
*/
function normalizePath(path: string): string {
return path.replace(/^\/|\/$/g, '');
}


/**
* 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 && LOCALES.includes(segments[0])) {
return segments.slice(1);
}
return segments;
}

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

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

// Handle prefixed routes by ignoring the base path
// Split paths into segments and compare relative paths
const currentSegments = currentPath.split('/');
const targetSegments = targetPath.split('/');
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 corresponding end of current segments
// Check if the target segments match the end of the current segments
return targetSegments.every(
(segment, index) =>
currentSegments[currentSegments.length - targetSegments.length + index] === segment
Expand Down

0 comments on commit f532c75

Please sign in to comment.