Avatar.tsx 865 B

12345678910111213141516171819202122232425262728293031323334
  1. import "./Avatar.scss";
  2. import React, { useState } from "react";
  3. import { getClientInitials } from "../clients";
  4. type AvatarProps = {
  5. onClick: (e: React.MouseEvent<HTMLDivElement, MouseEvent>) => void;
  6. color: string;
  7. border: string;
  8. name: string;
  9. src?: string;
  10. };
  11. export const Avatar = ({ color, onClick, name, src }: AvatarProps) => {
  12. const shortName = getClientInitials(name);
  13. const [error, setError] = useState(false);
  14. const loadImg = !error && src;
  15. const style = loadImg ? undefined : { background: color };
  16. return (
  17. <div className="Avatar" style={style} onClick={onClick}>
  18. {loadImg ? (
  19. <img
  20. className="Avatar-img"
  21. src={src}
  22. alt={shortName}
  23. referrerPolicy="no-referrer"
  24. onError={() => setError(true)}
  25. />
  26. ) : (
  27. shortName
  28. )}
  29. </div>
  30. );
  31. };