2021-09-14 08:45:28 +00:00
|
|
|
import * as AvatarPrimitive from "@radix-ui/react-avatar";
|
|
|
|
import * as Tooltip from "@radix-ui/react-tooltip";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
import classNames from "@lib/classNames";
|
2021-09-22 19:52:38 +00:00
|
|
|
import { defaultAvatarSrc } from "@lib/profile";
|
2021-09-14 08:45:28 +00:00
|
|
|
|
2021-10-14 19:22:01 +00:00
|
|
|
import { Maybe } from "@trpc/server";
|
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
export type AvatarProps = {
|
|
|
|
className?: string;
|
2021-09-27 14:47:55 +00:00
|
|
|
size?: number;
|
|
|
|
imageSrc?: Maybe<string>;
|
2021-09-14 08:45:28 +00:00
|
|
|
title?: string;
|
|
|
|
alt: string;
|
|
|
|
gravatarFallbackMd5?: string;
|
|
|
|
};
|
|
|
|
|
2021-09-27 14:47:55 +00:00
|
|
|
export default function Avatar(props: AvatarProps) {
|
|
|
|
const { imageSrc, gravatarFallbackMd5, size, alt, title } = props;
|
|
|
|
const className = classNames("rounded-full", props.className, size && `h-${size} w-${size}`);
|
2021-09-14 08:45:28 +00:00
|
|
|
const avatar = (
|
|
|
|
<AvatarPrimitive.Root>
|
|
|
|
<AvatarPrimitive.Image
|
2021-09-27 14:47:55 +00:00
|
|
|
src={imageSrc ?? undefined}
|
2021-09-14 08:45:28 +00:00
|
|
|
alt={alt}
|
2021-09-14 13:27:41 +00:00
|
|
|
className={classNames("rounded-full", `h-auto w-${size}`, props.className)}
|
2021-09-14 08:45:28 +00:00
|
|
|
/>
|
|
|
|
<AvatarPrimitive.Fallback delayMs={600}>
|
|
|
|
{gravatarFallbackMd5 && (
|
|
|
|
<img src={defaultAvatarSrc({ md5: gravatarFallbackMd5 })} alt={alt} className={className} />
|
|
|
|
)}
|
|
|
|
</AvatarPrimitive.Fallback>
|
|
|
|
</AvatarPrimitive.Root>
|
|
|
|
);
|
|
|
|
|
|
|
|
return title ? (
|
2021-09-14 13:27:41 +00:00
|
|
|
<Tooltip.Tooltip delayDuration={300}>
|
2021-09-14 08:45:28 +00:00
|
|
|
<Tooltip.TooltipTrigger className="cursor-default">{avatar}</Tooltip.TooltipTrigger>
|
2022-02-23 11:09:22 +00:00
|
|
|
<Tooltip.Content className="rounded-sm bg-black p-2 text-sm text-white shadow-sm">
|
2021-09-14 08:45:28 +00:00
|
|
|
<Tooltip.Arrow />
|
|
|
|
{title}
|
|
|
|
</Tooltip.Content>
|
|
|
|
</Tooltip.Tooltip>
|
|
|
|
) : (
|
|
|
|
<>{avatar}</>
|
|
|
|
);
|
|
|
|
}
|