Skip to content

Commit

Permalink
feat: 👤 Implement gravatar (#1268)
Browse files Browse the repository at this point in the history
* ✨ Implement gravatar

* ♻️  refact gravatar url generation with a custom hook

* review: do not add a default value to the email if undefined
  • Loading branch information
matthieujacq authored Sep 27, 2023
1 parent b2ad9ea commit 7470d38
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import Image from "next/image";
import Link from "next/link";
import { MdPerson } from "react-icons/md";

import { useSupabase } from "@/lib/context/SupabaseProvider";

import { useGravatar } from "../hooks/useGravatar";
import { sidebarLinkStyle } from "../styles/SidebarLinkStyle";

export const UserButton = (): JSX.Element => {
const { session } = useSupabase();
const { gravatarUrl } = useGravatar();

return (
<Link aria-label="account" className={sidebarLinkStyle} href={"/user"}>
<MdPerson className="text-4xl" />
<div className="relative w-8 h-8">
<Image
alt="gravatar"
layout="fill"
src={gravatarUrl}
className="rounded-xl"
/>
</div>
<span className="text-ellipsis overflow-hidden">
{session?.user.email ?? ""}
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createHash } from "crypto";
import { useEffect, useState } from "react";

import { useSupabase } from "@/lib/context/SupabaseProvider";

// Gravatar images may be requested just like a normal image, using an IMG tag. To get an image specific to a user, you must first calculate their email hash.
// The most basic image request URL looks like this:
// https://www.gravatar.com/avatar/HASH

const computeGravatarUrl = (email?: string) => {
const hash = createHash("md5")
.update(typeof email === "string" ? email.trim().toLowerCase() : "")
.digest("hex");

return `https://www.gravatar.com/avatar/${hash}?d=mp`;
};

export const useGravatar = (): { gravatarUrl: string } => {
const { session } = useSupabase();
const [gravatarUrl, setGravatarUrl] = useState<string>(computeGravatarUrl());

const email = session?.user.email;

useEffect(() => {
const computedUrl = computeGravatarUrl(email);
setGravatarUrl(computedUrl);
}, [email]);

return { gravatarUrl };
};
6 changes: 5 additions & 1 deletion frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const nextConfig = {
images: {
domains: ["www.quivr.app", "quivr-cms.s3.eu-west-3.amazonaws.com"]
domains: [
"www.quivr.app",
"quivr-cms.s3.eu-west-3.amazonaws.com",
"www.gravatar.com",
],
},
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
async headers() {
Expand Down

0 comments on commit 7470d38

Please sign in to comment.