redesign app layout: set different background colors to avatars depending on user's name

This commit is contained in:
Daniel Cojocea committed 2024-08-13 20:24:29 -04:00
1 parent baeacd15a2
commit 826779dcd0
2 files changed
+31 -9

No files matched your search

+23 -1
View File
@@ -3,6 +3,27 @@ import PropTypes from "prop-types";
import { useSelector } from "react-redux";
import { useEffect, useState } from "react";
/**
* Generates a color based on the input string.
* @param {string} string - The input string to generate the color from.
* @returns {string}
*/
const stringToColor = (string) => {
let hash = 0;
let i;
for (i = 0; i < string.length; i += 1) {
hash = string.charCodeAt(i) + ((hash << 5) - hash);
}
let color = "#";
for (i = 0; i < 3; i += 1) {
const value = (hash >> (i * 8)) & 0xff;
color += `00${value.toString(16)}`.slice(-2);
}
return color;
};
/**
* @component
* @param {Object} props
@@ -37,6 +58,7 @@ const Avatar = ({ src, small, sx }) => {
sx={{
fontSize: small ? "16px" : "22px",
fontWeight: 400,
backgroundColor: stringToColor(`${user?.firstName} ${user?.lastName}`),
display: "inline-flex",
"&::before": {
content: `""`,
@@ -53,7 +75,7 @@ const Avatar = ({ src, small, sx }) => {
}}
>
{user.firstName?.charAt(0)}
{!small && user.lastName?.charAt(0)}
{user.lastName?.charAt(0)}
</MuiAvatar>
);
};