Files
Checkmate/Client/src/Components/Avatar/index.jsx
T

65 lines
1.7 KiB
React
Raw Normal View History

2024-05-07 15:33:08 -07:00
import { Avatar as MuiAvatar } from "@mui/material";
import PropTypes from "prop-types";
2024-07-04 20:47:23 -04:00
import { useSelector } from "react-redux";
import { useEffect, useState } from "react";
2024-05-07 15:33:08 -07:00
/**
* @component
* @param {Object} props
* @param {string} props.src - Path to image for avatar
* @param {boolean} props.small - Specifies if avatar should be large
* @param {Object} [props.sx] - Additional styles to apply to the button.
2024-05-07 15:33:08 -07:00
* @returns {JSX.Element}
* @example
* // Render a red label
* <Avatar src="assets/img" first="Alex" last="Holliday" small />
*/
2024-07-04 20:47:23 -04:00
const Avatar = ({ src, small, sx }) => {
const { user } = useSelector((state) => state.auth);
const style = small ? { width: 25, height: 25 } : { width: 64, height: 64 };
const border = small ? 1 : 3;
const [image, setImage] = useState();
2024-07-08 16:06:12 -04:00
useEffect(() => {
if (user.avatarImage) {
setImage(`data:image/png;base64,${user.avatarImage}`);
}
}, [user?.avatarImage]);
2024-07-04 20:47:23 -04:00
2024-05-07 15:33:08 -07:00
return (
<MuiAvatar
2024-07-04 20:47:23 -04:00
alt={`${user?.firstname} ${user?.lastname}`}
2024-07-05 14:43:29 -04:00
src={
2024-07-08 16:06:12 -04:00
src ? src : user?.avatarImage ? image : "/static/images/avatar/2.jpg"
2024-07-05 14:43:29 -04:00
}
2024-05-07 15:33:08 -07:00
sx={{
2024-07-04 20:47:23 -04:00
fontSize: small ? "12px" : "20px",
2024-05-07 15:33:08 -07:00
display: "inline-flex",
2024-07-04 20:47:23 -04:00
"&::before": {
content: `""`,
position: "absolute",
top: "0",
left: "0",
width: `calc(100% - ${border * 2}px)`,
height: `calc(100% - ${border * 2}px)`,
border: `${border}px solid rgba(255,255,255,0.2)`,
borderRadius: "50%",
},
...style,
...sx,
2024-05-07 15:33:08 -07:00
}}
2024-07-08 16:26:08 -04:00
>
{user.firstname?.charAt(0)}{user.lastname?.charAt(0)}
</MuiAvatar>
2024-05-07 15:33:08 -07:00
);
};
Avatar.propTypes = {
src: PropTypes.string,
small: PropTypes.bool,
sx: PropTypes.object,
2024-05-07 15:33:08 -07:00
};
export default Avatar;