Files
element-call/src/UserMenu.jsx

99 lines
2.7 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";
2021-12-13 14:54:44 -08:00
import { useClient, useDisplayName } from "./ConferenceCallManagerHooks";
import { useModalTriggerState } from "./Modal";
import { ProfileModal } from "./ProfileModal";
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();
2021-12-13 14:54:44 -08:00
const { isAuthenticated, isGuest, logout, userName, client } = useClient();
const { displayName } = useDisplayName(client);
const { modalState, modalProps } = useModalTriggerState();
2021-12-09 12:58:30 -08:00
const onAction = useCallback(
(value) => {
switch (value) {
case "user":
2021-12-13 14:54:44 -08:00
modalState.open();
2021-12-09 12:58:30 -08:00
break;
case "logout":
logout();
break;
case "login":
history.push("/login", { state: { from: location } });
break;
case "register":
history.push("/register", { state: { from: location } });
break;
}
},
2021-12-13 14:54:44 -08:00
[history, location, logout, modalState]
2021-12-09 12:58:30 -08:00
);
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,
2021-12-13 14:54:44 -08:00
label: displayName || userName,
2021-12-09 12:58:30 -08:00
});
}
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;
2021-12-13 14:54:44 -08:00
}, [isAuthenticated, isGuest, userName, displayName]);
2021-12-03 16:42:29 -08:00
return (
2021-12-13 14:54:44 -08:00
<>
<PopoverMenuTrigger placement="bottom right">
<Button variant="icon" className={styles.userButton}>
<ButtonTooltip>Profile</ButtonTooltip>
<UserIcon />
</Button>
{(props) => (
<Menu {...props} label="User menu" onAction={onAction}>
{items.map(({ key, icon: Icon, label }) => (
<Item key={key} textValue={label}>
<Icon />
<span>{label}</span>
</Item>
))}
</Menu>
)}
</PopoverMenuTrigger>
{modalState.isOpen && <ProfileModal client={client} {...modalProps} />}
</>
2021-12-03 16:42:29 -08:00
);
}