import { FC, ImgHTMLAttributes, useState } from "react"; import { getInitials } from "../utils"; interface Props extends ImgHTMLAttributes { // className?: string; // alt?: string; // src?: string; width: number; height: number; name?: string; type?: "user" | "channel"; } function getFontSize(width: number): number { if (width <= 16) return 8; if (width <= 24) return 12; if (width <= 32) return 16; if (width <= 40) return 18; if (width <= 56) return 22; if (width <= 80) return 48; return 64; } const Avatar: FC = ({ src = "", name = "Deleted User", type = "user", width, height, ...rest }) => { const [error, setError] = useState(false); const handleError = () => { setError(true); }; if (!error && src) { return ; } // 长度限制在六个字符 let initials = getInitials(name).substring(0, 6); const len = initials.length; const scaleVal = len > 2 ? (11 - len) / 10 : 1; return (
{initials}
); }; export default Avatar;