Use gravatar with user email (#1688)

*  Use gravatar with user email
This commit is contained in:
Tagaishi
2023-12-07 18:15:07 +01:00
committed by GitHub
parent d6af6af0b6
commit 51e96c0ccf

View File

@@ -8,6 +8,7 @@ import {
IconSun, IconSun,
IconUserCog, IconUserCog,
} from '@tabler/icons-react'; } from '@tabler/icons-react';
import { createHash } from 'crypto';
import { User } from 'next-auth'; import { User } from 'next-auth';
import { signOut, useSession } from 'next-auth/react'; import { signOut, useSession } from 'next-auth/react';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
@@ -26,7 +27,6 @@ export const AvatarMenu = () => {
const defaultBoardHref = useBoardLink('/board'); const defaultBoardHref = useBoardLink('/board');
return ( return (
<>
<UnstyledButton> <UnstyledButton>
<Menu width={256}> <Menu width={256}>
<Menu.Target> <Menu.Target>
@@ -85,7 +85,6 @@ export const AvatarMenu = () => {
</Menu.Dropdown> </Menu.Dropdown>
</Menu> </Menu>
</UnstyledButton> </UnstyledButton>
</>
); );
}; };
@@ -93,12 +92,29 @@ type CurrentUserAvatarProps = {
user: User | null; user: User | null;
}; };
const getGravatar = (email?: string | undefined | null) => {
if (!email) return null;
const emailHash = createHash('sha256').update(email.trim().toLowerCase()).digest('hex');
return `https://gravatar.com/avatar/${emailHash}?d=null`;
};
const CurrentUserAvatar = forwardRef<HTMLDivElement, CurrentUserAvatarProps>( const CurrentUserAvatar = forwardRef<HTMLDivElement, CurrentUserAvatarProps>(
({ user, ...others }, ref) => { ({ user, ...others }, ref) => {
const { primaryColor } = useMantineTheme(); const { primaryColor } = useMantineTheme();
if (!user) return <Avatar ref={ref} {...others} />; const { fn } = useMantineTheme();
const border = fn.variant({ variant: 'default' }).border;
if (!user)
return <Avatar ref={ref} styles={{ root: { border: `1px solid ${border}` } }} {...others} />;
return ( return (
<Avatar ref={ref} color={primaryColor} {...others}> <Avatar
ref={ref}
color={primaryColor}
src={getGravatar(user.email)}
styles={{ root: { border: `1px solid ${border}` } }}
{...others}
>
{user.name?.slice(0, 2).toUpperCase()} {user.name?.slice(0, 2).toUpperCase()}
</Avatar> </Avatar>
); );