11import { useState } from "react" ;
2- import { NavLink , Outlet , useLocation } from "react-router-dom" ;
2+ import { NavLink , Outlet , useLocation , useNavigate } from "react-router-dom" ;
33import { Header } from "./Header" ;
44import { Accordion } from "@radix-ui/react-accordion" ;
55import { sideMenu } from "@/config/menu" ;
@@ -15,6 +15,8 @@ import { useTranslation } from "react-i18next";
1515export function SideLayout ( ) {
1616 const { t } = useTranslation ( ) ;
1717 const location = useLocation ( ) ;
18+ const navigate = useNavigate ( ) ;
19+ const [ history , setHistory ] = useState ( new Map ( ) ) ;
1820 const [ accordionValue , setAccordionValue ] = useState ( ( ) => {
1921 return "item-" + sideMenu . findIndex ( item => item . items !== undefined ? item . items . filter ( subitem => subitem . to !== undefined ) . map ( subitem => subitem . to ) . includes ( location . pathname ) : false )
2022 } ) ;
@@ -44,12 +46,19 @@ export function SideLayout() {
4446 { sideMenu . filter ( ( item ) => ! item . hide || item . hide ( ) !== true ) . map ( ( item , index ) => (
4547 item . items !== undefined ? (
4648 < AccordionItem key = { index } value = { `item-${ index } ` } className = "border-b-0" >
47- < AccordionTrigger className = { cn (
48- buttonVariants ( { variant : "ghost" } ) ,
49- ( item . items . filter ( subitem => subitem . to !== undefined ) . map ( subitem => subitem . to ) )
50- . includes ( location . pathname ) ? 'bg-accent' : 'hover:bg-accent' ,
51- "justify-between hover:no-underline"
52- ) } >
49+ < AccordionTrigger
50+ className = { cn (
51+ buttonVariants ( { variant : "ghost" } ) ,
52+ ( item . items . filter ( subitem => subitem . to !== undefined ) . map ( subitem => subitem . to ) )
53+ . includes ( location . pathname ) ? 'bg-accent' : 'hover:bg-accent' ,
54+ "justify-between hover:no-underline"
55+ ) }
56+ onClick = { ( ) => {
57+ const target = history . get ( item . title ) || item . items ?. at ( 0 ) ?. to ;
58+ if ( target ) {
59+ navigate ( target ) ;
60+ }
61+ } } >
5362 < div className = "flex items-center" > { item . icon && < item . icon className = "mr-2" /> } { t ( item . title ) } </ div >
5463 </ AccordionTrigger >
5564 < AccordionContent className = "pb-2 pl-6" >
@@ -59,7 +68,13 @@ export function SideLayout() {
5968 < NavLink
6069 key = { subindex }
6170 to = { submenu . to }
62- onClick = { ( ) => setShowNav ( false ) }
71+ onClick = { ( ) => {
72+ setShowNav ( false ) ;
73+ if ( item . title && submenu . to ) {
74+ // Keep track of which submenus we navigated to
75+ setHistory ( map => new Map ( map . set ( item . title , submenu . to ) ) ) ;
76+ }
77+ } }
6378 className = { ( { isActive } ) => cn (
6479 buttonVariants ( { variant : "ghost" } ) ,
6580 isActive ? "bg-accent" : "hover:bg-accent" ,
0 commit comments