@@ -7,22 +7,29 @@ import { useEffect } from 'react';
7
7
export interface ISectionProps {
8
8
title : string ;
9
9
link : string ;
10
- hasActiveLink : boolean ;
11
10
}
12
11
13
12
export const Section : React . FunctionComponent < React . PropsWithChildren < ISectionProps > > = ( {
14
13
title,
15
14
link,
16
- hasActiveLink,
17
15
children
18
16
} : React . PropsWithChildren < ISectionProps > ) => {
19
17
const router = useRouter ( ) ;
20
18
const [ isActive , setIsActive ] = React . useState ( false ) ;
21
19
const [ showChildren , setShowChildren ] = React . useState < boolean | undefined > ( undefined ) ;
20
+ const [ hasActiveLink , setHasActiveLink ] = React . useState < boolean | undefined > ( undefined ) ;
22
21
23
22
useEffect ( ( ) => {
24
23
const page = router . asPath ;
25
- setIsActive ( page === link || link === `${ page } /` || page . includes ( `${ link } #` ) || page . includes ( `${ link } index` ) ) ;
24
+ console . log ( page , link ) ;
25
+ // Remove the last slash
26
+ const crntLink = link . endsWith ( '/' ) ? link . slice ( 0 , - 1 ) : link ;
27
+ console . log ( page , crntLink ) ;
28
+ setIsActive ( page === crntLink || crntLink === `${ page } /` || page . includes ( `${ crntLink } #` ) || page . includes ( `${ crntLink } index` ) ) ;
29
+
30
+ if ( crntLink . split ( '/' ) . length > 2 ) {
31
+ setHasActiveLink ( page . includes ( crntLink ) )
32
+ }
26
33
} , [ router . asPath , link ] ) ;
27
34
28
35
return (
@@ -40,7 +47,11 @@ export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionPr
40
47
title = { `Show children of ${ title . toLowerCase ( ) } ` }
41
48
onClick = { ( ) => setShowChildren ( prev => ! prev ) } >
42
49
{
43
- ( ( isActive && typeof showChildren === "undefined" ) || ( hasActiveLink && typeof showChildren === "undefined" ) || showChildren ) ? (
50
+ (
51
+ ( isActive && typeof showChildren === "undefined" ) ||
52
+ ( hasActiveLink && typeof showChildren === "undefined" ) ||
53
+ showChildren
54
+ ) ? (
44
55
< ChevronDownIcon className = 'h-4' />
45
56
) : (
46
57
< ChevronRightIcon className = 'h-4' />
@@ -52,8 +63,8 @@ export const Section: React.FunctionComponent<React.PropsWithChildren<ISectionPr
52
63
{
53
64
(
54
65
( isActive && typeof showChildren === "undefined" ) ||
55
- ( showChildren && typeof showChildren ! == "undefined" ) ||
56
- ( hasActiveLink && typeof showChildren = == "undefined" )
66
+ ( hasActiveLink && typeof showChildren = == "undefined" ) ||
67
+ ( showChildren && typeof showChildren ! == "undefined" )
57
68
) && children
58
69
}
59
70
</ >
0 commit comments