Skip to content

Commit

Permalink
Merge pull request #1059 from CMU-17313Q/fixing-issue-regarding-routi…
Browse files Browse the repository at this point in the history
…ng-based-on-nonlocalized-routes

* Fixed route highlighting in the left sidebar for localized routes (e.g., French, German).

* fixing issue #963 through Locale Removal and using  more robust comparisons

* looked to refix issue #963 using adjustments to helper functions by refining path identification functions.

* made changes to normalizePath function within routeUtils.ts to better remove leading slashes

* created helper function for normalizePath in routeUtils.ts to improve readability

* adjusted isRouteActive to better handle conditions and routing in routeUtils.ts by implementing helper functions and locals for each language for issue #963

---------

Co-authored-by: “Aya-Abdel-Hamid” <[email protected]>
  • Loading branch information
andrewtavis and Aya-Abdel-Hamid authored Dec 11, 2024
2 parents 5af7294 + f532c75 commit 18fa43e
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions frontend/utils/routeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +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();

// Helper function to remove leading and trailing slashes
const normalizePath = (path: string) => path.replace(/^\/|\/$/g, '');

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 18fa43e

Please sign in to comment.