-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ 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
1 parent
b2ad9ea
commit 7470d38
Showing
3 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
13 changes: 11 additions & 2 deletions
13
frontend/app/chat/components/ChatsList/components/ChatsListItem/components/UserButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
frontend/app/chat/components/ChatsList/components/ChatsListItem/hooks/useGravatar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters