File size: 5,348 Bytes
cdf301a
 
 
 
 
 
 
 
b2a31fd
cdf301a
 
 
 
 
 
 
 
 
 
 
b2a31fd
 
 
 
 
 
 
cdf301a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a31fd
 
 
 
 
cdf301a
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"use client";
import { usePathname } from "next/navigation";
import { Bars3Icon, Cog6ToothIcon, ArrowRightStartOnRectangleIcon, MagnifyingGlassIcon, ChatBubbleOvalLeftEllipsisIcon } from "@heroicons/react/20/solid";
import Link from "next/link";
import { useEffect, useState, useRef } from "react";
import { useRouter } from "next/navigation";
import Image from "next/image";
import logo from "@assets/logo.jpg";
import NexusAuthApi from "@/lib/Nexus_Auth_API";

export default function ChatSidebar() {
  const pathname = usePathname();
  const [isCollapsed, setIsCollapsed] = useState(true);
  const sidebarRef = useRef(null);
  const router = useRouter();

  const toggleSidebar = () => {
    setIsCollapsed(!isCollapsed);
  };

  const clearLocalStorage = () => {
    localStorage.removeItem("me");
    localStorage.removeItem("s_tkn");
    localStorage.removeItem("u_id");
    localStorage.removeItem("a_l");
  };

  useEffect(() => {
    const handleClickOutside = (event) => {
      if (sidebarRef.current && !sidebarRef.current.contains(event.target)) {
        setIsCollapsed(true);
      }
    };

    document.addEventListener("mousedown", handleClickOutside);
    return () => document.removeEventListener("mousedown", handleClickOutside);
  }, []);

  const logoStyle = {
    width: isCollapsed ? "60px" : "150px",
    borderRadius: "50px",
    transition: "width 0.3s ease, height 0.3s ease",
    marginBottom: 0,
    marginTop: "35px",
  };

  const sidebarStyle = {
    width: "180px",
    left: isCollapsed ? "-180px" : "0",
    backgroundColor: "var(--sidebar-background)",
    color: "var(--foreground)",
    padding: isCollapsed ? "15px" : "20px",
    position: "fixed",
    height: "100vh",
    display: "flex",
    flexDirection: "column",
    borderRight: "1px solid rgba(255, 255, 255, 0.1)",
    transition: "left 0.3s ease, width 0.3s ease, padding 0.3s ease",
    zIndex: 10,
  };

  const navLinkStyle = {
    display: "flex",
    alignItems: "center",
    gap: "5px",
    padding: "10px 5px",
    transition: "all 0.3s ease",
    color: "var(--foreground)",
    fontSize: "1em",
  };

  const activeNavLinkStyle = {
    backgroundColor: "var(--hover-accent)",
    borderRadius: "5px",
    animation: "fadeIn .5s ease-in-out, pulse .5s ease-in-out",
  };

  const textStyle = {
    opacity: isCollapsed ? 0 : 1,
    transition: "opacity 0.3s ease",
    pointerEvents: isCollapsed ? "none" : "auto",
  };

  const iconStyle = {
    width: "30px",
    height: "30px",
    flexShrink: 0,
  };

  const toggleButtonStyle = {
    position: "absolute",
    top: "20px",
    right: isCollapsed ? "-50px" : "10px",
    width: "35px",
    height: "35px",
    backgroundColor: "var(--button-background)",
    color: "var(--foreground)",
    border: "none",
    borderRadius: "10px",
    cursor: "pointer",
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    boxShadow: "0 2px 5px rgba(0, 0, 0, 0.2)",
    transition: "background-color 0.3s ease, right 0.3s ease",
  };

  const handleLogout = () => {
    const userID = localStorage.getItem('u_id');
    const token = localStorage.getItem('s_tkn');
    NexusAuthApi.logout(userID, token)
      .then(() => {
        clearLocalStorage();
        router.push('/');
        window.location.reload();
      })
      .catch((error) => {
        console.error("Logout failed", error);
      });
  };

  return (
    <div ref={sidebarRef} className="sidebar-container" style={sidebarStyle}>
      <div className="logo-container" style={{ display: "flex", flexDirection: "column", alignItems: "center" }}>
        <Image style={logoStyle} src={logo} alt="Chat App Logo" />
        <button
          style={toggleButtonStyle}
          onClick={() => toggleSidebar(!isCollapsed)}
        >
          <Bars3Icon style={{ transform: `rotate(${isCollapsed ? 0 : 90}deg)`, transition: "transform .3s", width: "20px" }} />
        </button>
      </div>
      <ul className="nav-links" style={{ listStyleType: "none", marginTop: "20px", paddingLeft: "0" }}>
        <li>
          <Link href="/" legacyBehavior>
            <a style={{ ...navLinkStyle, ...(pathname === "/" && activeNavLinkStyle) }}>
              <ChatBubbleOvalLeftEllipsisIcon style={iconStyle} />
              <span style={textStyle}>Chats</span>
            </a>
          </Link>
        </li>
        <li>
          <Link href="/search" legacyBehavior>
            <a style={{ ...navLinkStyle, ...(pathname === "/search" && activeNavLinkStyle) }}>
              <MagnifyingGlassIcon style={iconStyle} />
              <span style={textStyle}>Find</span>
            </a>
          </Link>
        </li>
        <li>
          <Link href="/settings" legacyBehavior>
            <a style={{ ...navLinkStyle, ...(pathname === "/settings" && activeNavLinkStyle) }}>
              <Cog6ToothIcon style={iconStyle} />
              <span style={textStyle}>Settings</span>
            </a>
          </Link>
        </li>
        <li>
          <Link href="#logout" legacyBehavior>
            <a style={{ ...navLinkStyle, ...(pathname === "#logout" && activeNavLinkStyle) }} onClick={handleLogout}>
              <ArrowRightStartOnRectangleIcon style={iconStyle} />
              <span style={textStyle}>Logout</span>
            </a>
          </Link>
        </li>
      </ul>
    </div>
  );
}