From f532c7584dd1f9d13ae64f0a7fb78ec863ad403f Mon Sep 17 00:00:00 2001 From: aaditya nair Date: Wed, 11 Dec 2024 21:55:15 +0300 Subject: [PATCH] adjusted isRouteActive to better handle conditions and routing in routeUtils.ts by implementing helper functions and locals for each language for issue #963 --- frontend/utils/routeUtils.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/frontend/utils/routeUtils.ts b/frontend/utils/routeUtils.ts index ea9befffe..7a9e14b90 100644 --- a/frontend/utils/routeUtils.ts +++ b/frontend/utils/routeUtils.ts @@ -1,4 +1,7 @@ +// 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. */ @@ -6,18 +9,37 @@ 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