import { Show, createEffect, createSignal } from "solid-js"; import { getUserAvatarUrl } from "@/api/users"; import { avatarRevision } from "@/lib/avatar"; import { cn } from "@/lib/cn"; import { personInitials } from "@/lib/person"; const SIZES = { xs: "h-6 w-6 text-[10px]", sm: "h-8 w-8 text-[11px]", md: "h-10 w-10 text-sm", lg: "h-16 w-16 text-lg", xl: "h-24 w-24 text-2xl", } as const; export type UserAvatarSize = keyof typeof SIZES; export function UserAvatar(props: { userId: string; /** Label the initials and the alt text are built from. */ name: string; /** * Pass the profile's `avatar != null` when it is known: false skips the * request entirely, so a person without a photo costs no 404. Omit it where * the caller has no profile in hand (the sidebar) and the image is simply * attempted, falling back to initials if it is not there. */ hasAvatar?: boolean; size?: UserAvatarSize; class?: string; }) { const [broken, setBroken] = createSignal(false); // A once-404'd avatar must not stay broken after the person uploads one, and // the same component instance is reused across /profile/$userId navigations. createEffect(() => { props.userId; avatarRevision(); setBroken(false); }); const showImage = () => props.hasAvatar !== false && !broken(); return ( {personInitials(props.name)} {props.name} setBroken(true)} /> ); }