| 1 | import { Link } from "react-router-dom";
|
|---|
| 2 | import axiosInstance, { baseURL } from "../api/axiosInstance";
|
|---|
| 3 | import Logo from "../assets/logo-finkwave.png";
|
|---|
| 4 | import { useAuth } from "../context/authContext";
|
|---|
| 5 |
|
|---|
| 6 | const Nav = () => {
|
|---|
| 7 | const { user, setUser, isAuthLoading } = useAuth();
|
|---|
| 8 |
|
|---|
| 9 | const handleLogout = async (e: React.MouseEvent<HTMLButtonElement>) => {
|
|---|
| 10 | e.preventDefault();
|
|---|
| 11 | try {
|
|---|
| 12 | await axiosInstance.post("/auth/logout");
|
|---|
| 13 | setUser(undefined);
|
|---|
| 14 | } catch (error) {
|
|---|
| 15 | console.error("Logout failed:", error);
|
|---|
| 16 | }
|
|---|
| 17 | };
|
|---|
| 18 |
|
|---|
| 19 | return (
|
|---|
| 20 | <div className="bg-gray-800 p-4 flex justify-between items-center">
|
|---|
| 21 | <Link to="/" className="text-white text-lg font-semibold">
|
|---|
| 22 | <img src={Logo} alt="Finkwave Logo" className="h-12 w-auto" />
|
|---|
| 23 | </Link>
|
|---|
| 24 |
|
|---|
| 25 | <div className="flex items-center space-x-4">
|
|---|
| 26 | {!isAuthLoading && (
|
|---|
| 27 | <div className="flex items-center space-x-3">
|
|---|
| 28 | {user ? (
|
|---|
| 29 | <div className="flex items-center space-x-3">
|
|---|
| 30 | <div className="flex items-center space-x-2 bg-slate-700 rounded-lg px-3 py-2">
|
|---|
| 31 | {user.profilePhoto ? (
|
|---|
| 32 | <img
|
|---|
| 33 | src={`${baseURL}/${user.profilePhoto}`}
|
|---|
| 34 | alt={`${user.username}'s profile`}
|
|---|
| 35 | className="w-8 h-8 rounded-full object-cover"
|
|---|
| 36 | />
|
|---|
| 37 | ) : (
|
|---|
| 38 | <div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
|
|---|
| 39 | <span className="text-white text-sm font-semibold">
|
|---|
| 40 | {user.username.charAt(0).toUpperCase()}
|
|---|
| 41 | </span>
|
|---|
| 42 | </div>
|
|---|
| 43 | )}
|
|---|
| 44 |
|
|---|
| 45 | <div className="text-white">
|
|---|
| 46 | <p className="text-sm font-medium">{user.username}</p>
|
|---|
| 47 | <p className="text-xs text-gray-300 capitalize">
|
|---|
| 48 | {user.role}
|
|---|
| 49 | </p>
|
|---|
| 50 | </div>
|
|---|
| 51 | </div>
|
|---|
| 52 | <button
|
|---|
| 53 | onClick={handleLogout}
|
|---|
| 54 | className="bg-red-500 hover:bg-red-600 text-white px-4 py-2 rounded-lg text-sm
|
|---|
| 55 | font-medium transition-colors duration-200 flex items-center space-x-1 cursor-pointer"
|
|---|
| 56 | >
|
|---|
| 57 | Logout
|
|---|
| 58 | </button>
|
|---|
| 59 | </div>
|
|---|
| 60 | ) : (
|
|---|
| 61 | <div className="flex items-center space-x-4">
|
|---|
| 62 | <Link
|
|---|
| 63 | to="/login"
|
|---|
| 64 | className="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg text-sm
|
|---|
| 65 | font-medium transition-colors duration-200 cursor-pointer"
|
|---|
| 66 | >
|
|---|
| 67 | Login
|
|---|
| 68 | </Link>
|
|---|
| 69 | <Link
|
|---|
| 70 | to="/register"
|
|---|
| 71 | className="bg-green-500 hover:bg-green-600 text-white px-4 py-2 rounded-lg text-sm
|
|---|
| 72 | font-medium transition-colors duration-200 cursor-pointer"
|
|---|
| 73 | >
|
|---|
| 74 | Register
|
|---|
| 75 | </Link>
|
|---|
| 76 | </div>
|
|---|
| 77 | )}
|
|---|
| 78 | </div>
|
|---|
| 79 | )}
|
|---|
| 80 | </div>
|
|---|
| 81 | </div>
|
|---|
| 82 | );
|
|---|
| 83 | };
|
|---|
| 84 |
|
|---|
| 85 | export default Nav;
|
|---|