2022-02-11 14:34:12 +00:00
|
|
|
import { User } from "@prisma/client";
|
2022-07-14 10:28:46 +00:00
|
|
|
import * as Tooltip from "@radix-ui/react-tooltip";
|
2022-02-11 14:34:12 +00:00
|
|
|
|
|
|
|
import classNames from "@lib/classNames";
|
|
|
|
|
2022-07-14 10:28:46 +00:00
|
|
|
export type AvatarProps = (
|
|
|
|
| {
|
|
|
|
user: Pick<User, "name" | "username" | "avatar"> & { emailMd5?: string };
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
user?: null;
|
|
|
|
imageSrc: string;
|
|
|
|
}
|
|
|
|
) & {
|
2022-02-11 14:34:12 +00:00
|
|
|
className?: string;
|
|
|
|
size?: number;
|
|
|
|
title?: string;
|
|
|
|
alt: string;
|
|
|
|
};
|
|
|
|
|
2022-03-05 15:28:24 +00:00
|
|
|
// defaultAvatarSrc from profile.tsx can't be used as it imports crypto
|
2022-04-04 20:26:14 +00:00
|
|
|
function defaultAvatarSrc(md5: string) {
|
2022-09-05 10:23:39 +00:00
|
|
|
return `https://www.gravatar.com/avatar/${md5}?s=160&d=mp&r=PG`;
|
2022-03-05 15:28:24 +00:00
|
|
|
}
|
|
|
|
|
2022-02-11 14:34:12 +00:00
|
|
|
// An SSR Supported version of Avatar component.
|
|
|
|
export function AvatarSSR(props: AvatarProps) {
|
2022-07-14 10:28:46 +00:00
|
|
|
const { size, title } = props;
|
|
|
|
|
|
|
|
let imgSrc = "";
|
|
|
|
let alt: string = props.alt;
|
|
|
|
|
|
|
|
if (props.user) {
|
|
|
|
const user = props.user;
|
|
|
|
const nameOrUsername = user.name || user.username || "";
|
|
|
|
alt = alt || nameOrUsername;
|
|
|
|
|
|
|
|
if (user.avatar) {
|
|
|
|
imgSrc = user.avatar;
|
|
|
|
} else if (user.emailMd5) {
|
|
|
|
imgSrc = defaultAvatarSrc(user.emailMd5);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
imgSrc = props.imageSrc;
|
2022-02-11 14:34:12 +00:00
|
|
|
}
|
2022-07-14 10:28:46 +00:00
|
|
|
|
|
|
|
const className = classNames("rounded-full", props.className, size && `h-${size} w-${size}`);
|
|
|
|
|
|
|
|
const avatar = imgSrc ? <img alt={alt} className={className} src={imgSrc} /> : null;
|
|
|
|
return title ? (
|
|
|
|
<Tooltip.Tooltip delayDuration={300}>
|
|
|
|
<Tooltip.TooltipTrigger className="cursor-default">{avatar}</Tooltip.TooltipTrigger>
|
2022-07-15 16:39:11 +00:00
|
|
|
<Tooltip.Content className="rounded-sm bg-black p-2 text-sm text-white">
|
2022-07-14 10:28:46 +00:00
|
|
|
<Tooltip.Arrow />
|
|
|
|
{title}
|
|
|
|
</Tooltip.Content>
|
|
|
|
</Tooltip.Tooltip>
|
|
|
|
) : (
|
|
|
|
<>{avatar}</>
|
|
|
|
);
|
2022-02-11 14:34:12 +00:00
|
|
|
}
|