calcom/apps/web/components/ui/AvatarSSR.tsx
Hariom Balhara ce0c8347fb
[Perf Improvement] Booking Page score should be in green now (#2057)
* Avoid crypto to land in the browser

* Avoid prefetching as it has Crypto code bundled in AvatarGroup

* Use md5 directly if avatar not available

Co-authored-by: Bailey Pumfleet <pumfleet@hey.com>
2022-03-05 15:28:24 +00:00

32 lines
1 KiB
TypeScript

import { User } from "@prisma/client";
import classNames from "@lib/classNames";
export type AvatarProps = {
user: Pick<User, "name" | "username" | "avatar"> & { emailMd5?: string };
className?: string;
size?: number;
title?: string;
alt: string;
};
// defaultAvatarSrc from profile.tsx can't be used as it imports crypto
function defaultAvatarSrc({ md5 }) {
return `https://www.gravatar.com/avatar/${md5}?s=160&d=identicon&r=PG`;
}
// An SSR Supported version of Avatar component.
// FIXME: title support is missing
export function AvatarSSR(props: AvatarProps) {
const { user, size } = props;
const nameOrUsername = user.name || user.username || "";
const className = classNames("rounded-full", props.className, size && `h-${size} w-${size}`);
let imgSrc;
const alt = props.alt || nameOrUsername;
if (user.avatar) {
imgSrc = user.avatar;
} else if (user.emailMd5) {
imgSrc = defaultAvatarSrc({ md5: user.emailMd5 });
}
return imgSrc ? <img alt={alt} className={className} src={imgSrc}></img> : null;
}