Files
Web-Frontend/client/src/components/layout/Sidebar/Sidebar.jsx
T

113 lines
3.6 KiB
React
Raw Normal View History

2025-07-28 15:10:57 -07:00
import { useSlotRecipe, Box, Button, IconButton } from "@chakra-ui/react";
2025-07-31 08:56:32 -07:00
import { ColorModeButton, useColorMode } from "@/components/ui/color-mode";
2025-07-29 11:08:36 -07:00
// import { Tooltip } from "@/components/ui/tooltip";
import Tooltip from "../Canvas/components/Tooltip.jsx";
import { FaVolcano } from "react-icons/fa6";
2025-07-28 13:42:12 -07:00
import { FaHome } from "react-icons/fa";
2025-07-28 15:10:57 -07:00
import { FaGlobeAmericas } from "react-icons/fa";
import { FaMapMarkerAlt } from "react-icons/fa";
import { HiUsers } from "react-icons/hi2";
import { VscThreeBars } from "react-icons/vsc";
import { useState, useEffect } from "react";
import { Seismic } from "../Canvas/views/DashboardCanvasNav/Seismic";
import { DASHBOARD_VIEWS } from "@/constants/viewKeys";
2025-07-31 08:56:32 -07:00
export default function Sidebar({ view, onChangeView, darkMode, setDarkMode }) {
const [isOpen, setIsOpen] = useState(true);
const [activeButton, setActiveButton] = useState(0)
2025-07-29 11:08:36 -07:00
const recipe = useSlotRecipe({ key: "sidebar" });
const styles = recipe();
2025-07-31 08:56:32 -07:00
const { toggleColorMode } = useColorMode();
2025-07-17 09:45:36 -07:00
2025-07-23 13:44:24 -07:00
// this list of btns will come from the db (not every usr will have access to admin)
2025-07-17 09:45:36 -07:00
const buttons = [
2025-07-28 13:42:12 -07:00
{icon: FaHome, name: "Home", view: "widget"},
2025-07-28 15:10:57 -07:00
{icon: FaGlobeAmericas, name: "Global Map", view: "global"},
{icon: FaMapMarkerAlt, name: "Regional Map", view: "regional"},
{icon: FaVolcano, name: "Volcano", view: "volcano"},
2025-07-28 15:10:57 -07:00
{icon: HiUsers, name: "Admin", view: "admin"}
2025-07-17 09:45:36 -07:00
];
const toggleSidebar = () => {
2025-07-17 09:45:36 -07:00
if (isOpen) setIsOpen(false);
else setIsOpen(true);
};
// Track currrent view from both Sidebar and Canvas when 'view' changes
useEffect(() => {
// console.log("typeof view =", typeof view);
console.log("Current view = ", view);
const newIndex = buttons.findIndex((btn) => {
if (btn.view === "widget") {
return DASHBOARD_VIEWS.includes(view); // Group match
}
return btn.view === view; // Exact match
});
if (newIndex !== -1 ) {
setActiveButton(newIndex);
}
}, [view]);
return (
<Box
// key={button.name}
css={styles.root}
width={isOpen ? "210px" : "71px"}
transition="width 0.4s ease"
2025-07-29 11:08:36 -07:00
position="relative"
>
<IconButton
aria-label="Toggle Sidebar"
2025-07-22 09:26:40 -07:00
variant="plain"
size="md"
onClick={toggleSidebar}
2025-07-22 09:26:40 -07:00
width="54px"
>
2025-07-28 15:10:57 -07:00
<VscThreeBars />
</IconButton>
2025-07-17 09:45:36 -07:00
{buttons.map((button, idx) => {
const Icon = button.icon;
2025-07-29 11:08:36 -07:00
return (
<Tooltip key={button.name} tooltipkeykey={idx} content={button.name} sidebarIsOpen={isOpen}>
2025-07-29 11:08:36 -07:00
<Button
key={button.name}
2025-07-29 11:08:36 -07:00
css={styles.buttons}
onClick={() => {
// setActiveButton(idx);
2025-07-29 11:08:36 -07:00
onChangeView(button.view); // Trigger state change in Dashboard
}}
bg={activeButton === idx ? "primary":undefined}
color={activeButton === idx ? "#FFFFFF":undefined}
justifyContent="flex-start"
>
<Icon />
<span
style={{
2025-07-17 09:45:36 -07:00
opacity: isOpen ? 1 : 0,
transition: "opacity 0.4s ease",
overflow: "hidden",
whiteSpace: "nowrap",
display: "inline-block",
lineHeight: 1.2
}}
>
{button.name}
</span>
2025-07-29 11:08:36 -07:00
</Button>
</Tooltip>
2025-07-29 11:08:36 -07:00
)
2025-07-17 09:45:36 -07:00
})}
2025-07-31 08:56:32 -07:00
<ColorModeButton
onClick={
() => {
setDarkMode(!darkMode);
toggleColorMode();
}}
position="absolute"
bottom="2"/>
</Box>
);
2025-07-17 09:45:36 -07:00
}