feat: integrate Avatar component across various modules for improved user representation and enhance profile page with Gravatar support

This commit is contained in:
Julien Froidefond
2025-11-28 10:52:48 +01:00
parent cb4873cd40
commit ff7c846ed1
9 changed files with 108 additions and 27 deletions

View File

@@ -3,6 +3,7 @@ import { redirect } from 'next/navigation';
import { getUserById } from '@/services/auth'; import { getUserById } from '@/services/auth';
import { ProfileForm } from './ProfileForm'; import { ProfileForm } from './ProfileForm';
import { PasswordForm } from './PasswordForm'; import { PasswordForm } from './PasswordForm';
import { getGravatarUrl } from '@/lib/gravatar';
export default async function ProfilePage() { export default async function ProfilePage() {
const session = await auth(); const session = await auth();
@@ -19,17 +20,36 @@ export default async function ProfilePage() {
return ( return (
<main className="mx-auto max-w-2xl px-4 py-8"> <main className="mx-auto max-w-2xl px-4 py-8">
<div className="mb-8"> <div className="mb-8 flex items-center gap-6">
<h1 className="text-3xl font-bold text-foreground">Mon Profil</h1> <img
<p className="mt-1 text-muted">Gérez vos informations personnelles</p> src={getGravatarUrl(user.email, 160)}
alt={user.name || user.email}
width={80}
height={80}
className="rounded-full border-2 border-border"
/>
<div>
<h1 className="text-3xl font-bold text-foreground">Mon Profil</h1>
<p className="mt-1 text-muted">Gérez vos informations personnelles</p>
</div>
</div> </div>
<div className="space-y-8"> <div className="space-y-8">
{/* Profile Info */} {/* Profile Info */}
<section className="rounded-xl border border-border bg-card p-6"> <section className="rounded-xl border border-border bg-card p-6">
<h2 className="mb-6 text-xl font-semibold text-foreground"> <div className="mb-6 flex items-start justify-between">
Informations personnelles <h2 className="text-xl font-semibold text-foreground">
</h2> Informations personnelles
</h2>
<a
href="https://gravatar.com"
target="_blank"
rel="noopener noreferrer"
className="text-xs text-muted hover:text-primary transition-colors"
>
Changer mon avatar sur Gravatar
</a>
</div>
<ProfileForm <ProfileForm
initialData={{ initialData={{
name: user.name || '', name: user.name || '',

View File

@@ -5,6 +5,7 @@ import { useSessionLive, type LiveEvent } from '@/hooks/useSessionLive';
import { LiveIndicator } from './LiveIndicator'; import { LiveIndicator } from './LiveIndicator';
import { ShareModal } from './ShareModal'; import { ShareModal } from './ShareModal';
import { Button } from '@/components/ui/Button'; import { Button } from '@/components/ui/Button';
import { Avatar } from '@/components/ui/Avatar';
import type { ShareRole } from '@prisma/client'; import type { ShareRole } from '@prisma/client';
interface ShareUser { interface ShareUser {
@@ -84,13 +85,13 @@ export function SessionLiveWrapper({
{shares.length > 0 && ( {shares.length > 0 && (
<div className="flex -space-x-2"> <div className="flex -space-x-2">
{shares.slice(0, 3).map((share) => ( {shares.slice(0, 3).map((share) => (
<div <Avatar
key={share.id} key={share.id}
className="flex h-8 w-8 items-center justify-center rounded-full border-2 border-card bg-primary/10 text-xs font-medium text-primary" email={share.user.email}
title={share.user.name || share.user.email} name={share.user.name}
> size={32}
{share.user.name?.[0]?.toUpperCase() || share.user.email[0].toUpperCase()} className="border-2 border-card"
</div> />
))} ))}
{shares.length > 3 && ( {shares.length > 3 && (
<div className="flex h-8 w-8 items-center justify-center rounded-full border-2 border-card bg-muted/20 text-xs font-medium text-muted"> <div className="flex h-8 w-8 items-center justify-center rounded-full border-2 border-card bg-muted/20 text-xs font-medium text-muted">

View File

@@ -5,6 +5,7 @@ import { Modal } from '@/components/ui/Modal';
import { Input } from '@/components/ui/Input'; import { Input } from '@/components/ui/Input';
import { Button } from '@/components/ui/Button'; import { Button } from '@/components/ui/Button';
import { Badge } from '@/components/ui/Badge'; import { Badge } from '@/components/ui/Badge';
import { Avatar } from '@/components/ui/Avatar';
import { shareSessionAction, removeShareAction } from '@/actions/share'; import { shareSessionAction, removeShareAction } from '@/actions/share';
import type { ShareRole } from '@prisma/client'; import type { ShareRole } from '@prisma/client';
@@ -120,9 +121,7 @@ export function ShareModal({
className="flex items-center justify-between rounded-lg border border-border bg-card p-3" className="flex items-center justify-between rounded-lg border border-border bg-card p-3"
> >
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary/10 text-sm font-medium text-primary"> <Avatar email={share.user.email} name={share.user.name} size={32} />
{share.user.name?.[0]?.toUpperCase() || share.user.email[0].toUpperCase()}
</div>
<div> <div>
<p className="text-sm font-medium text-foreground"> <p className="text-sm font-medium text-foreground">
{share.user.name || share.user.email} {share.user.name || share.user.email}

View File

@@ -5,6 +5,7 @@ import { usePathname } from 'next/navigation';
import { useSession, signOut } from 'next-auth/react'; import { useSession, signOut } from 'next-auth/react';
import { useTheme } from '@/contexts/ThemeContext'; import { useTheme } from '@/contexts/ThemeContext';
import { useState } from 'react'; import { useState } from 'react';
import { Avatar } from '@/components/ui';
export function Header() { export function Header() {
const { theme, toggleTheme } = useTheme(); const { theme, toggleTheme } = useTheme();
@@ -109,8 +110,13 @@ export function Header() {
<div className="relative"> <div className="relative">
<button <button
onClick={() => setMenuOpen(!menuOpen)} onClick={() => setMenuOpen(!menuOpen)}
className="flex h-9 items-center gap-2 rounded-lg border border-border bg-card px-3 transition-colors hover:bg-card-hover" className="flex h-9 items-center gap-2 rounded-lg border border-border bg-card pl-1.5 pr-3 transition-colors hover:bg-card-hover"
> >
<Avatar
email={session.user.email!}
name={session.user.name}
size={24}
/>
<span className="text-sm font-medium text-foreground"> <span className="text-sm font-medium text-foreground">
{session.user.name || session.user.email?.split('@')[0]} {session.user.name || session.user.email?.split('@')[0]}
</span> </span>

View File

@@ -5,6 +5,7 @@ import { useMotivatorLive, type MotivatorLiveEvent } from '@/hooks/useMotivatorL
import { LiveIndicator } from '@/components/collaboration/LiveIndicator'; import { LiveIndicator } from '@/components/collaboration/LiveIndicator';
import { MotivatorShareModal } from './MotivatorShareModal'; import { MotivatorShareModal } from './MotivatorShareModal';
import { Button } from '@/components/ui/Button'; import { Button } from '@/components/ui/Button';
import { Avatar } from '@/components/ui/Avatar';
import type { ShareRole } from '@prisma/client'; import type { ShareRole } from '@prisma/client';
interface ShareUser { interface ShareUser {
@@ -84,13 +85,13 @@ export function MotivatorLiveWrapper({
{shares.length > 0 && ( {shares.length > 0 && (
<div className="flex -space-x-2"> <div className="flex -space-x-2">
{shares.slice(0, 3).map((share) => ( {shares.slice(0, 3).map((share) => (
<div <Avatar
key={share.id} key={share.id}
className="flex h-8 w-8 items-center justify-center rounded-full border-2 border-card bg-primary/10 text-xs font-medium text-primary" email={share.user.email}
title={share.user.name || share.user.email} name={share.user.name}
> size={32}
{share.user.name?.[0]?.toUpperCase() || share.user.email[0].toUpperCase()} className="border-2 border-card"
</div> />
))} ))}
{shares.length > 3 && ( {shares.length > 3 && (
<div className="flex h-8 w-8 items-center justify-center rounded-full border-2 border-card bg-muted/20 text-xs font-medium text-muted"> <div className="flex h-8 w-8 items-center justify-center rounded-full border-2 border-card bg-muted/20 text-xs font-medium text-muted">

View File

@@ -5,6 +5,7 @@ import { Modal } from '@/components/ui/Modal';
import { Input } from '@/components/ui/Input'; import { Input } from '@/components/ui/Input';
import { Button } from '@/components/ui/Button'; import { Button } from '@/components/ui/Button';
import { Badge } from '@/components/ui/Badge'; import { Badge } from '@/components/ui/Badge';
import { Avatar } from '@/components/ui/Avatar';
import { shareMotivatorSession, removeMotivatorShare } from '@/actions/moving-motivators'; import { shareMotivatorSession, removeMotivatorShare } from '@/actions/moving-motivators';
import type { ShareRole } from '@prisma/client'; import type { ShareRole } from '@prisma/client';
@@ -120,9 +121,7 @@ export function MotivatorShareModal({
className="flex items-center justify-between rounded-lg border border-border bg-card p-3" className="flex items-center justify-between rounded-lg border border-border bg-card p-3"
> >
<div className="flex items-center gap-3"> <div className="flex items-center gap-3">
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-primary/10 text-sm font-medium text-primary"> <Avatar email={share.user.email} name={share.user.name} size={32} />
{share.user.name?.[0]?.toUpperCase() || share.user.email[0].toUpperCase()}
</div>
<div> <div>
<p className="text-sm font-medium text-foreground"> <p className="text-sm font-medium text-foreground">
{share.user.name || share.user.email} {share.user.name || share.user.email}

View File

@@ -0,0 +1,31 @@
'use client';
import { getGravatarUrl, GravatarDefault } from '@/lib/gravatar';
interface AvatarProps {
email: string;
name?: string | null;
size?: number;
fallback?: GravatarDefault;
className?: string;
}
export function Avatar({
email,
name,
size = 32,
fallback = 'identicon',
className = '',
}: AvatarProps) {
return (
<img
src={getGravatarUrl(email, size * 2, fallback)} // 2x for retina displays
alt={name || email}
width={size}
height={size}
className={`rounded-full ${className}`}
loading="lazy"
/>
);
}

View File

@@ -1,7 +1,8 @@
export { Avatar } from './Avatar';
export { Badge } from './Badge';
export { Button } from './Button'; export { Button } from './Button';
export { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from './Card'; export { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from './Card';
export { Input } from './Input'; export { Input } from './Input';
export { Textarea } from './Textarea';
export { Badge } from './Badge';
export { Modal, ModalFooter } from './Modal'; export { Modal, ModalFooter } from './Modal';
export { Textarea } from './Textarea';

23
src/lib/gravatar.ts Normal file
View File

@@ -0,0 +1,23 @@
import { createHash } from 'crypto';
export type GravatarDefault =
| 'identicon' // Geometric pattern
| 'mp' // Mystery person silhouette
| 'retro' // 8-bit pixel art
| 'robohash' // Generated robot
| 'wavatar' // Stylized face
| 'monsterid' // Colorful monster
| 'blank'; // Transparent
export function getGravatarUrl(
email: string,
size: number = 40,
fallback: GravatarDefault = 'identicon'
): string {
const hash = createHash('md5')
.update(email.toLowerCase().trim())
.digest('hex');
return `https://www.gravatar.com/avatar/${hash}?d=${fallback}&s=${size}`;
}