30 lines
1.1 KiB
TypeScript

import { getAvatarById, getAvatarUrl } from './avatars';
const DICEBEAR_BASE = 'https://api.dicebear.com/9.x/adventurer/svg';
/**
* Resolves the `profiles.avatar` field zu einer renderbaren URL.
*
* Drei Quellen-Formate werden unterstützt:
* 1. Hero-Avatar-ID (z.B. "spider", "hulk") → DiceBear-URL aus HERO_AVATARS
* 2. Custom-Photo-URL (https://... — User hat Foto via Profile-Edit
* hochgeladen, gespeichert in Supabase-Storage) → unverändert durchreichen
* 3. Leer / unbekannt → Dicebear-Initials-Fallback per nickname
*
* Wichtig: NICHT blindly getAvatarUrl(avatarId) aufrufen — das gab vorher den
* Dicebear-anonym-Fallback zurück wenn avatarId zwar truthy aber kein Hero
* (z.B. Foto-URL). Jetzt wird zuerst auf URL geprüft.
*/
export function resolveAvatar(avatarId: string | null | undefined, nickname: string): string {
if (avatarId) {
if (/^https?:\/\//i.test(avatarId)) {
return avatarId;
}
if (getAvatarById(avatarId)) {
return getAvatarUrl(avatarId);
}
}
const seed = encodeURIComponent(nickname || 'anonym');
return `${DICEBEAR_BASE}?seed=${seed}&backgroundColor=374151`;
}