element-call-Github/src/UserMenu.tsx

145 lines
4.0 KiB
TypeScript
Raw Normal View History

/*
Copyright 2022 - 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { FC, ReactNode, useCallback, useMemo } from "react";
2021-12-24 06:40:23 +08:00
import { Item } from "@react-stately/collections";
import { useLocation } from "react-router-dom";
2022-10-10 21:19:10 +08:00
import { useTranslation } from "react-i18next";
2021-12-18 03:51:12 +08:00
import { Button, LinkButton } from "./button";
2022-01-06 09:21:30 +08:00
import { PopoverMenuTrigger } from "./popover/PopoverMenu";
2021-12-24 06:40:23 +08:00
import { Menu } from "./Menu";
import { TooltipTrigger } from "./Tooltip";
import { Avatar, Size } from "./Avatar";
2023-09-28 07:06:10 +08:00
import UserIcon from "./icons/User.svg?react";
import SettingsIcon from "./icons/Settings.svg?react";
import LoginIcon from "./icons/Login.svg?react";
import LogoutIcon from "./icons/Logout.svg?react";
2022-01-19 05:34:42 +08:00
import { Body } from "./typography/Typography";
import styles from "./UserMenu.module.css";
interface Props {
preventNavigation: boolean;
isAuthenticated: boolean;
isPasswordlessUser: boolean;
userId: string;
displayName: string;
avatarUrl?: string;
onAction: (value: string) => void;
}
2021-12-04 08:42:29 +08:00
export const UserMenu: FC<Props> = ({
preventNavigation,
2021-12-24 06:40:23 +08:00
isAuthenticated,
isPasswordlessUser,
userId,
2021-12-24 06:40:23 +08:00
displayName,
avatarUrl,
onAction,
}) => {
2022-10-10 21:19:10 +08:00
const { t } = useTranslation();
2021-12-10 04:58:30 +08:00
const location = useLocation();
2021-12-04 08:42:29 +08:00
const items = useMemo(() => {
2021-12-10 04:58:30 +08:00
const arr = [];
2021-12-24 06:40:23 +08:00
if (isAuthenticated) {
2021-12-10 04:58:30 +08:00
arr.push({
key: "user",
icon: UserIcon,
2021-12-24 06:40:23 +08:00
label: displayName,
2023-05-03 19:54:06 +08:00
dataTestid: "usermenu_user",
2021-12-10 04:58:30 +08:00
});
arr.push({
key: "settings",
icon: SettingsIcon,
label: t("common.settings"),
});
2021-12-10 04:58:30 +08:00
if (isPasswordlessUser && !preventNavigation) {
2021-12-18 03:51:12 +08:00
arr.push({
key: "login",
2023-11-20 21:22:40 +08:00
label: t("action.sign_in"),
2021-12-18 03:51:12 +08:00
icon: LoginIcon,
2023-05-03 19:54:06 +08:00
dataTestid: "usermenu_login",
2021-12-18 03:51:12 +08:00
});
}
2021-12-18 03:27:33 +08:00
if (!isPasswordlessUser && !preventNavigation) {
2021-12-18 03:51:12 +08:00
arr.push({
key: "logout",
2023-11-20 21:22:40 +08:00
label: t("action.sign_out"),
2021-12-18 03:51:12 +08:00
icon: LogoutIcon,
2023-05-03 19:54:06 +08:00
dataTestid: "usermenu_logout",
2021-12-18 03:51:12 +08:00
});
}
2021-12-04 08:42:29 +08:00
}
2021-12-10 04:58:30 +08:00
return arr;
2022-10-10 21:19:10 +08:00
}, [isAuthenticated, isPasswordlessUser, displayName, preventNavigation, t]);
const tooltip = useCallback(() => t("common.profile"), [t]);
2021-12-04 08:42:29 +08:00
2021-12-24 06:40:23 +08:00
if (!isAuthenticated) {
2021-12-18 03:51:12 +08:00
return (
<LinkButton to={{ pathname: "/login", state: { from: location } }}>
2023-11-23 04:07:15 +08:00
{t("log_in")}
2021-12-18 03:51:12 +08:00
</LinkButton>
);
}
2021-12-04 08:42:29 +08:00
return (
2021-12-24 06:40:23 +08:00
<PopoverMenuTrigger placement="bottom right">
2022-10-10 21:19:10 +08:00
<TooltipTrigger tooltip={tooltip} placement="bottom left">
2023-05-03 19:54:06 +08:00
<Button
variant="icon"
className={styles.userButton}
data-testid="usermenu_open"
>
2022-01-22 08:33:43 +08:00
{isAuthenticated && (!isPasswordlessUser || avatarUrl) ? (
2021-12-24 06:40:23 +08:00
<Avatar
id={userId}
name={displayName}
size={Size.SM}
2021-12-24 06:40:23 +08:00
src={avatarUrl}
/>
) : (
<UserIcon />
2021-12-15 14:00:00 +08:00
)}
2021-12-24 06:40:23 +08:00
</Button>
</TooltipTrigger>
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(props: any): ReactNode => (
<Menu {...props} label={t("a11y.user_menu")} onAction={onAction}>
{items.map(({ key, icon: Icon, label, dataTestid }) => (
<Item key={key} textValue={label}>
<Icon
width={24}
height={24}
className={styles.menuIcon}
data-testid={dataTestid}
/>
<Body overflowEllipsis>{label}</Body>
</Item>
))}
</Menu>
)
}
2021-12-24 06:40:23 +08:00
</PopoverMenuTrigger>
2021-12-04 08:42:29 +08:00
);
};