File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change 1
1
import Container from 'react-bootstrap/Container' ;
2
2
import Navbar from 'react-bootstrap/Navbar' ;
3
+ import { useLocation , useNavigate } from 'react-router-dom' ; // Import hooks
3
4
import UserAvatarBox from "./user/userAvatarBox" ;
4
5
5
6
function NavigationBar ( ) {
7
+ const location = useLocation ( ) ; // Get the current location
8
+ const navigate = useNavigate ( ) ; // Function to navigate programmatically
9
+
10
+ const handleBrandClick = ( ) => {
11
+ if ( location . pathname === '/home' ) {
12
+ // Scroll to top if on the home page
13
+ window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
14
+ } else if ( location . pathname !== '/home' ) {
15
+ // Navigate to /home if not on the home page
16
+ navigate ( '/home' ) ;
17
+ }
18
+ // Do nothing if already on /home
19
+ } ;
6
20
7
21
return (
8
22
< Navbar className = 'bg-light' sticky = "top" >
9
23
< Container fluid className = "d-flex justify-content-between" style = { { paddingLeft : "32px" , paddingRight : "32px" } } >
10
24
< Navbar . Brand
11
- onClick = { ( ) => window . scrollTo ( { top : 0 , behavior : 'smooth' } ) } // Scroll to top of home page on click
12
- style = { { cursor : 'pointer' } }
25
+ onClick = { handleBrandClick } // Handle the click event
26
+ style = { { cursor : 'pointer' } }
13
27
>
14
28
PeerPrep
15
29
</ Navbar . Brand >
16
- < UserAvatarBox />
30
+ < UserAvatarBox />
17
31
</ Container >
18
32
</ Navbar >
19
33
) ;
You can’t perform that action at this time.
0 commit comments