Skip to content

Commit

Permalink
looked to refix issue activist-org#963 using adjustments to helper fu…
Browse files Browse the repository at this point in the history
…nctions by refining path identification functions.
  • Loading branch information
aadityanairaaq committed Dec 11, 2024
1 parent 8fb5c10 commit c438d80
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions frontend/utils/routeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// A helper array for supported locales. Adjust as needed.
// Define the supported locales. Adjust as needed to match your project.
const LOCALES = ['en', 'fr', 'de'];

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

/**
* If the first segment of the given segments is a known locale, remove it.
* 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])) {
Expand All @@ -19,8 +20,8 @@ function removeLocaleSegment(segments: string[]): string[] {
}

/**
* Remove locale prefix from a routeName if it follows the pattern like `en___something`.
* If there's no locale segment, this function returns the routeName unchanged.
* Remove locale prefixes from route names that follow the pattern "locale___restOfTheRoute".
* For example, "en___organizations" -> "organizations".
*/
function removeLocaleFromRouteName(routeName: string): string {
const parts = routeName.split('___');
Expand All @@ -43,30 +44,32 @@ export function isRouteActive(routePath: string): boolean {
currentSegments = removeLocaleSegment(currentSegments);
targetSegments = removeLocaleSegment(targetSegments);

// Check if the target segments match the corresponding ending segments of the current path
// 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 isCurrentRoutePathSubpageOf(path: string, routeName: string): boolean {
// Remove locale info from the routeName first
const baseName = removeLocaleFromRouteName(routeName);

// After removing locale, we expect something like "projects-subpage"
// Splitting by `path + '-'` should isolate the subpage if it exists
// Split the baseName on `path + '-'` to find a subpage if one exists
// For example, if path = 'organizations' and routeName = 'organizations-subpage',
// splitting on 'organizations-' gives ['', 'subpage'].
const segments = baseName.split(`${path}-`);
const subpage = segments.length > 1 ? segments[1] : "";

// Check that the subpage is neither "search" nor "create" and that it has some length
// 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 {
// Remove locale info from the routeName
const baseName = removeLocaleFromRouteName(routeName);

// Check if the cleaned route name includes the given path
return baseName.includes(path);
}

0 comments on commit c438d80

Please sign in to comment.