2026-09-01 14:32:19 +02:00
|
|
|
import { useEffect, useState } from "react";
|
2026-08-06 08:36:47 +02:00
|
|
|
import { cn } from "@/lib/utils";
|
2026-09-01 14:32:19 +02:00
|
|
|
import { urlAvatar } from "@/lib/avatar-store";
|
2026-08-06 08:36:47 +02:00
|
|
|
|
|
|
|
|
export function Avatar({
|
|
|
|
|
id,
|
|
|
|
|
fallback,
|
|
|
|
|
className,
|
|
|
|
|
alt,
|
2026-09-01 14:32:19 +02:00
|
|
|
bust,
|
2026-08-06 08:36:47 +02:00
|
|
|
}: {
|
|
|
|
|
id: string;
|
|
|
|
|
fallback: string;
|
|
|
|
|
className?: string;
|
|
|
|
|
alt?: string;
|
2026-09-01 14:32:19 +02:00
|
|
|
/** Forza il ricaricamento ignorando la cache: usato dopo aver cambiato la propria foto. */
|
|
|
|
|
bust?: number;
|
2026-08-06 08:36:47 +02:00
|
|
|
}) {
|
2026-09-01 14:32:19 +02:00
|
|
|
const [errore, setErrore] = useState(false);
|
|
|
|
|
useEffect(() => setErrore(false), [id, bust]);
|
|
|
|
|
|
|
|
|
|
if (!errore) {
|
|
|
|
|
const src = bust ? `${urlAvatar(id)}?v=${bust}` : urlAvatar(id);
|
2026-08-06 08:36:47 +02:00
|
|
|
return (
|
|
|
|
|
<img
|
|
|
|
|
src={src}
|
2026-09-05 12:42:24 +02:00
|
|
|
// `fallback` è spesso il numero di maglia: "Foto di 12" non descrive
|
|
|
|
|
// niente. Senza un nome vero l'immagine è decorativa e il nome sta già
|
|
|
|
|
// scritto accanto, quindi alt vuoto.
|
|
|
|
|
alt={alt ?? ""}
|
|
|
|
|
// Le dimensioni reali le dà la classe; width/height servono solo a
|
|
|
|
|
// riservare il rapporto e non far saltare il layout al caricamento.
|
|
|
|
|
width={96}
|
|
|
|
|
height={96}
|
|
|
|
|
loading="lazy"
|
|
|
|
|
decoding="async"
|
2026-09-01 14:32:19 +02:00
|
|
|
onError={() => setErrore(true)}
|
2026-09-05 12:42:24 +02:00
|
|
|
className={cn("aspect-square shrink-0 rounded-2xl object-cover", className)}
|
2026-08-06 08:36:47 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<span
|
|
|
|
|
className={cn(
|
|
|
|
|
"grid shrink-0 place-items-center rounded-2xl bg-secondary font-display",
|
|
|
|
|
className,
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{fallback}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
}
|