hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { Show, createEffect, createSignal } from "solid-js"; |
| 2 | import { getUserAvatarUrl } from "@/api/users"; |
| 3 | import { avatarRevision } from "@/lib/avatar"; |
| 4 | import { cn } from "@/lib/cn"; |
| 5 | import { personInitials } from "@/lib/person"; |
| 6 | |
| 7 | const SIZES = { |
| 8 | xs: "h-6 w-6 text-[10px]", |
| 9 | sm: "h-8 w-8 text-[11px]", |
| 10 | md: "h-10 w-10 text-sm", |
| 11 | lg: "h-16 w-16 text-lg", |
| 12 | xl: "h-24 w-24 text-2xl", |
| 13 | } as const; |
| 14 | |
| 15 | export type UserAvatarSize = keyof typeof SIZES; |
| 16 | |
| 17 | export function UserAvatar(props: { |
| 18 | userId: string; |
| 19 | /** Label the initials and the alt text are built from. */ |
| 20 | name: string; |
| 21 | /** |
| 22 | * Pass the profile's `avatar != null` when it is known: false skips the |
| 23 | * request entirely, so a person without a photo costs no 404. Omit it where |
| 24 | * the caller has no profile in hand (the sidebar) and the image is simply |
| 25 | * attempted, falling back to initials if it is not there. |
| 26 | */ |
| 27 | hasAvatar?: boolean; |
| 28 | size?: UserAvatarSize; |
| 29 | class?: string; |
| 30 | }) { |
| 31 | const [broken, setBroken] = createSignal(false); |
| 32 | |
| 33 | // A once-404'd avatar must not stay broken after the person uploads one, and |
| 34 | // the same component instance is reused across /profile/$userId navigations. |
| 35 | createEffect(() => { |
| 36 | props.userId; |
| 37 | avatarRevision(); |
| 38 | setBroken(false); |
| 39 | }); |
| 40 | |
| 41 | const showImage = () => props.hasAvatar !== false && !broken(); |
| 42 | |
| 43 | return ( |
| 44 | <span |
| 45 | class={cn( |
| 46 | "relative flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-primary/15 font-bold text-primary-text shadow-xs ring-1 ring-border/70 dark:bg-emerald-100 dark:text-emerald-950 dark:ring-white/20", |
| 47 | SIZES[props.size ?? "sm"], |
| 48 | props.class, |
| 49 | )} |
| 50 | > |
| 51 | {personInitials(props.name)} |
| 52 | <Show when={showImage()}> |
| 53 | <img |
| 54 | src={getUserAvatarUrl(props.userId, avatarRevision())} |
| 55 | alt={props.name} |
| 56 | class="absolute inset-0 h-full w-full object-cover" |
| 57 | onError={() => setBroken(true)} |
| 58 | /> |
| 59 | </Show> |
| 60 | </span> |
| 61 | ); |
| 62 | } |