2020-10-16 13:16:01 +00:00
|
|
|
import React, { memo } from "react";
|
|
|
|
import styled, { css } from "styled-components";
|
|
|
|
import PropTypes from "prop-types";
|
2020-11-27 17:40:38 +00:00
|
|
|
import Icons from "../icons";
|
2020-10-16 13:16:01 +00:00
|
|
|
import Link from "../link";
|
2019-06-27 11:28:13 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
const whiteColor = "#FFFFFF";
|
|
|
|
const avatarBackground = "#ECEEF1";
|
|
|
|
const namedAvatarBackground = "#2DA7DB";
|
2019-06-18 13:20:49 +00:00
|
|
|
|
2019-08-08 09:24:20 +00:00
|
|
|
const noneUserSelect = css`
|
2020-10-16 13:16:01 +00:00
|
|
|
-webkit-touch-callout: none;
|
|
|
|
-webkit-user-select: none;
|
|
|
|
-moz-user-select: none;
|
|
|
|
-ms-user-select: none;
|
|
|
|
user-select: none;
|
2019-08-08 09:24:20 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-18 13:20:49 +00:00
|
|
|
const StyledAvatar = styled.div`
|
2020-10-16 13:16:01 +00:00
|
|
|
position: relative;
|
|
|
|
width: ${(props) =>
|
|
|
|
(props.size === "max" && "160px") ||
|
|
|
|
(props.size === "big" && "82px") ||
|
|
|
|
(props.size === "medium" && "48px") ||
|
2020-11-05 08:34:14 +00:00
|
|
|
(props.size === "small" && "36px") ||
|
|
|
|
(props.size === "min" && "32px")};
|
2020-10-16 13:16:01 +00:00
|
|
|
height: ${(props) =>
|
|
|
|
(props.size === "max" && "160px") ||
|
|
|
|
(props.size === "big" && "82px") ||
|
|
|
|
(props.size === "medium" && "48px") ||
|
2020-11-05 08:34:14 +00:00
|
|
|
(props.size === "small" && "36px") ||
|
|
|
|
(props.size === "min" && "32px")};
|
2020-10-16 13:16:01 +00:00
|
|
|
|
|
|
|
font-family: "Open Sans", sans-serif, Arial;
|
|
|
|
font-style: normal;
|
2019-06-18 13:20:49 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-27 11:28:13 +00:00
|
|
|
const RoleWrapper = styled.div`
|
2020-10-16 13:16:01 +00:00
|
|
|
position: absolute;
|
|
|
|
left: ${(props) =>
|
|
|
|
(props.size === "max" && "0px") ||
|
|
|
|
(props.size === "big" && "0px") ||
|
|
|
|
(props.size === "medium" && "-2px") ||
|
2020-11-05 08:34:14 +00:00
|
|
|
(props.size === "small" && "-2px") ||
|
|
|
|
(props.size === "min" && "-2px")};
|
2020-10-16 13:16:01 +00:00
|
|
|
bottom: ${(props) =>
|
|
|
|
(props.size === "max" && "0px") ||
|
|
|
|
(props.size === "big" && "5px") ||
|
|
|
|
(props.size === "medium" && "3px") ||
|
2020-11-05 08:34:14 +00:00
|
|
|
(props.size === "small" && "3px") ||
|
|
|
|
(props.size === "min" && "3px")};
|
2020-10-16 13:16:01 +00:00
|
|
|
width: ${(props) => (props.size === "max" && "24px") || "12px"};
|
|
|
|
height: ${(props) => (props.size === "max" && "24px") || "12px"};
|
2019-06-18 13:20:49 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-27 11:28:13 +00:00
|
|
|
const ImageStyled = styled.img`
|
2020-10-16 13:16:01 +00:00
|
|
|
width: 100%;
|
|
|
|
height: auto;
|
|
|
|
border-radius: 50%;
|
2019-08-08 09:24:20 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
${noneUserSelect}
|
2019-06-27 11:28:13 +00:00
|
|
|
`;
|
|
|
|
|
2019-07-31 17:47:22 +00:00
|
|
|
const AvatarWrapper = styled.div`
|
2020-10-16 13:16:01 +00:00
|
|
|
border-radius: 50%;
|
|
|
|
height: 100%;
|
|
|
|
background-color: ${(props) =>
|
|
|
|
(props.source === "" && props.userName !== "" && namedAvatarBackground) ||
|
|
|
|
avatarBackground};
|
|
|
|
|
|
|
|
& > svg {
|
|
|
|
display: block;
|
|
|
|
width: 50% !important;
|
|
|
|
height: 100% !important;
|
|
|
|
margin: auto;
|
|
|
|
}
|
2019-06-18 13:20:49 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-27 11:28:13 +00:00
|
|
|
const NamedAvatar = styled.div`
|
2020-10-16 13:16:01 +00:00
|
|
|
position: absolute;
|
|
|
|
left: 50%;
|
|
|
|
top: 50%;
|
|
|
|
transform: translate(-50%, -50%);
|
|
|
|
font-weight: 600;
|
|
|
|
font-size: ${(props) =>
|
|
|
|
(props.size === "max" && "72px") ||
|
|
|
|
(props.size === "big" && "34px") ||
|
|
|
|
(props.size === "medium" && "20px") ||
|
2020-11-05 08:49:00 +00:00
|
|
|
(props.size === "small" && "12px") ||
|
|
|
|
(props.size === "min" && "12px")};
|
2020-10-16 13:16:01 +00:00
|
|
|
color: ${whiteColor};
|
|
|
|
|
|
|
|
${noneUserSelect}
|
2019-06-18 13:20:49 +00:00
|
|
|
`;
|
|
|
|
|
2019-06-27 11:28:13 +00:00
|
|
|
const EditContainer = styled.div`
|
2020-10-16 13:16:01 +00:00
|
|
|
box-sizing: border-box;
|
|
|
|
position: absolute;
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
|
|
|
top: 50%;
|
|
|
|
left: 50%;
|
|
|
|
transform: translate(-50%, -50%);
|
|
|
|
padding: 75% 16px 5px;
|
|
|
|
text-align: center;
|
|
|
|
line-height: 19px;
|
|
|
|
border-radius: 50%;
|
|
|
|
background: ${(props) =>
|
|
|
|
props.gradient
|
|
|
|
? "linear-gradient(180deg, rgba(6, 22, 38, 0) 24.48%, rgba(6, 22, 38, 0.75) 100%)"
|
|
|
|
: "transparent"};
|
2019-07-10 14:05:34 +00:00
|
|
|
`;
|
2019-06-27 11:28:13 +00:00
|
|
|
|
2019-07-31 17:47:22 +00:00
|
|
|
const EmptyIcon = styled(Icons.CameraIcon)`
|
2020-10-16 13:16:01 +00:00
|
|
|
border-radius: 50%;
|
2019-07-31 17:47:22 +00:00
|
|
|
`;
|
|
|
|
|
2019-08-13 11:38:09 +00:00
|
|
|
const EditLink = styled.div`
|
|
|
|
padding-left: 10px;
|
|
|
|
padding-right: 10px;
|
|
|
|
|
2019-09-04 08:54:36 +00:00
|
|
|
a:hover {
|
2020-10-16 13:16:01 +00:00
|
|
|
border-bottom: none;
|
2019-09-04 08:54:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 11:38:09 +00:00
|
|
|
span {
|
|
|
|
display: inline-block;
|
2019-08-13 12:19:48 +00:00
|
|
|
max-width: 100%;
|
2019-09-04 08:54:36 +00:00
|
|
|
text-decoration: underline dashed;
|
2019-08-13 11:38:09 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
const getRoleIcon = (role) => {
|
2019-08-14 12:51:50 +00:00
|
|
|
switch (role) {
|
2020-10-16 13:16:01 +00:00
|
|
|
case "guest":
|
|
|
|
return <Icons.GuestIcon size="scale" />;
|
|
|
|
case "admin":
|
|
|
|
return <Icons.AdministratorIcon size="scale" />;
|
|
|
|
case "owner":
|
|
|
|
return <Icons.OwnerIcon size="scale" />;
|
2019-08-14 12:51:50 +00:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
const getInitials = (userName) =>
|
2019-09-13 05:51:14 +00:00
|
|
|
userName
|
|
|
|
.split(/\s/)
|
2020-10-16 13:16:01 +00:00
|
|
|
.reduce((response, word) => (response += word.slice(0, 1)), "")
|
2019-09-13 05:51:14 +00:00
|
|
|
.substring(0, 2);
|
2019-08-14 12:51:50 +00:00
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
const Initials = (props) => (
|
2019-08-14 12:51:50 +00:00
|
|
|
<NamedAvatar {...props}>{getInitials(props.userName)}</NamedAvatar>
|
|
|
|
);
|
|
|
|
|
2019-09-09 14:08:30 +00:00
|
|
|
Initials.propTypes = {
|
2020-10-16 13:16:01 +00:00
|
|
|
userName: PropTypes.string,
|
2019-09-09 14:08:30 +00:00
|
|
|
};
|
|
|
|
|
2019-09-10 07:36:55 +00:00
|
|
|
// eslint-disable-next-line react/display-name
|
2020-10-16 13:16:01 +00:00
|
|
|
const Avatar = memo((props) => {
|
2019-09-13 12:34:37 +00:00
|
|
|
//console.log("Avatar render");
|
2020-10-16 13:16:01 +00:00
|
|
|
const {
|
|
|
|
size,
|
|
|
|
source,
|
|
|
|
userName,
|
|
|
|
role,
|
|
|
|
editing,
|
|
|
|
editLabel,
|
|
|
|
editAction,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const avatarContent = source ? (
|
|
|
|
<ImageStyled src={source} />
|
|
|
|
) : userName ? (
|
|
|
|
<Initials userName={userName} size={size} />
|
|
|
|
) : (
|
|
|
|
<EmptyIcon size="scale" />
|
|
|
|
);
|
2019-09-13 12:34:37 +00:00
|
|
|
|
|
|
|
const roleIcon = getRoleIcon(role);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<StyledAvatar {...props}>
|
|
|
|
<AvatarWrapper source={source} userName={userName}>
|
|
|
|
{avatarContent}
|
|
|
|
</AvatarWrapper>
|
2020-10-16 13:16:01 +00:00
|
|
|
{editing && size === "max" && (
|
2019-12-24 09:26:16 +00:00
|
|
|
<EditContainer gradient={!!source}>
|
2019-09-13 12:34:37 +00:00
|
|
|
<EditLink>
|
|
|
|
<Link
|
2020-10-16 13:16:01 +00:00
|
|
|
type="action"
|
2019-09-13 12:34:37 +00:00
|
|
|
title={editLabel}
|
|
|
|
isTextOverflow={true}
|
2019-12-24 09:26:16 +00:00
|
|
|
isHovered={true}
|
2020-10-16 13:16:01 +00:00
|
|
|
fontSize="14px"
|
2019-12-24 09:26:16 +00:00
|
|
|
fontWeight={600}
|
2019-09-13 12:34:37 +00:00
|
|
|
color={whiteColor}
|
|
|
|
onClick={editAction}
|
|
|
|
>
|
|
|
|
{editLabel}
|
|
|
|
</Link>
|
|
|
|
</EditLink>
|
2020-10-16 13:16:01 +00:00
|
|
|
</EditContainer>
|
|
|
|
)}
|
|
|
|
<RoleWrapper size={size}>{roleIcon}</RoleWrapper>
|
2019-09-13 12:34:37 +00:00
|
|
|
</StyledAvatar>
|
|
|
|
);
|
|
|
|
});
|
2019-06-18 13:20:49 +00:00
|
|
|
|
|
|
|
Avatar.propTypes = {
|
2020-11-06 11:12:29 +00:00
|
|
|
size: PropTypes.oneOf(["max", "big", "medium", "small", "min"]),
|
2020-10-16 13:16:01 +00:00
|
|
|
role: PropTypes.oneOf(["owner", "admin", "guest", "user"]),
|
2019-08-07 07:29:33 +00:00
|
|
|
source: PropTypes.string,
|
|
|
|
editLabel: PropTypes.string,
|
|
|
|
userName: PropTypes.string,
|
2019-09-09 14:08:30 +00:00
|
|
|
editing: PropTypes.bool,
|
2019-11-22 12:33:41 +00:00
|
|
|
editAction: PropTypes.func,
|
2020-10-16 13:16:01 +00:00
|
|
|
|
2019-11-22 12:33:41 +00:00
|
|
|
className: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
2020-10-16 13:16:01 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2019-06-18 13:20:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Avatar.defaultProps = {
|
2020-10-16 13:16:01 +00:00
|
|
|
size: "medium",
|
|
|
|
role: "",
|
|
|
|
source: "",
|
|
|
|
editLabel: "Edit photo",
|
|
|
|
userName: "",
|
|
|
|
editing: false,
|
2019-06-18 13:20:49 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
export default Avatar;
|