DocSpace-buildtools/packages/asc-web-common/components/Article/sub-components/article-profile.js

92 lines
2.5 KiB
JavaScript
Raw Normal View History

import React, { useState, useRef } from "react";
2022-07-11 13:18:35 +00:00
import { inject, observer } from "mobx-react";
2022-07-11 20:10:44 +00:00
import { withRouter } from "react-router";
import { useTranslation } from "react-i18next";
2022-07-11 13:18:35 +00:00
import Avatar from "@appserver/components/avatar";
import Text from "@appserver/components/text";
import ContextMenuButton from "@appserver/components/context-menu-button";
import ContextMenu from "@appserver/components/context-menu";
2022-07-12 08:19:31 +00:00
import { isTablet } from "@appserver/components/utils/device";
import { StyledArticleProfile } from "../styled-article";
const ArticleProfile = (props) => {
const { user, showText, getUserRole, getActions } = props;
2022-07-11 20:10:44 +00:00
const { t } = useTranslation("Common");
const [isOpen, setIsOpen] = useState(false);
const ref = useRef(null);
const menuRef = useRef(null);
2022-07-11 13:18:35 +00:00
2022-07-12 08:19:31 +00:00
const tablet = isTablet();
const avatarSize = tablet ? "min" : "base";
const userRole = getUserRole(user);
const toggle = (e, isOpen) => {
isOpen ? menuRef.current.show(e) : menuRef.current.hide(e);
setIsOpen(isOpen);
};
const onAvatarClick = (e) => {
if (tablet && !showText) toggle(e, !isOpen);
};
const onHide = () => {
setIsOpen(false);
};
const model = getActions(t);
2022-07-11 13:18:35 +00:00
return (
<StyledArticleProfile showText={showText} tablet={tablet}>
<div ref={ref}>
<Avatar
size={avatarSize}
role={userRole}
source={user.avatar}
userName={user.displayName}
onClick={onAvatarClick}
/>
<ContextMenu
model={model}
containerRef={ref}
ref={menuRef}
onHide={onHide}
scaled={false}
leftOffset={-50}
/>
</div>
2022-07-12 08:19:31 +00:00
{(!tablet || showText) && (
<>
<Text className="userName" fontWeight={600} noSelect truncate>
2022-07-12 08:19:31 +00:00
{user.displayName}
</Text>
2022-07-21 13:41:12 +00:00
<ContextMenuButton
className="option-button"
iconClassName="option-button-icon"
2022-07-21 13:41:12 +00:00
zIndex={402}
directionX="left"
directionY="top"
iconName="/static/images/vertical-dots.react.svg"
size={15}
isFill
getData={() => getActions(t)}
isDisabled={false}
usePortal={true}
/>
2022-07-12 08:19:31 +00:00
</>
)}
</StyledArticleProfile>
2022-07-11 13:18:35 +00:00
);
};
2022-07-11 20:10:44 +00:00
export default withRouter(
inject(({ auth, profileActionsStore }) => {
const { getActions, getUserRole } = profileActionsStore;
2022-07-11 13:18:35 +00:00
2022-07-11 20:10:44 +00:00
return {
user: auth.userStore.user,
getUserRole,
getActions,
2022-07-11 20:10:44 +00:00
};
})(observer(ArticleProfile))
2022-07-11 20:10:44 +00:00
);