@@ -4,6 +4,8 @@ import { useAuth } from "../utils/auth";
44import { logout } from "../utils/firebase" ;
55import { Button } from "@/components/ui/button" ;
66import { Moon , Sun } from "lucide-react" ;
7+ import { db } from "../utils/firebase" ;
8+ import { collection , where , query , getDocs } from "firebase/firestore" ;
79import {
810 DropdownMenu ,
911 DropdownMenuContent ,
@@ -15,6 +17,7 @@ import {
1517import { Avatar , AvatarImage , AvatarFallback } from "@/components/ui/avatar" ;
1618
1719const Navbar : React . FC = ( ) => {
20+ const [ userInitials , setUserInitials ] = useState ( "U" ) ;
1821 const { currentUser } = useAuth ( ) ;
1922 const navigate = useNavigate ( ) ;
2023
@@ -37,71 +40,111 @@ const Navbar: React.FC = () => {
3740 }
3841 } , [ ] ) ;
3942
40- return (
41- < div className = "bg-background border-b" >
42- < div className = "container flex h-16 items-center justify-between py-4" >
43- < Link to = "/" className = "flex items-center space-x-2" >
44- < span className = "font-bold text-xl tracking-tight text-primary transition-opacity duration-200" >
45- Pure< span className = "text-foreground" > Path</ span >
46- </ span >
47- </ Link >
43+
44+ useEffect ( ( ) => {
45+ const fetchUserInitials = async ( ) => {
46+ if ( currentUser && currentUser . email ) {
47+ try {
48+ const usersRef = collection ( db , "users" ) ;
49+ const q = query ( usersRef , where ( "email" , "==" , currentUser . email ) ) ;
50+ const querySnapshot = await getDocs ( q ) ;
51+ console . log ( q ) ;
52+ if ( ! querySnapshot . empty ) {
53+ // Assuming only one document matches the email.
54+ const userDoc = querySnapshot . docs [ 0 ] . data ( ) ;
55+ const firstname = userDoc . firstName || "" ;
56+ const lastname = userDoc . lastName || "" ;
4857
49- < nav >
50- < Button
51- variant = "ghost"
52- size = "icon"
53- onClick = { toggleDarkMode }
54- className = "ml-4"
55- aria-label = "Toggle dark mode"
56- >
57- { isDarkMode ? (
58- < Sun className = "h-5 w-5 text-yellow-400" />
59- ) : (
60- < Moon className = "h-5 w-5" />
61- ) }
62- </ Button >
58+ if ( firstname || lastname ) {
59+ const initials =
60+ ( firstname . charAt ( 0 ) . toUpperCase ( ) || "" ) +
61+ ( lastname . charAt ( 0 ) . toUpperCase ( ) || "" ) ;
62+ setUserInitials ( initials ) ;
63+ } else {
64+ setUserInitials ( "U" ) ;
65+ }
66+ } else {
67+ setUserInitials ( "U" ) ; // No document found for this email.
68+ }
69+ } catch ( error ) {
70+ console . error ( "Error fetching user initials:" , error ) ;
71+ setUserInitials ( "U" ) ; // Fallback in case of an error.
72+ }
73+ }
74+ } ;
6375
64- { currentUser ? (
65- < DropdownMenu >
66- < DropdownMenuTrigger asChild >
67- < Button
68- variant = "ghost"
69- className = "relative h-8 w-8 rounded-full"
70- >
71- < Avatar >
72- < AvatarImage src = "/placeholder.svg" alt = "User" />
73- < AvatarFallback >
74- { currentUser . email ?. charAt ( 0 ) . toUpperCase ( ) }
75- </ AvatarFallback >
76- </ Avatar >
77- </ Button >
78- </ DropdownMenuTrigger >
79- < DropdownMenuContent align = "end" >
80- < DropdownMenuLabel > My Account</ DropdownMenuLabel >
81- < DropdownMenuSeparator />
82- < DropdownMenuItem asChild >
83- < Link to = "/dashboard" > Dashboard</ Link >
84- </ DropdownMenuItem >
85- < DropdownMenuItem asChild >
86- < Link to = "/profile" > Profile</ Link >
87- </ DropdownMenuItem >
88- < DropdownMenuSeparator />
89- < DropdownMenuItem onClick = { handleLogout } >
90- Logout
91- </ DropdownMenuItem >
92- </ DropdownMenuContent >
93- </ DropdownMenu >
76+ fetchUserInitials ( ) ;
77+ } , [ currentUser ] ) ;
78+
79+
80+ return ( < div className = "bg-background border-b" >
81+ < div className = "container flex h-16 items-center justify-between py-4" >
82+ < Link to = "/" className = "flex items-center space-x-2" >
83+ < span className = "font-bold text-xl tracking-tight text-primary transition-opacity duration-200" >
84+ Pure< span className = "text-foreground" > Path</ span >
85+ </ span >
86+ </ Link >
87+
88+ < nav className = "flex items-center space-x-4" >
89+ { /* Dark Mode Button */ }
90+ < Button
91+ variant = "ghost"
92+ size = "icon"
93+ onClick = { toggleDarkMode }
94+ className = "ml-auto p-2 border rounded-md shadow-sm hover:bg-secondary transition-all"
95+ aria-label = "Toggle dark mode"
96+ >
97+ { isDarkMode ? (
98+ < Sun className = "h-5 w-5 text-yellow-400" />
9499 ) : (
95- < div >
96- < Link to = "/login" className = "mr-4" >
97- Login
98- </ Link >
99- < Link to = "/register" > Register</ Link >
100- </ div >
100+ < Moon className = "h-5 w-5" />
101101 ) }
102- </ nav >
103- </ div >
102+ </ Button >
103+
104+ { /* Dropdown Menu */ }
105+ { currentUser ? (
106+ < DropdownMenu >
107+ < DropdownMenuTrigger asChild >
108+ < Button
109+ variant = "ghost"
110+ size = "icon"
111+ className = "relative h-8 w-8 rounded-full ml-4"
112+ >
113+ < Avatar >
114+ < AvatarFallback > { userInitials } </ AvatarFallback >
115+ </ Avatar >
116+ </ Button >
117+ </ DropdownMenuTrigger >
118+ { /* Dropdown Menu Content */ }
119+ < DropdownMenuContent
120+ align = "end"
121+ className = "mt-2 w-56 animate-scale-in shadow-lg border rounded-md bg-background"
122+ >
123+ < DropdownMenuLabel > My Account</ DropdownMenuLabel >
124+ < DropdownMenuSeparator />
125+ < DropdownMenuItem asChild >
126+ < Link to = "/dashboard" > Dashboard</ Link >
127+ </ DropdownMenuItem >
128+ < DropdownMenuItem asChild >
129+ < Link to = "/profile" > Profile</ Link >
130+ </ DropdownMenuItem >
131+ < DropdownMenuSeparator />
132+ < DropdownMenuItem onClick = { handleLogout } >
133+ Logout
134+ </ DropdownMenuItem >
135+ </ DropdownMenuContent >
136+ </ DropdownMenu >
137+ ) : (
138+ < div className = "flex items-center space-x-4" >
139+ < Link to = "/login" className = "mr-4" >
140+ Login
141+ </ Link >
142+ < Link to = "/register" > Register</ Link >
143+ </ div >
144+ ) }
145+ </ nav >
104146 </ div >
147+ </ div >
105148 ) ;
106149} ;
107150
0 commit comments