2026-08-14 10:49:44 +07:00
|
|
|
import { cn } from "@/lib/utils";
|
|
|
|
|
|
|
|
|
|
function initials(name?: string | null): string {
|
|
|
|
|
if (!name) return "?";
|
2026-08-15 17:53:48 +07:00
|
|
|
const parts = name.replace(/[^\p{L}\p{N} _]/gu, "").trim().split(/\s+/);
|
2026-08-14 10:49:44 +07:00
|
|
|
if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase();
|
|
|
|
|
return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
export function Avatar({
|
|
|
|
|
src,
|
|
|
|
|
name,
|
|
|
|
|
size = 36,
|
|
|
|
|
className,
|
|
|
|
|
ring,
|
|
|
|
|
}: {
|
|
|
|
|
src?: string | null;
|
|
|
|
|
name?: string | null;
|
|
|
|
|
size?: number;
|
|
|
|
|
className?: string;
|
|
|
|
|
ring?: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const dim = { width: size, height: size };
|
2026-08-14 10:49:44 +07:00
|
|
|
return (
|
|
|
|
|
<span
|
|
|
|
|
className={cn(
|
|
|
|
|
"relative inline-flex shrink-0 items-center justify-center overflow-hidden rounded-full",
|
2026-08-15 17:53:48 +07:00
|
|
|
"bg-gradient-to-br from-white/10 to-white/[0.02] text-ink-soft",
|
|
|
|
|
ring && "ring-2 ring-signal/50",
|
2026-08-14 10:49:44 +07:00
|
|
|
className,
|
|
|
|
|
)}
|
2026-08-15 17:53:48 +07:00
|
|
|
style={dim}
|
2026-08-14 10:49:44 +07:00
|
|
|
>
|
|
|
|
|
{src ? (
|
2026-08-15 17:53:48 +07:00
|
|
|
// eslint-disable-next-line @next/next/no-img-element
|
2026-08-14 10:49:44 +07:00
|
|
|
<img
|
|
|
|
|
src={src}
|
2026-08-15 17:53:48 +07:00
|
|
|
alt={name ?? "avatar"}
|
|
|
|
|
className="h-full w-full object-cover"
|
2026-08-14 10:49:44 +07:00
|
|
|
loading="lazy"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
2026-08-15 17:53:48 +07:00
|
|
|
<span
|
|
|
|
|
className="font-semibold"
|
|
|
|
|
style={{ fontSize: Math.max(10, size * 0.36) }}
|
|
|
|
|
>
|
|
|
|
|
{initials(name)}
|
|
|
|
|
</span>
|
2026-08-14 10:49:44 +07:00
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|