Skip to content

Commit 080ccd5

Browse files
authored
Determine nav item selection using regex (#1131)
* Determine nav item selection using regex * Remove regex, simplify
1 parent 2ac98a7 commit 080ccd5

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/components/layout/dashboard/sideNav/navUtil.ts

+4
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ export const applySafePaths = <T>(
7272

7373
return config.map((child) => recur(child));
7474
};
75+
76+
export const addTrailingSlash = (path: string) => {
77+
return path.endsWith('/') ? path : path + '/';
78+
};

src/components/layout/dashboard/sideNav/useItemSelectionStatus.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMemo } from 'react';
22
import { useLocation } from 'react-router-dom';
33

44
import { NavItem } from './types';
5+
import { addTrailingSlash } from '@/components/layout/dashboard/sideNav/navUtil';
56
import useCurrentPath from '@/hooks/useCurrentPath';
67

78
export type Options<T> = {
@@ -15,17 +16,19 @@ export const useItemSelectionStatus = <T extends object>({
1516
const currentPath = useCurrentPath(); // /projects/:id/foo
1617

1718
const isSelected = useMemo(() => {
18-
if (!item.path && !item.activePaths) return false;
19-
// If current location starts with item path, then this nav item should be selected.
20-
// (e.g. item path is /enrollments/1/services and current location is /enrollments/1/services/2)
21-
if (item.path && currentLocation.startsWith(item.path)) {
22-
return true;
19+
if (item.path) {
20+
const matchesPath =
21+
currentLocation === item.path || // matches path exactly
22+
currentLocation.startsWith(addTrailingSlash(item.path)); // starts with path
23+
24+
if (matchesPath) return true;
2325
}
2426

2527
// If current route is listed in this item's activePaths, then this nav item should be selected.
26-
if (item.activePaths && item.activePaths.some((p) => p === currentPath)) {
27-
return true;
28+
if (item.activePaths) {
29+
return item.activePaths.some((p) => p === currentPath);
2830
}
31+
2932
return false;
3033
}, [item.path, item.activePaths, currentLocation, currentPath]);
3134

0 commit comments

Comments
 (0)