sendou.ink/app/components/Avatar.tsx
2025-10-25 17:46:17 +03:00

63 lines
1.4 KiB
TypeScript

import clsx from "clsx";
import * as React from "react";
import type { Tables } from "~/db/tables";
import { BLANK_IMAGE_URL, discordAvatarUrl } from "~/utils/urls";
const dimensions = {
xxxs: 16,
xxxsm: 20,
xxs: 24,
xs: 36,
sm: 44,
xsm: 62,
md: 81,
lg: 125,
} as const;
export function Avatar({
user,
url,
size = "sm",
className,
alt = "",
...rest
}: {
user?: Pick<Tables["User"], "discordId" | "discordAvatar">;
url?: string;
className?: string;
alt?: string;
size: keyof typeof dimensions;
} & React.ButtonHTMLAttributes<HTMLImageElement>) {
const [isErrored, setIsErrored] = React.useState(false);
// TODO: just show text... my profile?
// TODO: also show this if discordAvatar is stale and 404's
// biome-ignore lint/correctness/useExhaustiveDependencies: every avatar error state is unique and we want to avoid using key on every avatar
React.useEffect(() => {
setIsErrored(false);
}, [user?.discordAvatar]);
const src =
url ??
(user?.discordAvatar && !isErrored
? discordAvatarUrl({
discordAvatar: user.discordAvatar,
discordId: user.discordId,
size: size === "lg" ? "lg" : "sm",
})
: BLANK_IMAGE_URL); // avoid broken image placeholder
return (
<img
className={clsx("avatar", className)}
src={src}
alt={alt}
title={alt ? alt : undefined}
width={dimensions[size]}
height={dimensions[size]}
onError={() => setIsErrored(true)}
{...rest}
/>
);
}