Files
Checkmate/client/src/Pages/Account/index.jsx
T

99 lines
2.8 KiB
React
Raw Normal View History

2024-12-02 19:14:26 -05:00
import { useState } from "react";
2024-06-27 19:19:36 -04:00
import PropTypes from "prop-types";
2025-06-18 10:52:11 +08:00
import { useNavigate } from "react-router-dom";
2024-08-20 17:03:19 -04:00
import { useSelector } from "react-redux";
2024-06-27 19:19:36 -04:00
import { Box, Tab, useTheme } from "@mui/material";
2025-09-27 14:54:55 -07:00
import CustomTabList from "@/Components/v1/Tab/index.jsx";
2024-06-27 19:19:36 -04:00
import TabContext from "@mui/lab/TabContext";
2025-09-25 10:11:41 -07:00
import ProfilePanel from "./components/ProfilePanel.jsx";
import PasswordPanel from "./components/PasswordPanel.jsx";
import TeamPanel from "./components/TeamPanel.jsx";
import { useTranslation } from "react-i18next";
2024-06-27 19:19:36 -04:00
/**
* Account component renders a settings page with tabs for Profile, Password, and Team settings.
* @param {string} [props.open] - Specifies the initially open tab: 'profile', 'password', or 'team'.
* @returns {JSX.Element}
*/
const Account = ({ open = "profile" }) => {
const theme = useTheme();
const navigate = useNavigate();
2024-11-30 02:01:38 -05:00
const [focusedTab, setFocusedTab] = useState(null); // Track focused tab
const tab = open;
const handleTabChange = (event, newTab) => {
navigate(`/account/${newTab}`);
};
const { user } = useSelector((state) => state.auth);
const { t } = useTranslation();
2024-09-06 11:10:08 -07:00
const requiredRoles = ["superadmin", "admin"];
let tabList = [
{ name: t("menu.profile"), value: "profile" },
{ name: t("menu.password"), value: "password" },
2025-05-27 09:10:32 -07:00
{ name: t("menu.team"), value: "team" },
];
2025-07-22 12:27:06 -07:00
const hideTeams = !requiredRoles.some((role) => user.role.includes(role));
2025-07-22 12:27:06 -07:00
if (hideTeams) {
tabList = [
{ name: t("menu.profile"), value: "profile" },
2025-05-27 09:10:32 -07:00
{ name: t("menu.password"), value: "password" },
];
}
2024-07-24 14:46:49 -04:00
// Remove password for demo
if (user.role.includes("demo")) {
2025-05-27 09:10:32 -07:00
tabList = [{ name: t("menu.profile"), value: "profile" }];
}
2024-09-06 12:01:58 -07:00
2024-11-30 02:01:38 -05:00
const handleKeyDown = (event) => {
const currentIndex = tabList.findIndex((item) => item.value === tab);
2024-11-30 02:01:38 -05:00
if (event.key === "Tab") {
const nextIndex = (currentIndex + 1) % tabList.length;
setFocusedTab(tabList[nextIndex].value);
2024-11-30 02:01:38 -05:00
} else if (event.key === "Enter") {
event.preventDefault();
navigate(`/account/${focusedTab}`);
}
};
const handleFocus = (tabName) => {
setFocusedTab(tabName);
};
return (
<Box
px={theme.spacing(20)}
py={theme.spacing(12)}
>
<TabContext value={tab}>
2025-05-27 09:10:32 -07:00
<CustomTabList
value={tab}
onChange={handleTabChange}
aria-label="account tabs"
>
{tabList.map((item, index) => (
<Tab
label={item.name}
key={index}
value={item.value}
onKeyDown={handleKeyDown}
onFocus={() => handleFocus(item.value)}
tabIndex={index}
/>
))}
2025-04-10 17:41:54 -04:00
</CustomTabList>
<ProfilePanel />
{user.role.includes("superadmin") && <PasswordPanel />}
{!hideTeams && <TeamPanel />}
</TabContext>
</Box>
);
2024-06-27 19:19:36 -04:00
};
Account.propTypes = {
open: PropTypes.oneOf(["profile", "password", "team"]),
2024-06-27 19:19:36 -04:00
};
export default Account;