2022-07-23 00:39:50 +00:00
|
|
|
import * as AvatarPrimitive from "@radix-ui/react-avatar";
|
2023-04-13 10:59:32 +00:00
|
|
|
import * as TooltipPrimitive from "@radix-ui/react-tooltip";
|
2023-01-21 11:41:49 +00:00
|
|
|
import Link from "next/link";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
import { defaultAvatarSrc } from "@calcom/lib/defaultAvatarImage";
|
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Maybe } from "@trpc/server";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2023-04-12 15:26:31 +00:00
|
|
|
import { Check } from "../icon";
|
2023-04-13 10:59:32 +00:00
|
|
|
import { Tooltip } from "../tooltip";
|
2023-01-26 22:51:03 +00:00
|
|
|
|
2022-07-23 00:39:50 +00:00
|
|
|
export type AvatarProps = {
|
|
|
|
className?: string;
|
2023-01-21 11:41:49 +00:00
|
|
|
size: "xs" | "sm" | "md" | "mdLg" | "lg" | "xl";
|
2022-07-23 00:39:50 +00:00
|
|
|
imageSrc?: Maybe<string>;
|
|
|
|
title?: string;
|
|
|
|
alt: string;
|
2023-01-21 11:41:49 +00:00
|
|
|
href?: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
gravatarFallbackMd5?: string;
|
2022-12-22 12:35:01 +00:00
|
|
|
fallback?: React.ReactNode;
|
2022-07-23 00:39:50 +00:00
|
|
|
accepted?: boolean;
|
2022-12-22 12:35:01 +00:00
|
|
|
asChild?: boolean; // Added to ignore the outer span on the fallback component - messes up styling
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
const sizesPropsBySize = {
|
2022-12-22 12:35:01 +00:00
|
|
|
xs: "w-4 h-4", // 16px
|
|
|
|
sm: "w-6 h-6", // 24px
|
|
|
|
md: "w-8 h-8", // 32px
|
|
|
|
mdLg: "w-10 h-10", //40px
|
|
|
|
lg: "w-16 h-16", // 64px
|
2023-01-21 11:41:49 +00:00
|
|
|
xl: "w-24 h-24", // 96px
|
2022-08-25 19:35:01 +00:00
|
|
|
} as const;
|
2022-08-24 20:18:42 +00:00
|
|
|
|
2022-11-04 15:40:46 +00:00
|
|
|
export function Avatar(props: AvatarProps) {
|
2023-01-21 11:41:49 +00:00
|
|
|
const { imageSrc, gravatarFallbackMd5, size, alt, title, href } = props;
|
|
|
|
const rootClass = classNames("aspect-square rounded-full", sizesPropsBySize[size]);
|
|
|
|
let avatar = (
|
2022-08-25 19:35:01 +00:00
|
|
|
<AvatarPrimitive.Root
|
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"bg-emphasis item-center relative inline-flex aspect-square justify-center overflow-hidden rounded-full",
|
2023-01-24 16:10:43 +00:00
|
|
|
props.className,
|
|
|
|
sizesPropsBySize[size]
|
2022-08-25 19:35:01 +00:00
|
|
|
)}>
|
2022-12-22 12:35:01 +00:00
|
|
|
<>
|
2023-01-21 11:41:49 +00:00
|
|
|
<AvatarPrimitive.Image
|
|
|
|
src={imageSrc ?? undefined}
|
|
|
|
alt={alt}
|
|
|
|
className={classNames("aspect-square rounded-full", sizesPropsBySize[size])}
|
|
|
|
/>
|
2022-12-22 12:35:01 +00:00
|
|
|
<AvatarPrimitive.Fallback delayMs={600} asChild={props.asChild}>
|
|
|
|
<>
|
|
|
|
{props.fallback && !gravatarFallbackMd5 && props.fallback}
|
|
|
|
{gravatarFallbackMd5 && (
|
|
|
|
<img src={defaultAvatarSrc({ md5: gravatarFallbackMd5 })} alt={alt} className={rootClass} />
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
</AvatarPrimitive.Fallback>
|
|
|
|
{props.accepted && (
|
|
|
|
<div
|
|
|
|
className={classNames(
|
2023-04-05 18:14:46 +00:00
|
|
|
"text-inverted absolute bottom-0 right-0 block rounded-full bg-green-400 ring-2 ring-white",
|
2022-12-22 12:35:01 +00:00
|
|
|
size === "lg" ? "h-5 w-5" : "h-2 w-2"
|
|
|
|
)}>
|
|
|
|
<div className="flex h-full items-center justify-center p-[2px]">
|
2023-04-12 15:26:31 +00:00
|
|
|
{size === "lg" && <Check />}
|
2022-12-22 12:35:01 +00:00
|
|
|
</div>
|
2022-07-23 00:39:50 +00:00
|
|
|
</div>
|
2022-12-22 12:35:01 +00:00
|
|
|
)}
|
|
|
|
</>
|
2022-07-23 00:39:50 +00:00
|
|
|
</AvatarPrimitive.Root>
|
|
|
|
);
|
|
|
|
|
2023-01-21 11:41:49 +00:00
|
|
|
if (href) {
|
|
|
|
avatar = <Link href={href}>{avatar}</Link>;
|
|
|
|
}
|
|
|
|
|
2022-07-23 00:39:50 +00:00
|
|
|
return title ? (
|
2023-04-13 10:59:32 +00:00
|
|
|
<TooltipPrimitive.Provider>
|
|
|
|
<Tooltip content={title}>{avatar}</Tooltip>
|
|
|
|
</TooltipPrimitive.Provider>
|
2022-07-23 00:39:50 +00:00
|
|
|
) : (
|
|
|
|
<>{avatar}</>
|
|
|
|
);
|
|
|
|
}
|