Skip to content

Commit

Permalink
feat: support gravatar
Browse files Browse the repository at this point in the history
fix #590
  • Loading branch information
devrsi0n committed Oct 15, 2024
1 parent 814ec2f commit 070a929
Show file tree
Hide file tree
Showing 5 changed files with 16,230 additions and 12,506 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bundle-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ jobs:
- name: Set up node
uses: actions/setup-node@v3
with:
node-version: '16.x'
node-version: '20.x'

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: ^7.4.1
version: ^8.14.0
run_install: false

- name: Get pnpm store directory
Expand Down
70 changes: 0 additions & 70 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"debounce-promise": "3.1.2",
"framer-motion": "10.16.5",
"lucide-react": "0.265.0",
"md5": "2.3.0",
"next": "14.1.1",
"next-auth": "4.24.5",
"next-axiom": "0.17.0",
Expand Down Expand Up @@ -79,6 +80,7 @@
"@types/canvas-confetti": "1.6.3",
"@types/debounce-promise": "3.1.9",
"@types/jest": "29.5.8",
"@types/md5": "2.3.5",
"@types/node": "18.16.18",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.15",
Expand Down
33 changes: 33 additions & 0 deletions packages/ui/src/components/avatar/avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import clsx from 'clsx';
import md5 from 'md5';
import * as React from 'react';

import { IconUser } from '../icons';
Expand Down Expand Up @@ -36,6 +37,20 @@ export function Avatar({
}: AvatarProps): JSX.Element {
alt ||= 'Default avatar';
const [sizeStyle, iconSize] = sizeStyles[size];
const [gravatarUrl, setGravatarUrl] = React.useState('');

React.useEffect(() => {
if (email && !src) {
const img = new Image();
const url = `https://www.gravatar.com/avatar/${md5(
email.toLowerCase().trim(),
)}?s=${iconSize}&d=404`;
img.src = url;
img.addEventListener('load', () => setGravatarUrl(url));
// img.onerror = () => {};
}
}, [email, src, iconSize]);

if (!src) {
const placeholder = (
<IconUser
Expand All @@ -44,6 +59,24 @@ export function Avatar({
aria-label={alt}
/>
);

if (gravatarUrl) {
return (
<img
src={gravatarUrl}
alt={alt}
className={clsx(
bg,
ring,
`flex select-none items-center justify-center rounded-full`,
sizeStyle,
className,
)}
{...imgProps}
/>
);
}

// Keep this order to reduce name collision
const customAvatarValue = username || email || name;
if (customAvatarValue) {
Expand Down
Loading

0 comments on commit 070a929

Please sign in to comment.