2022-07-23 00:39:50 +00:00
|
|
|
import * as AvatarPrimitive from "@radix-ui/react-avatar";
|
|
|
|
import * as Tooltip 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-01-23 23:08:01 +00:00
|
|
|
import { FiCheck } from "@calcom/ui/components/icon";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import { Maybe } from "@trpc/server";
|
|
|
|
|
|
|
|
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-01-21 11:41:49 +00:00
|
|
|
"dark:bg-darkgray-300 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(
|
|
|
|
"absolute bottom-0 right-0 block rounded-full bg-green-400 text-white ring-2 ring-white",
|
|
|
|
size === "lg" ? "h-5 w-5" : "h-2 w-2"
|
|
|
|
)}>
|
|
|
|
<div className="flex h-full items-center justify-center p-[2px]">
|
2023-01-23 23:08:01 +00:00
|
|
|
{size === "lg" && <FiCheck />}
|
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 ? (
|
2022-09-01 10:09:08 +00:00
|
|
|
<Tooltip.Provider>
|
|
|
|
<Tooltip.Tooltip delayDuration={300}>
|
|
|
|
<Tooltip.TooltipTrigger className="cursor-default">{avatar}</Tooltip.TooltipTrigger>
|
2022-09-12 22:04:33 +00:00
|
|
|
<Tooltip.Content className="rounded-sm bg-black p-2 text-sm text-white shadow-sm">
|
2022-09-01 10:09:08 +00:00
|
|
|
<Tooltip.Arrow />
|
|
|
|
{title}
|
|
|
|
</Tooltip.Content>
|
|
|
|
</Tooltip.Tooltip>
|
|
|
|
</Tooltip.Provider>
|
2022-07-23 00:39:50 +00:00
|
|
|
) : (
|
|
|
|
<>{avatar}</>
|
|
|
|
);
|
|
|
|
}
|