import md5 from 'md5' const gravatarCache = new Map() /** * Returns Gravatar avatar URL for the given email, or empty string if no email. * Gravatar expects: trim, lowercase, then MD5 hex. Use empty string to show a placeholder (e.g. initial letter) in the UI. * Results are memoized by (email, size) to avoid recomputing MD5 on every render. * * @param email - User email (optional) * @param size - Avatar size in pixels (default 64; navbar 32, drawer 40, profile 64–80) */ export function useGravatarUrl(email: string | undefined, size?: number): string { const normalized = email?.trim().toLowerCase() if (!normalized) return '' const s = size ?? 64 const key = `${normalized}\n${s}` const cached = gravatarCache.get(key) if (cached !== undefined) return cached const hash = md5(normalized) const url = `https://www.gravatar.com/avatar/${hash}?s=${s}&d=identicon` gravatarCache.set(key, url) return url }