|
1 | 1 | import { useEffect, useRef, useState } from 'react'
|
2 |
| -import { Link } from 'react-router-dom' |
| 2 | +import { Link, matchPath, useLocation } from 'react-router-dom' |
3 | 3 | import {
|
4 | 4 | AppBar,
|
5 | 5 | Toolbar,
|
@@ -91,53 +91,60 @@ const NavBar = () => {
|
91 | 91 | </>
|
92 | 92 | )
|
93 | 93 | }
|
| 94 | +const NavItemButton = ({ children, to, path, current, icon }) => ( |
| 95 | + <Button |
| 96 | + component={Link} |
| 97 | + to={to} |
| 98 | + size="small" |
| 99 | + variant="text" |
| 100 | + startIcon={icon} |
| 101 | + sx={{ |
| 102 | + borderBottom: '2px solid', |
| 103 | + borderBottomLeftRadius: '0', |
| 104 | + borderBottomRightRadius: '0', |
| 105 | + borderBottomColor: matchPath({ path: path }, current) ? 'primary.main' : 'transparent', |
| 106 | + }} |
| 107 | + > |
| 108 | + {children} |
| 109 | + </Button> |
| 110 | +) |
94 | 111 |
|
95 | 112 | const NavItems = ({ user, t, languages, handleLanguageChange, language }) => {
|
96 | 113 | const anchorRef = useRef<HTMLButtonElement>(null)
|
| 114 | + const { pathname } = useLocation() |
97 | 115 | const [openLanguageSelect, setOpenLanguageSelect] = useState(false)
|
| 116 | + |
98 | 117 | return (
|
99 | 118 | <>
|
100 | 119 | {user?.preferences?.chatVersion !== 2 && (
|
101 |
| - <Link to="/v2" style={{ textDecoration: 'none' }}> |
102 |
| - <Button> |
103 |
| - <GradeOutlined sx={styles.icon} /> {t('tryNew')} |
104 |
| - </Button> |
105 |
| - </Link> |
| 120 | + <NavItemButton to="/v2" path="v2/*" current={pathname} icon={<GradeOutlined sx={styles.icon} />}> |
| 121 | + {t('tryNew')} |
| 122 | + </NavItemButton> |
106 | 123 | )}
|
107 | 124 | {user?.preferences?.chatVersion !== 1 && (
|
108 |
| - <Link to="/v1" style={{ textDecoration: 'none' }}> |
109 |
| - <Button> |
110 |
| - <GradeOutlined sx={styles.icon} /> {t('useOld')} |
111 |
| - </Button> |
112 |
| - </Link> |
| 125 | + <NavItemButton to="/v1" path="v1/*" current={pathname} icon={<GradeOutlined sx={styles.icon} />}> |
| 126 | + {t('useOld')} |
| 127 | + </NavItemButton> |
113 | 128 | )}
|
114 | 129 | {user.enrolledCourses.length > 0 && (
|
115 |
| - <Link to="/chats" style={{ textDecoration: 'none' }}> |
116 |
| - <Button> |
117 |
| - <BookmarksOutlined sx={styles.icon} /> {t('chats')} |
118 |
| - </Button> |
119 |
| - </Link> |
| 130 | + <NavItemButton to="/chats" path="chats/*" current={pathname} icon={<BookmarksOutlined sx={styles.icon} />}> |
| 131 | + {t('chats')} |
| 132 | + </NavItemButton> |
120 | 133 | )}
|
121 | 134 | {user.ownCourses.length > 0 && (
|
122 |
| - <Link to="/courses" style={{ textDecoration: 'none' }}> |
123 |
| - <Button> |
124 |
| - <BookmarksOutlined sx={styles.icon} /> {t('courses')} |
125 |
| - </Button> |
126 |
| - </Link> |
| 135 | + <NavItemButton to="/courses" path="courses/*" current={pathname} icon={<BookmarksOutlined sx={styles.icon} />}> |
| 136 | + {t('courses')} |
| 137 | + </NavItemButton> |
127 | 138 | )}
|
128 | 139 | {user.isStatsViewer && (
|
129 |
| - <Link to="/statistics" style={{ textDecoration: 'none' }}> |
130 |
| - <Button> |
131 |
| - <AdminPanelSettingsOutlined sx={styles.icon} /> {t('courseStats')} |
132 |
| - </Button> |
133 |
| - </Link> |
| 140 | + <NavItemButton to="/statistics" path="statistics/*" current={pathname} icon={<AdminPanelSettingsOutlined sx={styles.icon} />}> |
| 141 | + {t('courseStats')} |
| 142 | + </NavItemButton> |
134 | 143 | )}
|
135 | 144 | {user.isAdmin && (
|
136 |
| - <Link to="/admin" style={{ textDecoration: 'none' }}> |
137 |
| - <Button> |
138 |
| - <AdminPanelSettingsOutlined sx={styles.icon} /> {t('admin')} |
139 |
| - </Button> |
140 |
| - </Link> |
| 145 | + <NavItemButton to="/admin" path="admin/*" current={pathname} icon={<AdminPanelSettingsOutlined sx={styles.icon} />}> |
| 146 | + {t('admin')} |
| 147 | + </NavItemButton> |
141 | 148 | )}
|
142 | 149 | <Button
|
143 | 150 | ref={anchorRef}
|
|
0 commit comments