forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

user-avatar.tsx65 lines, 2.1 KiBHistoryRawDownload
1import { Show, createEffect, createSignal } from "solid-js";
2import { getUserAvatarUrl } from "@/api/users";
3import { avatarRevision } from "@/lib/avatar";
4import { cn } from "@/lib/cn";
5import { personInitials } from "@/lib/person";
6
7const 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
15export type UserAvatarSize = keyof typeof SIZES;
16
17export 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 // grid + place-items, not flex + items/justify-center: DataTable's phone
44 // cards rewrite descendant .items-center/.justify-center to start, which
45 // pushed the initials into the circle's top-left corner.
46 return (
47 <span
48 class={cn(
49 "relative grid shrink-0 place-items-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",
50 SIZES[props.size ?? "sm"],
51 props.class,
52 )}
53 >
54 {personInitials(props.name)}
55 <Show when={showImage()}>
56 <img
57 src={getUserAvatarUrl(props.userId, avatarRevision())}
58 alt={props.name}
59 class="absolute inset-0 h-full w-full object-cover"
60 onError={() => setBroken(true)}
61 />
62 </Show>
63 </span>
64 );
65}