Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Admin component to manage drawer state and handle outside clicks #42

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/admin/index.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { onAuthStateChanged } from "firebase/auth";
import React from "react";
import { Outlet } from "react-router-dom";
import { onAuthStateChanged } from "firebase/auth";
import { auth } from "../firebase";

import "./admin.css";

import { confirm, snackbar } from "mdui";
import { useNavigate } from "react-router-dom";
import Login from "./auth/Login";
Expand All @@ -13,10 +10,9 @@ import DrawerList from "./navigation/DrawerList";
export default function Admin() {
const [authUser, setAuthUser] = React.useState(false);
const [loading, setLoading] = React.useState(true);

const [open, setOpen] = React.useState(window.innerWidth > 840);

const navigate = useNavigate();
const drawerRef = React.useRef(null);

React.useEffect(() => {
const listen = onAuthStateChanged(auth, (user) => {
Expand All @@ -34,6 +30,22 @@ export default function Admin() {
};
}, []);

React.useEffect(() => {
const handleOutsideClick = (event) => {
if (drawerRef.current && !drawerRef.current.contains(event.target)) {
setOpen(false);
}
};

if (open && window.innerWidth < 840) {
document.addEventListener("mousedown", handleOutsideClick);
}

return () => {
document.removeEventListener("mousedown", handleOutsideClick);
};
}, [open]);
Comment on lines +33 to +47
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider improving the useEffect implementation.

While the click-outside detection works, there are several improvements that could make it more robust:

  1. The cleanup function should only remove the listener if it was actually added
  2. Window width changes should trigger a re-evaluation
  3. Add debouncing to prevent rapid event listener additions/removals

Here's a suggested implementation:

+ const [windowWidth, setWindowWidth] = React.useState(window.innerWidth);
+
+ React.useEffect(() => {
+   const handleResize = () => setWindowWidth(window.innerWidth);
+   window.addEventListener('resize', handleResize);
+   return () => window.removeEventListener('resize', handleResize);
+ }, []);

  React.useEffect(() => {
    const handleOutsideClick = (event) => {
      if (drawerRef.current && !drawerRef.current.contains(event.target)) {
        setOpen(false);
      }
    };

-   if (open && window.innerWidth < 840) {
+   if (open && windowWidth < 840) {
      document.addEventListener("mousedown", handleOutsideClick);
+     return () => document.removeEventListener("mousedown", handleOutsideClick);
    }
-
-   return () => {
-     document.removeEventListener("mousedown", handleOutsideClick);
-   };
-  }, [open]);
+  }, [open, windowWidth]);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
React.useEffect(() => {
const handleOutsideClick = (event) => {
if (drawerRef.current && !drawerRef.current.contains(event.target)) {
setOpen(false);
}
};
if (open && window.innerWidth < 840) {
document.addEventListener("mousedown", handleOutsideClick);
}
return () => {
document.removeEventListener("mousedown", handleOutsideClick);
};
}, [open]);
const [windowWidth, setWindowWidth] = React.useState(window.innerWidth);
React.useEffect(() => {
const handleResize = () => setWindowWidth(window.innerWidth);
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
React.useEffect(() => {
const handleOutsideClick = (event) => {
if (drawerRef.current && !drawerRef.current.contains(event.target)) {
setOpen(false);
}
};
if (open && windowWidth < 840) {
document.addEventListener("mousedown", handleOutsideClick);
return () => document.removeEventListener("mousedown", handleOutsideClick);
}
}, [open, windowWidth]);


if (loading) {
return <div />;
}
Expand All @@ -44,7 +56,11 @@ export default function Admin() {

return (
<mdui-layout style={{ width: "100vw", height: "100vh" }}>
{open && <DrawerList />}
{open && (
<div ref={drawerRef}>
<DrawerList />
</div>
)}
{open && window.innerWidth < 840 && (
<div
style={{
Expand Down
Loading