refactor: extract sidebar width logic into useSidebar hook

Create shared useSidebar hook to centralize sidebar width and transition
logic, addressing code review feedback about duplicated logic between
Sidebar and HomeLayout components.

Changes:
- Add useSidebar hook with collapsed state, width, and transition values
- Update Sidebar to use the hook instead of inline selectors and values
- Update HomeLayout to use the hook for spacer element styling

This ensures sidebar width logic is defined in one place and stays in sync.
This commit is contained in:
gorkem-bwl committed 2026-01-18 14:11:05 -05:00
1 parent a4cf53ee15
commit 487f6b222d
3 files changed
+37 -15

No files matched your search

@@ -1,12 +1,12 @@
import Sidebar from "../../Sidebar/index.jsx"; import Sidebar from "../../Sidebar/index.jsx";
import { Outlet } from "react-router"; import { Outlet } from "react-router";
import { Box, Stack } from "@mui/material"; import { Box, Stack } from "@mui/material";
import { useSelector } from "react-redux"; import { useSidebar } from "@/Hooks/useSidebar.js";
import "./index.css"; import "./index.css";
const HomeLayout = () => { const HomeLayout = () => {
const collapsed = useSelector((state) => state.ui.sidebar?.collapsed ?? false); const { width, transition } = useSidebar();
return ( return (
<Stack <Stack
@@ -17,11 +17,9 @@ const HomeLayout = () => {
{/* Spacer for fixed sidebar */} {/* Spacer for fixed sidebar */}
<Box <Box
sx={{ sx={{
width: collapsed width,
? "var(--env-var-side-bar-collapsed-width)"
: "var(--env-var-side-bar-width)",
flexShrink: 0, flexShrink: 0,
transition: "width 650ms cubic-bezier(0.36, -0.01, 0, 0.77)", transition,
}} }}
/> />
<Stack className="home-content-wrapper"> <Stack className="home-content-wrapper">
+4 -9
View File
@@ -12,9 +12,9 @@ import Icon from "../Icon";
// Utils // Utils
import { useTheme } from "@mui/material/styles"; import { useTheme } from "@mui/material/styles";
import { useSelector } from "react-redux";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router"; import { useNavigate } from "react-router";
import { useSidebar } from "@/Hooks/useSidebar.js";
const URL_MAP = { const URL_MAP = {
support: "https://discord.com/invite/NAb6H3UTjK", support: "https://discord.com/invite/NAb6H3UTjK",
@@ -65,8 +65,7 @@ const Sidebar = () => {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation(); const { t } = useTranslation();
const navigate = useNavigate(); const navigate = useNavigate();
// Redux state const { collapsed, width, transition } = useSidebar();
const collapsed = useSelector((state) => state.ui.sidebar?.collapsed ?? false);
const menu = getMenu(t); const menu = getMenu(t);
const otherMenuItems = getOtherMenuItems(t); const otherMenuItems = getOtherMenuItems(t);
@@ -75,11 +74,7 @@ const Sidebar = () => {
return ( return (
<Stack <Stack
height="100vh" height="100vh"
width={ width={width}
collapsed
? "var(--env-var-side-bar-collapsed-width)"
: "var(--env-var-side-bar-width)"
}
component="aside" component="aside"
position="fixed" position="fixed"
top={0} top={0}
@@ -88,7 +83,7 @@ const Sidebar = () => {
paddingBottom={theme.spacing(6)} paddingBottom={theme.spacing(6)}
gap={theme.spacing(6)} gap={theme.spacing(6)}
sx={{ sx={{
transition: "width 650ms cubic-bezier(0.36, -0.01, 0, 0.77)", transition,
backgroundColor: "#000000", backgroundColor: "#000000",
borderRight: "1px solid #344054", borderRight: "1px solid #344054",
zIndex: 1000, zIndex: 1000,
+29
View File
@@ -0,0 +1,29 @@
import { useSelector } from "react-redux";
// CSS variable names for sidebar widths
const SIDEBAR_WIDTH_VAR = "var(--env-var-side-bar-width)";
const SIDEBAR_COLLAPSED_WIDTH_VAR = "var(--env-var-side-bar-collapsed-width)";
// Transition timing for sidebar width changes
const SIDEBAR_TRANSITION = "width 650ms cubic-bezier(0.36, -0.01, 0, 0.77)";
/**
* Hook to get sidebar state and computed width
* Centralizes sidebar width logic to avoid duplication between Sidebar and HomeLayout
*
* @returns {Object} Sidebar state and styles
* @returns {boolean} collapsed - Whether the sidebar is collapsed
* @returns {string} width - CSS width value based on collapsed state
* @returns {string} transition - CSS transition for width changes
*/
export const useSidebar = () => {
const collapsed = useSelector((state) => state.ui.sidebar?.collapsed ?? false);
return {
collapsed,
width: collapsed ? SIDEBAR_COLLAPSED_WIDTH_VAR : SIDEBAR_WIDTH_VAR,
transition: SIDEBAR_TRANSITION,
};
};
export default useSidebar;