Files
element-call/src/UserMenu.jsx

91 lines
2.3 KiB
React
Raw Normal View History

2021-12-03 16:42:29 -08:00
import React, { useCallback, useMemo } from "react";
2021-12-07 11:59:57 -08:00
import { ButtonTooltip, Button } from "./button";
2021-12-06 17:34:10 -08:00
import { PopoverMenuTrigger } from "./PopoverMenu";
2021-12-03 16:42:29 -08:00
import { ReactComponent as UserIcon } from "./icons/User.svg";
import { ReactComponent as LoginIcon } from "./icons/Login.svg";
import { ReactComponent as LogoutIcon } from "./icons/Logout.svg";
import styles from "./UserMenu.module.css";
2021-12-06 17:34:10 -08:00
import { Item } from "@react-stately/collections";
import { Menu } from "./Menu";
2021-12-09 12:58:30 -08:00
import { useHistory, useLocation } from "react-router-dom";
import { useClient } from "./ConferenceCallManagerHooks";
2021-12-03 16:42:29 -08:00
2021-12-09 12:58:30 -08:00
export function UserMenu() {
const location = useLocation();
const history = useHistory();
const { isAuthenticated, isGuest, logout, userName } = useClient();
const onAction = useCallback(
(value) => {
switch (value) {
case "user":
break;
case "logout":
logout();
break;
case "login":
history.push("/login", { state: { from: location } });
break;
case "register":
history.push("/register", { state: { from: location } });
break;
}
},
[history, location, logout]
);
2021-12-03 16:42:29 -08:00
const items = useMemo(() => {
2021-12-09 12:58:30 -08:00
const arr = [];
if (isAuthenticated) {
arr.push({
key: "user",
icon: UserIcon,
label: userName,
});
}
if (!isAuthenticated || isGuest) {
arr.push(
2021-12-03 16:42:29 -08:00
{
key: "login",
label: "Sign In",
icon: LoginIcon,
},
2021-12-09 12:58:30 -08:00
{
key: "register",
label: "Register",
icon: LoginIcon,
}
);
} else {
arr.push({
key: "logout",
label: "Sign Out",
icon: LogoutIcon,
});
2021-12-03 16:42:29 -08:00
}
2021-12-09 12:58:30 -08:00
return arr;
}, [isAuthenticated, isGuest, userName]);
2021-12-03 16:42:29 -08:00
return (
2021-12-07 11:59:57 -08:00
<PopoverMenuTrigger placement="bottom right">
<Button variant="icon" className={styles.userButton}>
2021-12-03 16:42:29 -08:00
<ButtonTooltip>Profile</ButtonTooltip>
2021-12-06 12:19:38 -08:00
<UserIcon />
2021-12-07 11:59:57 -08:00
</Button>
2021-12-03 16:42:29 -08:00
{(props) => (
2021-12-07 11:59:57 -08:00
<Menu {...props} label="User menu" onAction={onAction}>
2021-12-03 16:42:29 -08:00
{items.map(({ key, icon: Icon, label }) => (
2021-12-06 17:34:10 -08:00
<Item key={key} textValue={label}>
2021-12-03 16:42:29 -08:00
<Icon />
<span>{label}</span>
2021-12-06 17:34:10 -08:00
</Item>
2021-12-03 16:42:29 -08:00
))}
2021-12-06 17:34:10 -08:00
</Menu>
2021-12-03 16:42:29 -08:00
)}
2021-12-06 17:34:10 -08:00
</PopoverMenuTrigger>
2021-12-03 16:42:29 -08:00
);
}