2020-11-11 10:54:12 +05:00
|
|
|
import { useAnimation } from "framer-motion";
|
2021-07-17 00:25:21 +05:00
|
|
|
import { Check } from "../icons";
|
2021-02-18 12:03:54 +05:00
|
|
|
import React, { useEffect, useMemo } from "react";
|
2021-06-16 09:58:15 +05:00
|
|
|
import { Flex, Box, Text, Button } from "rebass";
|
2021-07-08 12:22:59 +05:00
|
|
|
import { useIsUserPremium } from "../../hooks/use-is-user-premium";
|
2020-11-11 10:54:12 +05:00
|
|
|
import useMobile from "../../utils/use-mobile";
|
2021-10-23 10:58:44 +05:00
|
|
|
import { AnimatedFlex } from "../animated";
|
2019-12-02 10:19:32 +05:00
|
|
|
|
|
|
|
|
function Menu(props) {
|
2021-04-20 09:33:19 +05:00
|
|
|
const { menuItems, data, closeMenu, id, style, sx, state } = props;
|
2021-07-08 12:22:59 +05:00
|
|
|
const isUserPremium = useIsUserPremium();
|
2020-11-11 10:54:12 +05:00
|
|
|
const isMobile = useMobile();
|
2021-02-18 12:03:54 +05:00
|
|
|
const Container = useMemo(
|
|
|
|
|
() => (isMobile ? MobileMenuContainer : MenuContainer),
|
|
|
|
|
[isMobile]
|
|
|
|
|
);
|
2020-05-15 00:35:20 +05:00
|
|
|
|
2020-11-11 10:54:12 +05:00
|
|
|
return (
|
2021-07-17 00:25:21 +05:00
|
|
|
<Container id={id} title={data?.title} style={style} sx={sx} state={state}>
|
2021-02-18 12:03:54 +05:00
|
|
|
{menuItems.map(
|
2021-07-17 00:25:21 +05:00
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
title,
|
|
|
|
|
key,
|
|
|
|
|
onClick,
|
|
|
|
|
component: Component,
|
|
|
|
|
color,
|
|
|
|
|
isPro,
|
|
|
|
|
isNew,
|
|
|
|
|
disabled,
|
|
|
|
|
disableReason,
|
|
|
|
|
checked,
|
|
|
|
|
icon: Icon,
|
|
|
|
|
type,
|
|
|
|
|
},
|
|
|
|
|
index
|
|
|
|
|
) =>
|
|
|
|
|
type === "seperator" ? (
|
|
|
|
|
<Box
|
|
|
|
|
key={key}
|
|
|
|
|
width="95%"
|
|
|
|
|
height="0.5px"
|
|
|
|
|
bg="border"
|
|
|
|
|
my={2}
|
|
|
|
|
alignSelf="center"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
2021-12-11 11:56:03 +05:00
|
|
|
variant="menuitem"
|
|
|
|
|
color={color || "text"}
|
|
|
|
|
display="flex"
|
|
|
|
|
alignItems="center"
|
|
|
|
|
justifyContent="center"
|
2021-07-17 00:25:21 +05:00
|
|
|
title={
|
|
|
|
|
(disabled && disabled(data) && disableReason) || title(data)
|
2021-02-18 12:03:54 +05:00
|
|
|
}
|
2021-07-17 00:25:21 +05:00
|
|
|
disabled={disabled && disabled(data)}
|
|
|
|
|
data-test-id={`menuitem-${title(data)
|
|
|
|
|
.split(" ")
|
|
|
|
|
.join("")
|
|
|
|
|
.toLowerCase()}`}
|
|
|
|
|
key={key}
|
|
|
|
|
onClick={async (e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
if (closeMenu) {
|
|
|
|
|
closeMenu();
|
|
|
|
|
}
|
2020-11-11 10:54:12 +05:00
|
|
|
|
2021-07-17 00:25:21 +05:00
|
|
|
if (!Component) {
|
|
|
|
|
onClick(data, menuItems[index]);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{Icon && (
|
|
|
|
|
<Icon
|
|
|
|
|
color={color || "text"}
|
|
|
|
|
size={15}
|
|
|
|
|
sx={{ mr: 2, ml: -1 }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{Component ? (
|
|
|
|
|
<Component data={data} />
|
|
|
|
|
) : (
|
|
|
|
|
<Text
|
|
|
|
|
as="span"
|
|
|
|
|
textAlign="left"
|
|
|
|
|
fontFamily="body"
|
|
|
|
|
fontSize="menu"
|
|
|
|
|
flex={1}
|
|
|
|
|
>
|
|
|
|
|
{title(data)}
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
{isPro && !isUserPremium && (
|
|
|
|
|
<Text
|
|
|
|
|
fontSize="subBody"
|
|
|
|
|
bg="primary"
|
|
|
|
|
color="static"
|
|
|
|
|
px={1}
|
|
|
|
|
sx={{ borderRadius: "default" }}
|
|
|
|
|
>
|
|
|
|
|
Pro
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
{isNew && (
|
|
|
|
|
<Text
|
|
|
|
|
fontSize="subBody"
|
|
|
|
|
bg="primary"
|
|
|
|
|
color="static"
|
|
|
|
|
px={1}
|
|
|
|
|
sx={{ borderRadius: "default" }}
|
|
|
|
|
>
|
|
|
|
|
NEW
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
{checked && <Check size={14} />}
|
|
|
|
|
</Button>
|
|
|
|
|
)
|
2020-11-11 10:54:12 +05:00
|
|
|
)}
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-02-18 12:03:54 +05:00
|
|
|
export default React.memo(Menu, (prev, next) => {
|
|
|
|
|
return prev.state === next.state;
|
|
|
|
|
});
|
2020-11-11 10:54:12 +05:00
|
|
|
|
2021-07-17 00:25:21 +05:00
|
|
|
function MenuContainer({ id, style, sx, title, children }) {
|
2019-12-02 10:19:32 +05:00
|
|
|
return (
|
2019-12-19 10:04:46 +05:00
|
|
|
<Flex
|
2021-02-18 12:03:54 +05:00
|
|
|
id={id}
|
2020-01-08 15:04:18 +05:00
|
|
|
bg="background"
|
2019-12-19 10:04:46 +05:00
|
|
|
py={1}
|
2021-02-18 12:03:54 +05:00
|
|
|
style={style}
|
2019-12-19 10:04:46 +05:00
|
|
|
sx={{
|
|
|
|
|
position: "relative",
|
2021-07-17 00:25:21 +05:00
|
|
|
width: "11em",
|
2019-12-19 10:04:46 +05:00
|
|
|
borderRadius: "default",
|
2021-07-17 00:25:21 +05:00
|
|
|
boxShadow: "0px 10px 10px 0px #00000022",
|
|
|
|
|
border: "1px solid",
|
2020-03-28 23:29:04 +05:00
|
|
|
borderColor: "border",
|
2021-02-18 12:03:54 +05:00
|
|
|
...sx,
|
2019-12-19 10:04:46 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-16 09:58:15 +05:00
|
|
|
<Flex flexDirection="column" width="100%">
|
2020-03-02 15:00:53 +05:00
|
|
|
<Text
|
|
|
|
|
fontFamily="body"
|
2020-11-11 10:54:12 +05:00
|
|
|
fontSize="subtitle"
|
2020-03-02 15:00:53 +05:00
|
|
|
color="primary"
|
|
|
|
|
py={"8px"}
|
|
|
|
|
px={3}
|
|
|
|
|
sx={{ borderBottom: "1px solid", borderBottomColor: "border" }}
|
|
|
|
|
>
|
2021-07-17 00:25:21 +05:00
|
|
|
{title || "Properties"}
|
2020-03-02 15:00:53 +05:00
|
|
|
</Text>
|
2021-02-18 12:03:54 +05:00
|
|
|
{children}
|
2021-06-16 09:58:15 +05:00
|
|
|
</Flex>
|
2019-12-02 10:19:32 +05:00
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-11-11 10:54:12 +05:00
|
|
|
|
2021-07-17 00:25:21 +05:00
|
|
|
function MobileMenuContainer({ style, id, state, title, children }) {
|
2020-11-11 10:54:12 +05:00
|
|
|
const animation = useAnimation();
|
2021-04-20 09:33:19 +05:00
|
|
|
|
2020-11-11 10:54:12 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (state === "open") {
|
|
|
|
|
animation.start({ y: 0 });
|
|
|
|
|
const menu = document.getElementById(id);
|
|
|
|
|
menu.style.top = 0;
|
|
|
|
|
menu.style.left = 0;
|
|
|
|
|
} else {
|
|
|
|
|
animation.start({ y: 500 });
|
|
|
|
|
}
|
|
|
|
|
}, [state, animation, id]);
|
2021-02-18 12:03:54 +05:00
|
|
|
|
2020-11-11 10:54:12 +05:00
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
flexDirection="column"
|
|
|
|
|
id={id}
|
|
|
|
|
style={style}
|
|
|
|
|
width="100%"
|
|
|
|
|
height="100%"
|
|
|
|
|
bg="overlay"
|
|
|
|
|
overflow="hidden"
|
|
|
|
|
sx={{ position: "relative" }}
|
|
|
|
|
>
|
2021-10-23 10:58:44 +05:00
|
|
|
<AnimatedFlex
|
2020-11-11 10:54:12 +05:00
|
|
|
width="100%"
|
|
|
|
|
bg="background"
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
bottom: 0,
|
|
|
|
|
borderTopLeftRadius: 10,
|
|
|
|
|
borderTopRightRadius: 10,
|
|
|
|
|
overflow: "hidden",
|
|
|
|
|
}}
|
|
|
|
|
initial={{ y: 500 }}
|
|
|
|
|
animate={animation}
|
|
|
|
|
flexDirection="column"
|
|
|
|
|
p={2}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
width={50}
|
|
|
|
|
height={7}
|
|
|
|
|
bg="shade"
|
|
|
|
|
alignSelf="center"
|
|
|
|
|
sx={{ borderRadius: "default" }}
|
|
|
|
|
/>
|
|
|
|
|
<Flex flex="1" flexDirection="column" overflowY="scroll">
|
|
|
|
|
<Text variant="title" mt={2} alignSelf="center">
|
2021-07-17 00:25:21 +05:00
|
|
|
{title || "Properties"}
|
2020-11-11 10:54:12 +05:00
|
|
|
</Text>
|
2021-02-18 12:03:54 +05:00
|
|
|
{children}
|
2020-11-11 10:54:12 +05:00
|
|
|
</Flex>
|
2021-10-23 10:58:44 +05:00
|
|
|
</AnimatedFlex>
|
2020-11-11 10:54:12 +05:00
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|