Skip to content

Commit 0fa9361

Browse files
author
Ajit Kumar
committed
refactor: streamline user menu toggle logic and remove redundant functions
1 parent 2547a45 commit 0fa9361

1 file changed

Lines changed: 32 additions & 36 deletions

File tree

src/components/sidebar/index.js

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,45 @@ function create($container, $toggler) {
114114
() => {},
115115
);
116116
} else {
117-
toggleUserMenu();
117+
const menu = userContextMenu.el;
118+
const isActive = menu.classList.toggle("active");
119+
120+
if (isActive) {
121+
const menuName = userContextMenu.el.querySelector(".user-menu-name");
122+
const menuEmail =
123+
userContextMenu.el.querySelector(".user-menu-email");
124+
125+
if (menuName) {
126+
menuName.content = (
127+
<div style={{ display: "flex" }}>
128+
{Boolean(userInfo.verified) && (
129+
<span className="icon verified"></span>
130+
)}
131+
{userInfo.name}
132+
{Boolean(userInfo.acode_pro) && (
133+
<span className="badge">Pro</span>
134+
)}
135+
</div>
136+
);
137+
}
138+
139+
if (menuEmail) {
140+
menuEmail.textContent = userInfo.email || "";
141+
}
142+
143+
setTimeout(() => {
144+
document.addEventListener("click", handleClickOutside);
145+
}, 10);
146+
} else {
147+
document.removeEventListener("click", handleClickOutside);
148+
}
118149
}
119150
} catch (error) {
120151
console.error("Error checking login status:", error);
121152
toast("Error checking login status", 3000);
122153
}
123154
}
124155

125-
function toggleUserMenu() {
126-
const menu = userContextMenu.el;
127-
const isActive = menu.classList.toggle("active");
128-
129-
if (isActive) {
130-
updateUserMenuInfo();
131-
132-
setTimeout(() => {
133-
document.addEventListener("click", handleClickOutside);
134-
}, 10);
135-
} else {
136-
document.removeEventListener("click", handleClickOutside);
137-
}
138-
}
139-
140156
function handleClickOutside(e) {
141157
if (
142158
!userContextMenu.el.contains(e.target) &&
@@ -148,26 +164,6 @@ function create($container, $toggler) {
148164
}
149165
}
150166

151-
async function updateUserMenuInfo() {
152-
try {
153-
const userInfo = await auth.getLoggedInUser();
154-
const menuName = userContextMenu.el.querySelector(".user-menu-name");
155-
const menuEmail = userContextMenu.el.querySelector(".user-menu-email");
156-
menuName.content = (
157-
<div style={{ display: "flex" }}>
158-
{Boolean(userInfo.verified) && (
159-
<span className="icon verified"></span>
160-
)}
161-
{userInfo.name}
162-
{Boolean(userInfo.acode_pro) && <span className="badge">Pro</span>}
163-
</div>
164-
);
165-
menuEmail.textContent = userInfo.email || "";
166-
} catch (error) {
167-
console.error("Error fetching user info:", error);
168-
}
169-
}
170-
171167
async function handleLogout() {
172168
try {
173169
const success = await auth.logout();

0 commit comments

Comments
 (0)