From 51e96c0ccfc283d55f0ddd028d4653ba62fa7952 Mon Sep 17 00:00:00 2001 From: Tagaishi Date: Thu, 7 Dec 2023 18:15:07 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Use=20gravatar=20with=20user=20emai?= =?UTF-8?q?l=20(#1688)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ Use gravatar with user email --- src/components/layout/header/AvatarMenu.tsx | 132 +++++++++++--------- 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/src/components/layout/header/AvatarMenu.tsx b/src/components/layout/header/AvatarMenu.tsx index e0ea38d011f..f9d6b4d0de4 100644 --- a/src/components/layout/header/AvatarMenu.tsx +++ b/src/components/layout/header/AvatarMenu.tsx @@ -8,6 +8,7 @@ import { IconSun, IconUserCog, } from '@tabler/icons-react'; +import { createHash } from 'crypto'; import { User } from 'next-auth'; import { signOut, useSession } from 'next-auth/react'; import { useTranslation } from 'next-i18next'; @@ -26,66 +27,64 @@ export const AvatarMenu = () => { const defaultBoardHref = useBoardLink('/board'); return ( - <> - - - - - - - } - onClick={toggleColorScheme} - > - {t('actions.avatar.switchTheme')} - - {sessionData?.user && ( - <> - } - > - {t('actions.avatar.preferences')} - - } - > - {t('actions.avatar.defaultBoard')} - - }> - {t('actions.avatar.manage')} - - - - )} - {sessionData?.user ? ( + + + + + + + } + onClick={toggleColorScheme} + > + {t('actions.avatar.switchTheme')} + + {sessionData?.user && ( + <> } - color="red" - onClick={() => - signOut({ - redirect: false, - }).then(() => window.location.reload()) - } + component={Link} + passHref + href="/user/preferences" + icon={} > - {t('actions.avatar.logout', { - username: sessionData.user.name, - })} + {t('actions.avatar.preferences')} - ) : ( - } component={Link} href="/auth/login"> - {t('actions.avatar.login')} + } + > + {t('actions.avatar.defaultBoard')} + + }> + {t('actions.avatar.manage')} - )} - - - - + + + )} + {sessionData?.user ? ( + } + color="red" + onClick={() => + signOut({ + redirect: false, + }).then(() => window.location.reload()) + } + > + {t('actions.avatar.logout', { + username: sessionData.user.name, + })} + + ) : ( + } component={Link} href="/auth/login"> + {t('actions.avatar.login')} + + )} + + + ); }; @@ -93,12 +92,29 @@ type CurrentUserAvatarProps = { 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( ({ user, ...others }, ref) => { const { primaryColor } = useMantineTheme(); - if (!user) return ; + const { fn } = useMantineTheme(); + const border = fn.variant({ variant: 'default' }).border; + + if (!user) + return ; + return ( - + {user.name?.slice(0, 2).toUpperCase()} );