Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions apps/nextjs/src/components/user-avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import type { MantineSize } from "@mantine/core";

import { auth } from "@homarr/auth/next";
import { UserAvatar } from "@homarr/ui";
import { UserAvatar, UserProps } from "@homarr/ui";

interface UserAvatarProps {
interface CurrentUserAvatarProps {
size: MantineSize;
}

export const CurrentUserAvatar = async ({ size }: UserAvatarProps) => {
export const CurrentUserAvatar = async ({ size }: CurrentUserAvatarProps) => {
const currentSession = await auth();

const user = {
const user: UserProps = {
name: currentSession?.user.name ?? null,
image: currentSession?.user.image ?? null,
email: currentSession?.user.email ?? null,
};

return <UserAvatar user={user} size={size} />;
Expand Down
10 changes: 10 additions & 0 deletions packages/ui/src/components/user-avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"use client";

import type { AvatarProps } from "@mantine/core";
import { Avatar } from "@mantine/core";
import { createHash } from "crypto";

export interface UserProps {
name: string | null;
image: string | null;
email?: string | null;
}

interface UserAvatarProps {
Expand All @@ -13,9 +17,15 @@ interface UserAvatarProps {

export const UserAvatar = ({ user, size }: UserAvatarProps) => {
if (!user?.name) return <Avatar size={size} />;

if (user.image) {
return <Avatar src={user.image} alt={user.name} size={size} />;
}

if (user.email) {
const emailHash = createHash("md5").update(user.email.trim().toLowerCase()).digest("hex");
return <Avatar src={`https://seccdn.libravatar.org/avatar/${emailHash}?d=404`} alt={user.name} size={size} />;
}
Comment on lines +25 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to check if the usage of crypto actually works


return <Avatar name={user.name} color="initials" size={size}></Avatar>;
};