@@ -2,6 +2,7 @@ import { useMemo } from 'react';
2
2
import { useLocation } from 'react-router-dom' ;
3
3
4
4
import { NavItem } from './types' ;
5
+ import { addTrailingSlash } from '@/components/layout/dashboard/sideNav/navUtil' ;
5
6
import useCurrentPath from '@/hooks/useCurrentPath' ;
6
7
7
8
export type Options < T > = {
@@ -15,17 +16,19 @@ export const useItemSelectionStatus = <T extends object>({
15
16
const currentPath = useCurrentPath ( ) ; // /projects/:id/foo
16
17
17
18
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 ;
23
25
}
24
26
25
27
// 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 ) ;
28
30
}
31
+
29
32
return false ;
30
33
} , [ item . path , item . activePaths , currentLocation , currentPath ] ) ;
31
34
0 commit comments