2023-01-03 16:55:26 +00:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2023-01-03 16:55:26 +00:00
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-01-03 16:55:26 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-08-28 08:44:39 -04:00
|
|
|
import { FC, useMemo, useState } from "react";
|
2022-07-30 10:02:20 +02:00
|
|
|
import { useLocation } from "react-router-dom";
|
2022-10-10 09:19:10 -04:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-08-28 08:44:39 -04:00
|
|
|
import { Menu, MenuItem } from "@vector-im/compound-web";
|
2022-07-30 10:02:20 +02:00
|
|
|
|
2024-08-28 08:44:39 -04:00
|
|
|
import { LinkButton } from "./button";
|
2022-07-30 10:02:20 +02:00
|
|
|
import { Avatar, Size } from "./Avatar";
|
2023-09-27 19:06:10 -04: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-07-30 10:02:20 +02:00
|
|
|
import styles from "./UserMenu.module.css";
|
|
|
|
|
|
2023-09-22 18:05:13 -04:00
|
|
|
interface Props {
|
2022-07-30 10:02:20 +02:00
|
|
|
preventNavigation: boolean;
|
|
|
|
|
isAuthenticated: boolean;
|
|
|
|
|
isPasswordlessUser: boolean;
|
2023-08-31 15:46:09 +02:00
|
|
|
userId: string;
|
2022-07-30 10:02:20 +02:00
|
|
|
displayName: string;
|
2023-07-03 18:36:50 +01:00
|
|
|
avatarUrl?: string;
|
2022-07-30 10:02:20 +02:00
|
|
|
onAction: (value: string) => void;
|
|
|
|
|
}
|
2021-12-03 16:42:29 -08:00
|
|
|
|
2023-09-22 18:05:13 -04:00
|
|
|
export const UserMenu: FC<Props> = ({
|
2022-02-02 15:09:16 -08:00
|
|
|
preventNavigation,
|
2021-12-23 14:40:23 -08:00
|
|
|
isAuthenticated,
|
|
|
|
|
isPasswordlessUser,
|
2023-08-31 15:46:09 +02:00
|
|
|
userId,
|
2021-12-23 14:40:23 -08:00
|
|
|
displayName,
|
|
|
|
|
avatarUrl,
|
|
|
|
|
onAction,
|
2023-09-22 18:05:13 -04:00
|
|
|
}) => {
|
2022-10-10 09:19:10 -04:00
|
|
|
const { t } = useTranslation();
|
2021-12-09 12:58:30 -08:00
|
|
|
const location = useLocation();
|
2021-12-03 16:42:29 -08:00
|
|
|
|
|
|
|
|
const items = useMemo(() => {
|
2021-12-09 12:58:30 -08:00
|
|
|
const arr = [];
|
|
|
|
|
|
2021-12-23 14:40:23 -08:00
|
|
|
if (isAuthenticated) {
|
2021-12-09 12:58:30 -08:00
|
|
|
arr.push({
|
|
|
|
|
key: "user",
|
|
|
|
|
icon: UserIcon,
|
2021-12-23 14:40:23 -08:00
|
|
|
label: displayName,
|
2023-05-03 12:54:06 +01:00
|
|
|
dataTestid: "usermenu_user",
|
2021-12-09 12:58:30 -08:00
|
|
|
});
|
2023-05-05 11:44:35 +02:00
|
|
|
arr.push({
|
|
|
|
|
key: "settings",
|
|
|
|
|
icon: SettingsIcon,
|
2023-11-29 13:01:45 -05:00
|
|
|
label: t("common.settings"),
|
2023-05-05 11:44:35 +02:00
|
|
|
});
|
2021-12-09 12:58:30 -08:00
|
|
|
|
2022-02-02 15:09:16 -08:00
|
|
|
if (isPasswordlessUser && !preventNavigation) {
|
2021-12-17 11:51:12 -08:00
|
|
|
arr.push({
|
|
|
|
|
key: "login",
|
2023-11-20 13:22:40 +00:00
|
|
|
label: t("action.sign_in"),
|
2021-12-17 11:51:12 -08:00
|
|
|
icon: LoginIcon,
|
2023-05-03 12:54:06 +01:00
|
|
|
dataTestid: "usermenu_login",
|
2021-12-17 11:51:12 -08:00
|
|
|
});
|
|
|
|
|
}
|
2021-12-17 11:27:33 -08:00
|
|
|
|
2022-02-02 15:09:16 -08:00
|
|
|
if (!isPasswordlessUser && !preventNavigation) {
|
2021-12-17 11:51:12 -08:00
|
|
|
arr.push({
|
|
|
|
|
key: "logout",
|
2023-11-20 13:22:40 +00:00
|
|
|
label: t("action.sign_out"),
|
2021-12-17 11:51:12 -08:00
|
|
|
icon: LogoutIcon,
|
2023-05-03 12:54:06 +01:00
|
|
|
dataTestid: "usermenu_logout",
|
2021-12-17 11:51:12 -08:00
|
|
|
});
|
|
|
|
|
}
|
2021-12-03 16:42:29 -08:00
|
|
|
}
|
2021-12-09 12:58:30 -08:00
|
|
|
|
|
|
|
|
return arr;
|
2022-10-10 09:19:10 -04:00
|
|
|
}, [isAuthenticated, isPasswordlessUser, displayName, preventNavigation, t]);
|
|
|
|
|
|
2024-08-28 08:44:39 -04:00
|
|
|
const [open, setOpen] = useState(false);
|
2021-12-03 16:42:29 -08:00
|
|
|
|
2021-12-23 14:40:23 -08:00
|
|
|
if (!isAuthenticated) {
|
2021-12-17 11:51:12 -08:00
|
|
|
return (
|
|
|
|
|
<LinkButton to={{ pathname: "/login", state: { from: location } }}>
|
2023-11-22 20:07:15 +00:00
|
|
|
{t("log_in")}
|
2021-12-17 11:51:12 -08:00
|
|
|
</LinkButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 16:42:29 -08:00
|
|
|
return (
|
2024-08-28 08:44:39 -04:00
|
|
|
<Menu
|
|
|
|
|
title={t("a11y.user_menu")}
|
|
|
|
|
showTitle={false}
|
|
|
|
|
align="end"
|
|
|
|
|
open={open}
|
|
|
|
|
onOpenChange={setOpen}
|
|
|
|
|
trigger={
|
|
|
|
|
<button
|
|
|
|
|
aria-label={t("common.profile")}
|
2023-05-03 12:54:06 +01:00
|
|
|
className={styles.userButton}
|
|
|
|
|
data-testid="usermenu_open"
|
|
|
|
|
>
|
2022-01-21 16:33:43 -08:00
|
|
|
{isAuthenticated && (!isPasswordlessUser || avatarUrl) ? (
|
2021-12-23 14:40:23 -08:00
|
|
|
<Avatar
|
2023-08-31 15:46:09 +02:00
|
|
|
id={userId}
|
|
|
|
|
name={displayName}
|
2022-07-30 10:02:20 +02:00
|
|
|
size={Size.SM}
|
2021-12-23 14:40:23 -08:00
|
|
|
src={avatarUrl}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<UserIcon />
|
2021-12-14 22:00:00 -08:00
|
|
|
)}
|
2024-08-28 08:44:39 -04:00
|
|
|
</button>
|
2023-07-04 13:58:09 +01:00
|
|
|
}
|
2024-08-28 08:44:39 -04:00
|
|
|
>
|
|
|
|
|
{items.map(({ key, icon: Icon, label, dataTestid }) => (
|
|
|
|
|
<MenuItem
|
|
|
|
|
key={key}
|
|
|
|
|
Icon={Icon}
|
|
|
|
|
label={label}
|
|
|
|
|
data-test-id={dataTestid}
|
|
|
|
|
onSelect={() => onAction(key)}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</Menu>
|
2021-12-03 16:42:29 -08:00
|
|
|
);
|
2023-09-22 18:05:13 -04:00
|
|
|
};
|