39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
|
|
import { avatarUrl, hueFromId, initials } from '@/lib/identity';
|
||
|
|
|
||
|
|
interface AvatarProps {
|
||
|
|
name: string;
|
||
|
|
/** Стабильный id для оттенка заглушки. */
|
||
|
|
seed: string;
|
||
|
|
fileId?: string | undefined;
|
||
|
|
size?: 'sm' | 'md' | 'lg';
|
||
|
|
/** Квадратная заглушка для серверов, круглая — для пользователей. */
|
||
|
|
shape?: 'circle' | 'square';
|
||
|
|
}
|
||
|
|
|
||
|
|
const sizes = {
|
||
|
|
sm: 'h-7 w-7 text-[11px]',
|
||
|
|
md: 'h-9 w-9 text-xs',
|
||
|
|
lg: 'h-14 w-14 text-lg',
|
||
|
|
} as const;
|
||
|
|
|
||
|
|
/** Аватар: картинка файлового сервиса либо инициалы на цветной заглушке. */
|
||
|
|
export function Avatar({ name, seed, fileId, size = 'md', shape = 'circle' }: AvatarProps) {
|
||
|
|
const src = avatarUrl(fileId);
|
||
|
|
const hue = hueFromId(seed);
|
||
|
|
const radius = shape === 'circle' ? 'rounded-full' : 'rounded-[var(--radius-md)]';
|
||
|
|
|
||
|
|
return (
|
||
|
|
<span
|
||
|
|
aria-hidden="true"
|
||
|
|
className={`inline-flex shrink-0 items-center justify-center overflow-hidden font-semibold text-white ${radius} ${sizes[size]}`}
|
||
|
|
style={src === null ? { backgroundColor: `oklch(0.55 0.13 ${String(hue)})` } : undefined}
|
||
|
|
>
|
||
|
|
{src === null ? (
|
||
|
|
initials(name)
|
||
|
|
) : (
|
||
|
|
<img src={src} alt="" className="h-full w-full object-cover" />
|
||
|
|
)}
|
||
|
|
</span>
|
||
|
|
);
|
||
|
|
}
|