Files
neo-blog/web/src/components/gravatar.tsx

82 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import React from "react";
import Image from "next/image";
import crypto from "crypto";
// 生成 Gravatar URL 的函数
function getGravatarUrl(email: string, size: number = 40, defaultType: string = "identicon"): string {
const hash = crypto.createHash('md5').update(email.toLowerCase().trim()).digest('hex');
return `https://www.gravatar.com/avatar/${hash}?s=${size}&d=${defaultType}`;
}
interface GravatarAvatarProps {
email: string;
size?: number;
className?: string;
alt?: string;
url?: string;
defaultType?: 'mm' | 'identicon' | 'monsterid' | 'wavatar' | 'retro' | 'robohash' | 'blank';
}
const GravatarAvatar: React.FC<GravatarAvatarProps> = ({
email,
size = 40,
className = "",
alt = "avatar",
url,
defaultType = "identicon"
}) => {
// 如果有自定义URL使用自定义URL
if (url) {
return (
<Image
src={url}
width={size}
height={size}
className={`rounded-full object-cover ${className}`}
alt={alt}
referrerPolicy="no-referrer"
/>
);
}
const gravatarUrl = getGravatarUrl(email, size * 10, defaultType);
return (
<Image
src={gravatarUrl}
width={size}
height={size}
className={`rounded-full object-cover ${className}`}
alt={alt}
referrerPolicy="no-referrer"
/>
);
};
// 用户类型定义(如果还没有的话)
interface User {
email?: string;
displayName?: string;
name?: string;
avatarUrl?: string;
}
export function getGravatarByUser(user?: User, className: string = ""): React.ReactElement {
if (!user) {
return <GravatarAvatar email="" className={className} />;
}
return (
<GravatarAvatar
email={user.email || ""}
size={40}
className={className}
alt={user.displayName || user.name || "User Avatar"}
url={user.avatarUrl}
/>
);
}
export default GravatarAvatar;