11import { Box , Flex , Icon , Text } from "@chakra-ui/react"
22import { useQueryClient } from "@tanstack/react-query"
3- import { Link as RouterLink } from "@tanstack/react-router"
4- import { FiBriefcase , FiHome , FiSettings , FiUsers } from "react-icons/fi"
3+ import { Link as RouterLink , useRouter } from "@tanstack/react-router"
4+ import { FiBriefcase , FiHome , FiSettings , FiUsers , FiChevronDown , FiChevronRight } from "react-icons/fi"
55import type { IconType } from "react-icons/lib"
6+ import React , { useState } from "react"
67
78import type { UserPublic } from "@/client"
89
@@ -25,31 +26,89 @@ interface Item {
2526 path : string
2627}
2728
29+ const trafficAnalysisSubItems = [
30+ { title : "上客点密度分析" , tab : "pickup-density" } ,
31+ { title : "车辆轨迹可视化" , tab : "vehicle-trajectory" } ,
32+ { title : "统计数据" , tab : "statistics" } ,
33+ ]
34+
2835const SidebarItems = ( { onClose } : SidebarItemsProps ) => {
2936 const queryClient = useQueryClient ( )
3037 const currentUser = queryClient . getQueryData < UserPublic > ( [ "currentUser" ] )
38+ const router = useRouter ( )
39+ const currentPath = router . state . location . pathname
40+ const currentTab = router . state . location . search ?. tab
41+ const [ expandedMenu , setExpandedMenu ] = useState < string | null > ( null )
3142
3243 const finalItems : Item [ ] = currentUser ?. is_superuser
3344 ? [ ...items , { icon : FiUsers , title : "Admin" , path : "/admin" } ]
3445 : items
3546
36- const listItems = finalItems . map ( ( { icon, title, path } ) => (
37- < RouterLink key = { title } to = { path } onClick = { onClose } >
38- < Flex
39- gap = { 4 }
40- px = { 4 }
41- py = { 2 }
42- _hover = { {
43- background : "gray.subtle" ,
44- } }
45- alignItems = "center"
46- fontSize = "sm"
47- >
48- < Icon as = { icon } alignSelf = "center" />
49- < Text ml = { 2 } > { title } </ Text >
50- </ Flex >
51- </ RouterLink >
52- ) )
47+ const handleExpand = ( title : string ) => {
48+ setExpandedMenu ( prev => ( prev === title ? null : title ) )
49+ }
50+
51+ const listItems = finalItems . map ( ( { icon, title, path } ) => {
52+ if ( title === "交通数据分析" ) {
53+ return (
54+ < Box key = { title } >
55+ < Flex
56+ gap = { 4 }
57+ px = { 4 }
58+ py = { 2 }
59+ alignItems = "center"
60+ fontSize = "sm"
61+ cursor = "pointer"
62+ _hover = { { background : "gray.subtle" } }
63+ onClick = { ( ) => handleExpand ( title ) }
64+ >
65+ < Icon as = { icon } alignSelf = "center" />
66+ < Text ml = { 2 } userSelect = "none" > { title } </ Text >
67+ < Icon as = { expandedMenu === title ? FiChevronDown : FiChevronRight } ml = "auto" />
68+ </ Flex >
69+ { expandedMenu === title && (
70+ < Box pl = { 12 } mt = { 1 } >
71+ { trafficAnalysisSubItems . map ( ( sub , idx ) => (
72+ < RouterLink
73+ key = { sub . tab }
74+ to = { `/traffic-analysis?tab=${ sub . tab } ` }
75+ onClick = { onClose }
76+ style = { { display : "block" } }
77+ >
78+ < Text
79+ py = { 1 }
80+ mb = { idx !== trafficAnalysisSubItems . length - 1 ? 2 : 0 }
81+ color = { currentTab === sub . tab ? "blue.500" : "gray.700" }
82+ fontWeight = { currentTab === sub . tab ? "bold" : "normal" }
83+ fontSize = "sm"
84+ _hover = { { color : "blue.600" } }
85+ userSelect = "none"
86+ >
87+ { sub . title }
88+ </ Text >
89+ </ RouterLink >
90+ ) ) }
91+ </ Box >
92+ ) }
93+ </ Box >
94+ )
95+ }
96+ return (
97+ < RouterLink key = { title } to = { path } onClick = { onClose } >
98+ < Flex
99+ gap = { 4 }
100+ px = { 4 }
101+ py = { 2 }
102+ _hover = { { background : "gray.subtle" } }
103+ alignItems = "center"
104+ fontSize = "sm"
105+ >
106+ < Icon as = { icon } alignSelf = "center" />
107+ < Text ml = { 2 } > { title } </ Text >
108+ </ Flex >
109+ </ RouterLink >
110+ )
111+ } )
53112
54113 return (
55114 < >
0 commit comments