Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP-2406: Make menu be able to persist on IDE #218

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 48 additions & 26 deletions src/Components/ProfileMenu/ProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
useCallback,
useMemo,
useRef,
MutableRefObject
} from "react";
import IconButton from "@material-ui/core/IconButton";
import Menu from "@material-ui/core/Menu";
Expand All @@ -19,30 +20,25 @@ import ResetPasswordModal from "./ResetPassword";
import { ProfileMenuProps } from "./types";

function getCustomMenuElements(menuItemConf: any, classes: any) {
return Object.entries(menuItemConf ?? {}).map(([key, menuItem]: [string, any]) => {
if (React.isValidElement(menuItem))
return menuItem;

return (<MenuItem
key={key}
data-test-id={"input_" + key}
className={classes.menuItemSpacing}
onClick={menuItem.handler}
>
{ i18n.t(menuItem.title) }
</MenuItem>);
});
return Object.entries(menuItemConf ?? {}).map(
([key, menuItem]: [string, any]) => {
if (React.isValidElement(menuItem)) return menuItem;

return (
<MenuItem
key={key}
data-testid={"input_" + key}
className={classes.menuItemSpacing}
onClick={menuItem.handler}
>
{i18n.t(menuItem.title)}
</MenuItem>
);
}
);
}

const ProfileMenu = (props: ProfileMenuProps) => {
// State hooks
const [anchorEl, setAnchorEl] = useState(null);
const [username, setUsername] = useState("");
// Other hooks
const user = useMemo(() => new User(), []);
const classes = profileMenuStyles();
// Refs
const resetModalRef = useRef<{ open: Function }>();
// Props
const {
welcomeLabel = "Hello",
Expand All @@ -51,11 +47,24 @@ const ProfileMenu = (props: ProfileMenuProps) => {
version = "",
extraItems = [],
isDarkTheme = true,
menuItemConf,
isMenuOpen,
handleLogout = () => console.log("logout"),
handleToggleTheme,
menuItemConf,
onClose
} = props;

// State hooks
const [anchorEl, setAnchorEl] = useState(null);
const [username, setUsername] = useState("");
// Other hooks
const triggerButtonRef: MutableRefObject<HTMLElement | undefined> = useRef();
const menuOpenAnimation: MutableRefObject<number | "auto"> = useRef("auto");
const user = useMemo(() => new User(), []);
const classes = profileMenuStyles();
// Refs
const resetModalRef = useRef<{ open: Function }>();

//========================================================================================
/* *
* Handlers *
Expand All @@ -75,7 +84,8 @@ const ProfileMenu = (props: ProfileMenuProps) => {
*/
const handleClose = useCallback(() => {
setAnchorEl(null);
}, []);
onClose && onClose();
}, [onClose]);

/**
* Open Password Reset modal
Expand All @@ -90,21 +100,31 @@ const ProfileMenu = (props: ProfileMenuProps) => {
*/
const handleLogoutClick = useCallback(() => {
handleLogout();
}, []);
}, [handleLogout]);

//========================================================================================
/* *
* React Lifecycle *
* */
//========================================================================================

useEffect(() => {
if (isMenuOpen) {
menuOpenAnimation.current = 1;
triggerButtonRef.current?.click();
}
}, [isMenuOpen]);

// On component mount
useEffect(() => {
// Set authenticated user name
setUsername(user.getUsername());
}, [user]);

const customEl = useMemo(() => getCustomMenuElements(menuItemConf, classes), [menuItemConf, classes]);
const customEl = useMemo(
() => getCustomMenuElements(menuItemConf, classes),
[menuItemConf, classes]
);

//========================================================================================
/* *
Expand All @@ -116,6 +136,7 @@ const ProfileMenu = (props: ProfileMenuProps) => {
<div data-testid="section_profile-menu">
<Tooltip title={i18n.t("Settings") || ""}>
<IconButton
buttonRef={triggerButtonRef}
data-testid="input_button"
aria-haspopup="true"
onClick={handleClick}
Expand All @@ -124,6 +145,7 @@ const ProfileMenu = (props: ProfileMenuProps) => {
</IconButton>
</Tooltip>
<Menu
transitionDuration={menuOpenAnimation.current}
data-testid="section_menu"
anchorEl={anchorEl}
keepMounted
Expand Down Expand Up @@ -159,7 +181,7 @@ const ProfileMenu = (props: ProfileMenuProps) => {
{i18n.t("Change Password")}
</MenuItem>
)}
{ customEl }
{customEl}
{handleToggleTheme && (
<div className={classes.menuItemSpacing}>
{darkThemeLabel}
Expand Down
14 changes: 9 additions & 5 deletions src/Components/ProfileMenu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ export interface ProfileMenuProps {
version: string;
extraItems: { func: MouseEventHandler; label: string }[];
isDarkTheme: boolean;
isMenuOpen?: boolean;
onClose?: Function;
handleLogout: Function;
handleToggleTheme: (
event: React.ChangeEvent<HTMLInputElement>,
checked: boolean
) => void;
menuItemConf: {
[key: string]: {
handler: Function,
title: string,
} | React.ElementType,
},
[key: string]:
| {
handler: Function;
title: string;
}
| React.ElementType;
};
}