Files
Checkmate/client/src/Components/sidebar/Authfooter.tsx
T

194 lines
4.8 KiB
TypeScript
Raw Normal View History

2026-02-10 22:47:39 +00:00
import Stack from "@mui/material/Stack";
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import IconButton from "@mui/material/IconButton";
import Menu from "@mui/material/Menu";
import MenuItem from "@mui/material/MenuItem";
import { useTheme } from "@mui/material";
2026-02-28 20:57:44 +00:00
import { SPACING, LAYOUT } from "@/Utils/Theme/constants";
2026-02-10 23:24:51 +00:00
import { useSelector, useDispatch } from "react-redux";
2026-02-10 22:47:39 +00:00
import { useTranslation } from "react-i18next";
import { useState } from "react";
import { useNavigate } from "react-router";
2026-02-10 23:24:51 +00:00
import { MoreVertical, LogOut } from "lucide-react";
2026-02-20 18:13:39 +00:00
import { Avatar, Icon } from "@/Components/design-elements";
2026-02-10 22:47:39 +00:00
import { clearAuthState } from "@/Features/Auth/authSlice.js";
import type { RootState } from "@/Types/state.js";
2026-02-10 23:24:51 +00:00
interface AuthFooterProps {
2026-02-10 22:47:39 +00:00
collapsed: boolean;
2026-02-10 23:24:51 +00:00
accountMenuItems: Array<{
name: string;
path: string;
icon: React.ReactNode;
}>;
}
export const AuthFooter = ({ collapsed, accountMenuItems }: AuthFooterProps) => {
2026-02-10 22:47:39 +00:00
const { t } = useTranslation();
const theme = useTheme();
2026-02-10 23:24:51 +00:00
const user = useSelector((state: RootState) => state.auth.user);
2026-02-10 22:47:39 +00:00
const navigate = useNavigate();
const dispatch = useDispatch();
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
2026-02-10 23:24:51 +00:00
const menuOpen = Boolean(anchorEl);
const handleMenuOpen = (e: React.MouseEvent<HTMLElement>) =>
setAnchorEl(e.currentTarget);
const handleMenuClose = () => setAnchorEl(null);
2026-02-10 22:47:39 +00:00
2026-02-10 23:24:51 +00:00
const handleNavigate = (path: string) => {
handleMenuClose();
navigate(path);
2026-02-10 22:47:39 +00:00
};
2026-02-10 23:24:51 +00:00
const handleLogout = () => {
2026-02-10 22:47:39 +00:00
dispatch(clearAuthState());
navigate("/login");
};
2026-02-10 23:24:51 +00:00
const getRoleText = () => {
const role = user?.role ?? "";
if (role.includes("superadmin"))
return t("components.sidebar.authFooter.roles.superAdmin");
if (role.includes("admin")) return t("components.sidebar.authFooter.roles.admin");
2026-02-10 23:32:58 +00:00
if (role.includes("user")) return t("components.sidebar.authFooter.roles.user");
2026-02-10 23:24:51 +00:00
if (role.includes("demo")) return t("components.sidebar.authFooter.roles.demoUser");
return role;
2026-02-10 22:47:39 +00:00
};
2026-02-10 23:24:51 +00:00
const filteredMenuItems = accountMenuItems.filter((item) => {
if (!user) return false;
if (item.name === "Password" && user.role?.includes("demo")) return false;
if (item.name === "Team" && !user.role?.includes("superadmin")) return false;
return true;
});
const menuItemSx = {
gap: theme.spacing(LAYOUT.MD),
2026-02-10 23:24:51 +00:00
borderRadius: theme.shape.borderRadius,
2026-02-28 20:57:44 +00:00
pl: theme.spacing(LAYOUT.XS),
"& svg": {
height: 16,
width: 16,
opacity: 0.81,
stroke: theme.palette.text.secondary,
},
2026-02-10 23:24:51 +00:00
};
2026-02-10 22:47:39 +00:00
return (
<Stack
direction="row"
alignItems="center"
2026-02-28 20:57:44 +00:00
py={theme.spacing(LAYOUT.XS)}
px={theme.spacing(LAYOUT.MD)}
gap={theme.spacing(LAYOUT.MD)}
2026-02-10 22:47:39 +00:00
>
<Avatar
2026-02-10 23:24:51 +00:00
small
onClick={collapsed ? handleMenuOpen : undefined}
sx={{ cursor: collapsed ? "pointer" : "default" }}
2026-02-10 22:47:39 +00:00
/>
2026-02-10 23:24:51 +00:00
{!collapsed && (
2026-02-10 22:47:39 +00:00
<Stack
2026-02-10 23:24:51 +00:00
direction="row"
alignItems="center"
2026-02-28 20:57:44 +00:00
gap={theme.spacing(SPACING.MD)}
2026-02-10 23:24:51 +00:00
minWidth={0}
flex={1}
2026-02-10 22:47:39 +00:00
>
2026-02-10 23:24:51 +00:00
<Stack
minWidth={0}
flex={1}
2026-02-10 22:47:39 +00:00
>
2026-02-10 23:24:51 +00:00
<Typography
fontWeight={500}
noWrap
sx={{ color: theme.palette.text.primary }}
>
{user?.firstName} {user?.lastName}
</Typography>
<Typography
noWrap
sx={{ textTransform: "capitalize", color: theme.palette.text.secondary }}
>
{getRoleText()}
</Typography>
</Stack>
2026-02-10 22:47:39 +00:00
<IconButton
2026-02-10 23:24:51 +00:00
onClick={handleMenuOpen}
2026-02-10 22:47:39 +00:00
sx={{
"&:focus": { outline: "none" },
2026-02-10 23:24:51 +00:00
"& svg": { stroke: theme.palette.text.secondary },
2026-02-10 22:47:39 +00:00
}}
>
<Icon
icon={MoreVertical}
size={22}
/>
</IconButton>
2026-02-10 23:24:51 +00:00
</Stack>
)}
2026-02-10 22:47:39 +00:00
<Menu
anchorEl={anchorEl}
2026-02-10 23:24:51 +00:00
open={menuOpen}
onClose={handleMenuClose}
2026-02-10 22:47:39 +00:00
disableScrollLock
2026-02-10 23:24:51 +00:00
anchorOrigin={{ vertical: "top", horizontal: "right" }}
2026-02-10 22:47:39 +00:00
slotProps={{
2026-02-28 20:57:44 +00:00
paper: {
sx: {
mt: theme.spacing(-LAYOUT.XS),
ml: collapsed ? theme.spacing(SPACING.LG) : 0,
},
},
2026-02-10 22:47:39 +00:00
}}
2026-02-10 23:24:51 +00:00
MenuListProps={{ sx: { p: 2, "& li": { m: 0 } } }}
2026-02-28 20:57:44 +00:00
sx={{ ml: theme.spacing(LAYOUT.XS) }}
2026-02-10 22:47:39 +00:00
>
{collapsed && (
2026-02-10 23:24:51 +00:00
<Box
px={2}
pb={2}
sx={{ pointerEvents: "none" }}
>
<Typography
fontWeight={500}
fontSize={13}
2026-02-10 22:47:39 +00:00
>
2026-02-10 23:24:51 +00:00
{user?.firstName} {user?.lastName}
</Typography>
<Typography
fontSize={12}
sx={{ textTransform: "capitalize" }}
>
{getRoleText()}
</Typography>
</Box>
2026-02-10 22:47:39 +00:00
)}
2026-02-10 23:24:51 +00:00
{filteredMenuItems.map((item) => (
<MenuItem
key={item.name}
onClick={() => handleNavigate(item.path)}
sx={menuItemSx}
>
{item.icon}
{item.name}
</MenuItem>
))}
2026-02-10 22:47:39 +00:00
<MenuItem
2026-02-10 23:24:51 +00:00
onClick={handleLogout}
sx={menuItemSx}
2026-02-10 22:47:39 +00:00
>
<Icon
icon={LogOut}
size={20}
/>
2026-02-10 23:16:12 +00:00
{t("components.sidebar.authFooter.logOut")}
2026-02-10 22:47:39 +00:00
</MenuItem>
</Menu>
</Stack>
);
};