Files
zitadel/apps/login/ui/Avatar.tsx

94 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-05-19 13:02:09 +02:00
"use client";
import { ColorShade, getColorHash } from "#/utils/colors";
import { useTheme } from "next-themes";
interface AvatarProps {
name: string | null | undefined;
loginName: string;
imageUrl?: string;
2023-05-19 13:02:09 +02:00
size?: "small" | "base" | "large";
shadow?: boolean;
}
2023-05-24 17:00:42 +02:00
function getInitials(name: string, loginName: string) {
let credentials = "";
if (name) {
const split = name.split(" ");
2023-05-17 15:25:25 +02:00
if (split) {
const initials =
split[0].charAt(0) + (split[1] ? split[1].charAt(0) : "");
credentials = initials;
} else {
2023-05-19 13:02:09 +02:00
credentials = name.charAt(0);
2023-05-17 15:25:25 +02:00
}
} else {
const username = loginName.split("@")[0];
let separator = "_";
if (username.includes("-")) {
separator = "-";
}
if (username.includes(".")) {
separator = ".";
}
const split = username.split(separator);
const initials = split[0].charAt(0) + (split[1] ? split[1].charAt(0) : "");
credentials = initials;
}
2023-05-19 13:02:09 +02:00
return credentials;
}
export function Avatar({
size = "base",
name,
loginName,
imageUrl,
shadow,
}: AvatarProps) {
const { resolvedTheme } = useTheme();
2023-05-24 17:00:42 +02:00
const credentials = getInitials(name ?? loginName, loginName);
2023-05-19 13:02:09 +02:00
2023-04-24 15:32:57 +02:00
const color: ColorShade = getColorHash(loginName);
2023-05-19 13:02:09 +02:00
const avatarStyleDark = {
backgroundColor: color[900],
color: color[200],
};
2023-05-19 13:02:09 +02:00
const avatarStyleLight = {
backgroundColor: color[200],
color: color[900],
};
return (
<div
2023-04-25 09:16:19 +02:00
className={`w-full h-full flex-shrink-0 flex justify-center items-center cursor-default pointer-events-none group-focus:outline-none group-focus:ring-2 transition-colors duration-200 dark:group-focus:ring-offset-blue bg-primary-light-500 text-primary-light-contrast-500 hover:bg-primary-light-400 hover:dark:bg-primary-dark-500 group-focus:ring-primary-light-200 dark:group-focus:ring-primary-dark-400 dark:bg-primary-dark-300 dark:text-primary-dark-contrast-300 dark:text-blue rounded-full ${
shadow ? "shadow" : ""
} ${
2023-05-19 13:02:09 +02:00
size === "large"
? "h-20 w-20 font-normal"
2023-05-19 13:02:09 +02:00
: size === "base"
2023-04-24 15:32:57 +02:00
? "w-[38px] h-[38px] font-bold"
2023-05-19 13:02:09 +02:00
: size === "small"
? "w-[32px] h-[32px] font-bold text-[13px]"
: ""
}`}
2023-05-19 13:02:09 +02:00
style={resolvedTheme === "light" ? avatarStyleLight : avatarStyleDark}
>
{imageUrl ? (
<img
2023-04-24 09:54:47 +02:00
className="border border-divider-light dark:border-divider-dark rounded-full w-12 h-12"
src={imageUrl}
/>
) : (
<span
2023-05-19 13:02:09 +02:00
className={`uppercase ${size === "large" ? "text-xl" : "text-13px"}`}
>
{credentials}
</span>
)}
</div>
);
2023-05-19 13:02:09 +02:00
}