front-hub/src/components/Navbar/DialogMenu.tsx

195 lines
6.3 KiB
TypeScript
Raw Normal View History

2023-07-25 22:31:04 +00:00
import { useState } from "react";
2023-08-02 08:31:58 +00:00
import { Link, useLocation } from "react-router-dom";
2023-08-16 19:23:22 +00:00
import { Box, Button, List, ListItem, Typography, useMediaQuery, useTheme } from "@mui/material";
2023-08-02 08:31:58 +00:00
2023-07-05 03:05:48 +00:00
import { useUserStore } from "@root/stores/user";
2023-07-07 13:53:08 +00:00
import { currencyFormatter } from "@root/utils/currencyFormatter";
2023-08-22 10:28:22 +00:00
import { cardShadow } from "@root/utils/theme";
2023-05-26 11:19:10 +00:00
2023-08-02 08:31:58 +00:00
import CustomAvatar from "./Avatar";
2023-07-25 22:31:04 +00:00
type MenuItem = {
name: string;
url: string;
subMenu?: MenuItem[];
};
const arrayMenu: MenuItem[] = [
{
name: "Тарифы",
url: "/tariffs",
subMenu: [
{ name: "Тарифы на время", url: "/tariffs/time" },
{ name: "Тарифы на объём", url: "/tariffs/volume" },
{ name: "Кастомный тариф", url: "/tariffconstructor" },
],
},
2023-08-16 19:23:22 +00:00
{ name: "Профиль", url: "/settings" },
2023-07-25 22:31:04 +00:00
{ name: "Вопросы и ответы", url: "/faq" },
2023-08-01 00:56:03 +00:00
{ name: "Корзина", url: "/cart" },
{ name: "Поддержка", url: "/support" },
2023-07-28 13:24:21 +00:00
{ name: "История", url: "/history" },
2023-05-26 11:19:10 +00:00
];
interface DialogMenuProps {
2023-07-25 22:31:04 +00:00
handleClose: () => void;
2023-05-26 11:19:10 +00:00
}
2023-08-02 08:31:58 +00:00
export default function DialogMenu({ handleClose }: DialogMenuProps) {
2023-07-25 22:31:04 +00:00
const [activeSubMenuIndex, setActiveSubMenuIndex] = useState<number>(-1);
const theme = useTheme();
const location = useLocation();
2023-08-23 13:24:47 +00:00
const isTablet = useMediaQuery(theme.breakpoints.down(1000));
2023-07-25 22:31:04 +00:00
const isMobile = useMediaQuery(theme.breakpoints.down(600));
const user = useUserStore((state) => state.user);
const cash = useUserStore((state) => state.userAccount?.wallet.cash) ?? 0;
const closeDialogMenu = () => {
setActiveSubMenuIndex(-1);
handleClose();
};
2023-08-16 19:23:22 +00:00
const handleSubMenu = (index: number) => setActiveSubMenuIndex((activeIndex) => (activeIndex !== index ? index : -1));
2023-07-05 04:16:49 +00:00
2023-07-25 22:31:04 +00:00
return (
2023-08-03 12:14:31 +00:00
<Box sx={{ height: "100%", maxHeight: "calc(100vh - 51px)" }}>
2023-07-25 22:31:04 +00:00
<List
sx={{
maxWidth: "250px",
background: location.pathname === "/" ? "#333647" : "#FFFFFF",
2023-08-02 08:31:58 +00:00
height: "100%",
2023-07-25 22:31:04 +00:00
}}
>
<ListItem
sx={{
pl: 0,
pr: 0,
flexDirection: "column",
alignItems: isMobile ? "start" : "end",
}}
2023-05-26 11:19:10 +00:00
>
2023-07-25 22:31:04 +00:00
{arrayMenu.map(({ name, url, subMenu = [] }, index) => (
<Box sx={{ width: "100%" }} key={name + index}>
<Button
key={index}
component={Link}
to={url}
2023-08-08 15:47:02 +00:00
state={{ previousUrl: location.pathname }}
2023-07-25 22:31:04 +00:00
disableRipple
variant="text"
2023-08-16 19:23:22 +00:00
onClick={() => (!subMenu.length ? closeDialogMenu() : handleSubMenu(index))}
2023-05-26 11:19:10 +00:00
sx={{
2023-07-25 22:31:04 +00:00
padding: "10px 10px 10px 20px",
display: "block",
fontWeight: 500,
2023-08-16 19:23:22 +00:00
color: location.pathname === url ? "#7E2AEA" : location.pathname === "/" ? "white" : "black",
2023-07-25 22:31:04 +00:00
textTransform: "none",
fontSize: "16px",
borderRadius: 0,
"&:hover, &:active": {
color: "#7E2AEA",
2023-08-16 19:23:22 +00:00
background: index === activeSubMenuIndex ? theme.palette.background.default : "none",
2023-07-25 22:31:04 +00:00
},
2023-05-26 11:19:10 +00:00
}}
2023-07-25 22:31:04 +00:00
>
<Box>{name}</Box>
</Button>
{subMenu.length ? (
<Box
sx={{
backgroundColor: theme.palette.background.paper,
width: "100%",
2023-08-07 13:36:59 +00:00
boxShadow: !isTablet ? cardShadow : null,
2023-07-25 22:31:04 +00:00
}}
2023-05-26 11:19:10 +00:00
>
2023-07-25 22:31:04 +00:00
{index === activeSubMenuIndex &&
subMenu.map(({ name, url }) => (
<Link
2023-08-07 10:58:42 +00:00
key={name + url}
2023-07-25 22:31:04 +00:00
style={{
paddingLeft: "30px",
display: "block",
textDecoration: "none",
}}
to={url}
onClick={closeDialogMenu}
2023-08-08 15:47:02 +00:00
state={{ previousUrl: location.pathname }}
2023-07-25 22:31:04 +00:00
>
<Typography
variant="body2"
sx={{
padding: "12px",
whiteSpace: "nowrap",
fontWeight: 400,
color:
2023-08-16 19:23:22 +00:00
location.pathname === url ? "#7E2AEA" : location.pathname === "/" ? "white" : "black",
2023-07-25 22:31:04 +00:00
}}
2023-07-05 04:16:49 +00:00
>
2023-07-25 22:31:04 +00:00
{name}
</Typography>
</Link>
2023-07-05 04:16:49 +00:00
))}
2023-07-25 22:31:04 +00:00
</Box>
) : (
<></>
)}
</Box>
))}
</ListItem>
2023-08-02 08:31:58 +00:00
{location.pathname === "/" ? (
<Button
component={Link}
to={user ? "/tariffs" : "/signin"}
state={user ? undefined : { backgroundLocation: location }}
variant="outlined"
sx={{
width: "188px",
color: "white",
border: "1px solid white",
ml: "40px",
mt: "35px",
textTransform: "none",
fontWeight: "400",
fontSize: "18px",
lineHeight: "24px",
borderRadius: "8px",
padding: "8px 17px",
}}
>
Личный кабинет
</Button>
2023-07-25 22:31:04 +00:00
) : (
<Box
sx={{
2023-08-02 08:31:58 +00:00
width: "100%",
height: "72px",
background: "#F2F3F7",
display: "flex",
alignItems: "center",
2023-07-25 22:31:04 +00:00
position: "absolute",
2023-08-02 08:31:58 +00:00
bottom: "0",
2023-07-25 22:31:04 +00:00
}}
>
2023-08-02 08:31:58 +00:00
<CustomAvatar />
<Box sx={{ ml: "8px", whiteSpace: "nowrap" }}>
<Typography
sx={{
fontSize: "12px",
lineHeight: "14px",
2023-08-22 10:28:22 +00:00
color: theme.palette.gray.dark,
2023-08-02 08:31:58 +00:00
}}
>
Мой баланс
</Typography>
2023-08-22 10:28:22 +00:00
<Typography variant="body2" color={theme.palette.purple.main}>
2023-08-02 08:31:58 +00:00
{currencyFormatter.format(cash / 100)}
</Typography>
</Box>
2023-07-25 22:31:04 +00:00
</Box>
)}
</List>
2023-08-02 08:31:58 +00:00
</Box>
2023-07-25 22:31:04 +00:00
);
2023-05-26 11:19:10 +00:00
}