improve maintainability
This commit is contained in:
1 parent
4623aa8931
commit
cefe93b8a1
1 file changed
+19
-18
@@ -87,6 +87,13 @@ const URL_MAP = {
|
|||||||
changelog: "https://github.com/bluewave-labs/bluewave-uptime/releases",
|
changelog: "https://github.com/bluewave-labs/bluewave-uptime/releases",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const PATH_MAP = {
|
||||||
|
monitors: "Dashboard",
|
||||||
|
pagespeed: "Dashboard",
|
||||||
|
account: "Account",
|
||||||
|
settings: "Other",
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @component
|
* @component
|
||||||
* Sidebar component serves as a sidebar containing a menu.
|
* Sidebar component serves as a sidebar containing a menu.
|
||||||
@@ -134,15 +141,13 @@ function Sidebar() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
const matchedKey = Object.keys(PATH_MAP).find((key) =>
|
||||||
location.pathname.includes("monitors") ||
|
location.pathname.includes(key)
|
||||||
location.pathname.includes("pagespeed")
|
);
|
||||||
)
|
|
||||||
setOpen((prev) => ({ ...prev, Dashboard: true }));
|
if (matchedKey) {
|
||||||
else if (location.pathname.includes("/account"))
|
setOpen((prev) => ({ ...prev, [PATH_MAP[matchedKey]]: true }));
|
||||||
setOpen((prev) => ({ ...prev, Account: true }));
|
}
|
||||||
else if (location.pathname.includes("/settings"))
|
|
||||||
setOpen((prev) => ({ ...prev, Other: true }));
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -218,7 +223,9 @@ function Sidebar() {
|
|||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setOpen({ Dashboard: false, Account: false, Other: false });
|
setOpen(prev =>
|
||||||
|
Object.fromEntries(Object.keys(prev).map(key => [key, false]))
|
||||||
|
)
|
||||||
dispatch(toggleSidebar());
|
dispatch(toggleSidebar());
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -393,14 +400,8 @@ function Sidebar() {
|
|||||||
<ListItemButton
|
<ListItemButton
|
||||||
onClick={() =>
|
onClick={() =>
|
||||||
setOpen((prev) => ({
|
setOpen((prev) => ({
|
||||||
...Object.keys(prev).reduce(
|
...Object.fromEntries(Object.keys(prev).map(key => [key, false])),
|
||||||
(acc, key) => ({
|
[item.name]: !prev[item.name]
|
||||||
...acc,
|
|
||||||
[key]: false, // close all other sections
|
|
||||||
}),
|
|
||||||
{}
|
|
||||||
),
|
|
||||||
[`${item.name}`]: !prev[`${item.name}`], // toggle clicked section
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
sx={{
|
sx={{
|
||||||
|
|||||||
Reference in new issue
Block a user