infoalloggi-monorepo/apps/infoalloggi/src/components/user_avatar.tsx

42 lines
1 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { colorIsDarkSimple } from "~/components/etichette";
2025-08-28 18:27:07 +02:00
import { Avatar, AvatarFallback } from "~/components/ui/avatar";
2025-08-04 17:45:44 +02:00
import { cn, getHexColorFromString, getInitials } from "~/lib/utils";
import type { UsersId } from "~/schemas/public/Users";
const colorOptions = new Map<string, string>();
export const getUserColor = (str: string) => {
2025-08-28 18:27:07 +02:00
if (colorOptions.has(str)) {
// biome-ignore lint/style/noNonNullAssertion: <exists>
return colorOptions.get(str)!;
}
const usercolor = getHexColorFromString(str);
colorOptions.set(str, usercolor);
return usercolor;
2025-08-04 17:45:44 +02:00
};
export const UserAvatar = ({
2025-08-28 18:27:07 +02:00
userId,
username,
className,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
userId: UsersId;
username?: string;
className?: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
const userColor = getUserColor(userId);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<Avatar className={cn("size-20", className)}>
<AvatarFallback
style={{
backgroundColor: userColor,
color: colorIsDarkSimple(userColor) ? "#FFFFFF" : "#000000",
}}
>
{username && getInitials(username)}
</AvatarFallback>
</Avatar>
);
2025-08-04 17:45:44 +02:00
};