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";
|
2023-09-05 17:56:02 +00:00
|
|
|
import { AVATAR_FALLBACK } from "@calcom/lib/constants";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { Maybe } from "@trpc/server";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
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-08-15 21:07:38 +00:00
|
|
|
size?: "xxs" | "xs" | "xsm" | "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-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
|
2023-09-07 15:26:40 +00:00
|
|
|
indicator?: React.ReactNode;
|
2023-09-29 20:25:56 +00:00
|
|
|
"data-testid"?: string;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
2022-08-24 20:18:42 +00:00
|
|
|
const sizesPropsBySize = {
|
2023-06-26 10:37:59 +00:00
|
|
|
xxs: "w-3.5 h-3.5 min-w-3.5 min-h-3.5", // 14px
|
2023-06-18 20:54:57 +00:00
|
|
|
xs: "w-4 h-4 min-w-4 min-h-4 max-h-4", // 16px
|
|
|
|
xsm: "w-5 h-5 min-w-5 min-h-5", // 20px
|
2023-06-15 10:42:47 +00:00
|
|
|
sm: "w-6 h-6 min-w-6 min-h-6", // 24px
|
|
|
|
md: "w-8 h-8 min-w-8 min-h-8", // 32px
|
|
|
|
mdLg: "w-10 h-10 min-w-10 min-h-10", //40px
|
|
|
|
lg: "w-16 h-16 min-w-16 min-h-16", // 64px
|
|
|
|
xl: "w-24 h-24 min-w-24 min-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-09-07 15:26:40 +00:00
|
|
|
const { imageSrc, size = "md", alt, title, href, indicator } = props;
|
2023-01-21 11:41:49 +00:00
|
|
|
const rootClass = classNames("aspect-square rounded-full", sizesPropsBySize[size]);
|
|
|
|
let avatar = (
|
2022-08-25 19:35:01 +00:00
|
|
|
<AvatarPrimitive.Root
|
2023-09-29 20:25:56 +00:00
|
|
|
data-testid={props?.["data-testid"]}
|
2022-08-25 19:35:01 +00:00
|
|
|
className={classNames(
|
2023-09-07 15:26:40 +00:00
|
|
|
"bg-emphasis item-center relative inline-flex aspect-square justify-center rounded-full",
|
|
|
|
indicator ? "overflow-visible" : "overflow-hidden",
|
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])}
|
|
|
|
/>
|
2023-08-30 20:42:34 +00:00
|
|
|
<AvatarPrimitive.Fallback
|
|
|
|
delayMs={600}
|
|
|
|
asChild={props.asChild}
|
|
|
|
className="flex h-full items-center justify-center">
|
2022-12-22 12:35:01 +00:00
|
|
|
<>
|
2023-09-05 17:56:02 +00:00
|
|
|
{props.fallback ? props.fallback : <img src={AVATAR_FALLBACK} alt={alt} className={rootClass} />}
|
2022-12-22 12:35:01 +00:00
|
|
|
</>
|
|
|
|
</AvatarPrimitive.Fallback>
|
2023-09-07 15:26:40 +00:00
|
|
|
{indicator}
|
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}</>
|
|
|
|
);
|
|
|
|
}
|